|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import re |
| 3 | +import sys |
| 4 | +import ssl |
| 5 | +import time |
| 6 | +import json |
| 7 | +import random |
| 8 | +import socket |
| 9 | +import httplib |
| 10 | +from datetime import datetime |
| 11 | + |
| 12 | +HOST = 'iot.espressif.cn' |
| 13 | +PORT = 8000 |
| 14 | +TOKEN1 = '513d09340e29eb61f91f5cb4e717682c48444d6f' |
| 15 | +TOKEN2 = '44590e7ccb254203073aeb61777a4c425b20a018' |
| 16 | +MBOX_NAME = 'c08c4b6' |
| 17 | + |
| 18 | +SERVER_PING_URL = '{"nonce": %s, "path": "/v1/ping/", "method": "GET", "meta": {"Authorization": "token %s"}}\n' |
| 19 | +IDENTIFY_URL = '{"nonce": %s, "path": "/v1/device/identify/", "method": "POST", "meta": {"Authorization": "token %s"}}\n' |
| 20 | +ACK_DELIVER_URL = '{"nonce": %s, "status": 200, "deliver_to_device": true}\n' |
| 21 | +MBOX_SUBSCRIBE = '{"path": "/v1/mbox/", "method": "POST", "body": {"name": "%s", "action": "subscribe"}, "meta": {"Authorization": "token %s"}}\n' |
| 22 | +MBOX_PUBLISH = '{"path": "/v1/mbox/", "method": "POST", "body": {"name": "%s", "action": "publish", "data": "%s"}, "meta": {"Authorization": "token %s"}}\n' |
| 23 | +MBOX_UNSUBSCRIBE = '{"path": "/v1/mbox/", "method": "POST", "body": {"name": "%s", "action": "unsubscribe"}, "meta": {"Authorization": "token %s"}}\n' |
| 24 | + |
| 25 | +class Analoguer(): |
| 26 | + |
| 27 | + def __init__(self, token): |
| 28 | + self.token = token |
| 29 | + self._lines = [] |
| 30 | + self._buffer = '' |
| 31 | + self._socket = None |
| 32 | + |
| 33 | + def quit(self): |
| 34 | + self.send('\x06') |
| 35 | + |
| 36 | + def read(self): |
| 37 | + while len(self._lines) == 0: |
| 38 | + recv = self._socket.recv(4096) |
| 39 | + assert(recv is not None and recv != '') |
| 40 | + self._buffer += recv |
| 41 | + find_newline = self._buffer.rfind('\n') |
| 42 | + if find_newline == -1: |
| 43 | + continue |
| 44 | + for x in self._buffer[0:find_newline].split('\n'): |
| 45 | + self._lines.append(x) |
| 46 | + self._buffer = self._buffer[find_newline+1:] |
| 47 | + |
| 48 | + while len(self._lines) > 0: |
| 49 | + pop_line = self._lines.pop() |
| 50 | + if pop_line is None or pop_line == '': |
| 51 | + continue |
| 52 | + return json.loads(pop_line) |
| 53 | + |
| 54 | + return None |
| 55 | + |
| 56 | + def send(self, data): |
| 57 | + if not data.endswith('\n'): |
| 58 | + data = data + '\n' |
| 59 | + self._socket.sendall(data) |
| 60 | + |
| 61 | + def connect(self): |
| 62 | + self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 63 | + self._socket.settimeout(5) |
| 64 | + self._socket.connect((HOST, PORT)) |
| 65 | + |
| 66 | + def close(self): |
| 67 | + if not self._socket: |
| 68 | + return |
| 69 | + try: |
| 70 | + self._socket.close() |
| 71 | + except Exception, e: |
| 72 | + pass |
| 73 | + self._socket = None |
| 74 | + |
| 75 | + def ping(self): |
| 76 | + randint = self.get_randint() |
| 77 | + self.send(SERVER_PING_URL % (randint, self.token)) |
| 78 | + return randint |
| 79 | + |
| 80 | + def identify(self): |
| 81 | + randint = self.get_randint() |
| 82 | + self.send(IDENTIFY_URL % (randint, self.token)) |
| 83 | + return randint |
| 84 | + |
| 85 | + def subscribe(self, name): |
| 86 | + self.send(MBOX_SUBSCRIBE % (name, self.token)) |
| 87 | + |
| 88 | + def publish(self, name, data): |
| 89 | + self.send(MBOX_PUBLISH % (name, data, self.token)) |
| 90 | + |
| 91 | + def unsubscribe(self, name): |
| 92 | + self.send(MBOX_UNSUBSCRIBE % (name, self.token)) |
| 93 | + |
| 94 | + def ack_deliver(self, nonce): |
| 95 | + self.send(ACK_DELIVER_URL % (nonce)) |
| 96 | + |
| 97 | + def get_randint(self): |
| 98 | + return random.randint(1, 100000000) |
| 99 | + |
| 100 | + def get_randstr(self, bits_len): |
| 101 | + return '%x' % random.getrandbits(bits_len) |
| 102 | + |
| 103 | + def get_token(self): |
| 104 | + return ('%x' % random.getrandbits(200))[0:40] |
| 105 | + |
| 106 | + |
| 107 | +def test_mbox(analoguer1, analoguer2): |
| 108 | + if hasattr(socket, 'setdefaulttimeout'): |
| 109 | + socket.setdefaulttimeout(3) |
| 110 | + total = 0 |
| 111 | + success = 0 |
| 112 | + ping = 0 |
| 113 | + exception_occur = True |
| 114 | + while True: |
| 115 | + total = total + 1 |
| 116 | + try: |
| 117 | + socket.gethostbyname('www.baidu.com') |
| 118 | + ping = ping + 1 |
| 119 | + if exception_occur: |
| 120 | + analoguer1.close(); analoguer1.connect(); analoguer1.identify(); analoguer1.read(); analoguer1.subscribe(MBOX_NAME); analoguer1.read() |
| 121 | + analoguer2.close(); analoguer2.connect(); analoguer2.identify(); analoguer2.read(); analoguer2.subscribe(MBOX_NAME); analoguer2.read() |
| 122 | + data = analoguer1.get_randstr(30) |
| 123 | + if total % 2 == 0: |
| 124 | + analoguer1.publish(MBOX_NAME, data) |
| 125 | + r = analoguer1.read() |
| 126 | + assert(r['peers'] == 1) |
| 127 | + r = analoguer2.read() |
| 128 | + assert(r['body']['data'] == data) |
| 129 | + else: |
| 130 | + analoguer2.publish(MBOX_NAME, data) |
| 131 | + r = analoguer2.read() |
| 132 | + assert(r['peers'] == 1) |
| 133 | + r = analoguer1.read() |
| 134 | + assert(r['body']['data'] == data) |
| 135 | + exception_occur = False |
| 136 | + success = success + 1 |
| 137 | + except Exception, e: |
| 138 | + exception_occur = True |
| 139 | + print e |
| 140 | + print 'total: %s, success: %s, ping: %s, percentage: %s%%' % (total, success, ping, int(success*10000/total)/100.0) |
| 141 | + time.sleep(3) |
| 142 | + |
| 143 | +if __name__ == '__main__': |
| 144 | + analoguer1 = Analoguer(TOKEN1) |
| 145 | + analoguer2 = Analoguer(TOKEN2) |
| 146 | + test_mbox(analoguer1, analoguer2) |
| 147 | + |
0 commit comments