def message_handler(conn, addr):
    raw_data = conn.recv(1024)
    data = protocolUtils.MessageHandler(raw_data).message_loads()
    server_interface = switch_operations(data[1])

    result = protocolUtils.MessageBuilder(server_interface[0], server_interface[1], False).message_builder()
    conn.send(result.encode())
Exemplo n.º 2
0
def main():
    try:
        num1 = input("Insert a number: ")
        num2 = input("Insert a number: ")
        op = input("Insert the operation that you wants: ")

        message_builder = protocolUtils.MessageBuilder(num1, num2, op)
        function_op = protocolUtils.ClientThread(server_of_names.get_server(message_builder.operation))
        function_op.start()

        op1, op2 = message_builder.get_operands()
        print("The result are: ", function_op.call_function(op1, op2))

    except client.Fault as e:
        print(e)
Exemplo n.º 3
0
 def handle(self):
     protocol_instance = protocolUtils.MessageHandler(self.request.recv(1024))
     array_operands = protocol_instance.message_loads()
     if array_operands[1] == "+":
         result = self.operation(array_operands[0], array_operands[2])
         self.request.send(str(result).encode())
     else:
         self.request.send("Bad Request")
Exemplo n.º 4
0
def message_handler(conn, address):
    while True:
        raw_data = conn.recv(1024)
        received_data = protocolUtils.MessageHandler(raw_data).message_loads()

        if received_data[0] == "list_groups":
            print("New list_groups operation from ", address)
            conn.send(process_group_instance.get_all_groups())

        elif received_data[0] == "create_group":
            print("New group created by ", address)
            process_group_instance.add_process(address)
            conn.send("Group already created.".encode())

        elif received_data[0] == "join_group":
            error = False
            try:
                group_id = int(received_data[2])
            except ValueError as e:
                error = True
                conn.send("Incorrect group id.".encode())
            print("New request to join into a group from ", address,
                  " to the group ", group_id)
            result = process_group_instance.add_process(address, group_id)
            if result:
                conn.send("You're now into the group".encode())
            else:
                conn.send("Incorrect group id.".encode())

        elif received_data[0] == "send_message":
            error = False
            try:
                group_id = int(received_data[2])
            except ValueError as e:
                error = True
                conn.send("Incorrect group id.".encode())
            list_members = process_group_instance.get_group(group_id)
            print("New message from ", address, " to the group ", group_id)
            if list_members:
                send_messages = thread.Thread(target=thread_broadcast_group,
                                              args=(
                                                  list_members,
                                                  received_data[1],
                                              ))
                send_messages.start()
            else:
                error = True
                conn.send(
                    "Incorrect group id or the group doesn't have members.".
                    encode())

            if not error:
                conn.send("Message sent.".encode())

        else:
            conn.send("yesss".encode())
Exemplo n.º 5
0
def message_handler(conn, addr):
    data = protocolUtils.MessageHandler(conn.recv(1024)).message_loads()

    print("New operation in queue ", data[0], " ", data[2])
    try:
        result = str(float(data[0]) - float(data[2]))
    except ValueError:
        result = "The operands requires be numbers"

    conn.send(result.encode())
Exemplo n.º 6
0
def message_handler(conn, addr):
    raw_data = conn.recv(1024)
    data = protocolUtils.MessageHandler(raw_data).message_loads()
    server_interface = switch_operations(data[1])

    server_instance = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_instance.connect((server_interface[0], server_interface[1]))

    server_instance.send(raw_data)
    result = server_instance.recv(1024)
    conn.send(result)
    def handle(self):
        server_prop = self.request.recv(1024).decode("utf-8")
        server_prop = self.get_server(server_prop)

        if server_prop:
            builder_instance = protocolUtils.MessageBuilder(
                server_prop[0], server_prop[1], True)
            self.request.send(builder_instance.message_builder().encode())
        else:
            error = "Error: Operation doesnt exists"
            self.request.send(error.encode())
Exemplo n.º 8
0
def main():
    try:
        num1 = input("Insert a number: ")
        num2 = input("Insert a number: ")
        op = input("Insert the operation that you wants: ")

        message_builder = protocolUtils.MessageBuilder(num1, num2, op)
        function_op = switch_operations(message_builder.operation)

        op1, op2 = message_builder.get_operands()
        print("The result are: ", function_op(op1, op2))

    except client.Fault as e:
        print(e)
Exemplo n.º 9
0
def main():
    socket_instance = socket.socket()
    socket_instance.connect((protocolUtils.host, protocolUtils.port))

    num1 = input("Ingrese un numero: ")
    num2 = input("Ingrese un numero: ")
    op = input("Ingrese la operacion a realizar: ")

    message_builder = protocolUtils.MessageBuilder(num1, num2, op)

    socket_instance.send(message_builder.operation.encode())

    server = protocolUtils.MessageHandler(
        socket_instance.recv(1024)).message_loads()
    server_host, server_port = server[0], int(server[2])

    # Close the actual session to start the new with the server of the operation
    socket_instance.close()

    socket_instance = socket.socket()
    socket_instance.connect((server_host, server_port))
    socket_instance.send(message_builder.message_builder().encode())
    result = socket_instance.recv(1024).decode()
    print("Result: ", result)
Exemplo n.º 10
0
def main():
    try:
        num1 = input("Insert a number: ")
        num2 = input("Insert a number: ")
        op = input("Insert the operation that you wants: ")

        message_builder = protocolUtils.MessageBuilder(num1, num2, op)

        # Get the absolute URL to do the connection directly
        server_op = names_resolver_server.get_operation_server(message_builder.operation)
        # Do the connection
        server_op = client.ServerProxy(server_op)

        op1, op2 = message_builder.get_operands()
        print("The result are: ", server_op.function(op1, op2))
    except client.Fault:
        print("Error: The operation is not supported")
Exemplo n.º 11
0
def message_handler(conn, addr):
    data = protocolUtils.MessageHandler(conn.recv(1024)).message_loads()

    print("New operation in queue ", data[0], " ", data[2])
    try:
        val1 = float(data[0])
        val2 = float(data[2])
    except ValueError:
        result = "The operands requires be numbers"
        conn.send(result.encode())

    try:
        result = str(math.pow(val1, (1 / val2)))
        conn.send(result.encode())
    except ZeroDivisionError as e:
        result = "You cant do root by 0"
        conn.send(result.encode())
Exemplo n.º 12
0
    for member in list_of_members:
        try:
            socket_temp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            socket_temp.connect((member[0], member[1]))
            socket_temp.send(message.encode())
        except Exception as e:
            print("Disconnected client.")
    # while True:
    #     socketInsa = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #     socketInsa.connect((address[0], address[1]))
    #     socketInsa.send("Yupale".encode())


if __name__ == "__main__":

    process_group_instance = protocolUtils.ProcessGroup()

    socket_instance = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socket_instance.bind(('', 9999))
    socket_instance.listen(10)

    threads_list = []
    print("Server running ...")
    while True:
        conn, address = socket_instance.accept()
        print("New connection entry from ", address)
        temp_thread = thread.Thread(target=message_handler,
                                    args=(
                                        conn,
                                        address,
                                    ))
Exemplo n.º 13
0
import socket
import lib.ProtocolUtils as protocolUtils

if __name__ == "__main__":
    socket_instance = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socket_instance.connect(("localhost", 9999))

    num1 = input("Ingrese un numero: ")
    num2 = input("Ingrese un numero: ")
    op = input("Ingrese la operacion a realizar: ")

    message_builder = protocolUtils.MessageBuilder(num1, num2, op)
    socket_instance.send(message_builder.message_builder().encode())

    server_interface = protocolUtils.MessageHandler(
        socket_instance.recv(1024)).message_loads()
    server_instance = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_instance.connect((server_interface[0], int(server_interface[2])))

    server_instance.send(message_builder.message_builder().encode())
    result = server_instance.recv(1024)
    print("Resultado de la operacion fue ", result.decode("utf-8"))
    socket_instance.close()
Exemplo n.º 14
0
def main():
    # Create server
    server = protocolUtils.ServerThread("localhost", 9005)
    server.register_class_functions(RegisteredFunctions())
    server.start()
    print("Running pow server...")
Exemplo n.º 15
0
import socket
import lib.ProtocolUtils as protocolUtils

if __name__ == "__main__":
    # socket_instance = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # socket_instance.connect(("localhost", 9999))
    #
    # print(socket_instance.getsockname())
    #
    #
    # num1 = input("Ingrese un numero: ")
    # num2 = input("Ingrese un numero: ")
    # op = input("Ingrese la operacion a realizar: ")
    #
    # message_builder = protocolUtils.MessageBuilder(num1, num2, op)
    # socket_instance.send(message_builder.message_builder().encode())
    #
    # result = socket_instance.recv(1024)
    # print("Resultado de la operacion fue ", result.decode("utf-8"))
    # socket_instance.close()
    client = protocolUtils.ClientBuilder()
Exemplo n.º 16
0
import socket
import lib.ProtocolUtils as protocolUtils

if __name__ == "__main__":
    socket_instance = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socket_instance.connect(("localhost", 9999))

    num1 = input("Ingrese un numero: ")
    num2 = input("Ingrese un numero: ")
    op = input("Ingrese la operacion a realizar: ")

    message_builder = protocolUtils.MessageBuilder(num1, num2, op)
    socket_instance.send(message_builder.message_builder().encode())

    result = socket_instance.recv(1024)
    print("Resultado de la operacion fue ", result.decode("utf-8"))
    socket_instance.close()
def main():
    # Create server
    server = protocolUtils.ServerThread("localhost", 8060)
    server.register_class_functions(RegisteredFunctions())
    server.start()