-
Notifications
You must be signed in to change notification settings - Fork 2
/
minion.py
executable file
·85 lines (73 loc) · 3.12 KB
/
minion.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
#!/usr/bin/python
import socket, os, re, hashlib, time, threading
class SocketManager(threading.Thread):
def __init__(self, pid, name, hash, aff):
threading.Thread.__init__(self)
self.screen=aff
self.workspace=name
self.hash=hash
self.myAddress='\0sschat|'+str(pid)+'|'+self.hash
self.initPeers()
self.mySock=socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
self.mySock.bind(self.myAddress)
def run(self):
while 1:
incomingMessage = self.mySock.recvfrom(4096)
self.handlerGetComm(incomingMessage[0])
def handlerGetComm(self, incMess):
if incMess[0] == "/":
if incMess[1:4] == "add":
self.handlerAddPeer(incMess[5:])
if incMess[1:4] == "rem":
self.handlerRemPeer(incMess[5:])
if incMess[1:4] == "msg":
self.handlerGetMess(incMess[5:])
def handlerAddPeer(self, mess):
pid, name = mess.split("|")
self.peers.append(pid)
self.screen.setTitle(self.workspace, len(self.peers))
self.screen.printMessage(name+"("+pid+") joined.")
def handlerRemPeer(self, mess):
pid, name, reason = mess.split("|")
self.peers.remove(pid)
self.screen.setTitle(self.workspace, len(self.peers))
self.screen.printMessage(name+"("+pid+") leaved. (Reason:"+reason+")")
def handlerGetMess(self, message):
self.screen.printMessage(str(message))
def initPeers(self):
#je preferrerais aller les demander a un random peer
self.peers=[]
expr = re.compile(r'.*%s.*' % self.hash)
for line in open("/proc/net/unix"):
match = expr.search(line)
if match:
matchLine = match.group()
matchLine = matchLine.split("@", 1)
matchLine = matchLine[1]
matchLine = matchLine.split("|", 3)
self.peers.append(matchLine[1])
class Minion:
def __init__(self, workspaceName, aff, nickname):
self.myPid=os.getpid()
self.workspace=workspaceName
self.hashWorkspace=hashlib.sha256(self.workspace).hexdigest()
self.nickname=nickname
self.mySocket=SocketManager(self.myPid,self.workspace, self.hashWorkspace, aff)
self.mySocket.setDaemon(True)
self.mySocket.start()
message="/add "+str(self.myPid)+"|"+nickname
self.sendMessage(message)
def sendMessageTo(self, outMessage, peerPid, failed=0):
peerSock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
peerAddress = '\0sschat|'+str(peerPid)+'|'+self.hashWorkspace
try :
peerSock.sendto(outMessage, peerAddress)
except :
if failed >= 5:
self.mySocket.handlerRemPeer(peerPid+"|Somebody("+peerPid+")|DeliveryError")
return
failed += 1
self.sendMessageTo(outMessage, peerPid, failed)
def sendMessage(self, outMessage):
for peerPid in self.mySocket.peers:
self.sendMessageTo(outMessage, peerPid)