def write(register_n, new_value): for i in Ports: print("opening channel...") with grpc.insecure_channel(i) as channel: print("creating stub...") stub = abd_pb2_grpc.ABDServiceStub(channel) print("making write request...") #Since new value is given as an argument , no need of calcuating it #new_value = val + 1 response = stub.write( abd_pb2.WriteRequest(register=register_n, timestampe=int(time.time()), value=new_value)) Response.append(response) #If more than half of the servers respond, then success else failed if len(Response) >> int(len(Ports) / 2): print("Register Name:", register_n, " Value:", new_value, "Timestamp:", int(time.time())) print("Success") Response.clear() else: print("Failed") Response.clear()
def invoke(self, hp): ts = self.getTime() print("opening channel to {}".format(hp)) channel = grpc.insecure_channel(hp) print("creating stub to {}".format(hp)) stubs = abd_pb2_grpc.ABDServiceStub(channel) print("making W request to {} with ts:{}".format(hp, ts)) try: response = stubs.write( abd_pb2.WriteRequest(register=self.reg, timestampe=ts, value=self.value)) while self.acks_lock: time.sleep(1) self.acquireLock() self.acks[hp] = 'ACK' self.releaseLock() print('{}: responds with {}'.format(hp, 'ACK')) except: response = 'X' self.acquireLock() self.acks[hp] = response self.releaseLock() print('{}: responds with {}'.format(hp, response))
def invoke_2(self, hp): print("opening channel to {}".format(hp)) channel = grpc.insecure_channel(hp) print("creating stub to {}".format(hp)) stubs = abd_pb2_grpc.ABDServiceStub(channel) print("making R2 request to {}".format(hp)) try: response = stubs.read2( abd_pb2.Read2Request(register=self.reg, timestamp=self.max_ts, value=self.value)) #mod while self.acks_lock: time.sleep(1) self.acquireLock() self.acks[hp] = 'ACK' self.releaseLock() print('{}: responds with {}'.format(hp, self.acks[hp])) #mod except: response = 'X' self.acquireLock() self.acks[hp] = response self.releaseLock() print('{}: responds with {}'.format(hp, response))
def read(register_n): #Read1 Operation with value and timestamp as Response for i in Ports: print("opening channel...") with grpc.insecure_channel(i) as channel: print("creating stub...") stub = abd_pb2_grpc.ABDServiceStub(channel) print("making read1 request...") response = stub.read1(abd_pb2.Read1Request(register=register_n)) Response.append(response) #Calculating maximal of the values from each Server max_response = max(Response) Register = str(max_response) Q = Register.split() Response.clear() timestamp.append(int(Q[1])) val = Q[3] Value.append(val) #Read2 containg max value and it receives Ack(void) as Response for i in Ports: print("creating stub...") with grpc.insecure_channel(i) as channel: stub = abd_pb2_grpc.ABDServiceStub(channel) print("making read1 request... ") response = stub.read2( abd_pb2.Read2Request(register=register_n, timestamp=int(Q[1]), value=val)) Response.append(response) #If more than half of the servers respond, then print the value of register and version else fail. if len(Response) >> int(len(Ports) / 2): print("Value of Register and Version number:") print("Register Name: ", register_n) print("Value: ", val, "Timestamp: ", int(Q[1])) Response.clear() else: print("Failed") Response.clear()
def read(register_n): #Read1 Operation with value and timestamp as Response for i in Ports: print("II", Ports) print("opening channel...") with grpc.insecure_channel(i) as channel: print("creating stub...") stub = abd_pb2_grpc.ABDServiceStub(channel) print("making read1 request...") response = stub.read1(abd_pb2.Read1Request(register=register_n)) Response.append(response) All_value.append(response.value) All_timestamp.append(response.timestamp) #Calculating maximal of the values from each Server max1 = max(All_timestamp) d = All_timestamp.index(max1) value_1 = All_value[d] max_response = max(All_timestamp) Response.clear() #Read2 containg max value ytand it receives Ack(void) as Response for i in Ports: print("creating stub...") with grpc.insecure_channel(i) as channel: stub = abd_pb2_grpc.ABDServiceStub(channel) print("making read2 request... ") response = stub.read2( abd_pb2.Read2Request(register=register_n, timestamp=int(max1), value=value_1)) Response.append(response) #If more than half of the servers respond, then print the value of register and version else fail. if len(Response) >> int(len(Ports) / 2): print("Value of Register and Version number:") print("Register Name: ", register_n) print("Value: ", value_1, "Timestamp: ", max1) Response.clear() else: print("Failed") Response.clear()
def invoke(self, hp): print("opening channel to {}".format(hp)) channel = grpc.insecure_channel(hp) print("creating stub to {}".format(hp)) stubs = abd_pb2_grpc.ABDServiceStub(channel) print("making R1 request to {}".format(hp)) try: response = stubs.read1(abd_pb2.Read1Request(register=self.reg)) while self.acks_lock: time.sleep(1) self.acquireLock() self.acks[hp] = response self.releaseLock() print('{}: responds with {}'.format(hp, self.acks[hp])) #MOD except: response = 'X' self.acquireLock() self.acks[hp] = response self.releaseLock() print('{}: responds with {}'.format(hp, self.acks[hp])) #MOD
import grpc import abd_pb2 import abd_pb2_grpc print("opening channel") with grpc.insecure_channel('127.0.0.1:2222') as channel: print("creating stub") stub = abd_pb2_grpc.ABDServiceStub(channel) print("making request") response = stub.read1(abd_pb2.Read1Request(register="i am python")) print("got: " + str(response))
current_milli_time = lambda: int(round(time.time() * 1000)) server_list = connection_params.split(',') channels = {} stubs = {} num_acks = 0 num_faults = 0 ack_count_lock = threading.Lock() fault_count_lock = threading.Lock() for i, server in enumerate(server_list, 1): print("Client : creating channel to {}".format(server)) channels[i - 1] = grpc.insecure_channel(server) print("Client : channel created to {}".format(server)) print("Client : creating stub to {}".format(server)) stubs[i - 1] = abd_pb2_grpc.ABDServiceStub(channels[i - 1]) print("Client : stub created to {}".format(server)) # TODO: Subscribe() a callback to see if the connection state changes and look at how to handle ChannelConnectivity state changes. # Note: Not doing it as of now if operation.lower() == 'write': write_request = abd_pb2.WriteRequest(register=register_name, timestampe=current_milli_time(), value=value) result = communicate_threading_sync(write_request, channels, stubs) if isinstance(result, bool): # I only return false, so this check is enough. print('write failure') else: print('write successful') elif operation.lower() == 'read':