17
17
#*********************************************************************/
18
18
19
19
import socket
20
+ import time
21
+ import estreamer .crossprocesslogging as logging
20
22
from estreamer .common .convert import isInt
21
23
from estreamer .streams .base import Base
22
24
@@ -38,26 +40,48 @@ def __init__( self, host, port, encoding = 'utf-8' ):
38
40
self .encoding = encoding
39
41
self .socket = None
40
42
41
-
43
+ self . logger = logging . getLogger ( self . __class__ . __name__ )
42
44
43
45
def __connect ( self ):
44
- self .socket = socket .socket (socket .AF_INET , socket .SOCK_DGRAM , socket .IPPROTO_UDP )
45
- self .socket .connect ( ( self .host , self .port ) )
46
46
47
47
48
+ while True :
49
+ try :
50
+
51
+ self .logger .debug ('Connecting to {0}:{1}' .format (self .host , self .port ))
52
+
53
+ self .socket = socket .socket (socket .AF_INET , socket .SOCK_DGRAM , socket .IPPROTO_UDP )
54
+ self .socket .connect ((self .host ,self .port ))
55
+ break
56
+
57
+ except OSError as ose :
58
+
59
+ self .logger .error ( "Socket Connection Error [{2}] - Cannot connect to host {0}:{1} - Retrying ..." .format (self .host ,self .port , ose ))
60
+ time .sleep (2 )
48
61
49
62
def close ( self ):
50
63
try :
51
64
self .socket .shutdown ( socket .SHUT_RDWR )
52
65
self .socket .close ()
66
+ self .socket = None
53
67
54
68
except AttributeError :
55
69
pass
56
70
71
+ def write ( self , data ):
72
+
73
+ while True :
74
+ if self .socket is None :
75
+ self .__connect ()
57
76
77
+ else :
78
+ try :
79
+ self .logger .debug ('Sending {2} to {0}:{1}' .format (self .host , self .port , data ))
80
+ self .socket .sendall ( data .encode ( self .encoding ) )
81
+ break
58
82
59
- def write ( self , data ):
60
- if self .socket is None :
61
- self .__connect ()
83
+ except OSError as ex :
84
+ self .logger .error ("Error [{0}] writing to endpoint {1}:{2} -- Retrying..." .format (ex , self .host , self .port ))
85
+ time .sleep (1 )
86
+ self .socket = None
62
87
63
- self .socket .send ( data .encode ( self .encoding ) )
0 commit comments