-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.py
executable file
·151 lines (117 loc) · 3.89 KB
/
misc.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
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python
import re,sys,socket,threading
from time import sleep,asctime,time
CONFFILE = "mcast.conf"
LOGFILE = "mcast.log"
LOGERROR=0
LOGWARNING=1
LOGCONTROL=2
LOGMESSAGE=3
LOGDEBUG=4
def readConf():
""" Le arquivo de configuracao mcast.conf e retorna dicionario
{opcao:valor} """
res = {}
for line in open(CONFFILE):
if not re.match("^(#|$)",line):
opt,value = [ i.strip() for i in line.split("=") ]
res[opt] = value
return res
class Log(object):
""" Salva mensagens no arquivo de log LOGFILE, respeitando o verboso
desejado escrito em CONFFILE """
def __init__(self,logFile=LOGFILE):
self.__conf = readConf()
self.__file = open(logFile,"a")
self.verbose = int(self.__conf["verbose"])
def log(self, verbose, msg):
""" Escreve mensagem de log no arquivo se verbose for <= ao verbose
definido no arquivo de configuracao """
if int(verbose) <= self.verbose:
timestamp = asctime()[4:].replace(":","")
msg = "%s: %s" % (timestamp,msg)
self.__write(msg)
if self.verbose == 10:
print msg
def __write(self,msg):
self.__file.write(msg+"\n")
self.__file.flush()
class Timeout(threading.Thread):
""" Roda em uma thread separada o metodo timeoutFunction que deve
existir dentro da classe passada como argumento em objMethod. O
argumento timeout determina de quanto em quanto tempo essa funcao deve
ser executada """
def __init__(self, objMethod, timeout):
threading.Thread.__init__(self)
self.__objMethod = objMethod
self.__timeout = timeout
self.__quit = False
def run(self):
while not self.__quit:
self.runMethod()
sleep(self.getTimeout())
def quit(self):
self.__quit = True
def getTimeout(self):
return self.__timeout
def runMethod(self):
return self.__objMethod()
class Server(object):
""" Armazena informacoes sobre um servidor """
def __init__(self,serverID,serverHostname,serverPort,alive=False):
self.__id = int(serverID)
self.__hostname = serverHostname
self.__alive = alive
self.__port = int(serverPort)
self.__ip = socket.gethostbyname(serverHostname)
self.__lastContact = 0.0
def getLastContact(self):
return self.__lastContact
def setLastContact(self):
self.__lastContact = time()
def getIP(self):
return self.__ip
def getID(self):
return self.__id
def getHostname(self):
return self.__hostname
def getPort(self):
return self.__port
def getAddr(self):
return (self.getPort(),self.getHostname())
def imalive(self):
return self.__alive
def setAlive(self):
self.setLastContact()
self.__alive = True
def setNotAlive(self):
self.__alive = False
def __cmp__(self, x):
if self.getID() == x.getID():
return 0
else:
return 1
class Request(object):
""" Esta classe armazena informacoes sobre uma requisicao.
request eh no formato -> IP:PORT:REQUEST """
def __init__(self,request):
self.__IP, self.__port, self.__request = request.split(":")
self.__hostName = socket.gethostbyaddr(self.__IP)[0]
def getIP(self):
return self.__IP
def getHostname(self):
return self.__hostName
def getPort(self):
return self.__port
def getRequest(self):
return self.__request
def __str__(self):
return "%s:%s:%s" %(self.getIP(),self.getPort(),self.getRequest())
def __cmp__(self,x):
i1,i2 = self.getIP(),x.getIP()
p1,p2 = self.getPort(),x.getPort()
r1,r2 = self.getRequest(),x.getRequest()
if i1 == i2 and p1 == p2 and r1 == r2:
return 0
else:
return 1