-
Notifications
You must be signed in to change notification settings - Fork 1
/
DragonflyNode.py
139 lines (114 loc) · 3.99 KB
/
DragonflyNode.py
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
import mdlog
log = mdlog.getLogger(__name__)
from hotCode import importOrReload
import time, socket, errno, select, struct, os
if os.name == "posix":
import fcntl
from protocol import parseStream, HeartbeatMsg, makeJSON
from EventList import DisconnectedEvent
class DragonflyNode(object):
def __init__(self, eventQ=None):
self.other = None
self.lastMsgSendTime = time.time()
self.eventQ = eventQ
self.outgoing = []
self.nextMsgSize = 0
self.buf = ""
def makeSocket(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
if os.name == "posix":
fd = sock.fileno()
old_flags = fcntl.fcntl(fd, fcntl.F_GETFD)
fcntl.fcntl(fd, fcntl.F_SETFD, old_flags | fcntl.FD_CLOEXEC)
return sock
def retrieveMessages(self):
if self.other is None:
return
messages = []
try:
self.buf += self.recv()
(messages, self.buf, self.nextMsgSize) = parseStream(messages, self.buf, self.nextMsgSize)
except socket.timeout as e:
pass
except socket.error as e:
if e.errno == errno.EAGAIN or e.errno == errno.EINTR:
log.info(os.strerror(e.errno))
else:
self.dumpOther()
return
self.messageBatch(messages)
def messageBatch(self, messages):
for msg in messages:
self.onMessage(msg)
def heartbeat(self):
if self.other is None:
return
# heartbeating
newtime = time.time()
if newtime - self.lastMsgSendTime > 1 and self.other is not None:
self.sendMsg(makeJSON(HeartbeatMsg("")))
def recv(self):
# log.info('receiving...')
self.other.setblocking(0)
buf = []
received = True
try:
while received:
#received = unicode(self.other.recv(4096 * 1000), 'utf-8')
received = self.other.recv(4096 * 1000)
buf.append(received)
#log.info("received: [%s]" % received)
except socket.error as e:
if e.errno == errno.EWOULDBLOCK:
pass
else:
raise
# log.info("buf: [%s]" % buf)
return ''.join(buf)
def cleanup(self):
if self.other is not None:
self.other.shutdown(socket.SHUT_RDWR)
self.other.close()
self.other = None
def onMessage(self):
pass
def onConnect(self):
pass
def dumpOther(self):
if self.other is not None:
log.info('other lost')
self.other.close()
self.other = None
if self.eventQ:
self.eventQ.put(DisconnectedEvent)
def sendMsg(self, msg):
if self.other is None:
log.info("can't send msg, not connected")
return False
try:
try:
encodedMsg = msg.encode('utf-8')
#log.info("Sending: [%s]" % encodedMsg)
data = struct.pack("!I", len(encodedMsg)) + encodedMsg
self.other.settimeout(None)
self.other.sendall(data)
return True
except UnicodeDecodeError as e:
log.error(str(e))
log.error("attempted msg: [%s]" % msg)
self.lastMsgSendTime = time.time()
except socket.error as e:
log.info("Socket error: %s" % e)
if e.errno == errno.EPIPE or e.errno == errno.EBADF:
self.dumpOther()
else:
raise
except Exception as e:
log.info("Unknown error while sending: %s" % e)
self.dumpOther()
raise
return False
### DRAGONSHARE RSYNC