11package comm
22
33import (
4- "bufio"
54 "fmt"
65 "log"
7- "strings"
86 "time"
97
108 "crypto/tls"
@@ -15,56 +13,50 @@ import (
1513// ReliableConnect attempts to connect to defined remote node
1614// as longs as the error from previous attempts is possible
1715// to be dealt with.
18- func ReliableConnect (name string , remoteName string , remoteIP string , remotePort string , tlsConfig * tls.Config , retry int ) (* tls.Conn , error ) {
16+ func ReliableConnect (remoteName string , remoteAddr string , tlsConfig * tls.Config , retry int ) (* tls.Conn , error ) {
1917
2018 var err error
2119 var c * tls.Conn
2220
23- // Define address we are trying to connect to.
24- nodeAddr := fmt .Sprintf ("%s:%s" , remoteIP , remotePort )
25-
2621 // Define what an error looks like we can deal with.
27- okError := fmt .Sprintf ("dial tcp %s: getsockopt: connection refused" , nodeAddr )
22+ okError := fmt .Sprintf ("dial tcp %s: getsockopt: connection refused" , remoteAddr )
2823
2924 // Initially, set error string to the one we can deal with.
3025 err = fmt .Errorf (okError )
3126
3227 for err != nil {
3328
3429 // Attempt to connect to worker node.
35- c , err = tls .Dial ("tcp" , nodeAddr , tlsConfig )
30+ c , err = tls .Dial ("tcp" , remoteAddr , tlsConfig )
3631 if err != nil {
3732
3833 if err .Error () == okError {
3934 time .Sleep (time .Duration (retry ) * time .Millisecond )
4035 } else {
41- return nil , fmt .Errorf ("%s: Could not connect to port of node '%s' because of: %s\n " , name , remoteName , err .Error ())
36+ return nil , fmt .Errorf ("Could not connect to port of node '%s' because of: %s\n " , remoteName , err .Error ())
4237 }
4338 }
4439 }
4540
46- log .Printf ("%s: Successfully connected to worker node '%s'.\n " , name , remoteName )
41+ log .Printf ("Successfully connected to worker node '%s'.\n " , remoteName )
4742
4843 return c , nil
4944}
5045
5146// ReliableSend sends text to other node specified and
5247// tries to reconnect in case of simple disconnects.
53- func ReliableSend (conn * tls.Conn , text string , name string , remoteName string , remoteIP string , remotePort string , tlsConfig * tls.Config , timeout int , retry int ) ( * tls. Conn , bool , error ) {
48+ func ReliableSend (conn * tls.Conn , text string , remoteName string , remoteAddr string , tlsConfig * tls.Config , timeout int , retry int ) error {
5449
5550 var err error
5651 var replacedConn * tls.Conn
5752
58- // Track if we replaced the connection.
59- replaced := false
60-
6153 // Set configured timeout on waiting for response.
6254 conn .SetWriteDeadline (time .Now ().Add (time .Duration (timeout ) * time .Millisecond ))
6355
6456 // Test long-lived connection.
6557 _ , err = conn .Write ([]byte ("> ping <\r \n " ))
6658 if err != nil {
67- return nil , false , fmt .Errorf ("sending ping to node '%s' failed with: %s\n " , remoteName , err .Error ())
59+ return fmt .Errorf ("sending ping to node '%s' failed with: %s\n " , remoteName , err .Error ())
6860 }
6961
7062 // Wait for configured time to pass.
@@ -85,75 +77,17 @@ func ReliableSend(conn *tls.Conn, text string, name string, remoteName string, r
8577 if err .Error () == okError {
8678
8779 // Connection was lost. Reconnect.
88- replacedConn , err = ReliableConnect (name , remoteName , remoteIP , remotePort , tlsConfig , retry )
80+ replacedConn , err = ReliableConnect (remoteName , remoteAddr , tlsConfig , retry )
8981 if err != nil {
90- return nil , false , fmt .Errorf ("could not reestablish connection with '%s': %s\n " , remoteName , err .Error ())
82+ return fmt .Errorf ("could not reestablish connection with '%s': %s\n " , remoteName , err .Error ())
9183 }
9284
93- // Indicate we replaced connection.
94- replaced = true
95-
96- // Wait configured time before attempting next transfer.
97- time .Sleep (time .Duration (retry ) * time .Millisecond )
98-
9985 // Retry transfer.
10086 _ , err = fmt .Fprintf (replacedConn , "%s\r \n " , text )
10187 } else {
102- return nil , false , fmt .Errorf ("could not reestablish connection with '%s': %s\n " , remoteName , err .Error ())
88+ return fmt .Errorf ("could not reestablish connection with '%s': %s\n " , remoteName , err .Error ())
10389 }
10490 }
10591
106- if replaced {
107- return replacedConn , replaced , nil
108- }
109-
110- return conn , replaced , nil
111- }
112-
113- // InternalSend is used by nodes of the pluto system to
114- // successfully transmit a message to another node or
115- // fail definitely. This prevents further handler advancement
116- // in case a link failed.
117- func InternalSend (conn * tls.Conn , text string , name string , remoteName string ) error {
118-
119- // Test long-lived connection.
120- _ , err := conn .Write ([]byte ("> ping <\r \n " ))
121- if err != nil {
122- return fmt .Errorf ("%s: sending ping to node '%s' failed: %s\n " , name , remoteName , err .Error ())
123- }
124-
125- // Write message to TLS connections.
126- _ , err = fmt .Fprintf (conn , "%s\r \n " , text )
127- for err != nil {
128- return fmt .Errorf ("%s: sending message to node '%s' failed: %s\n " , name , remoteName , err .Error ())
129- }
130-
13192 return nil
13293}
133-
134- // InternalReceive is used by nodes in the pluto system
135- // receive an incoming message and filter out all prior
136- // received ping message.
137- func InternalReceive (reader * bufio.Reader ) (string , error ) {
138-
139- var err error
140-
141- // Initial value for received message in order
142- // to skip past the mandatory ping message.
143- text := "> ping <\r \n "
144-
145- for text == "> ping <\r \n " {
146-
147- text , err = reader .ReadString ('\n' )
148- if err != nil {
149- break
150- }
151- }
152-
153- // If an error happened, return it.
154- if err != nil {
155- return "" , err
156- }
157-
158- return strings .TrimRight (text , "\r \n " ), nil
159- }
0 commit comments