worker.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import json
  2. import config as cfg
  3. from connection.socketClient import Socket
  4. LenFunc = cfg.LenFunc
  5. class Worker():
  6. def __init__(self,child,parent):
  7. self.child = child
  8. self.parent = parent
  9. self.bearer = None
  10. self.crypter = None
  11. self.connector = None
  12. self.connectorIP = None
  13. self.connectorPort = None
  14. self.id = None
  15. self.server = None
  16. self.key_exchange = None
  17. def toJSON(self, data):
  18. return json.loads(data)
  19. def toSTRING(self, data):
  20. return json.dumps(data)
  21. def toSEND(self, data):
  22. j = json.dumps(data)
  23. l = LenFunc - len(str(len(j)))
  24. self.send("0000"[0:l]+str(len(j))+j)
  25. def send(self,Message):
  26. Message=bytes(Message,'utf-8')
  27. self.connector.sendall(Message)
  28. print("Send",Message)
  29. def receive(self):
  30. while True:
  31. len = self.connector.recv(LenFunc)
  32. if not len:
  33. break
  34. data = self.connector.recv(int(len))
  35. print("receive",data)
  36. self.parser(data)
  37. def parser(self,data,conn):
  38. data = self.toJSON(data)
  39. print(data)
  40. if (data["type"] == "init" ):
  41. self.id = data["id"]
  42. self.connectorIP = data["connectorIP"]
  43. self.connectorPort = data["connectorPort"]
  44. self.server = data["server"]
  45. self.child.start(data)
  46. elif (data["type"] == "oracleInit" ):
  47. if data["oracle"] == "Cryptor":
  48. self.crypter = conn
  49. elif data["oracle"] == "Bearer":
  50. self.bearer = conn
  51. else:
  52. print("Wrong INITIAL ORACLE")
  53. elif (data["type"] == "key_exchange" ):
  54. self.key_exchange = data["key_exchange"]
  55. self.child.createConnection()