-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathortc_extensibility.py
185 lines (159 loc) · 5.64 KB
/
ortc_extensibility.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
import http.client
import re
import random
import string
import time
import websocket
import json
import threading
REST_TIMEOUT = 5
class OrtcError(Exception):
def __init__(self, message):
self.message = message
def __str__(self):
return self.message
class Channel(object):
@property
def name(self):
return self._name
@name.setter
def name(self, name):
self._name = name
@property
def subscribe_on_reconnecting(self):
return self._subscribe_on_reconnecting
@subscribe_on_reconnecting.setter
def subscribe_on_reconnecting(self, subscribe_on_reconnecting):
self._subscribe_on_reconnecting = subscribe_on_reconnecting
@property
def is_subscribing(self):
return self._is_subscribing
@is_subscribing.setter
def is_subscribing(self, is_subscribing):
self._is_subscribing = is_subscribing
@property
def is_subscribed(self):
return self._is_subscribed
@is_subscribed.setter
def is_subscribed(self, is_subscribed):
self._is_subscribed = is_subscribed
@property
def callback(self):
return self._callback
@callback.setter
def callback(self, callback):
self._callback = callback
def __init__(self, name, subscribe_on_reconnecting, callback):
self._name = name
self._subscribe_on_reconnecting = subscribe_on_reconnecting
self._is_subscribing = False
self._is_subscribed = False
self._callback = callback
class MultiMessage(object):
@property
def total_parts(self):
return self._total_parts
@total_parts.setter
def total_parts(self, total_parts):
self._total_parts = total_parts
@property
def ready_parts(self):
return self._ready_parts
@ready_parts.setter
def ready_parts(self, ready_parts):
self._ready_parts = ready_parts
def __init__(self, total_parts):
self._total_parts = total_parts
self._ready_parts = 0
self._parts = [None]*total_parts
def set_part(self, part_id, part):
if self._parts[part_id] == None:
self._ready_parts += 1
self._parts[part_id] = part
def is_ready(self):
return True if self._ready_parts == self._total_parts else False
def get_all_message(self):
return ''.join([str(x) for x in self._parts])
class Private:
@staticmethod
def _get_cluster(host, app_key):
try:
host += '?appkey='+app_key
from urllib.parse import urlparse
uri = urlparse(host)
conn = http.client.HTTPConnection(uri.netloc, timeout=REST_TIMEOUT)
conn.request("GET", uri.path + "?" + uri.query)
res = conn.getresponse()
if res.status == 200:
rbody = re.search('"(.*)"', res.read().decode()).group(0)
return rbody[1:][:-1]
except Exception as e:
#print(e)
return None
@staticmethod
def _call_exception_callback(sender, exception):
if hasattr(sender,"on_exception_callback"):
sender.on_exception_callback(sender, exception)
@staticmethod
def _validate_url(url):
return True if re.compile('^\s*(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?\s*$').match(url) else False
@staticmethod
def _validate_input(var):
return True if re.compile('^[\w\-:\/\.]*$').match(var) else False
@staticmethod
def _enum_state(**state):
return type('Enum state', (), state)
@staticmethod
def _remove_slashes(text):
text = text.replace("\\\\\\\"", '"')
text = text.replace("\\\\\\\\", '\\')
text = text.replace("\\\\n", '\n')
return text
@staticmethod
def _check_permission(permissions, channel):
if permissions == {}:
return True, ''
if channel in permissions:
return True, permissions[channel]
if ':' in channel:
if channel[:channel.index(':')]+':*' in permissions:
return True, permissions[channel[:channel.index(':')]+':*']
return False, ''
@staticmethod
def _rest_post_request(url, body, callback):
def p_thread():
try:
from urllib.parse import urlparse
uri = urlparse(url)
conn = http.client.HTTPSConnection(uri.netloc, timeout=REST_TIMEOUT)
headers = {}
headers['Content-Length'] = len(body)
conn.request("POST", uri.path, body, headers)
res = conn.getresponse()
if res.status==200:
callback(None, res.read())
else:
callback(str(res.status), None)
except Exception as e:
callback(str(e), None)
t = threading.Thread(target=p_thread)
t.setDaemon(True)
t.start()
@staticmethod
def _prepare_server(url, is_cluster, app_key, callback):
server = Private._get_cluster(url, app_key) if is_cluster else url
if server == None:
callback('Error getting server from Cluster', None)
return
server += '/' if not server[-1] == '/' else ''
return server
@staticmethod
def _prepare_server_internal(url, cluster_url, app_key, callback):
if app_key == None:
callback('Please, do connect first', None)
return False, None
server = Private._get_cluster(cluster_url, app_key) if not cluster_url == None else url
if server == None:
callback('Error getting server from Cluster', None)
return False, None
return True, server