finally!!!

pull/20/head
quadrismegistus 4 years ago
parent fcd8c521b0
commit 5a9acfe1dd

@ -375,10 +375,7 @@ class MainApp(MDApp):
newdata=[]
for d in data:
self.log('data d:',d)
if not 'val' in d: continue
newdict = dict(d['val'].items())
newdict['timestamp']=float(d['time'])
newdata.append(newdict)
newdata.append(d)
# return index
return newdata

@ -266,7 +266,7 @@ class Api(object):
# encrypt?
encrypt_for_pubkey_b = serialize_pubkey(encrypt_for_pubkey)
time_b=str(timestamp).encode()
time_b=base64.b64encode(str(timestamp).encode('utf-8')) #.encode()
msg=value_bytes
# whole binary package
@ -313,8 +313,8 @@ class Api(object):
# get data
try:
encrypted_payload, decryption_tools = split_binary(entire_packet, sep=sep) #entire_packet.split(sep)
decryption_tools=split_binary(decryption_tools,sep=sep2)
encrypted_payload, decryption_tools = entire_packet.split(sep) #split_binary(entire_packet, sep=sep) #entire_packet.split(sep)
decryption_tools=decryption_tools.split(sep2) #split_binary(decryption_tools,sep=sep2)
except AssertionError as e:
self.log('!! decode_data() got incorrect format:',e)
@ -323,7 +323,7 @@ class Api(object):
### NEW FIRST LINE: Try to decrypt!
val=None
for keyname,privkey,pubkey in self.keys:
for keyname,privkey in self.keys.items():
try:
val = aes_rsa_decrypt(encrypted_payload,privkey,*decryption_tools)
#self.log('decrypted =',val)
@ -357,7 +357,7 @@ class Api(object):
# private_key=self.private_key_global
WDV={
'time':float(time_b.decode()),
'time':float(base64.b64decode(time_b).decode()),
'val':base64.b64decode(msg),
'to':receiver_pubkey_b,
'from':sender_pubkey_b,
@ -440,7 +440,7 @@ class Api(object):
def jsonize_res(res0):
# parse differently?
res=json.loads(res0.decode())
res=json.loads(base64.b64decode(res0).decode('utf-8'))
self.log(f'jsonize_res({res0} [{type(res0)}] --> {res} [{type(res)}')
return res
@ -465,7 +465,7 @@ class Api(object):
self.log(f'value_json = {value_json}')
set_res = await self.set(
key,
value_json.encode('utf-8'),
base64.b64encode(value_json.encode('utf-8')),
private_signature_key=private_signature_key,
encode_data=encode_data,
encrypt_for_pubkey=encrypt_for_pubkey)
@ -719,25 +719,23 @@ class Api(object):
async def get_posts(self,uri='/inbox/earth'):
self.log(f'api.get_posts(uri={uri}) --> ...')
index = await self.get(uri,decode_data=False)
self.log('api.get_posts index <-',index)
# if not index: return []
self.log('api.get_posts index1 <-',index,bool(index))
if not index: return []
index = json.loads(index)
index = json.loads(base64.b64decode(index).decode())
self.log('got index?',index,type(index))
# if index is None: return []
if index is None: return []
if type(index)!=list: index=[index]
self.log('api.get_posts index <-',index)
self.log('api.get_posts index2 <-',index)
index = [x for x in index if x is not None]
self.log('api.get_posts index <-',index)
self.log('api.get_posts index3 <-',index)
## get full json
uris = [os.path.join(uri,x) for x in index]
self.log('URIs:',uris)
x = await self.get(uris,decode_data=True)
stop
self.log('api.get_posts got back from .get() <-',x)
return [y for y in x if y is not None]

@ -171,8 +171,9 @@ class Server:
Returns:
:class:`None` if not found, the value otherwise.
"""
self.log("Looking up key %s" % key)
dkey = digest(key)
self.log("Looking up key %s %s" % (key,dkey))
# if this node has it, return it
if self.storage.get(dkey) is not None:
self.log(f'already have {key} ({dkey}) in storage, returning...')
@ -186,22 +187,22 @@ class Server:
found = None
while found is None:
spider = ValueSpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha)
self.log(f'spider crawling... {spider}')
found = await spider.find()
self.log('spider found <-',found,'for key',key,'(',dkey,')')
await asyncio.sleep(5)
#while found is None:
spider = ValueSpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha)
self.log(f'spider crawling... {spider}')
found = await spider.find()
self.log('spider found <-',found,'for key',key,'(',dkey,')')
#await asyncio.sleep(5)
self.log(f"Eventually found for key {key} value {found}")
if not found:
# if not found:
# return None
raise Exception('nothing found!')
#raise Exception('nothing found!')
# set it locally? @EDIT
if store_anywhere and found:
self.log(f'storing anywhere: {dkey} -> {found}')
self.storage[dkey]=found
# # set it locally? @EDIT
# if store_anywhere and found:
# self.log(f'storing anywhere: {dkey} -> {found}')
# self.storage[dkey]=found
return found
@ -240,19 +241,20 @@ class Server:
self.log(f"setting '%s' on %s" % (dkey.hex(), list(map(str, nodes))))
# if this node is close too, then store here as well
if store_anywhere:
self.log(f'store_anywhere -> {dkey} --> {value}')
self.storage[dkey]=value
else:
biggest = max([n.distance_to(node) for n in nodes])
if self.node.distance_to(node) < biggest:
self.log(f'< bigges -> {dkey} --> {value}')
self.storage[dkey] = value
biggest = max([n.distance_to(node) for n in nodes])
if self.node.distance_to(node) < biggest:
self.log(f'< bigges -> {dkey} --> {value}')
self.storage[dkey] = value
results = [self.protocol.call_store(n, dkey, value) for n in nodes]
results = await asyncio.gather(*results)
self.log(f'--> set() results --> {results}')
if store_anywhere:
self.log(f'store_anywhere -> {dkey} --> {value}')
self.storage[dkey]=value
# return true only if at least one store call succeeded
return any(results)

Loading…
Cancel
Save