Skip to content

Commit

Permalink
running an offsite node
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayBlagoev committed Apr 25, 2024
1 parent 9f59053 commit 10a86cd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
9 changes: 6 additions & 3 deletions deccom/protocols/defaultprotocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ async def send_ping(self, addr, success, error, dt = 10):
while self.pings.get(msg_id) != None:
bts = os.urandom(4)
msg_id = int.from_bytes(bts, "big")

# with open(f"log{self.p.pub_key}.txt","a") as log:
# log.write(f"SENDING PING TO {addr} {bts}\n")
timeout = loop.call_later(dt+2,
self.timeout, addr,error,msg_id)
self.pings[msg_id] = (success, timeout)
Expand All @@ -102,14 +103,16 @@ async def send_ping(self, addr, success, error, dt = 10):
async def handle_ping(self, addr, data):
trmp = bytearray([DefaultProtocol.PONG])
trmp = trmp + data

# with open(f"log{self.p.pub_key}.txt","a") as log:
# log.write(f"RESPONDING PING TO {addr} {data}\n")
await self.send_datagram(trmp, addr=addr)

return

async def handle_pong(self, addr, data):
msg_id = int.from_bytes(data, "big")

# with open(f"log{self.p.pub_key}.txt","a") as log:
# log.write(f"rECEIVED PONG FROM {addr} {data}\n")
if self.pings.get(msg_id) is None:
return
success, timeout = self.pings[msg_id]
Expand Down
8 changes: 6 additions & 2 deletions deccom/protocols/streamprotocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from deccom.protocols.abstractprotocol import AbstractProtocol
from deccom.protocols.wrappers import *
import socket
import traceback
class DictItem:
def __init__(self,reader: asyncio.StreamReader,writer: asyncio.StreamWriter,fut: asyncio.Future, opened_by_me: int) -> None:
self.reader = reader
Expand Down Expand Up @@ -226,7 +227,7 @@ async def listen_for_data(self, reader: asyncio.StreamReader, node_id = None, ad
log.write(f"listening for data {node_id} \n")

try:
data = await reader.read(32)
data = await reader.readexactly(32)
except (ConnectionResetError, BrokenPipeError) as e:
with open(f"log{self.peer.pub_key}.txt", "a") as log:
log.write(datetime.now().strftime("%d/%m/%Y, %H:%M:%S"))
Expand Down Expand Up @@ -312,7 +313,10 @@ def set_stream_close_callback(self, callback):
self.stream_close_callback = callback
async def _caller(self,data,node_id,addr):
print("received data... ", len(data))
self.stream_callback(data,node_id,addr)
try:
self.stream_callback(data,node_id,addr)
except Exception:
traceback.print_exc(file=log)
@bindfrom("stream_callback")
def process_data(self,data,node_id,addr):

Expand Down
31 changes: 31 additions & 0 deletions discovery_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# this node is used as a template for
# a default running bootstrap node


import asyncio
from deccom.protocols.peerdiscovery import KademliaDiscovery
from deccom.protocols.defaultprotocol import DefaultProtocol
from deccom.nodes import Node
from deccom.protocols.securityprotocols import Noise
from deccom.peers.peer import Peer


base_peer = Peer(None, "ava")


protocol = DefaultProtocol()
discovery = KademliaDiscovery([],interval=12, always_split = True)
discovery.set_lower(base_peer)
noise = Noise(strict=False)
noise.set_lower(discovery)
loop = asyncio.get_event_loop()
me = Node(base_peer , noise,port = 10015,call_back=lambda *args:...)
loop.run_until_complete(me.listen())
loop.run_forever()







0 comments on commit 10a86cd

Please sign in to comment.