def thread_handler(object, MYNAME, MYIP, MYPORT): # Connect to introduced clients with config.MAINLOCK: thread3 = network.myThread( len(config.NODES) + 1, "Client Thread", len(config.NODES) + 1) config.THREADS.update({object.nodeNum: thread3}) print("CURRENT THREADS:, ", config.THREADS.keys()) thread3.config("INTRO_CONNECT", MYNAME, MYIP, MYPORT) thread3.targetConfig(object.nodeNum, object.ip, object.port) thread3.start()
def test(): t = network.myThread(1, "Service Thread", 1) # test incomplete first message print("TESTING INCOMPLETE FIRST MESSAGE") m = TEST_MESSAGES[0][:5] print(m) ret = t.serialDecode(m) assert ret == None print(t.prevMessage) print("##") # parse 1 full messages print("TESTING ONE FULL MESSAGE") m = TEST_MESSAGES[0] print(m) t.firstMessage = True ret = t.serialDecode(m) print(ret) print("##") # parse greater than one message at a time print("TESTING THREE MESSAGES AT A TIME") m = TEST_MESSAGES[0] + TEST_MESSAGES[1] + TEST_MESSAGES[2] print(m) t.firstMessage = True ret = t.serialDecode(m) print(ret) print("##") # parse one message that is not sent fully all at once print("TEST ONE CHAR AT A TIME MESSAGE") t.firstMessage = True m = TEST_MESSAGES[0] for idx in range(len(m)): ret = t.serialDecode(m[idx]) if ret != None: print(ret, end="aaaaaaaa") # parse buffer that contains 1.5 full messages and then 0.5 full messages print("TEST 1.5 FULL MESSAGE THEN 0.5 MESSAGE") t.firstMessage = True m = TEST_MESSAGES[0] + TEST_MESSAGES[1][:5] m2 = TEST_MESSAGES[1][5:] ret = t.serialDecode(m) print(ret) ret = t.serialDecode(m2) print(ret) return 0
def block_logger(): with open( "nodeBlocks" + str(g.MYNAME) + "-" + str(g.INSTANCEID) + ".txt", "w") as fd: sorted_trans = [] with network.solvedBlocksDictLock: t = network.myThread(0, 0) for block in network.solvedBlocksDict.values(): for tx in t.getTxFromBlock(block): sorted_trans.append(tx.split()) sorted_trans = sorted(sorted_trans, key=lambda x: x[1]) for entry in sorted_trans: fd.write(str(entry[3] + " " + entry[4] + " " + entry[5])) fd.write("\n") fd.write("\n\n##################################\n\n") with network.solvedBlocksDictLock: for block in network.solvedBlocksDict.items(): fd.write(str(block)) fd.write("\n") fd.write("\n")
if __name__ == '__main__': """ Load Global Variables """ g.init() """ User input starts from here """ # Saves command line information newSys = SystemInput() g.BASEPORT = 2000 g.COMMAND, g.MYIP, g.MYNAME, g.MYPORT, g.INSTANCEID = newSys.run() print("main:", g.COMMAND, g.MYIP, g.MYNAME, g.MYPORT) # Setup ctrl+c handler: signal killer signal.signal(signal.SIGINT, SystemInput.signal_handler) """ Networking starts from here""" # Connect to service.py thread1 = network.myThread(1, "service") thread1.config(g.COMMAND, g.MYIP, g.MYNAME, g.MYPORT) g.THREADS.update({"service": thread1}) thread1.start() # Runs it's own server # this thread will handle requests from other nodes create # a connection to them to receive their gossip # in addition we also create an INTRODUCE message for # gossipsenderhandler so that it is able to create a send thread thread2 = network.myThread(2, "gossiprecvhandler") thread2.config(g.COMMAND, g.MYIP, g.MYNAME, g.MYPORT) g.THREADS.update({"gossiprecvhandler": thread2}) thread2.start() # this is the thread which will handle respones to INTRODUCE messages
''' if __name__ == '__main__': # test() # exit() """ Load Global Variables """ config.init() """ User input starts from here """ # Saves command line information newSys = SystemInput() COMMAND, MYNAME, MYIP, MYPORT = newSys.run() # Setup ctrl+c handler: signal killer signal.signal(signal.SIGINT, SystemInput.signal_handler) """ Networking starts from here""" # Connect to service.py thread1 = network.myThread(1, "Service Thread", 1) thread1.config(COMMAND, MYNAME, MYIP, MYPORT) config.THREADS.update({"service": thread1}) thread1.start() # Runs it's own server thread2 = network.myThread(2, "Server Thread", 2) thread2.config(COMMAND, MYNAME, MYIP, MYPORT) config.THREADS.update({"server": thread2}) thread2.start() # Wait for all threads to complete try: for t in config.THREADS.values(): t.join() except: