From 445c92c1b2219b8282d93d989718fa290e46db90 Mon Sep 17 00:00:00 2001 From: James Muscat Date: Sat, 15 Nov 2014 17:12:23 +0000 Subject: [PATCH] Skeletal but functional system. Gateway just pumps messages into zmq to a Relay, Client parses them as JSON and spits them out. --- src/eddn/Client.py | 21 ++++++++++++++++ src/eddn/Gateway.py | 39 +++++++++++++++++++++++++++++ src/eddn/conf/Settings.py | 52 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 src/eddn/Client.py create mode 100644 src/eddn/Gateway.py diff --git a/src/eddn/Client.py b/src/eddn/Client.py new file mode 100644 index 0000000..0a84d98 --- /dev/null +++ b/src/eddn/Client.py @@ -0,0 +1,21 @@ +import zlib +import zmq.green as zmq +import simplejson +import sys + + +def main(): + context = zmq.Context() + subscriber = context.socket(zmq.SUB) + + subscriber.connect('tcp://localhost:9500') + subscriber.setsockopt(zmq.SUBSCRIBE, "") + + while True: + market_json = zlib.decompress(subscriber.recv()) + market_data = simplejson.loads(market_json) + print market_data + sys.stdout.flush() + +if __name__ == '__main__': + main() diff --git a/src/eddn/Gateway.py b/src/eddn/Gateway.py new file mode 100644 index 0000000..36dc003 --- /dev/null +++ b/src/eddn/Gateway.py @@ -0,0 +1,39 @@ +""" +Contains the necessary ZeroMQ socket and a helper function to publish +market data to the Announcer daemons. +""" +import logging +import zlib +import zmq.green as zmq +from eddn.conf import Settings + +logger = logging.getLogger(__name__) + +# This socket is used to push market data out to the Announcers over ZeroMQ. +context = zmq.Context() +sender = context.socket(zmq.PUB) +# Get the list of transports to bind from settings. This allows us to PUB +# messages to multiple announcers over a variety of socket types +# (UNIX sockets and/or TCP sockets). +for binding in Settings.GATEWAY_SENDER_BINDINGS: + sender.bind(binding) + + +def push_message(parsed_message): + """ + Spawned as a greenlet to push parsed messages through ZeroMQ. + """ + #=========================================================================== + # try: + # # This will be the representation to send to the Announcers. + # json_str = unified.encode_to_json(parsed_message) + # except TypeError: + # logger.error('Unable to serialize a parsed message.') + # return + #=========================================================================== + + # Push a zlib compressed JSON representation of the message to + # announcers. + compressed_msg = zlib.compress(parsed_message) + logging.info("Message recieved, sending...") + sender.send(compressed_msg) diff --git a/src/eddn/conf/Settings.py b/src/eddn/conf/Settings.py index 4859629..88037c0 100644 --- a/src/eddn/conf/Settings.py +++ b/src/eddn/conf/Settings.py @@ -4,8 +4,56 @@ Created on 15 Nov 2014 @author: james ''' -RELAY_RECEIVER_BINDINGS = ["tcp://*:8500"] +import logging.config -RELAY_SENDER_BINDINGS = ["tcp://*:9500"] +############################################################################### +# Relay settings +############################################################################### + +RELAY_RECEIVER_BINDINGS = ["tcp://127.0.0.1:8500"] + +RELAY_SENDER_BINDINGS = ["tcp://127.0.0.1:9500"] RELAY_DECOMPRESS_MESSAGES = False + +############################################################################### +# Gateway settings +############################################################################### + +GATEWAY_SENDER_BINDINGS = ["tcp://127.0.0.1:8500"] + +############################################################################### +# Logging Settings +############################################################################### +LOGGING = { + 'version': 1, + 'disable_existing_loggers': True, + 'formatters': { + 'verbose': { + 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s' + }, + 'simple': { + 'format': '%(name)s -- %(levelname)s -- %(asctime)s: %(message)s' + }, + }, + 'handlers': { + 'console': { + 'level': 'DEBUG', + 'class': 'logging.StreamHandler', + 'formatter': 'simple' + }, + 'null': { + 'level': 'DEBUG', + 'class': 'logging.NullHandler', + }, + }, + 'loggers': { + '': { + 'handlers': ['console'], + 'level': 'INFO', + 'propagate': True, + }, + }, +} + +logging.config.dictConfig(LOGGING)