-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverXmpp.py
97 lines (78 loc) · 3.13 KB
/
serverXmpp.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
#!/usr/bin/env python
import sys, json, xmpp, random, string,os
from common import payload, RpcClient
import logging
import logging.config
root = logging.getLogger()
root.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)
SERVER = 'gcm.googleapis.com'
PORT = 5235
USERNAME = ""
PASSWORD = ""
REGISTRATION_ID = ""
unacked_messages_quota = 100
send_queue = []
# Return a random alphanumerical id
def random_id():
rid = ''
for x in range(8): rid += random.choice(string.ascii_letters + string.digits)
return rid
def message_callback(session, message):
global unacked_messages_quota
gcm = message.getTags('gcm')
if gcm:
gcm_json = gcm[0].getData()
msg = json.loads(gcm_json)
if not msg.has_key('message_type'):
# Acknowledge the incoming message immediately.
send({'to': msg['from'],
'message_type': 'ack',
'message_id': msg['message_id']})
# Queue a response back to the server.
if msg.has_key('from'):
# Send a dummy echo response back to the app that sent the upstream message.
print " [x] Requesting cel" + msg['from']
msg['data']['idCel'] = msg['from']
payloadObj= payload("command", msg['data']['command'] , msg['data'])
response = rpc.call(payloadObj)
print " [.] Got %r" % (response,)
if response['type'] == 'response' :
send_queue.append({'to': msg['from'],
'message_id': random_id(),
'data': {'response': response['response'],'type': 'response'}})
else:
send_queue.append({'to': msg['from'],
'message_id': random_id(),
'data': {'error': response['error'],'type': 'error'}})
elif msg['message_type'] == 'ack' or msg['message_type'] == 'nack':
unacked_messages_quota += 1
def send(json_dict):
template = ("<message><gcm xmlns='google:mobile:data'>{1}</gcm></message>")
client.send(xmpp.protocol.Message(
node=template.format(client.Bind.bound[0], json.dumps(json_dict))))
def flush_queued_messages():
global unacked_messages_quota
while len(send_queue) and unacked_messages_quota > 0:
send(send_queue.pop(0))
unacked_messages_quota -= 1
client = xmpp.Client('gcm.googleapis.com', debug=['socket'],
port=int(os.environ.get("PORT")))
client.connect(server=(SERVER,PORT), secure=1, use_srv=False)
auth = client.auth(USERNAME, PASSWORD)
if not auth:
print 'Authentication failed!'
sys.exit(1)
client.RegisterHandler('message', message_callback)
#send_queue.append({'to': REGISTRATION_ID,
#'message_id': 'reg_id',
#'data': {'message_destination': 'RegId',
#'message_id': random_id()}})
while True:
rpc = RpcClient()
client.Process(1)
flush_queued_messages()