2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-07 15:20:23 +00:00
Comrad/p2p/p2p.py

64 lines
1.4 KiB
Python
Raw Normal View History

2020-08-16 14:59:36 +00:00
import logging
import asyncio
2020-08-18 09:01:22 +00:00
2020-08-16 14:59:36 +00:00
import shelve
from collections import OrderedDict
import pickle,os
# NODES_PRIME = [("128.232.229.63",8468), ("68.66.241.111",8468)]
2020-08-17 10:27:11 +00:00
NODES_PRIME = [("68.66.241.111",8468)]
2020-08-18 12:08:06 +00:00
async def echo(msg):
print('echo',msg)
def boot_selfless_node(port=8468, loop=None):
2020-08-17 16:23:40 +00:00
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
log = logging.getLogger('kademlia')
log.addHandler(handler)
2020-08-17 20:59:53 +00:00
log.setLevel(logging.INFO)
2020-08-17 16:23:40 +00:00
if not loop: loop = asyncio.get_event_loop()
2020-08-17 10:27:11 +00:00
loop.set_debug(True)
2020-08-17 20:59:53 +00:00
# shelf = HalfForgetfulStorage()
shelf = None
2020-08-18 12:08:06 +00:00
print('starting kad server')
2020-08-17 10:27:11 +00:00
2020-08-18 09:01:22 +00:00
#server = Server(storage=shelf)
try:
from kad import KadServer,HalfForgetfulStorage
except ImportError:
from .kad import KadServer,HalfForgetfulStorage
2020-08-18 09:01:22 +00:00
server = KadServer(storage=HalfForgetfulStorage())
loop.create_task(server.listen(port))
# try:
# loop.run_forever()
# except KeyboardInterrupt:
# pass
# finally:
# server.stop()
# loop.close()
return loop
2020-08-17 10:27:11 +00:00
def boot_lonely_selfless_node(port=8467):
loop = boot_selfless_node(port)
2020-08-17 10:27:11 +00:00
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
server.stop()
loop.close()
2020-08-16 14:59:36 +00:00