This repository has been archived by the owner on May 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheddn.py
66 lines (57 loc) · 2.49 KB
/
eddn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import zlib
import zmq
import json
import time
from module.config import Config
from module.influence import ConsumeFSDJump
# Configuration specified for the EDDN connection
__EDDN_RELAY = Config.getString('eddn', 'relay')
__EDDN_TIMEOUT = Config.getInteger('eddn', 'timeout', 60000)
__EDDN_RECONNECT = Config.getInteger('eddn', 'reconnect', 10)
# Only interested in the Journal Schema ($schemaRef)
_SCHEMA_REFS = [ "http://schemas.elite-markets.net/eddn/journal/1", "https://eddn.edcd.io/schemas/journal/1" ]
def processMessage(message, logger):
"""Processes the specified message, if possible."""
try:
jsonmsg = json.loads(message)
if jsonmsg["$schemaRef"] in _SCHEMA_REFS:
content = jsonmsg["message"]
# Only interested in FSDJump or Location events
if content["event"] in ["FSDJump", "Location"]:
ConsumeFSDJump(content)
except Exception:
logger.exception('Received message caused unexpected exception, Message: %s' % message)
def main():
"""Main method that connects to EDDN and processes messages."""
logger = Config.getLogger("eddn")
context = zmq.Context()
subscriber = context.socket(zmq.SUB)
subscriber.setsockopt(zmq.SUBSCRIBE, "")
while True:
try:
subscriber.connect(__EDDN_RELAY)
logger.info('Connected to EDDN at %s', __EDDN_RELAY)
poller = zmq.Poller()
poller.register(subscriber, zmq.POLLIN)
while True:
socks = dict(poller.poll(__EDDN_TIMEOUT))
if socks:
if socks.get(subscriber) == zmq.POLLIN:
message = subscriber.recv(zmq.NOBLOCK)
message = zlib.decompress(message)
processMessage(message, logger)
else:
logger.warning('Disconnect from EDDN (After timeout)')
subscriber.disconnect(__EDDN_RELAY)
break
except zmq.ZMQError, e:
logger.warning('Disconnect from EDDN (After receiving ZMQError)', exc_info=True)
subscriber.disconnect(__EDDN_RELAY)
logger.debug('Reconnecting to EDDN in %d seconds.' % __EDDN_RECONNECT)
time.sleep(__EDDN_RECONNECT)
except Exception:
logger.critical('Unhandled exception occurred while processing EDDN messages.', exc_info=True)
break # exit the main loop
# Enable command line execution
if __name__ == '__main__':
main()