-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
542 additions
and
828 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,49 @@ | ||
FROM python:3 | ||
FROM ubuntu:17.04 | ||
|
||
RUN \ | ||
apt-get update \ | ||
&& apt-get install -y \ | ||
apt-utils \ | ||
build-essential \ | ||
cmake \ | ||
git \ | ||
wget \ | ||
libncurses5-dev \ | ||
libreadline-dev \ | ||
nettle-dev \ | ||
libgnutls28-dev \ | ||
libuv1-dev \ | ||
libmsgpack-dev \ | ||
libargon2-0-dev \ | ||
libssl-dev \ | ||
net-tools \ | ||
nmap \ | ||
&& apt-get dist-upgrade -y \ | ||
&& apt-get clean | ||
|
||
RUN wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz \ | ||
&& tar xfzv Python-3.6.3.tgz \ | ||
&& cd Python-3.6.3 \ | ||
&& ./configure \ | ||
&& make \ | ||
&& make install \ | ||
&& pip3 install cython | ||
|
||
RUN git clone --branch 1.3.6 https://github.com/savoirfairelinux/opendht.git \ | ||
&& cd opendht \ | ||
&& mkdir build \ | ||
&& cd build \ | ||
&& cmake .. -DCMAKE_INSTALL_PREFIX=/usr -DOPENDHT_PYTHON=On -DOPENDHT_LTO=On \ | ||
&& make -j8 \ | ||
&& make install \ | ||
&& cd ../.. \ | ||
&& rm -rf opendht | ||
|
||
ENV PYTHONUNBUFFERED 1 | ||
|
||
WORKDIR /code | ||
|
||
ADD requirements.txt /code | ||
RUN pip install -r requirements.txt | ||
RUN pip3 install -r requirements.txt | ||
|
||
ADD . /code |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,23 @@ | ||
from aiohttp import web | ||
|
||
from sn_agent.agent import AgentSettings | ||
from sn_agent.app import create_app | ||
import ssl | ||
|
||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
app = create_app() | ||
|
||
# sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23) | ||
# sslcontext.load_cert_chain('server.crt', 'server.key') | ||
|
||
# TODO Make the port configurable from the ENV | ||
web.run_app(app, port=8000) | ||
# web.run_app(app, port=8000, ssl_context=sslcontext) | ||
|
||
settings = AgentSettings() | ||
|
||
logger.info('Host setting: %s', settings.WEB_HOST) | ||
|
||
web.run_app(app, port=settings.WEB_PORT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import logging | ||
import os | ||
from aiohttp import web, WSMsgType | ||
from aiohttp.web_response import Response | ||
from jsonrpcserver.aio import methods | ||
|
||
from sn_agent.api.job import submit_job | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
async def http_handler(request): | ||
request = await request.text() | ||
response = await methods.dispatch(request) | ||
if response.is_notification: | ||
return web.Response() | ||
else: | ||
return web.json_response(response, status=response.http_status) | ||
|
||
|
||
WS_FILE = os.path.join(os.path.dirname(__file__), 'websocket.html') | ||
|
||
|
||
async def ws_handler(request): | ||
logger.debug('WebSocket Handler started') | ||
|
||
app = request.app | ||
|
||
resp = web.WebSocketResponse() | ||
|
||
ok, protocol = resp.can_prepare(request) | ||
if not ok: | ||
with open(WS_FILE, 'rb') as fp: | ||
return Response(body=fp.read(), content_type='text/html') | ||
|
||
await resp.prepare(request) | ||
|
||
logger.debug('WebSocket data received') | ||
|
||
try: | ||
|
||
request.app['sockets'].append(resp) | ||
|
||
async for msg in resp: | ||
|
||
logger.debug('Processing WebSocket message: %s', msg.type) | ||
|
||
if msg.type == WSMsgType.TEXT: | ||
|
||
response = await methods.dispatch(msg.data, app) | ||
if not response.is_notification: | ||
await resp.send_str(str(response)) | ||
|
||
elif msg.type == WSMsgType.ERROR: | ||
logger.debug('ws connection closed with exception %s' % resp.exception()) | ||
|
||
else: | ||
logger.debug("Unhandled message type") | ||
return resp | ||
return resp | ||
|
||
finally: | ||
request.app['sockets'].remove(resp) | ||
logger.debug('Someone disconnected.') | ||
|
||
|
||
async def on_shutdown(app): | ||
for ws in app['sockets']: | ||
await ws.close() | ||
|
||
def setup_api(app): | ||
methods.add(submit_job) | ||
|
||
app['sockets'] = [] | ||
|
||
app.router.add_post('/api', http_handler) | ||
app.router.add_get('/api/ws', ws_handler) | ||
|
||
app.on_shutdown.append(on_shutdown) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import logging | ||
from urllib.parse import urlparse | ||
|
||
import aiohttp | ||
|
||
from sn_agent import ontology | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
async def submit_job(context=None): | ||
process_job(context) | ||
|
||
return 'pong' | ||
|
||
|
||
async def process_job(app): | ||
logger.debug("Job submission") | ||
|
||
blockchain = app['blockchain'] | ||
dht = app['dht'] | ||
|
||
ontology_id = ontology.DOCUMENT_SUMMARIZER_ID | ||
agent_ids = blockchain.get_agents_for_ontology(ontology_id) | ||
|
||
available_agents = [] | ||
for agent_id in agent_ids: | ||
|
||
connection_info = dht.get(agent_id) | ||
|
||
for value in connection_info: | ||
logger.debug('received value: %s', value) | ||
|
||
if isinstance(value, dict): | ||
if 'url' in value.keys(): | ||
url = urlparse(value['url']) | ||
url_str = url.geturl() | ||
|
||
logger.debug('Connection URL: %s', url_str) | ||
# if url.scheme == 'ws' or url.scheme == 'wss': | ||
|
||
try: | ||
session = aiohttp.ClientSession() | ||
async with session.ws_connect(url_str, heartbeat=10000) as ws: | ||
|
||
logger.debug("************** Successfully connected to %s", url) | ||
async for msg in ws: | ||
if msg.type == aiohttp.WSMsgType.TEXT: | ||
if msg.data == 'close cmd': | ||
await ws.close() | ||
break | ||
else: | ||
await ws.send_str(msg.data + '/answer') | ||
elif msg.type == aiohttp.WSMsgType.CLOSED: | ||
break | ||
elif msg.type == aiohttp.WSMsgType.ERROR: | ||
break | ||
|
||
except aiohttp.ClientConnectorError: | ||
logger.error('Client Connector error for: %s', url_str) | ||
pass | ||
|
||
except aiohttp.ServerDisconnectedError: | ||
logger.error('Server disconnected error for: %s', url_str) | ||
pass | ||
|
||
except aiohttp.WSServerHandshakeError: | ||
logger.error('Incorrect WS handshake for: %s', url_str) | ||
pass | ||
|
||
except aiohttp.ClientOSError: | ||
logger.error('Client OS error for: %s', url_str) | ||
pass | ||
|
||
finally: | ||
session.close() |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.