-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservent.py
168 lines (129 loc) · 5.45 KB
/
servent.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'''
TP3 - Redes de Computadores - servent.py
Desenvolvido por:
- Gabriela Brant Alves 2013062901
- Guilherme Rangel da Silva Moura 2013062960
'''
#Programa servent, responsavel pelo armazenamento da base de dados chave-valor e pelo controle da troca de mensagem com seus pares.
import socket
import sys
import struct
'''
Formato do quadro:
TYP TTL IP PORT SQN TXT
2 2 4 2 4
'''
def make_pkt(typ, ttl, ip, port, sqn, txt):
TYP = struct.pack('>H', typ)
TTL = struct.pack('>H', ttl)
IP = struct.pack('=4sl', socket.inet_aton(ip), socket.INADDR_ANY)
PORT = struct.pack('>H', port)
SQN = struct.pack('>I', sqn)
txt = map(lambda x: ord(x), txt)
txt = struct.pack('%dB' % len(txt), *txt)
pkt = TYP + TTL + IP + PORT + SQN + txt
return pkt
def make_pkt_client(typ, txt):
TYP = struct.pack('>H', typ)
txt = map(lambda x: ord(x), txt)
txt = struct.pack('%dB' % len(txt), *txt)
pkt = TYP + txt
return pkt
# Cria o dicionario de chave-valor passado no arquivo como parametro
def read_file(file_name):
try:
f = open(file_name, "r")
except (OSError, IOError) as e:
print "Problem to open the file."
dictionary = {}
for line in f:
if not line.isspace(): # verifica se a linha nao esta vazia
words = line.split()
if (words[0] != '#'): # primeira palavra da linha
if (str.strip(words[0][0]) != '#'): # primeiro caracter da palavra
key = str.strip(words[0])
text = words[1:]
values = ' '.join(text)
dictionary.update({key:values})
f.close()
return dictionary
def servent():
num_params = len(sys.argv)
if num_params < 3:
print 'Execution format: $ python servent.py [localport] [key-values] [ip1:port1] ... [ipN:PortN]'
sys.exit()
LOCALPORT = int(sys.argv[1])
key_values_file = sys.argv[2]
neighbors = list()
for i in range(3, num_params):
neighbors.append(sys.argv[i])
key_values = {}
key_values = read_file(key_values_file)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the port
server_address = ('localhost', LOCALPORT)
print 'Connection established at',server_address
sock.bind(server_address)
print '\nWaiting to receive message... \n'
sqn = 0 #sequence number
# Message types
# 1 - CLIREQ
# 2 - QUERY
# 3 - RESPONSE
msg_history = list()
while (1):
data, address = sock.recvfrom(250)
TYP = struct.unpack(">H", data[0:2])[0]
if(TYP == 1): # mensagem vinda do client
sqn = sqn + 1
address_client = address
TXT = data[2:] # chave que esta procurando
TTL = 3
print '\nTYP = CLIREQ'
print 'Client', address, 'is looking for key', 'TXT =', TXT
# Enviar a mnsg para todos os vizinhos
for i in neighbors:
IP_neighbor = i.split(":")[0]
PORT_neighbor = int(i.split(":")[1])
address_neighbor = (IP_neighbor,PORT_neighbor)
QUERY = make_pkt(2, TTL, address_client[0], address_client[1], sqn, TXT)
sock.sendto(QUERY, address_neighbor)
if TXT in key_values.keys():
# responder ao cliente que a chave foi encontrada
R = (TXT + '\t' + key_values[TXT] + '\0')
RESPONSE = make_pkt_client(3, R)
sock.sendto(RESPONSE, address_client)
#mensagem vinda de outro servent
elif (TYP == 2):
address_server_before = address
TTL = struct.unpack(">H", data[2:4])[0]
IP = socket.inet_ntoa(struct.unpack("=4sl", data[4:12])[0])
PORT = struct.unpack(">H", data[12:14])[0]
SQN = struct.unpack(">I", data[14:18])[0]
TXT = data[18:]
client_address = (IP,PORT)
print '\nTYP = QUERY'
print 'Message from servent', address, 'TTL =', TTL
print 'Client', client_address, 'is looking for key', TXT
# Se a mnsg n foi vista anteriormente, procurar chave
received_msg = (IP, PORT, SQN, TXT)
if (received_msg not in msg_history):
msg_history.append(received_msg)
# Alagamento confiavel OSPF
if TXT in key_values.keys():
# responder ao cliente que a chave foi encontrada
R = (TXT + '\t' + key_values[TXT] + '\0')
RESPONSE = make_pkt_client(3, R)
sock.sendto(RESPONSE, client_address)
TTL = TTL - 1
if TTL > 0:
# Manda pros vizinhos, menos aquele que chamou
for i in neighbors:
IP_neighbor = i.split(":")[0]
PORT_neighbor = int(i.split(":")[1])
address_neighbor = (IP_neighbor, PORT_neighbor)
if address_neighbor != address_server_before: # Verificando o vizinho que chamou
QUERY = make_pkt(TYP, TTL, IP, PORT, SQN, TXT)
sock.sendto(QUERY, address_neighbor)
if __name__ == "__main__":
sys.exit(servent())