def __init__(self, client_id, serverID): super(Client, self).__init__() # call __init__ from multiprocessing.Process self.server_id = multiprocessing.Value('i', 0) with self.server_id.get_lock(): self.server_id.value = serverID + 1 # (1,10) self.socket_status = multiprocessing.Value('i', 0) with self.socket_status.get_lock(): self.socket_status.value = 0 """ Read config from file process_info[id] = (ip, port) addr_dict[(ip, port)] = id """ self.process_info, self.addr_dict = configreader.get_processes_info() self.total_server = configreader.get_total_servers() self.batch_cmd = '' self.reconnect_try = 0 self.client_id = client_id # self.exit = sys.exit() """ init TCP connect to server """ with self.server_id.get_lock(): self.address = self.process_info[self.server_id.value] self.ip, self.port = self.address[0], self.address[1] # print(self.ip, self.port) for res in socket.getaddrinfo(self.ip, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM): af, socktype, proto, canonname, sa = res try: self.socket = socket.socket(af, socktype, proto) # build a socket with self.socket_status.get_lock(): self.socket_status.value = 1 except socket.error as msg: self.socket = None with self.socket_status.get_lock(): self.socket_status.value = 0 continue try: self.socket.connect(sa) # connect to socket except socket.error as msg: self.socket.close() self.socket = None with self.socket_status.get_lock(): self.socket_status.value = 0 continue break if self.socket is None: print('could not open socket') sys.exit(1) """ Init unicast channel """ print("client, no ordering") # pass Client obj as arg to Channel self.channel = Channel(self, self.client_id, self.socket, self.process_info, self.addr_dict)
def __init__(self, id, ordering): super(Process, self).__init__() # call __init__ from multiprocessing.Process self.id = id # Read config from file self.process_info, self.addr_dict = configreader.get_processes_info() address = self.process_info[id] ip, port = address[0], address[1] print(ip, port) # Init a socket self.socket = socket.socket( socket.AF_INET, socket.SOCK_DGRAM) self.socket.bind(address) # Init channel # Causal Order Channel if ordering == 'causal': print 'causal ordering' self.channel = CausalOrderChannel(self, self.id, self.socket, self.process_info, self.addr_dict) elif ordering == 'total': print 'total ordering' # Total Order Channel if id == 1: # Select process 1 to be the sequencer self.channel = TotalOrderChannel(self, self.id, self.socket, self.process_info, self.addr_dict, True) else: self.channel = TotalOrderChannel(self, self.id, self.socket, self.process_info, self.addr_dict) else: # Regular channel print 'no ordering' self.channel = Channel(self, self.id, self.socket, self.process_info, self.addr_dict)
def __init__(self, server_id, arg_consistency, w, r): super(Server, self).__init__() # get address and port info self.process_info, self.addr_dict = configreader.get_processes_info() self.address = self.process_info[server_id] self.ip, self.port = self.address[0], self.address[1] self.arg_consistency = arg_consistency self.lock = multiprocessing.Lock() self.server_id = server_id self.w = w self.r = r """ init TCP connect to clients """ for res in socket.getaddrinfo(self.ip, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): af, socktype, proto, canonname, sa = res try: print("establishing a socket") self.socket = socket.socket(af, socktype, proto) # establish a socket except socket.error as msg: self.socket = None continue try: print("binding to a socket") self.socket.bind(sa) # build to a socket print("listening") self.socket.listen(1) # listen except socket.error as msg: self.socket.close() self.socket = None continue break if self.socket is None: print('could not open socket') """ init consistency model """ if self.arg_consistency == "eventual": if self.server_id == 1: self.consistency = EventualConsistency(self, self.server_id, self.process_info, self.addr_dict, self.w, self.r, self.lock, True) else: self.consistency = EventualConsistency(self, self.server_id, self.process_info, self.addr_dict, self.w, self.r, self.lock) elif self.arg_consistency == "linearizability": if self.server_id == 1: self.consistency = LinearizabilityConsistency(self, self.server_id, self.process_info, self.addr_dict, self.lock, True) else: self.consistency = LinearizabilityConsistency(self, self.server_id, self.process_info, self.addr_dict, self.lock) else: print("consistency model not known")
def __init__(self, client_id, server_id): super(Client, self).__init__() # call __init__ from multiprocessing.Process # a shared variable to show serverID self.server_id = multiprocessing.Value('i', 0) with self.server_id.get_lock(): self.server_id.value = server_id + 1 # (1,10) # a shared variable to show socket status self.socket_status = multiprocessing.Value('i', 0) with self.socket_status.get_lock(): self.socket_status.value = 0 # read process config from file self.process_info, self.addr_dict = configreader.get_processes_info() self.total_server = configreader.get_total_servers() self.batch_cmd = '' self.reconnect_try = 0 self.client_id = client_id # init a TCP socket to connect to server with self.server_id.get_lock(): self.address = self.process_info[self.server_id.value] self.ip, self.port = self.address[0], self.address[1] # robust way to find an available port for res in socket.getaddrinfo(self.ip, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM): af, socktype, proto, canonname, sa = res try: self.socket = socket.socket(af, socktype, proto) # init TCP socket with self.socket_status.get_lock(): self.socket_status.value = 1 except socket.error as msg: self.socket = None with self.socket_status.get_lock(): self.socket_status.value = 0 continue try: self.socket.connect(sa) # connect to TCP socket except socket.error as msg: self.socket.close() self.socket = None with self.socket_status.get_lock(): self.socket_status.value = 0 continue break if self.socket is None: print('could not open socket') sys.exit(1) # init a unicast channel with delayed function, reuse code from MP1 self.channel = Channel(self, self.client_id, self.socket, self.process_info, self.addr_dict)
def __init__(self, client_id, serverID): super(Client, self).__init__() # call __init__ from multiprocessing.Process self.serverID = serverID - 1 # (0,9) self.client_id = client_id """ Read config from file process_info[id] = (ip, port) addr_dict[(ip, port)] = id """ self.process_info, self.addr_dict = configreader.get_processes_info() self.total_server = configreader.get_total_servers() self.address = self.process_info[self.serverID + 1] self.ip, self.port = self.address[0], self.address[1] self.batch_cmd = '' self.client_id = client_id self.reconnect_try = 0 """ init TCP connect to server """ self.address = self.process_info[self.serverID + 1] self.ip, self.port = self.address[0], self.address[1] for res in socket.getaddrinfo(self.ip, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM): af, socktype, proto, canonname, sa = res try: self.socket = socket.socket(af, socktype, proto) # build a socket except socket.error as msg: self.socket = None continue try: self.socket.connect(sa) # connect to socket except socket.error as msg: self.socket.close() self.socket = None continue break if self.socket is None: print('could not open socket') sys.exit(1) """ Init unicast channel """ print 'client, no ordering' # pass Client obj as arg to Channel self.channel = Channel(self, self.client_id, self.socket, self.process_info, self.addr_dict)
def __init__(self, id, ordering): super(Process, self).__init__() # call __init__ from multiprocessing.Process self.id = id # Read config from file self.process_info, self.addr_dict = configreader.get_processes_info() address = self.process_info[id] ip, port = address[0], address[1] print(ip, port) # Init a socket self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.socket.bind(address) # Init channel # Causal Order Channel if ordering == 'causal': print 'causal ordering' self.channel = CausalOrderChannel(self, self.id, self.socket, self.process_info, self.addr_dict) elif ordering == 'total': print 'total ordering' # Total Order Channel if id == 1: # Select process 1 to be the sequencer self.channel = TotalOrderChannel(self, self.id, self.socket, self.process_info, self.addr_dict, True) else: self.channel = TotalOrderChannel(self, self.id, self.socket, self.process_info, self.addr_dict) else: # Regular channel print 'no ordering' self.channel = Channel(self, self.id, self.socket, self.process_info, self.addr_dict)
def __init__(self, serverID, arg_consistency, W, R): super(Server, self).__init__() # get address and port info self.process_info, self.addr_dict = configreader.get_processes_info() self.address = self.process_info[serverID] self.ip, self.port = self.address[0], self.address[1] self.arg_consistency = arg_consistency self.lock = multiprocessing.Lock() self.serverID = serverID self.W = W self.R = R """ init TCP connect to clients """ for res in socket.getaddrinfo(self.ip, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): af, socktype, proto, canonname, sa = res try: print("building a socket") self.socket = socket.socket(af, socktype, proto) # build a socket except socket.error as msg: self.socket = None continue try: print("binding to a socket") self.socket.bind(sa) # build to a socket print("listening") self.socket.listen(1) # listen except socket.error as msg: self.socket.close() self.socket = None continue break if self.socket is None: print('could not open socket') """ init consistency model """ if (self.arg_consistency == "eventual"): if (self.serverID == 1): self.consistency = EventualConsistency(self, self.serverID, self.process_info, self.addr_dict, self.W, self.R, self.lock, True) else: self.consistency = EventualConsistency(self, self.serverID, self.process_info, self.addr_dict, self.W, self.R, self.lock) elif (self.arg_consistency == "linearizability"): if (self.serverID == 1): self.consistency = LinearizabilityConsistency( self, self.serverID, self.process_info, self.addr_dict, self.lock, True) else: self.consistency = LinearizabilityConsistency( self, self.serverID, self.process_info, self.addr_dict, self.lock) else: print("consistency model not known")