Example #1
0
    def auth(self, protocol):
        data = protocol.message.split(":")
        self.isAuthenticated = sign(data[0], data[1])
        if self.isAuthenticated:
            self.username = data[0]
            return Comunication.accepted()

        return Comunication.notPermitted()
Example #2
0
 def listFiles(self):
     path = self.path()
     onlyfiles = ";".join([
         f for f in os.listdir(path)
         if os.path.isfile(os.path.join(path, f))
     ])
     return Comunication.send(Protocol.data(onlyfiles.encode()))
Example #3
0
    def observe(self):
        protocol = self.transaction.waitForResponse()
        if not protocol:
            self.transaction.commit(Comunication.send(
                Protocol.cutConnection()))
            return False

        print(protocol.asRaw())
        self.didReceived(protocol)
        return True
Example #4
0
    def didReceived(self, protocol):
        if self.isAuthenticated:
            toCommit = self.asUser(protocol)
        else:
            toCommit = self.asGuest(protocol)

        if not toCommit:
            self.transaction.commit(Comunication.refused())
            return

        if type(toCommit) == Comunication:
            self.transaction.commit(toCommit)
def getActiveServers():
    def onResponse(response):
        print(response)
        return Protocol.fromData(response)

    transaction = Transaction(socket.socket(socket.AF_INET,
                                            socket.SOCK_STREAM))
    transaction.connect('localhost', 9889)
    protocol = transaction.commit(
        Comunication.send(Protocol.alive()).onResponse(onResponse))
    transaction.close()

    if not protocol:
        print("Can't communicate with main server")
        return None

    if protocol.code != ResponseCode.data:
        print("I've got an unexpected response")
        return None

    servers = protocol.message.split(";")
    print("Choose a server\n")

    for i in range(0, len(servers)):
        server = servers[i]
        print("(" + str(i) + ") " + str(" ".join(server.split(","))))

    print("(q) Quit")
    option = input("\n:: ")

    def communicationType(type):
        if type == "tcp":
            return socket.SOCK_STREAM
        return socket.SOCK_DGRAM

    if not option.isnumeric():
        return None

    option = int(option)
    server = servers[int(option)].split(",")
    t = Transaction(socket.socket(socket.AF_INET,
                                  communicationType(server[1])))
    try:
        t.connect(server[0], int(server[2]))
        return t
    except:
        print("Server offline")
        return None
Example #6
0
    def asUser(self, protocol):
        if protocol.code == ResponseCode.logout:
            self.isAuthenticated = False
            self.username = ""
            return Comunication.accepted()
        if protocol.code == ResponseCode.send:
            if len(protocol.message.split("/")) >= 2:
                return self.transaction.refused()

            return self.transaction.send(self.path() + protocol.message)

        if protocol.code == ResponseCode.receive:
            if len(protocol.message.split("/")) >= 2:
                return self.transaction.refused()

            return self.transaction.receive(self.path() + protocol.message)

        if protocol.code == ResponseCode.listFiles:
            return self.listFiles()
Example #7
0
    def asGuest(self, protocol):
        if protocol.code == ResponseCode.auth:
            return self.auth(protocol)

        return Comunication.send(Protocol.notPermitted())
import sys, random, time
import threading

import socket
from NetworkCore import Protocol, Transaction, ResponseCode, Comunication

transaction = Transaction(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
transaction.connect('localhost', 9889)

def onResponse(response):
      print(response)
      protocol = Protocol.fromData(response)
      if (not protocol):
            return

      if protocol.code == ResponseCode.accepted:
            print("Consumer: did consume one item")
            return

      if protocol.code == ResponseCode.refused:
            print("Consumer: sleeping")
            return
      
      print("Consumer: error")

while (True):
      transaction = Transaction(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
      transaction.connect('localhost', 9889)
      transaction.commit(Comunication.send(Protocol.data("2".encode())).onResponse(onResponse))
      transaction.close()
      time.sleep(random.randint(0, 5))
def commit(protocol):
    def onResponse(data):
        return Protocol.fromData(data)

    return transaction.commit(
        Comunication.send(protocol).onResponse(onResponse))
Example #10
0
from NetworkCore import Protocol, Transaction, ResponseCode, Comunication

transaction = Transaction(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
transaction.connect('localhost', 9889)


def onResponse(response):
    print(response)
    protocol = Protocol.fromData(response)
    if (not protocol):
        return

    if protocol.code == ResponseCode.accepted:
        print("Worker: did produce one item")
        return

    if protocol.code == ResponseCode.refused:
        print("Worker: sleeping")
        return

    print("Worker: error")


while (True):
    transaction = Transaction(socket.socket(socket.AF_INET,
                                            socket.SOCK_STREAM))
    transaction.connect('localhost', 9889)
    transaction.commit(
        Comunication.send(Protocol.data("1".encode())).onResponse(onResponse))
    transaction.close()
    time.sleep(random.randint(0, 5))