pull/20/head
quadrismegistus 4 years ago
parent da34b0f529
commit 3e053351e7

@ -348,7 +348,7 @@ class MainApp(MDApp):
if file_id: jsond['file_id']=str(file_id)
if file_ext: jsond['file_ext']=str(file_ext)
if channel and channel[0]=='@': channel=channel[1:]
self.log(f'''app.json(
self.log(f'''app.post(
content={content},
file_id={file_id},
file_ext={file_ext},
@ -374,6 +374,7 @@ class MainApp(MDApp):
return await self.api.get_post(post_id)
async def get_posts(self,uri='/inbox/world'):
if uri.count('/')<2: raise Exception('not a URI: '+uri)
self.log(f'app.get_posts(uri={uri} -> ...')
data = await self.api.get_posts(uri)
self.log('app.get_posts() got back from api.get_posts():',data)

@ -109,7 +109,7 @@ class PostScreen(ProtectedScreen):
post_json = {'author':self.app.username, 'timestamp':time.time()}
keys=[k for k in self.app.keys if k!=self.app.username]
if keys:
if False: # ??
key=keys[0]
post_json['to_name']=key
self.recipient=key

@ -55,6 +55,7 @@ if not os.path.exists(KEYDIR): os.makedirs(KEYDIR)
KEYDIR_BUILTIN = '.'
class NetworkStillConnectingError(OSError): pass
async def _getdb(self=None,port=PORT_LISTEN):
from kademlia.network import Server
@ -66,8 +67,12 @@ async def _getdb(self=None,port=PORT_LISTEN):
if self: self.log(os.getcwd())
node = Server(log=self.log if self else print) #fn='../p2p/data.db',log=(self.log if self else print)))
if self: self.log('listening..')
await node.listen(port)
try:
if self: self.log('listening..')
await node.listen(port)
except OSError:
raise NetworkStillConnectingError('Still connecting...')
#await asyncio.sleep(3)
if self: self.log('bootstrapping server..')
await node.bootstrap(NODES_PRIME)
@ -523,7 +528,14 @@ class Api(object):
## Register
async def register(self,name,passkey=None,just_return_keys=False):
# if not (name and passkey): return {'error':'Name and password needed'}
person = await self.get_person(name)
import kademlia
try:
person = await self.get_person(name)
except kademlia.network.CannotReachNetworkError:
return {'error':'Network disconnected'}
except NetworkStillConnectingError:
return {'error':'Network still connecting...'}
keys = self.get_keys()
if person is not None:
self.log('register() person <-',person)
@ -704,7 +716,7 @@ class Api(object):
return file_store
async def flush(self):
self.log('saving back to db file...')
#self.log('saving back to db file...')
node = await self.node
node.storage.dump()
# self.log('DONE saving back to db file...')

@ -9,6 +9,7 @@ import pickle
import asyncio
import logging
class CannotReachNetworkError(Exception): pass
from kademlia.protocol import KademliaProtocol
from kademlia.utils import digest
@ -175,7 +176,7 @@ class Server:
nearest = self.protocol.router.find_neighbors(node)
self.log(f'nearest = {nearest}')
if not nearest:
raise Exception("There are no known neighbors to get key %s" % key)
raise CannotReachNetworkError("There are no known neighbors to get key %s" % key)
found = None

Loading…
Cancel
Save