Esempio n. 1
0
def main():
    if rank == 0:
        n = int(input("n: "))

        if n <= p:
            print("count of elements should be more than count of processes")
            return 0

        array = [random.randint(-100000000, 100000000) for _ in range(n)]

        # S count of elements for per process = N count of elements / P count of processes
        # and round to the nearest integer
        s = round(n / p)

        # processes minimum
        mins = [min(array[:s])]

        for pi in range(1, p):
            start = pi * s
            end = start + s
            send(data=array[start:end], dest=pi)
            mins.append(receive(source=pi))

        print("processes mins:", mins)
        print("final:", min(mins))
    else:
        parent_process = 0
        array = receive(source=parent_process)
        min_num = min(array)
        send(data=min_num, dest=parent_process)
Esempio n. 2
0
def main():
    if rank == 0:
        m = int(input("count of subarray:"))
        n = int(input("count of elements:"))

        if n != p:
            print("count of elements should be equals to count of processes")
            return 0

        array = [[random.randint(-1000000, 1000000) for _ in range(n)]
                 for _ in range(m)]
        responses = [multiple(array[:][0])]

        for i in range(1, n):
            vector = []
            for j in range(m):
                vector.append(array[j][i])
            send(data=vector, dest=i)
            responses.append(receive(source=i))

        print(sum(responses))
    else:
        parent_rank = 0
        vector = receive(source=parent_rank)
        send(data=multiple(vector), dest=parent_rank)
Esempio n. 3
0
def server_handle(server):
    server.bind(ADDR)
    server.listen()
    print(f'[SERVER UPDATE] Server is running and listening on port {PORT}')
    conn, addr = server.accept()
    print(f'[SERVER CONNECTION UPDATE] Connected to {addr}')
    print('[SERVER INFO] Waiting for initiation message')
    initiated = False
    while not initiated:
        initiated = initiate(conn)

    print(
        f'[SERVER CONNECTION UPDATE] Received initiation message from {addr}')
    connected = True
    while connected:
        message = receive(conn)
        if not message is None:
            if message == DISCONNECT_MESSAGE:
                connected = False
            else:
                global game
                response = game.get_info()
                send(conn, response)
                print(message)

    print(f'[SERVER CONNECTION UPDATE] {addr} disconnected')
    conn.close()
Esempio n. 4
0
def main():
    if rank == 0:
        try:
            data = int(input("Введите число: "))
            send(data * 2, rank + 1)
            data = receive(p - 1)
            print(data)
        except:
            send(0, rank + 1)
            data = receive(p - 1)
            print("Произошла ошибка", data)
    else:
        data = receive(rank - 1)
        if rank == p - 1:
            send(data * 2, 0)
        else:
            send(data * 2, rank + 1)
Esempio n. 5
0
def main():
    if p % 2 != 0:
        print("n должно быть четным")
        return

    if rank % 2 == 0:
        data = random.randint(1, 10)
        send(data, rank + 1)
    else:
        data = receive(rank - 1)
        display(rank, data, "sending to rank 0")
        send(data, 0)

    if rank == 0:
        data = 0
        for i in range(1, p, 2):
            data += receive(i)
        display(rank, data, "final result")
Esempio n. 6
0
def main():
    data = None
    if rank == p - 1:
        data = 125
        send(data, rank - 1)
    else:
        data = receive(rank + 1)
        if rank != 0:
            send(data, rank - 1)
    print(rank, data)
Esempio n. 7
0
def client_handle(client):
    client.connect(ADDR)
    print('[CLIENT CONNECTION UPDATE] Connected to the server')
    initiated = False
    send(client, INITIATION_MESSAGE)
    connected = True
    message = INITIATION_MESSAGE
    while connected:
        if message == DISCONNECT_MESSAGE:
            connected = False
        else:
            global game
            game.update(message)
            response = datetime.now()
            send(client, response)
            print(message)

        message = receive(client)
    print('[CLIENT CONNECTION UPDATE] Disconnecting from the server')
    client.close()
Esempio n. 8
0
decrypted_msg = rsa.decrypt(C, d, n)
print("\n\n local test for encryption and decryption\n\nmsg is: " + str(msg) + "\nEncrypted msg is: " + str(C)
      + "\nDecrypted msg is: " + str(decrypted_msg))


print("\n\n--------------------------------------- local check for signing and verification --------------------------"
      "-------------\n\n")


# A send signed msg to B
msg = rsa.random.randint(2, 99999)
print("msg is: " + str(msg))
signed_msg = rsa.send(msg, n1, e1, d, n)
print("encrypted msg is:" + signed_msg[0] + "\nsignature is: " + signed_msg[1])
# B received signed_msg and knows e and n
[check, k] = rsa.receive(signed_msg[0], signed_msg[1], n1, d1, e, n)
if check:
    print("Signature is valid\nencrypted msg is: " + str(k))
else:
    print("invalid signature")


print("\n\n--------------------------------------- user-server check for signing and verification"
      " ---------------------------------------\n\n")


# exchange with server
pub_k_request = f"http://asymcryptwebservice.appspot.com/rsa/serverKey?keySize=515"
keygen_request = requests.get(pub_k_request)
print("Server public key:" + str(keygen_request.json()))
server_n = int(keygen_request.json()["modulus"], 16)
Esempio n. 9
0
 def receive(self, data):
     receive(self, data)