40
40
package main
41
41
42
42
import (
43
- "strings"
44
- "fmt"
45
- "net"
46
- "flag"
47
43
"bufio"
44
+ "flag"
45
+ "fmt"
48
46
"log"
47
+ "net"
48
+ "strings"
49
49
)
50
50
51
51
const DEFAULT_LISTEN_PORT = 10000
@@ -64,9 +64,9 @@ func ServiceConnection(conn net.Conn) {
64
64
65
65
// In our trivial protocol, the first line contains the client identity
66
66
// on a line by itself
67
- intro ,err := in .ReadString ('\n' )
67
+ intro , err := in .ReadString ('\n' )
68
68
if err != nil {
69
- log .Printf ( "[%s] Aborting connection before we ever received client identity" , addr )
69
+ log .Printf ("[%s] Aborting connection before we ever received client identity" , addr )
70
70
conn .Close ()
71
71
}
72
72
identity := strings .TrimSpace (intro )
@@ -81,26 +81,26 @@ func ServiceConnection(conn net.Conn) {
81
81
82
82
// Now handle existing entry
83
83
if existingConn != nil {
84
- log .Printf ( "[%s@%s] Closing connection to make room for new connection from '%s'" , identity , existingConn .RemoteAddr ().String (), addr )
84
+ log .Printf ("[%s@%s] Closing connection to make room for new connection from '%s'" , identity , existingConn .RemoteAddr ().String (), addr )
85
85
existingConn .Close ()
86
86
}
87
- log .Printf ( "[%s@%s] Added connection" , identity , addr )
87
+ log .Printf ("[%s@%s] Added connection" , identity , addr )
88
88
89
89
// Keep reading until connection is closed
90
90
for {
91
- line ,err := in .ReadString ('\n' )
91
+ line , err := in .ReadString ('\n' )
92
92
if err != nil {
93
93
conn .Close ()
94
94
95
95
// Are we stil in the map?
96
96
if g_mapClientConnections [identity ] == conn {
97
- log .Printf ( "[%s@%s] Connecton closed. %s" , identity , addr , err )
97
+ log .Printf ("[%s@%s] Connecton closed. %s" , identity , addr , err )
98
98
delete (g_mapClientConnections , identity )
99
99
} else {
100
100
// Assume it's because we got replaced by another connection.
101
101
// The other connection already logged, so don't do anything here
102
102
}
103
- break ;
103
+ break
104
104
}
105
105
106
106
// Our protocol is just [destination peer identity] [payload]
@@ -110,7 +110,7 @@ func ServiceConnection(conn net.Conn) {
110
110
log .Printf ("[%s@%s] Ignoring weird input '%s' (maybe truncated?)" , identity , addr , line )
111
111
continue
112
112
}
113
- dest_identity := strings .TrimSpace (dest_and_msg [0 ]);
113
+ dest_identity := strings .TrimSpace (dest_and_msg [0 ])
114
114
payload := dest_and_msg [1 ]
115
115
116
116
// Locate the destination peer's connection.
@@ -135,9 +135,9 @@ func ServiceConnection(conn net.Conn) {
135
135
// Main entry point
136
136
func main () {
137
137
138
- // Parse command line flags
138
+ // Parse command line flags
139
139
port := flag .Int ("port" , DEFAULT_LISTEN_PORT , "Port to listen on" )
140
- flag .Parse ()
140
+ flag .Parse ()
141
141
listen_addr := fmt .Sprintf ("0.0.0.0:%d" , * port )
142
142
143
143
// Start listening
@@ -151,7 +151,7 @@ func main() {
151
151
for {
152
152
153
153
// Wait for the next incoming connection
154
- conn ,err := listener .Accept ()
154
+ conn , err := listener .Accept ()
155
155
if err != nil {
156
156
log .Panic (err )
157
157
}
@@ -161,4 +161,3 @@ func main() {
161
161
}
162
162
163
163
}
164
-
0 commit comments