This repository was archived by the owner on May 25, 2018. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathClient.py
More file actions
104 lines (78 loc) · 3.69 KB
/
Copy pathClient.py
File metadata and controls
104 lines (78 loc) · 3.69 KB
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
import time
import socket
import select
import Messenger
from Util import printd
from Messenger import MessageType
import random
import os
'''
This class is responsible for sending chat messages to the replicas
'''
class Client:
client_timeout = 2 # timeout for client response
def __init__ (self, replica_list, client_name):
# Each request should be identifiable by a client sequence number
self.client_seq_number = 0
self.replica_list = [x for x in replica_list]
self.connection_sockets = []
self.msg = None
self.client_name = client_name
with open('words.txt') as f:
self.word_list = f.readlines()
self.word_list = [x.strip() for x in self.word_list]
def connect_to_all_replicas (self):
for replica in self.replica_list:
self.connect_to_proposer(replica)
def connect_to_proposer (self, replica):
connected = False
while not connected:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # This is a bug if we move this out... see man connect for python
s.connect((replica[0], int(replica[1]))) # Connect to (ip, port)
header = '%8s' % len(self.client_name)
s.send(header + self.client_name)
connected = True
except Exception as e:
time.sleep(0) # yield thread
self.connection_sockets.append(s)
def recv_message (self):
while 1:
rd, wd, ed = select.select(self.connection_sockets, [], [], self.client_timeout)
if len(rd) == 0: # Haven't received a message in a while...
printd("Client {} timed-out and is resending {}." .format(self.client_name, self.msg))
Messenger.broadcast_message(self.connection_sockets, self.msg)
# Handle received messages
for s in rd:
message = Messenger.recv_message(s)
if int(message.split('-')[1]) < int(self.client_seq_number): # Only should accept one message back
pass
elif message is not '':
self.client_seq_number += 1 # move on to next client sequence number and next command
self.msg = None
return message
else: # We got a socket disconnection from one of our replicas which means its kaputs for good...
self.connection_sockets.remove(s)
def send_message (self, value):
self.msg = "{}:{},{},{}".format(MessageType.REQUEST.value, self.client_name, self.client_seq_number, value)
Messenger.broadcast_message(self.connection_sockets, self.msg)
#Messenger.send_message(self.connection_socket, full_msg)
printd("Client {} sent message to all replicas with value {}".format(self.client_name,str(value)))
def generate_msg_text (self):
text = ''
for i in range(random.randint(1,10)):
text = text + ' ' + str(self.word_list[random.randint(1,999)])
return text
def operate (self, num_messages, manual_messages):
printd("Client {} is operating".format(self.client_name))
for i in range(num_messages):
if manual_messages:
msg = raw_input("What is Client {}'s msg? ".format(self.client_name))
else:
msg = self.generate_msg_text()
self.send_message(str(msg))#self.client_seq_number) + ":" + msg)
recvd_msg = str(self.recv_message())
printd("Client received message {}.".format(recvd_msg))
time.sleep(.1)
# after completion, wait for the os to shut off client
while not False: time.sleep(1)