/
ChRIS_DB.py
executable file
·310 lines (244 loc) · 9.23 KB
/
ChRIS_DB.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
_____ _ ______ _____ _____ _ _ _ _
/ __ \ | | ___ \_ _/ ___| | | | | | |
| / \/ |__ | |_/ / | | \ `--. | | | | | |_ _ __ ___ _ __
| | | '_ \| / | | `--. \ | | | | | __| '__/ _ \| '_ \
| \__/\ | | | |\ \ _| |_/\__/ / | |_| | | |_| | | (_) | | | |
\____/_| |_\_| \_|\___/\____/ \___/|_|\__|_| \___/|_| |_|
font generated by:
http://patorjk.com/software/taag/#p=display&f=Doom&t=ChRIS%20Ultron
This module implements a server side feed controller/model.
"""
import abc
import json
import sys
import datetime
import os
import threading
import zmq
import json
from urlparse import urlparse
import C_snode
import message
class ChRIS_DB(object):
"""
The server class for the C_snode server
"""
__metaclass__ = abc.ABCMeta
def log(self, *args):
"""
get/set the internal pipeline log message object.
Caller can further manipulate the log object with object-specific
calls.
"""
if len(args):
self._log = args[0]
else:
return self._log
def name(self, *args):
"""
get/set the descriptive name text of this object.
"""
if len(args):
self.__name = args[0]
else:
return self.__name
def __init__(self, **kwargs):
"""
"""
self._stree = C_snode.C_stree()
self.debug = message.Message(logTo = './debug.log')
self.debug._b_syslog = True
self._log = message.Message()
self._log._b_syslog = True
self.__name = "ChRIS_DB"
self._name = ""
self.within = None # An encapsulating object
# DB
self.str_DBpath = '/tmp/ChRIS_DB'
self._stree = C_snode
# Comms
self.str_protocol = "tcp"
self.str_IP = "127.0.0.1"
self.str_port = "5001"
self.DB_read()
for key, val in kwargs.iteritems():
if key == 'within': self.within = val
self.zmq_context = zmq.Context()
def DB_read(self, **kwargs):
"""
Read the DB from filesystem
"""
print("Reading DB from disk...")
self.debug("Reading DB from disk...\n")
self._stree = C_snode.C_stree.tree_load(
pathDiskRoot = self.str_DBpath,
loadJSON = False,
loadPickle = True)
self.debug("DB read from disk...\n")
print("DB read from disk...")
def start(self):
"""
Main execution.
Instantiate workers, Accept client connections,
distribute computation requests among workers and route computed results back to clients.
"""
print("Starting server...")
# Front facing socket to accept client connections.
socket_front = self.zmq_context.socket(zmq.ROUTER)
socket_front.bind('tcp://127.0.0.1:5010')
# Backend socket to distribute work.
socket_back = self.zmq_context.socket(zmq.DEALER)
socket_back.bind('inproc://backend')
# Start three workers.
for i in range(1,2):
worker = Worker(self.zmq_context, i, self._stree)
worker.start()
print("Threaded worker %d started..." % i)
# Use built in queue device to distribute requests among workers.
# What queue device does internally is,
# 1. Read a client's socket ID and request.
# 2. Send socket ID and request to a worker.
# 3. Read a client's socket ID and result from a worker.
# 4. Route result back to the client using socket ID.
zmq.device(zmq.QUEUE, socket_front, socket_back)
def __iter__(self):
yield('Feed', dict(self._stree.snode_root))
# @abc.abstractmethod
# def create(self, **kwargs):
# """Create a new tree
#
# """
def __str__(self):
"""Print
"""
return str(self.stree.snode_root)
@property
def stree(self):
"""STree Getter"""
return self._stree
@stree.setter
def stree(self, value):
"""STree Getter"""
self._stree = value
class Worker(threading.Thread):
""" Workers accept computation requests from front facing server.
Does computations and return results back to server. """
def log(self, *args):
"""
get/set the internal pipeline log message object.
Caller can further manipulate the log object with object-specific
calls.
"""
if len(args):
self._log = args[0]
else:
return self._log
def name(self, *args):
"""
get/set the descriptive name text of this object.
"""
if len(args):
self.__name = args[0]
else:
return self.__name
def __init__(self, zmq_context, _id, stree):
self.debug = message.Message(logTo = './debug.log')
self.debug._b_syslog = True
self._log = message.Message()
self._log._b_syslog = True
self.__name = "Worker"
threading.Thread.__init__(self)
self.zmq_context = zmq_context
self.worker_id = _id
self._stree = stree
def run(self):
""" Main execution. """
# Socket to communicate with front facing server.
socket = self.zmq_context.socket(zmq.DEALER)
socket.connect('inproc://backend')
while True:
print("\n\nWorker ID - %s: run() - Ready to serve..." % self.worker_id)
# First string received is socket ID of client
client_id = socket.recv()
request = socket.recv()
print('Worker ID - %s: run() - Received comms from client.' % (self.worker_id))
result = self.process(request)
# try:
# result = self.process(request)
# except:
# print('Worker ID - %s. some error was detected' % (self.worker_id))
# os._exit(1)
print('Worker ID - %s: run() - Sending response to client.' % (self.worker_id))
print(result)
# For successful routing of result to correct client, the socket ID of client should be sent first.
socket.send(client_id, zmq.SNDMORE)
socket.send(result)
def process(self, request):
""" Process the call return result.
Since the client only ever processes a "single" feed, there is no need to
ever send the whole data structure back and forth. Only the feed that
is requested needs to be processed.
This process() method in fact implements a mini-REST API of its own.
"""
s = C_snode.C_stree()
b_returnFeedList = False
s.cd('/')
s.mkcd('users')
s.mkcd('chris')
s.graft(self._stree, '/users/chris/plugins')
s.graft(self._stree, '/users/chris/login')
print("Worker ID - %s: process() - request = '%s'" % (self.worker_id, request))
d_request = json.loads(request)
str_verb = d_request['VERB']
str_URL = d_request['URL']
d_data = d_request['data']
o_URL = urlparse(str_URL)
str_path = o_URL.path
l_path = str_path.split('/')[2:]
if str_verb == 'QUIT':
print('Shutting down server...')
os._exit(1)
# 'paths' are of form:
# /v1/Feeds/NAME__Feed-1
# -- or --
# /v1/Feeds/NAME
str_feed = l_path[1]
# Strip out the NAME<__>
str_feed = str_feed.replace('NAME', '')
str_feed = str_feed.replace('__', '')
if str_verb == 'PULL' or str_verb == 'GET':
if len(l_path) >= 2 and len(str_feed):
s.mkcd('feeds')
s.graft(self._stree, '/users/chris/feeds/%s' % str_feed)
if len(l_path) == 1:
b_returnFeedList = True
if len(l_path) == 2 and not len(str_feed):
b_returnFeedList = True
if b_returnFeedList:
s.mkcd('feeds')
self._stree.cd('/users/chris/feeds')
l_feeds = self._stree.lstr_lsnode()
s.mknode(l_feeds)
for f in l_feeds:
s.cd(f)
s.graft(self._stree, '/users/chris/feeds/%s/detail' % f)
s.cd('../')
if str_verb == 'PUSH':
if len(l_path) >= 2 and len(str_feed):
self._stree.cd('/users/chris/feeds')
# self._stree.rm(str_feed)
s.initFromDict(d_data)
self._stree.graft(s, '/users/chris/feeds/%s' % str_feed)
else:
s.mkcd('feeds')
self._stree.cd('/users/chris/feeds')
l_feeds = self._stree.lstr_lsnode()
s.mknode(l_feeds)
# print(s)
# print(dict(s.snode_root))
return json.dumps(dict(s.snode_root))
if __name__ == "__main__":
comm = ChRIS_DB().start()