File: net.go

package info (click to toggle)
receptor 1.5.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,772 kB
  • sloc: python: 1,643; makefile: 305; sh: 174
file content (151 lines) | stat: -rw-r--r-- 3,724 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main

import (
	"context"
	"fmt"
	"io"
	"net"
	"os"
	"time"

	"github.com/ansible/receptor/pkg/backends"
	"github.com/ansible/receptor/pkg/logger"
	"github.com/ansible/receptor/pkg/netceptor"
)

/*
   This is an example of the use of Receptor as a Go library.
*/

func main() {
	// Suppress log output.  Remove this if you want to see log information.
	logger.SetGlobalQuietMode()

	// Create two nodes of the Receptor network-layer protocol (Netceptors).
	n1 := netceptor.New(context.Background(), "node1")
	n2 := netceptor.New(context.Background(), "node2")

	// Start a TCP listener on the first node
	b1, err := backends.NewTCPListener("localhost:3333", nil, n1.Logger)
	if err != nil {
		fmt.Printf("Error listening on TCP: %s\n", err)
		os.Exit(1)
	}
	err = n1.AddBackend(b1)
	if err != nil {
		fmt.Printf("Error starting backend: %s\n", err)
		os.Exit(1)
	}

	// Start a TCP dialer on the second node - this will connect to the listener we just started
	b2, err := backends.NewTCPDialer("localhost:3333", false, nil, n2.Logger)
	if err != nil {
		fmt.Printf("Error dialing on TCP: %s\n", err)
		os.Exit(1)
	}
	err = n2.AddBackend(b2)
	if err != nil {
		fmt.Printf("Error starting backend: %s\n", err)
		os.Exit(1)
	}

	// Start an echo server on node 1
	l1, err := n1.Listen("echo", nil)
	if err != nil {
		fmt.Printf("Error listening on Receptor network: %s\n", err)
		os.Exit(1)
	}
	go func() {
		// Accept an incoming connection - note that conn is just a regular net.Conn
		conn, err := l1.Accept()
		if err != nil {
			fmt.Printf("Error accepting connection: %s\n", err)

			return
		}
		fmt.Printf("Accepted a connection\n")
		go func() {
			defer conn.Close()
			buf := make([]byte, 1024)
			done := false
			for !done {
				n, err := conn.Read(buf)
				if err == io.EOF {
					done = true
				} else if err != nil {
					fmt.Printf("Read error in Receptor listener: %s\n", err)

					return
				}
				fmt.Printf("Echo server got %d bytes\n", n)
				if n > 0 {
					_, err := conn.Write(buf[:n])
					if err != nil {
						fmt.Printf("Write error in Receptor listener: %s\n", err)

						return
					}
				}
			}
		}()
	}()

	// Connect to the echo server from node 2.  We expect this to error out at first with
	// "no route to node" because it takes a second or two for node1 and node2 to exchange
	// routing information and form a mesh.
	var c2 net.Conn
	for {
		fmt.Printf("Dialing node1\n")
		c2, err = n2.Dial("node1", "echo", nil)
		if err != nil {
			fmt.Printf("Error dialing on Receptor network: %s\n", err)
			time.Sleep(1 * time.Second)

			continue
		}

		break
	}

	// Start a listener function that prints received data to the screen
	// Note that because net.Conn is a stream connection, it is not guaranteed
	// that received messages will be the same size as the messages that are sent.
	// For datagram use, Receptor also provides a net.PacketConn.
	go func() {
		rbuf := make([]byte, 1024)
		for {
			n, err := c2.Read(rbuf)
			if n > 0 {
				fmt.Printf("Received data: %s\n", rbuf[:n])
			}
			if err == io.EOF {
				// Shut down the whole Netceptor when any connection closes, because this is just a demo
				n2.Shutdown()

				return
			}
			if err != nil {
				fmt.Printf("Read error in Receptor dialer: %s\n", err)

				return
			}
		}
	}()

	// Send some data, which should be processed through the echo server back to our
	// receive function and printed to the screen.
	_, err = c2.Write([]byte("Hello, world!"))
	if err != nil && err != io.EOF {
		fmt.Printf("Write error in Receptor dialer: %s\n", err)
	}

	// Close our end of the connection
	_ = c2.Close()

	// Wait for n2 to shut down
	n2.BackendWait()

	// Gracefully shut down n1
	n1.Shutdown()
	n1.BackendWait()
}