2222#define SOCKHUB_BUFFER_SIZE (1024*1024)
2323#define ERR_BUF_SIZE 1024
2424
25+ #define SHUB_TRACE (fmt , ...)
26+ /* #define SHUB_TRACE(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__) */
27+
2528void ShubAddSocket (Shub * shub , int fd );
2629
2730inline void ShubAddSocket (Shub * shub , int fd )
@@ -63,21 +66,23 @@ void ShubInitParams(ShubParams* params)
6366 params -> leader = NULL ;
6467}
6568
66- void ShubParamsSetHosts (ShubParams * params , char * hoststring )
69+ int ShubParamsSetHosts (ShubParams * params , char * hoststring )
6770{
6871 char * hstate , * pstate ;
6972 char * hostport , * host , * portstr ;
7073 int port ;
74+ int ok = 1 ;
7175
7276 char * hosts = strdup (hoststring );
73- fprintf ( stderr , "sockhub parsing hosts = '%s'\n" , hosts );
77+ SHUB_TRACE ( "sockhub parsing hosts = '%s'\n" , hosts );
7478 hostport = strtok_r (hosts , "," , & hstate );
7579
7680 while (hostport ) {
77- fprintf ( stderr , "hostport = '%s'\n" , hostport );
81+ SHUB_TRACE ( "hostport = '%s'\n" , hostport );
7882 host = strtok_r (hostport , ":" , & pstate );
7983 if (!host ) {
80- fprintf (stderr , "wrong host in host list\n" );
84+ SHUB_TRACE ("wrong host in host list\n" );
85+ ok = 0 ;
8186 break ;
8287 }
8388
@@ -88,20 +93,20 @@ void ShubParamsSetHosts(ShubParams* params, char* hoststring)
8893 port = 5431 ;
8994 }
9095
91- fprintf ( stderr , "adding host %s:%d\n" , host , port );
96+ SHUB_TRACE ( "adding host %s:%d\n" , host , port );
9297 host_t * h = malloc (sizeof (host_t ));
9398 h -> host = strdup (host );
9499 h -> port = port ;
95100 if (params -> leader ) {
96- // update pointers from
101+ /* update pointers from */
97102 h -> prev = params -> leader -> prev ;
98103 h -> next = params -> leader ;
99104
100- // update pointers to
105+ /* update pointers to */
101106 h -> prev -> next = h ;
102107 h -> next -> prev = h ;
103108 } else {
104- // the list is empty
109+ /* the list is empty */
105110 params -> leader = h ;
106111 h -> prev = h ;
107112 h -> next = h ;
@@ -111,6 +116,7 @@ void ShubParamsSetHosts(ShubParams* params, char* hoststring)
111116 }
112117
113118 free (hosts );
119+ return ok ;
114120}
115121
116122static int resolve_host_by_name (const char * hostname , unsigned * addrs , unsigned * n_addrs )
@@ -202,20 +208,20 @@ static void reconnect(Shub* shub)
202208 char * host = shub -> params -> leader -> host ;
203209 int port = shub -> params -> leader -> port ;
204210
205- fprintf ( stderr , "shub leader = %s:%d\n" , host , port );
211+ SHUB_TRACE ( "shub leader = %s:%d\n" , host , port );
206212
207213 shub -> params -> leader = shub -> params -> leader -> next ;
208214
209215 ShubErrorSeverity severity = SHUB_RECOVERABLE_ERROR ;
210216 sock_inet .sin_port = htons (port );
211217
212218 if (!resolve_host_by_name (host , addrs , & n_addrs )) {
213- shub -> params -> error_handler ("Failed to resolve host by name" , severity );
219+ shub -> params -> error_handler ("Sockhub failed to resolve host by name" , severity );
214220 continue ;
215221 }
216222 shub -> output = socket (AF_INET , SOCK_STREAM , 0 );
217223 if (shub -> output < 0 ) {
218- shub -> params -> error_handler ("Failed to create inet socket" , severity );
224+ shub -> params -> error_handler ("Sockhub failed to create inet socket" , severity );
219225 continue ;
220226 }
221227 while (1 ) {
@@ -231,16 +237,16 @@ static void reconnect(Shub* shub)
231237 }
232238 }
233239 if (rc < 0 ) {
234- if (errno != ENOENT && errno != ECONNREFUSED && errno != EINPROGRESS ) {
235- shub -> params -> error_handler ( "Connection can not be establish" , severity ) ;
236- continue ;
237- }
238- if ( max_attempts -- != 0 ) {
239- sleep ( 1 );
240- } else {
241- shub -> params -> error_handler ( "Failed to connect to host" , severity );
242- continue ;
243- }
240+ if (( errno != ENOENT && errno != ECONNREFUSED && errno != EINPROGRESS ) || max_attempts == 0 ) {
241+ char buf [ ERR_BUF_SIZE ] ;
242+ sprintf ( buf , "Sockhub failed to connect to %s:%d: %d" , host , port , errno ) ;
243+ shub -> params -> error_handler ( buf , severity );
244+ max_attempts = shub -> params -> max_attempts ;
245+ } else {
246+ max_attempts -= 1 ;
247+ sleep ( 1 );
248+ }
249+ continue ;
244250 } else {
245251 int optval = 1 ;
246252 setsockopt (shub -> output , IPPROTO_TCP , TCP_NODELAY , (char const * )& optval , sizeof (optval ));
@@ -493,7 +499,6 @@ void ShubLoop(Shub* shub)
493499 char buf [ERR_BUF_SIZE ];
494500 sprintf (buf , "Failed to read local socket chan=%d, rc=%d, min requested=%ld, max requested=%d, errno=%d" , chan , rc , sizeof (ShubMessageHdr ) - available , buffer_size - pos - available , errno );
495501 shub -> params -> error_handler (buf , SHUB_RECOVERABLE_ERROR );
496- //shub->params->error_handler("Failed to read local socket", SHUB_RECOVERABLE_ERROR);
497502 close_socket (shub , i );
498503 shub -> in_buffer_used = pos ;
499504 notify_disconnect (shub , i );
@@ -532,7 +537,6 @@ void ShubLoop(Shub* shub)
532537 char buf [ERR_BUF_SIZE ];
533538 sprintf (buf , "Failed to read local socket rc=%d, len=%d, errno=%d" , rc , n , errno );
534539 shub -> params -> error_handler (buf , SHUB_RECOVERABLE_ERROR );
535- //shub->params->error_handler("Failed to read local socket", SHUB_RECOVERABLE_ERROR);
536540 close_socket (shub , chan );
537541 if (hdr != NULL ) { /* if message header is not yet sent to the server... */
538542 /* ... then skip this message */
@@ -607,4 +611,3 @@ void ShubLoop(Shub* shub)
607611 }
608612}
609613
610- // vim: sts=4 ts=4 sw=4 expandtab
0 commit comments