forked from Pushjet/Pushjet-Connectors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverTCP.py
executable file
·60 lines (45 loc) · 2.09 KB
/
serverTCP.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
#!/usr/bin/env python2
from argparse import ArgumentParser
from sys import stdout
from protocolBase import PushjetProtocolBase
from twisted.internet import reactor, protocol
from twisted.python import log
class PushjetTCPBase(protocol.Protocol, PushjetProtocolBase):
magic_start = '\002'
magic_end = '\003'
uuid_len = 36
_buffer = b''
def __init__(self):
PushjetProtocolBase.__init__(self, args.api, args.pub)
def dataReceived(self, data):
self._buffer += data
if len(self._buffer) >= self.uuid_len:
frame = self._buffer[:self.uuid_len]
self._buffer = self._buffer[self.uuid_len:]
print frame
self.onClientMessage(frame)
def sendMessage(self, message):
self.transport.writeSequence((self.magic_start, message, self.magic_end))
class PushjetTCPBaseFactory(protocol.Factory):
protocol = PushjetTCPBase
def __init__(self, reactor):
self.reactor = reactor
if __name__ == '__main__':
parser = ArgumentParser(description='Pushjet websocket server')
parser.add_argument('--port', '-p', default='7171', type=int,
help='the port the server should bind to (default: 7171)')
parser.add_argument('--api', '-a', default='https://api.pushjet.io', type=str, metavar='SRV',
help='the api server url (default: https://api.pushjet.io)')
parser.add_argument('--pub', '-z', default='ipc:///tmp/pushjet-publisher.ipc', type=str,
help='the publisher uri for receiving messages (default: ipc:///tmp/pushjet-publisher.ipc)')
parser.add_argument('--log', '-l', default=None, type=str, metavar='PATH', dest='logfile',
help='log file path')
parser.add_argument('--quiet', '-q', action='store_true')
args = parser.parse_args()
if not args.quiet:
log.startLogging(stdout)
if args.logfile:
log.startLogging(open(args.logfile, 'a'))
print("Started logging to file %s" % args.logfile)
reactor.listenTCP(args.port, PushjetTCPBaseFactory(reactor))
reactor.run()