2017-07-29 09:54:03 +00:00
|
|
|
import logging
|
2017-06-04 22:27:44 +00:00
|
|
|
import os
|
2017-07-29 09:54:03 +00:00
|
|
|
from asyncio import get_event_loop, gather, ensure_future
|
2017-05-25 10:47:01 +00:00
|
|
|
from base64 import b64decode
|
|
|
|
|
2017-06-04 22:27:44 +00:00
|
|
|
import yaml
|
2017-07-29 09:54:03 +00:00
|
|
|
from colorlog import ColoredFormatter
|
2017-06-04 22:27:44 +00:00
|
|
|
from nacl.signing import SigningKey
|
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
from ssb.api import MuxRPCAPI
|
2017-05-25 10:47:01 +00:00
|
|
|
from ssb.packet_stream import PSClient
|
|
|
|
|
2017-06-04 22:27:44 +00:00
|
|
|
|
|
|
|
with open(os.path.expanduser('~/.ssb/secret')) as f:
|
|
|
|
config = yaml.load(f)
|
|
|
|
|
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
api = MuxRPCAPI()
|
2017-05-25 10:47:01 +00:00
|
|
|
|
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
@api.define('createHistoryStream')
|
|
|
|
def create_history_stream(connection, msg):
|
|
|
|
print('create_history_stream', msg)
|
|
|
|
# msg = PSMessage(PSMessageType.JSON, True, stream=True, end_err=True, req=-req)
|
|
|
|
# connection.write(msg)
|
2017-05-25 10:47:01 +00:00
|
|
|
|
2017-06-04 22:27:44 +00:00
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
@api.define('blobs.createWants')
|
|
|
|
def create_wants(connection, msg):
|
|
|
|
print('create_wants', msg)
|
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
handler = packet_stream.stream({
|
|
|
|
'name': 'createHistoryStream',
|
|
|
|
'args': [{
|
|
|
|
'id': "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519",
|
|
|
|
'seq': 1,
|
|
|
|
'live': False,
|
|
|
|
'keys': False
|
|
|
|
}],
|
|
|
|
'type': 'source'
|
|
|
|
})
|
|
|
|
async for msg in handler:
|
|
|
|
print('> RESPONSE:', msg)
|
|
|
|
|
|
|
|
|
|
|
|
# create console handler and set level to debug
|
|
|
|
ch = logging.StreamHandler()
|
|
|
|
ch.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
# create formatter
|
|
|
|
formatter = ColoredFormatter('%(log_color)s%(levelname)s%(reset)s:%(bold_white)s%(name)s%(reset)s - '
|
|
|
|
'%(cyan)s%(message)s%(reset)s')
|
|
|
|
|
|
|
|
# add formatter to ch
|
|
|
|
ch.setFormatter(formatter)
|
|
|
|
|
|
|
|
# add ch to logger
|
|
|
|
logger = logging.getLogger('packet_stream')
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
logger.addHandler(ch)
|
|
|
|
|
|
|
|
server_pub_key = b64decode(config['public'][:-8])
|
|
|
|
|
|
|
|
loop = get_event_loop()
|
2017-06-04 22:27:44 +00:00
|
|
|
packet_stream = PSClient('127.0.0.1', 8008, SigningKey.generate(), server_pub_key, loop=loop)
|
|
|
|
packet_stream.connect()
|
2017-07-29 09:54:03 +00:00
|
|
|
api.add_connection(packet_stream)
|
|
|
|
|
|
|
|
loop.run_until_complete(gather(ensure_future(api), main()))
|
2017-05-25 10:47:01 +00:00
|
|
|
loop.close()
|