Beispiel #1
0
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()
Beispiel #3
0
    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
Beispiel #4
0
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))
Beispiel #5
0
    #  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':
    print('begin read operation...')
    print('begin read1 operation of {}...'.format(register_name))
    read1_request = abd_pb2.Read1Request(register=register_name)
    responses = communicate_threading_sync(read1_request, channels, stubs)
    if isinstance(responses, bool):
        print('read failed')
        sys.exit(-1)
    print('read1 of {} complete : got {} responses'.format(
        register_name, len(responses)))
    # select the response with the largest label
    value, label = select_max_label(responses)
    print('read1 of {} : selected label : {} and value : {}'.format(
        register_name, label, value))
    print('begin read2 operation of {}...'.format(register_name))
    read2_request = abd_pb2.Read2Request(register=register_name,
                                         timestamp=label,
                                         value=value)
    result = communicate_threading_sync(read2_request, channels, stubs)