Fix zmq.green not handling zmq.RCVTIMEO in python 2.7 examples

This commit is contained in:
AnthorNet 2016-01-07 14:03:37 +01:00
parent 6e7b6b8b96
commit 7cc1e74130
2 changed files with 142 additions and 123 deletions

View File

@ -7,10 +7,11 @@ import sys, os, datetime, time
" Configuration
"""
__relayEDDN = 'tcp://eddn-relay.elite-markets.net:9500'
__timeoutEDDN = 600000
#__timeoutEDDN = 600000 # 10 minuts
__timeoutEDDN = 60000 # 1 minut
# Set False to listen to production stream
__debugEDDN = True;
# Set False to listen to production stream; True to listen to debug stream
__debugEDDN = False;
# Set to False if you do not want verbose logging
__logVerboseFile = os.path.dirname(__file__) + '/Logs_Verbose_EDDN_%DATE%.htm'
@ -27,7 +28,8 @@ __authorisedSoftwares = [
"EliteOCR",
"Maddavo's Market Share",
"RegulatedNoise",
"RegulatedNoise__DJ"
"RegulatedNoise__DJ",
"E:D Market Connector [Windows]"
]
# Used this to excludes yourself for example has you don't want to handle your own messages ^^
@ -100,119 +102,126 @@ def main():
echoLog('')
echoLog('')
poller = zmq.Poller()
poller.register(subscriber, zmq.POLLIN)
while True:
__message = subscriber.recv()
if __message == False:
socks = dict(poller.poll(__timeoutEDDN))
if socks:
if socks.get(subscriber) == zmq.POLLIN:
__message = subscriber.recv(zmq.NOBLOCK)
__message = zlib.decompress(__message)
__json = simplejson.loads(__message)
__converted = False
# Handle commodity v1
if __json['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/1' + ('/test' if (__debugEDDN == True) else ''):
echoLogJSON(__message)
echoLog('Receiving commodity-v1 message...')
echoLog(' - Converting to v2...')
__temp = {}
__temp['$schemaRef'] = 'http://schemas.elite-markets.net/eddn/commodity/2' + ('/test' if (__debugEDDN == True) else '')
__temp['header'] = __json['header']
__temp['message'] = {}
__temp['message']['systemName'] = __json['message']['systemName']
__temp['message']['stationName'] = __json['message']['stationName']
__temp['message']['timestamp'] = __json['message']['timestamp']
__temp['message']['commodities'] = []
__commodity = {}
if 'itemName' in __json['message']:
__commodity['name'] = __json['message']['itemName']
if 'buyPrice' in __json['message']:
__commodity['buyPrice'] = __json['message']['buyPrice']
if 'stationStock' in __json['message']:
__commodity['supply'] = __json['message']['stationStock']
if 'supplyLevel' in __json['message']:
__commodity['supplyLevel'] = __json['message']['supplyLevel']
if 'sellPrice' in __json['message']:
__commodity['sellPrice'] = __json['message']['sellPrice']
if 'demand' in __json['message']:
__commodity['demand'] = __json['message']['demand']
if'demandLevel' in __json['message']:
__commodity['demandLevel'] = __json['message']['demandLevel']
__temp['message']['commodities'].append(__commodity)
__json = __temp
del __temp, __commodity
__converted = True
# Handle commodity v2
if __json['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/2' + ('/test' if (__debugEDDN == True) else ''):
if __converted == False:
echoLogJSON(__message)
echoLog('Receiving commodity-v2 message...')
__authorised = False
__excluded = False
if __json['header']['softwareName'] in __authorisedSoftwares:
__authorised = True
if __json['header']['softwareName'] in __excludedSoftwares:
__excluded = True
echoLog(' - Software: ' + __json['header']['softwareName'] + ' / ' + __json['header']['softwareVersion'])
echoLog(' - ' + 'AUTHORISED' if (__authorised == True) else
('EXCLUDED' if (__excluded == True) else 'UNAUTHORISED')
)
if __authorised == True and __excluded == False:
# Do what you want with the data...
# Have fun !
# For example
echoLog(' - Timestamp: ' + __json['message']['timestamp'])
echoLog(' - Uploader ID: ' + __json['header']['uploaderID'])
echoLog(' - System Name: ' + __json['message']['systemName'])
echoLog(' - Station Name: ' + __json['message']['stationName'])
for __commodity in __json['message']['commodities']:
echoLog(' - Name: ' + __commodity['name'])
echoLog(' - Buy Price: ' + str(__commodity['buyPrice']))
echoLog(' - Supply: ' + str(__commodity['supply'])
+ ((' (' + __commodity['supplyLevel'] + ')') if 'supplyLevel' in __commodity else '')
)
echoLog(' - Sell Price: ' + str(__commodity['sellPrice']))
echoLog(' - Demand: ' + str(__commodity['demand'])
+ ((' (' + __commodity['demandLevel'] + ')') if 'demandLevel' in __commodity else '')
)
# End example
del __authorised, __excluded
echoLog('')
echoLog('')
del __converted
else:
print 'Disconnect from ' + __relayEDDN + ' (After timeout)'
echoLog('')
echoLog('')
sys.stdout.flush()
subscriber.disconnect(__relayEDDN)
echoLog('Disconnect from ' + __relayEDDN)
echoLog('')
echoLog('')
break
__message = zlib.decompress(__message)
__json = simplejson.loads(__message)
__converted = False
# Handle commodity v1
if __json['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/1' + ('/test' if (__debugEDDN == True) else ''):
echoLogJSON(__message)
echoLog('Receiving commodity-v1 message...')
echoLog(' - Converting to v2...')
__temp = {}
__temp['$schemaRef'] = 'http://schemas.elite-markets.net/eddn/commodity/2' + ('/test' if (__debugEDDN == True) else '')
__temp['header'] = __json['header']
__temp['message'] = {}
__temp['message']['systemName'] = __json['message']['systemName']
__temp['message']['stationName'] = __json['message']['stationName']
__temp['message']['timestamp'] = __json['message']['timestamp']
__temp['message']['commodities'] = []
__commodity = {}
if 'itemName' in __json['message']:
__commodity['name'] = __json['message']['itemName']
if 'buyPrice' in __json['message']:
__commodity['buyPrice'] = __json['message']['buyPrice']
if 'stationStock' in __json['message']:
__commodity['supply'] = __json['message']['stationStock']
if 'supplyLevel' in __json['message']:
__commodity['supplyLevel'] = __json['message']['supplyLevel']
if 'sellPrice' in __json['message']:
__commodity['sellPrice'] = __json['message']['sellPrice']
if 'demand' in __json['message']:
__commodity['demand'] = __json['message']['demand']
if'demandLevel' in __json['message']:
__commodity['demandLevel'] = __json['message']['demandLevel']
__temp['message']['commodities'].append(__commodity)
__json = __temp
del __temp, __commodity
__converted = True
# Handle commodity v2
if __json['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/2' + ('/test' if (__debugEDDN == True) else ''):
if __converted == False:
echoLogJSON(__message)
echoLog('Receiving commodity-v2 message...')
__authorised = False
__excluded = False
if __json['header']['softwareName'] in __authorisedSoftwares:
__authorised = True
if __json['header']['softwareName'] in __excludedSoftwares:
__excluded = True
echoLog(' - Software: ' + __json['header']['softwareName'] + ' / ' + __json['header']['softwareVersion'])
echoLog(' - ' + 'AUTHORISED' if (__authorised == True) else
('EXCLUDED' if (__excluded == True) else 'UNAUTHORISED')
)
if __authorised == True and __excluded == False:
# Do what you want with the data...
# Have fun !
# For example
echoLog(' - Timestamp: ' + __json['message']['timestamp'])
echoLog(' - Uploader ID: ' + __json['header']['uploaderID'])
echoLog(' - System Name: ' + __json['message']['systemName'])
echoLog(' - Station Name: ' + __json['message']['stationName'])
for __commodity in __json['message']['commodities']:
echoLog(' - Name: ' + __commodity['name'])
echoLog(' - Buy Price: ' + str(__commodity['buyPrice']))
echoLog(' - Supply: ' + str(__commodity['supply'])
+ ((' (' + __commodity['supplyLevel'] + ')') if 'supplyLevel' in __commodity else '')
)
echoLog(' - Sell Price: ' + str(__commodity['sellPrice']))
echoLog(' - Demand: ' + str(__commodity['demand'])
+ ((' (' + __commodity['demandLevel'] + ')') if 'demandLevel' in __commodity else '')
)
# End example
del __authorised, __excluded
echoLog('')
echoLog('')
del __converted
except zmq.ZMQError, e:
echoLog('')
echoLog('ZMQSocketException: ' + str(e))
subscriber.disconnect(__relayEDDN)
echoLog('Disconnect from ' + __relayEDDN)
echoLog('')
time.sleep(5)
echoLog('Disconnect from ' + __relayEDDN + ' (After receiving ZMQError)')
echoLog('ZMQSocketException: ' + str(e))
echoLog('')
time.sleep(10)

View File

@ -8,7 +8,8 @@ import time
" Configuration
"""
__relayEDDN = 'tcp://eddn-relay.elite-markets.net:9500'
__timeoutEDDN = 600000
#__timeoutEDDN = 600000 # 10 minuts
__timeoutEDDN = 60000 # 1 minut
@ -20,31 +21,40 @@ def main():
subscriber = context.socket(zmq.SUB)
subscriber.setsockopt(zmq.SUBSCRIBE, "")
subscriber.setsockopt(zmq.RCVTIMEO, __timeoutEDDN)
while True:
try:
subscriber.connect(__relayEDDN)
print 'Connect to EDDN'
sys.stdout.flush()
poller = zmq.Poller()
poller.register(subscriber, zmq.POLLIN)
while True:
__message = subscriber.recv()
if __message == False:
socks = dict(poller.poll(__timeoutEDDN))
if socks:
if socks.get(subscriber) == zmq.POLLIN:
__message = subscriber.recv(zmq.NOBLOCK)
__message = zlib.decompress(__message)
__json = simplejson.loads(__message)
print __json
sys.stdout.flush()
else:
print 'Disconnect from EDDN (After timeout)'
sys.stdout.flush()
subscriber.disconnect(__relayEDDN)
break
__message = zlib.decompress(__message)
__json = simplejson.loads(__message)
print __json
sys.stdout.flush()
except zmq.ZMQError, e:
print 'Disconnect from EDDN (After receiving ZMQError)'
print 'ZMQSocketException: ' + str(e)
sys.stdout.flush()
subscriber.disconnect(__relayEDDN)
time.sleep(5)
time.sleep(10)