예제 #1
0
def mayBeLogout(message):
    if not isLogout(message):
        return False

    printProtocol(commit(Protocol.logout()),
                  [(ResponseCode.accepted, "Logout Successfully"),
                   (Any, "Command helper: \"logout\"")])
예제 #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()))
예제 #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
예제 #4
0
def mayBeListFiles(message):
    if not isListFiles(message):
        return False

    print(message)
    protocol = printProtocol(commit(Protocol.listFiles()),
                             [(ResponseCode.data, "Files:"),
                              (Any, "Command helper: \"logout\"")])

    if type(protocol) == Protocol and protocol.code == ResponseCode.data:
        for file in protocol.message.split(";"):
            print(file)
예제 #5
0
def mayBeAuth(message):
    result = isAuth(message)
    if not result[0]:
        return False

    (username, password) = result[1]

    printProtocol(commit(Protocol.auth(username, password)),
                  [(ResponseCode.accepted, "Authorized Connection"),
                   (ResponseCode.notPermitted, "Wrong username or password"),
                   (Any, "Command helper: \"auth user:password\"")])

    return True
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")
예제 #7
0
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
예제 #8
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))
예제 #10
0
 def onResponse(data):
     return Protocol.fromData(data)
예제 #11
0
 def onResponse(response):
     print(response)
     return Protocol.fromData(response)
예제 #12
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))