/
snAppy17a.py
executable file
·908 lines (676 loc) · 34.8 KB
/
snAppy17a.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
#
"""#
from twisted.internet import reactor
import sys, time
import os
from snAppyModules.snUseCases import *
#from snAppyTests.snTests import *
from snAppyModules.snQueryComposers import *
from snAppyModules.snParsers import *
from snAppyModules.snAppyConfig import *
from snAppyModules.pyDaemon3 import Daemon3
from twisted.internet import task
from twisted.python import threadpool as tp
class nxtClientFactory(ClientFactory):
def __init__(self, ):
super(nxtClientFactory, self).__init__()
#log.msg("2c --nxtClientFactory----> build Client Protocol" )
self.ok=True
class nxtServerFactory(ServerFactory):
def __init__(self, queryComposers, parsers, environ):
super(nxtServerFactory, self).__init__()
self.ok=True
#print(environ)
self.parser_XML = parsers['parser_XML']
self.parser_777 = parsers['parser_777']
self.parser_RPC = parsers['parser_RPC']
self.parser_LOC = parsers['parser_LOC']
self.parser_LOC.environ = environ
self.qComp_777 = queryComposers['qComp_777'] # .API_calls
self.qComp_XML = queryComposers['qComp_XML']
self.qComp_LOC = queryComposers['qComp_LOC']
def buildProtocol(self, addr):
proto = ProxyServerProtocolSuperNET()
# this is where we plug this namespace into the Protocol!
proto.proxyServerFactory = self #
log.msg(1*"\n1 nxtServerFactory----> builds a Protocol", str(proto), '@',str(addr))
return proto
# Adapted from http://stackoverflow.com/a/15645169/221061
class ProxyServerProtocolSuperNET(protocol.Protocol):
"""# Client => Proxy
# Here we receive the initial request.
# Here we parse the request
# Here we route the request to the client that sends the specific query to the remote server.
"""#
def __init__(self):
self.buffer = None
self.client = None
def connectionMade(self):
""" here we must connect to EITHER jl777 OR some external data feed
so either we do this as a deferred OR we move this into the GET processing routine where we find out what API is called
Instantiate CLientFactory here, but not yet the Protocoal, because the Req needs to be parsed first in order to decide which Prot.
""" #
#log.msg("ProxyServerProtocolSuperNET 1 ----> build Protocol: connectionMade")
self.clientFactory = nxtClientFactory()
# NOTE: the nxtServerFactory instance is always the same, but the nxtClientFactory is a new one every time.
def dataReceived(self, rawRequest):
""" Protocol ONLY knows dataReceived. dataReceived Has to be overridden, and the processing pipeline added:
render_GET, etc... """ #
# ToDo: check mechnism: - self.client? this has to do with connection status somehow.
if self.client:
log.msg("ProxyServerProtocolSuperNET 2a::\n\n", str(rawRequest))
self.client.write(rawRequest)
else:
#log.msg("ProxyServerProtocolSuperNET 2b--dataReceived- ", str(rawRequest))
self.identify_req_type(rawRequest)
def identify_req_type(self, rawRequest):
"""- this is low level as opposed to the Site API
Doing this low level stuff myself is a bit tedious but it offers flexibility for custom mods.
Called whenever data is received.
Use this method to translate to a higher-level message. Usually, some
callback will be made upon the receipt of each complete protocol
message.
We can process both, GET and POST! (POST not implemented yet)
""" #
POST = rawRequest[:4]==bytes('POST',"utf-8")
GET = rawRequest[:3]==bytes('GET',"utf-8")
if POST:
self.render_POST(rawRequest)
elif GET:
self.render_GET(rawRequest)
#log.msg("ProxyServerProtocolSuperNET 3--parse_request-- ", str(rawRequest), " time: " ,str(time.time()))
else:
self.transport.loseConnection()
raise RuntimeError( ("request: "+str(rawRequest) ))
return None
def render_GET(self, request):
"""
The requests are received here as raw bytestrings.
Example: rawrequst: <class 'bytes'>
b'GET /nxt?requestType=getpeers HTTP/1.1\r\nUser-Agent: curl/7.35.0\r\nHost: 127.0.0.1:7777\r\nAccept: */*\r\ncontent-type: text/plain;\r\n\r\n'
We don't use a lib here because it is very easy and we can't get into trouble with libs.
Here we do all kinds of parsing. Can be modified at will to adapt.
""" #
#log.msg("ProxyServerProtocolSuperNET 4 render_GET GET. time---> ",str(time.time()))
reqMeth = request.split()[1]
###################################################################
#
# here we have some internal contollers to start and stop use cases, and to stop the whole api
#
###################################################################
stopSelf = ( reqMeth[:6].decode("utf-8") == '/stop?')
if stopSelf:
log.msg("snApi caught stop- shutting down @ time: " ,str(time.time()))
self.transport.loseConnection()
os.kill(os.getpid(),15)
startUC = ( reqMeth[:6].decode("utf-8") == '/ucstart?')
if startUC:
log.msg("snApi caught startUC- @ time: " ,str(time.time()))
self.transport.loseConnection()
return None
stopUC = ( reqMeth[:6].decode("utf-8") == '/ucstop?')
if startUC:
log.msg("snApi caught stopUC- @ time: " ,str(time.time()))
self.transport.loseConnection()
return None
reqOK = ( reqMeth[:5].decode("utf-8") == '/nxt?') # drop all else snAp.py
if not reqOK:
self.transport.write(b'unspecific error in ProxyServerProtocolSuperNET.render_GET')
self.transport.loseConnection()
return None
reqMeth = reqMeth[5:]
reqMeth = reqMeth.decode()
reqLi = reqMeth.split('&')
reqDict = {}
if len(reqLi) < 1:
raise LookupError(str(request))
self.transport.loseConnection()
return None
for req in reqLi:
try:
reqDict[req.split('=')[0]] = req.split('=')[1]
except:
raise LookupError(str(request))
self.transport.loseConnection()
if 'requestType' not in reqDict.keys():
self.transport.loseConnection()
return None
#
# This invokes different Client Server/Factory instances depending on what calls are requested.
# port, parser, qcomposer
#
if reqDict['requestType'] in self.proxyServerFactory.qComp_777.API_calls:
# send to the 777Composer
# self.clientFactory.protocol - the 'protocol' MUST remain GENERIC!!! keep this Note here.
self.clientFactory.protocol = ProxyClientProtocol777
self.clientFactory.protocol.parser_777 = self.proxyServerFactory.parser_777
self.clientFactory.server = self #
self.clientFactory.requestType = reqDict['requestType'] # just put it in here to be available for the Parser!
reactor.connectTCP( SERVER_ADDR_jl777, SERVER_PORT_SUPERNETHTTP, self.clientFactory)
#print(6*"\nreqDict:", reqDict)
self.newQuery = self.proxyServerFactory.qComp_777.make_777POST_Request(reqDict)
#print(6*"\nquery:", self.newQuery)
elif reqDict['requestType'] in ["start","stop" ]:
# direct to BTCD RPC, use that parser ONLY for START and stop call that must go through BTCD
# Note: there is a 'passthrough' call in the api to talk to RPC coins.
# only 'start' and 'stop' can't be issued to SuperNET directly.
# self.clientFactory.protocol - the 'protocol' MUST remain GENERIC!!! keep this Note here.
self.clientFactory.protocol = ProxyClientProtocolRPC
self.clientFactory.protocol.parser_RPC = self.proxyServerFactory.parser_RPC
self.clientFactory.server = self #
self.clientFactory.requestType = reqDict['requestType'] # just put it in here to be available for the Parser!
reactor.connectTCP( SERVER_ADDR_jl777, SERVER_PORT_BTCD_RPC, self.clientFactory)
# NOTE: this uses the same querycomposer that 777 also uses!!!
self.newQuery = self.proxyServerFactory.qComp_777.make_rawBytes_Request(reqDict)
self.transport.loseConnection() # this is because we won't get a reply afte stoppping and we must close ourself!
elif reqDict['requestType'] in self.proxyServerFactory.qComp_XML.API_calls:
# self.clientFactory.protocol - the 'protocol' MUST remain GENERIC!!! keep this Note here.
self.clientFactory.protocol = ProxyClientProtocolXML
self.clientFactory.protocol.parser_XML = self.proxyServerFactory.parser_XML
self.clientFactory.server = self #
self.clientFactory.requestType = reqDict['requestType'] # just put it in here to be available for the Parser!
reactor.connectTCP(SERVER_ADDR_xmlFeed1, SERVER_PORT_xmlFeed1, self.clientFactory)
self.newQuery = self.proxyServerFactory.qComp_XML.make_XML_Request(reqDict)
elif reqDict['requestType'] in self.proxyServerFactory.qComp_LOC.API_calls:
self.clientFactory.protocol = ProxyClientProtocolLOC
self.clientFactory.protocol.parser_LOC = self.proxyServerFactory.parser_LOC
self.clientFactory.server = self #
self.clientFactory.requestType = reqDict['requestType'] # just put it in here to be available for the Parser!
#
# LOCAL handling is different, we don't use POST or GET, but do other stuff as load files or so.
#
# use deferToThread in UCclasses
#
dataLocalCacheBytes = self.proxyServerFactory.qComp_LOC.make_LOC_Request(reqDict)
dataLocalCacheParsedBytes = self.proxyServerFactory.parser_LOC.parse_XML( dataLocalCacheBytes, reqDict)
self.write(dataLocalCacheParsedBytes)
# call the ClientReceiving Function DIRECTLY here, we don't send the request through the interwebz
self.newQuery = 'dummyUpForLocCache'
return None # MAYBE GOOD to do this here because we go to self.write() DIRECTLY FROM HERE!?!?!
else:
raise LookupError(str(request))
self.transport.loseConnection()
return None
if self.newQuery == 'error':
raise LookupError(str(request))
self.transport.loseConnection()
return None
def write(self, data):
""" Here the processed reply finally leaves the proxy again and goes back to the requester:
ServerPartOfProxy => WebClient
This function expects fully processed and formatted reply data, it only returns the data w/o any further processing.
""" #
log.msg("ProxyServerProtocolSuperNET 5: proxy returns to client:", str(len(data)), str(type(data)), str((data)[:100]))#
self.transport.write(data)
self.transport.loseConnection()
class ProxyClientProtocolLOC(protocol.Protocol):
""" This ProxyClient does local stuff """#
pass
class ProxyClientProtocolRPC(protocol.Protocol):
""" This ProxyClient queries the SuperNET server using POST DIRECTLY RPC """#
def connectionMade(self):
self.factory.server.client = self
try:
requestOUT = self.factory.server.newQuery #.decode("utf-8")
except:
self.transport.loseConnection()
return None
# two end Protocol: here we WRITE out to the remote.
# we expect the dataReceived back. What if that doenst happen? as in stop or start?
self.transport.write(requestOUT)
self.factory.server.requestOUT = '' # cleanup
def read_raw_ERR(self):
pass
# NEED A DEFERRED TO READ RAW
def read_raw_POST(self, data_RPC):
print("**** data_777 --->", str(data_RPC))
pass
def dataReceived(self, data_RPC):
""" this receives the RAW reply from the jl777lib. POSTprocessing needs to be done. """ #
log.msg("ProxyClientProtocolRPC dataReceived - from remoteServer LENGTH:: ", str(len(data_RPC)))
log.msg("ProxyClientProtocolRPC - from remoteServer data_777: ", str((data_RPC)))
data_RPC_parsed = self.parser_RPC.parse_RPC(data_RPC, self.factory.requestType )
self.factory.server.write(data_RPC_parsed)
self.transport.loseConnection()
return None
class ProxyClientProtocol777(protocol.Protocol):
""" This ProxyClient queries the SuperNET server using POST DIRECTLY """#
def connectionMade(self):
self.factory.server.client = self
#log.msg(15*"\n~~~~~~~~~~~~~>", self.factory.server.newQuery)
try:
preppedReq777 = self.factory.server.newQuery
except:
self.transport.write(b'ERROR in ProxyClientProtocol777.connectionMade')
self.transport.loseConnection()
return None
#log.msg(15*"\n~~~~~~~~~~~~~>", preppedReq777)
self.deferred = deferToThread(requests.post, FULL_URL, data=json.dumps(preppedReq777), headers= POSTHEADERS)
self.deferred.addCallback(self.rcvPOST)
self.deferred.addErrback(self.rcvPOSTERR)
# stat1 = reactor.threadpool.waiters
# stat2 = reactor.threadpool.workers
# stat3 = reactor.threadpool.threads
# stat4 = reactor.threadpool.q.queue
#
# log.msg("waiters: ", stat1)
# log.msg("workers: ", stat2)
# log.msg("threads: ", stat3)
# log.msg("queue: ", stat4)
# putting this into a Session() object fails miserabley due to some threading!
# this is for raw transport level bytes writing!
# self.transport.write(requestOUT)
# self.factory.server.requestOUT = '' # cleanup
def rcvPOSTERR(self,retPOSTERR):
log.msg(10*"\n++++++++++ERRR in ProxyClientProtocol777+++", retPOSTERR, str(retPOSTERR))
self.factory.server.write("rcvPOSTERR() ERROR")
self.transport.loseConnection()
return None
def rcvPOST(self, data_777):
""" this receives the RAW reply from the jl777lib. POSTprocessing needs to be done. """ #
log.msg("ProxyClientProtocol777 dataReceived - from remoteServer type:: ", type(data_777))
# here we receive the prepared data for sending back through the port to the GET requester
data_777_parsedBytes = self.parser_777.parse_777(data_777, self.factory.requestType )
#print(data_777_parsedBytes)
self.factory.server.write(data_777_parsedBytes) #data_777_parsed)
self.transport.loseConnection()
return None
class ProxyClientProtocolXML(protocol.Protocol):
""" This ProxyClient is using the TWISTED getPage function """#
def connectionMade(self):
self.factory.server.client = self
try:
requestOUT = self.factory.server.newQuery #.decode("utf-8")
except:
self.transport.loseConnection()
return None
self.getPage_deferred = getPage(requestOUT)
# """can I do multiple here??"""
self.getPage_deferred.addCallback(self.pageReceived)
self.getPage_deferred.addErrback(self.handleFailure)
self.factory.server.requestOUT = ''
self.query_xmlFeed1 = False
log.msg("-2e--ProxyClientProtocolXML----requestOUT -----register deferred ---------->:",requestOUT) # only for GET , ppOST is different
# Server => Proxy
def handleFailure(self, err):
raise RuntimeError(str(err))
# this will be the deferreed
def pageReceived(self, data_XML):
log.msg("3 ProxyClientProtocolXML dataReceived - from remoteServer: ", str(len(data_XML)))
log.msg(1*"\n3a dataReceived - from remoteServer: ", str(data_XML)[:200])
print(self.parser_XML)
print(self.parser_XML.parse_XML)
print(self.factory.requestType)
data_XML_parsed = self.parser_XML.parse_XML(data_XML, self.factory.requestType)
self.factory.server.write(data_XML_parsed)
self.transport.loseConnection()
return None
class SuperNETApiD(Daemon3):
""" This is the SuperNET API Main class.
This is instantiated by the DEMON controller app.
This is supposed to be a singleton,
and so are the class objects that are declared up here. """#
# instead of doing this in the class head here, we could also make a local method 'def makeContext'
environ['snApiDir'] = os.getcwd()
environ['localCacheDir'] = os.getcwd() + environ['CACHE_DIR']
# note: there is something really unpleasant going on with the 'environ name here!
# this creates the ApiCOntext!
qComp_777 = QueryComposer_777(environ) # Querycomposer is part of the SERVER part, composes the query, and hands the Q to the CLIENT part.
qComp_XML = QueryComposer_XML(environ)
qComp_LOC = QueryComposer_LOC(environ)
#qComp_RPC = QueryComposer_RPC(environ)
parser_XML = Parser_XML(environ)
parser_777 = Parser_777(environ)
parser_RPC = Parser_RPC(environ)
parser_LOC = Parser_LOC(environ)
parsers = {
'parser_XML': parser_XML,\
'parser_777': parser_777,\
'parser_RPC': Parser_RPC,\
'parser_LOC': parser_LOC
}
queryComposers = {
'qComp_777': qComp_777,\
'qComp_XML': qComp_XML,\
'qComp_LOC': qComp_LOC
}
UCs = [
'start', 'stop', 'restart',
'UC1', 'UC2', 'UC3', 'UC4', 'UC5', 'UC6',
'UC7', 'UC8', 'UC9', 'UC10', 'UC11','UC12',
]
def __init__(self, pidfile):
self.environ = environ
super(SuperNETApiD, self).__init__(pidfile)
def run(self):
log.msg("calling init")
self.init()
log.msg("init() done. starting reactor.run()")
reactor.run()
def init(self):
"""
serverfactory gets all parsers and qcomps, and builds the clientFactories according to what is supposed to happen
name scoping is very tricky here. In case of problems, check the object instance identities by print(self) here!
The SERVERfactory always the same object! It does NOT get re-instantiated with each new call.
Factory is somewhat flexible as to what argument types it gets! """#
log.startLogging(sys.stdout) # check: logfile or other output ?
# factory for ad hoc requests received from external sources
serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
reactor.suggestThreadPoolSize(500) # should be ok
log.msg("stats: ",reactor.threadpool.dumpStats())
log.msg("workers: ", tp.ThreadPool.workers)
serverFactory.reactor = reactor
uc_scd_XML_SportsdataLLC = UC_Scheduler_XML(serverFactory, self.environ ) # environ has the credentials and all
timer1 = task.LoopingCall(uc_scd_XML_SportsdataLLC.periodic, )
timer1.start( TIMER_15000, now=True ) # slow heartbeat, start now TODO: the NOW does not seem to work!
reactor.listenTCP(LISTEN_PORT_SNT, serverFactory)
# can make as many as we want here with specific timers and tasks
def runUC(self, UC):
log.msg( 1 * "start UC: ", UC)
if UC in self.UCs:
print("UC1 - 0")
self.initUC(UC)
else:
log.msg("UC name error")
log.msg("initUC() done. starting reactor.run()")
reactor.run()
def initUC(self, UC):
"""
serverfactory gets all parsers and qcomps, and builds the clientFactories according to what is supposed to happen
name scoping is very tricky here. In case of problems, check the object instance identities by print(self) here!
The SERVERfactory always the same object! It does NOT get re-instantiated with each new call.
Factory is somewhat flexible as to what argument types it gets!
"""#
self.UC_results = {'here we can collect results for individual test cases' : True}
if UC == 'UC1':
self.startUC1()
elif UC == 'UC2':
self.startUC2()
elif UC == 'UC3':
self.startUC3()
elif UC == 'UC4':
self.startUC4()
elif UC == 'UC5':
self.startUC5()
elif UC == 'UC6':
self.startUC6()
elif UC == 'UC7':
self.startUC7()
elif UC == 'UC8':
self.startUC8()
elif UC == 'UC9':
self.startUC9()
elif UC == 'UC10':
self.startUC10()
elif UC == 'UC11':
self.startUC11()
elif UC == 'UC12':
self.startUC12()
else:
log.msg("UC name error")
def startUC1(self):
log.startLogging(sys.stdout)
serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
log.msg(1*"initUC1")
uc1_pingPong = UC1_pingPong(serverFactory, self, self.environ , ) # also hand in 'self' here as a means to stop self
self.timer1 = task.LoopingCall(uc1_pingPong.periodic, )
self.timer1.start( TIMER_850 , now=True )
reactor.suggestThreadPoolSize(500)
reactor.listenTCP(LISTEN_PORT_SNT, serverFactory)
def stopUC1(self, result):
log.msg(1*"\n STOP UC1 with result: ", result, "\n")
self.timer1.stop( )
#self.stop()
self.startUC2()
def startUC2(self):
log.startLogging(sys.stdout)
serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
log.msg(1*"initUC2")
uc2_havenode = UC2_havenode(serverFactory, self, self.environ )
reactor.suggestThreadPoolSize(500)
serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
try:
reactor.listenTCP(LISTEN_PORT_SNT, serverFactory)
except Exception as e:
log.msg("already listening, continue.{0}".format(str(e)))
self.timer2 = task.LoopingCall(uc2_havenode.periodic, )
self.timer2.start( TIMER_850 , now=True )
def stopUC2(self, result):
log.msg(5*"\n\n STOP UC2 with result: ", result, "\n")
self.timer2.stop( )
#self.stop()
self.startUC3()
def startUC3(self):
log.startLogging(sys.stdout)
serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
log.msg(10*"\ninitUC3")
uc3_store_findvalue = UC3_store_findvalue(serverFactory, self, self.environ )
serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
reactor.suggestThreadPoolSize(500)
try:
reactor.listenTCP(LISTEN_PORT_SNT, serverFactory)
except Exception as e:
log.msg("already listening, continue.{0}".format(str(e)))
self.timer3 = task.LoopingCall(uc3_store_findvalue.periodic, )
self.timer3.start( TIMER_850 , now=True )
def stopUC3(self, result):
log.msg(5*"\n\n STOP UC3 with result: ", result, "\n")
self.timer3.stop( )
log.msg("STOP snappyDaemon")
#self.stop()
self.startUC4()
def startUC4(self):
log.startLogging(sys.stdout)
serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
log.msg(1*"initUC4")
reactor.suggestThreadPoolSize(500) # should be ok
serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
try:
reactor.listenTCP(LISTEN_PORT_SNT, serverFactory) # this is needed to also recevies GET queries
except Exception as e:
log.msg("already listening, continue.{0}".format(str(e)))
uc4_sendMSG = UC4_sendMSG(serverFactory, self, self.environ )
self.timer4 = task.LoopingCall(uc4_sendMSG.periodic, )
self.timer4.start( TIMER_850, now=True )
def stopUC4(self,result):
log.msg(5*"\n\n STOP UC4 with result: ", result, "\n")
self.timer4.stop( )
log.msg("STOP snappyDaemon")
#self.stop()
self.startUC5()
def startUC5(self):
log.startLogging(sys.stdout)
serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
log.msg(1*"initUC5")
reactor.suggestThreadPoolSize(500) # should be ok
serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
try:
reactor.listenTCP(LISTEN_PORT_SNT, serverFactory) # this is needed to also recevies GET queries
except Exception as e:
log.msg("already listening, continue.{0}".format(str(e)))
uc5_sendBIN = UC5_sendBIN(serverFactory, self, self.environ )
self.timer5 = task.LoopingCall(uc5_sendBIN.periodic, )
self.timer5.start( TIMER_850 , now=True )
def stopUC5(self,result):
log.msg(5*"\n\n STOP UC5 with result: ", result, "\n")
self.timer5.stop( )
log.msg("STOP snappyDaemon")
#self.stop()
self.startUC6()
def startUC6(self):
log.startLogging(sys.stdout)
serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
log.msg(1*"initUC6")
reactor.suggestThreadPoolSize(500) # should be ok
serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
try:
reactor.listenTCP(LISTEN_PORT_SNT, serverFactory) # this is needed to also recevies GET queries
except Exception as e:
log.msg("already listening, continue.{0}".format(str(e)))
uc6_checkMSG = UC6_checkMSG(serverFactory, self, self.environ )
self.timer6 = task.LoopingCall(uc6_checkMSG.periodic, )
self.timer6.start( TIMER_850 , now=True )
def stopUC6(self,result):
log.msg(5*"\n\n STOP UC6 with result: ", result, "\n")
self.timer6.stop( )
log.msg("STOP snappyDaemon")
#self.stop()
self.startUC7()
def startUC7(self):
log.startLogging(sys.stdout)
serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
log.msg(1*"initUC7")
reactor.suggestThreadPoolSize(500) # should be ok
serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
try:
reactor.listenTCP(LISTEN_PORT_SNT, serverFactory) # this is needed to also recevies GET queries
except Exception as e:
log.msg("already listening, continue.{0}".format(str(e)))
uc7_findaddress = UC7_findaddress(serverFactory, self, self.environ )
self.timer7 = task.LoopingCall(uc7_findaddress.periodic, )
self.timer7.start( TIMER_850 , now=True )
def stopUC7(self,result):
log.msg(5*"\n\n STOP UC7 with result: ", result, "\n")
self.timer7.stop( )
log.msg("STOP snappyDaemon")
self.stop()
#self.startUC8()
def startUC8(self):
log.startLogging(sys.stdout)
serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
log.msg(1*"initUC8")
reactor.suggestThreadPoolSize(500) # should be ok
serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
try:
reactor.listenTCP(LISTEN_PORT_SNT, serverFactory) # this is needed to also recevies GET queries
except Exception as e:
log.msg("already listening, continue.{0}".format(str(e)))
uc8_contacts = UC8_contacts(serverFactory, self, self.environ )
self.timer8 = task.LoopingCall(uc8_contacts.periodic, )
self.timer8.start( TIMER_850 , now=True )
def stopUC8(self,result):
log.msg(5*"\n\n STOP UC8 with result: ", result, "\n")
self.timer8.stop( )
log.msg("STOP snappyDaemon")
self.stop()
def startUC9(self):
log.startLogging(sys.stdout)
serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
log.msg(1*"initUC9")
reactor.suggestThreadPoolSize(500) # should be ok
serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
try:
reactor.listenTCP(LISTEN_PORT_SNT, serverFactory) # this is needed to also recevies GET queries
except Exception as e:
log.msg("already listening, continue.{0}".format(str(e)))
uc9_getdb = UC9_getdb(serverFactory, self, self.environ )
self.timer9 = task.LoopingCall(uc9_getdb.periodic, )
self.timer9.start( TIMER_850 , now=True )
def stopUC9(self,result):
log.msg(5*"\n\n STOP UC9 with result: ", result, "\n")
self.timer9.stop( )
log.msg("STOP snappyDaemon")
self.stop()
def startUC10(self):
log.startLogging(sys.stdout)
serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
log.msg(1*"initUC10")
reactor.suggestThreadPoolSize(500) # should be ok
serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
try:
reactor.listenTCP(LISTEN_PORT_SNT, serverFactory) # this is needed to also recevies GET queries
except Exception as e:
log.msg("already listening, continue.{0}".format(str(e)))
uc10_IDEX_placeAB = UC10_IDEX_placeAB(serverFactory, self, self.environ )
self.timer10 = task.LoopingCall(uc10_IDEX_placeAB.periodic, )
self.timer10.start( TIMER_850 , now=True )
def stopUC10(self,result):
log.msg(5*"\n\n STOP UC10 with result: ", result, "\n")
self.timer10.stop( )
log.msg("STOP snappyDaemon")
self.stop()
def startUC11(self):
log.startLogging(sys.stdout)
serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
log.msg(1*"initUC11")
reactor.suggestThreadPoolSize(500) # should be ok
serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
try:
reactor.listenTCP(LISTEN_PORT_SNT, serverFactory) # this is needed to also recevies GET queries
except Exception as e:
log.msg("already listening, continue.{0}".format(str(e)))
uc11_priceDB = UC11_priceDB(serverFactory, self, self.environ )
self.timer11 = task.LoopingCall(uc11_priceDB.periodic, )
self.timer11.start( TIMER_850 , now=True )
def stopUC11(self,result):
log.msg(5*"\n\n STOP UC11 with result: ", result, "\n")
self.timer11.stop( )
log.msg("STOP snappyDaemon")
self.stop()
def startUC12(self):
log.startLogging(sys.stdout)
serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
log.msg(1*"initUC12")
reactor.suggestThreadPoolSize(500) # should be ok
serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
try:
reactor.listenTCP(LISTEN_PORT_SNT, serverFactory) # this is needed to also recevies GET queries
except Exception as e:
log.msg("already listening, continue.{0}".format(str(e)))
uc12_save_restore_File = UC12_save_restore_File(serverFactory, self, self.environ )
self.timer12 = task.LoopingCall(uc12_save_restore_File.periodic, )
self.timer12.start( TIMER_850 , now=True )
def stopUC12(self,result):
log.msg(5*"\n\n STOP UC12 with result: ", result, "\n")
self.timer12.stop( )
log.msg("STOP snappyDaemon")
self.stop()
if __name__ == "__main__":
pidFileName = '/tmp/SuperNET_API.pid'
superNetApiD = SuperNETApiD(pidFileName) # '/tmp/daemon-example.pid')
#
# Note:
# we are using standard Daemonization here and NOT twistd,
# becasue that seems to be not properly ported to python3 yet.
# Also, it is difficult to understand and badly documented.
# It is better to do it with the standard python recipe, and have opportunity of intervention.
#
# Also, the sequence of starting reactor and Daemon is sensitive!
UCs = [
'start', 'stop', 'restart',
'UC1', 'UC2', 'UC3', 'UC4', 'UC5', 'UC6',
'UC7', 'UC8','UC9', 'UC10', 'UC11','UC12',
]
if len(sys.argv) == 2:
UC=sys.argv[1]
if UC not in UCs:
print("Unknown command")
sys.exit(2)
if 'start' == sys.argv[1]:
superNetApiD.start()
elif 'stop' == sys.argv[1]:
superNetApiD.stop()
elif 'restart' == sys.argv[1]:
superNetApiD.restart()
else:
superNetApiD.startUC(UC)
sys.exit(0)
else:
print("usage: %s start|stop|restart|UC" % sys.argv[0])
sys.exit(2)