github.com/micro/go-micro/examples@v0.0.0-20210105173217-bf4ab679e18b/tunnel/tunnel.go (about)

     1  package main
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  
     7  	"github.com/micro/go-micro/v2/transport"
     8  	"github.com/micro/go-micro/v2/tunnel"
     9  	"github.com/micro/go-micro/v2/util/log"
    10  )
    11  
    12  // testAccept will accept connections on the transport, create a new link and tunnel on top
    13  func testAccept(tun tunnel.Tunnel, wg *sync.WaitGroup) {
    14  	// listen on some virtual address
    15  	tl, err := tun.Listen("test-tunnel")
    16  	if err != nil {
    17  		log.Fatal(err)
    18  	}
    19  
    20  	log.Log("Listening on ", tun.Address())
    21  	wg.Done()
    22  
    23  	// accept a connection
    24  	c, err := tl.Accept()
    25  	if err != nil {
    26  		log.Fatal(err)
    27  	}
    28  	log.Log("Accepting connection")
    29  
    30  	// get a message
    31  	for {
    32  		m := new(transport.Message)
    33  		if err := c.Recv(m); err != nil {
    34  			log.Fatal(err)
    35  		}
    36  		log.Log("Received message")
    37  		wg.Done()
    38  		return
    39  	}
    40  
    41  }
    42  
    43  // testSend will create a new link to an address and then a tunnel on top
    44  func testSend(tun tunnel.Tunnel) {
    45  	// dial a new session
    46  	c, err := tun.Dial("test-tunnel")
    47  	if err != nil {
    48  		log.Fatal(err)
    49  	}
    50  	defer c.Close()
    51  
    52  	log.Log("Dialed connection")
    53  
    54  	m := transport.Message{
    55  		Header: map[string]string{
    56  			"test": "header",
    57  		},
    58  	}
    59  
    60  	if err := c.Send(&m); err != nil {
    61  		log.Fatal(err)
    62  	}
    63  
    64  	log.Log("Sent message")
    65  }
    66  
    67  func main() {
    68  	// create a new tunnel client
    69  	tunA := tunnel.NewTunnel(
    70  		tunnel.Address("127.0.0.1:9096"),
    71  		tunnel.Nodes("127.0.0.1:9097"),
    72  	)
    73  
    74  	// create a new tunnel server
    75  	tunB := tunnel.NewTunnel(
    76  		tunnel.Address("127.0.0.1:9097"),
    77  	)
    78  
    79  	// start tunB
    80  	err := tunB.Connect()
    81  	if err != nil {
    82  		log.Fatal(err)
    83  	}
    84  	defer tunB.Close()
    85  	log.Log("Connected tunnel B")
    86  
    87  	time.Sleep(time.Millisecond * 50)
    88  
    89  	// start tunA
    90  	err = tunA.Connect()
    91  	if err != nil {
    92  		log.Fatal(err)
    93  	}
    94  	defer tunA.Close()
    95  	log.Log("Connected tunnel A")
    96  
    97  	time.Sleep(time.Millisecond * 50)
    98  
    99  	var wg sync.WaitGroup
   100  
   101  	// start accepting connections
   102  	// on tunnel A
   103  	wg.Add(1)
   104  	go testAccept(tunA, &wg)
   105  	wg.Wait()
   106  
   107  	time.Sleep(time.Millisecond * 50)
   108  
   109  	// dial and send via B
   110  	wg.Add(1)
   111  	testSend(tunB)
   112  
   113  	// wait until done
   114  	wg.Wait()
   115  }