-
Notifications
You must be signed in to change notification settings - Fork 8
/
dojo.py
executable file
·212 lines (155 loc) · 4.42 KB
/
dojo.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
#!/usr/bin/env python
#
# DOJO Image Server
#
import json
import os
import socket
import sys
import tornado
import tornado.websocket
import tempfile
import signal
import _dojo
#
# default handler
#
class DojoHandler(tornado.web.RequestHandler):
def initialize(self, logic):
self.__logic = logic
def get(self, uri):
'''
'''
self.__logic.handle(self)
def post(self, uri):
'''
'''
self.__logic.handle(self)
class ServerLogic:
def __init__( self ):
'''
'''
pass
def run( self, mojo_dir, out_dir, port, configured ):
'''
'''
signal.signal(signal.SIGINT, self.close)
#monkey.patch_thread()
self.__mojo_dir = mojo_dir
self.__configured = configured
self.__out_dir = out_dir
# create temp folder
tmpdir = tempfile.mkdtemp()
self.__tmpdir = tmpdir
# register two data sources
self.__segmentation = _dojo.Segmentation(mojo_dir, tmpdir)
self.__image = _dojo.Image(mojo_dir, tmpdir)
# and the controller
self.__controller = _dojo.Controller(mojo_dir, out_dir, tmpdir, self.__segmentation.get_database())
# and the viewer
self.__viewer = _dojo.Viewer()
# and the setup
self.__setup = _dojo.Setup(self,mojo_dir,tmpdir)
ip = socket.gethostbyname(socket.gethostname())
dojo = tornado.web.Application([
# viewer
# (r'/', web.RedirectHandler, {'url':'/dojo/'}),
# (r'/dojo', web.RedirectHandler, {'url':'/dojo/'}),
# # image
# (r'/image/(.*)', _dojo.Image(mojo_dir)),
(r'/ws', _dojo.Websockets, dict(controller=self.__controller)),
(r'/(.*)', DojoHandler, dict(logic=self))
])
dojo.listen(port,max_buffer_size=1024*1024*150000)
print '*'*80
print '*', '\033[93m'+'DOJO RUNNING', '\033[0m'
print '*'
print '*', 'open', '\033[92m'+'http://' + ip + ':' + str(port) + '/dojo/' + '\033[0m'
print '*'*80
tornado.ioloop.IOLoop.instance().start()
def finish_setup(self):
'''
'''
mojo_dir = self.__mojo_dir
tmpdir = self.__tmpdir
out_dir = self.__out_dir
# register two data sources
self.__segmentation = _dojo.Segmentation(mojo_dir, tmpdir)
self.__image = _dojo.Image(mojo_dir, tmpdir)
# and the controller
self.__controller = _dojo.Controller(mojo_dir, out_dir, tmpdir, self.__segmentation.get_database())
self.__configured = True
print 'Setup finished.'
def handle( self, r ):
'''
'''
content = None
# if request.find_input_header('upgrade'):
# # special case for websockets
# self.__websockets.handle(request)
# return
# the access to the viewer
if not self.__configured:
content, content_type = self.__setup.handle(r.request)
else:
# viewer is ready
content, content_type = self.__viewer.handle(r.request)
# let the data sources handle the request
if not content:
content, content_type = self.__segmentation.handle(r.request)
if not content:
content, content_type = self.__image.handle(r.request)
# invalid request
if not content:
content = 'Error 404'
content_type = 'text/html'
print 'IP',r.request.remote_ip
r.set_header('Access-Control-Allow-Origin', '*')
r.set_header('Content-Type', content_type)
r.write(content)
def close(self, signal, frame):
'''
'''
print 'Saving..'
output = {}
output['origin'] = 'SERVER'
self.__controller.save(output)
sys.exit(0)
def print_help( scriptName ):
'''
'''
description = ''
print description
print
print 'Usage: ' + scriptName + ' MOJO_DIRECTORY OUTPUT_DIRECTORY PORT'
print
#
# entry point
#
if __name__ == "__main__":
# always show the help if no arguments were specified
if len(sys.argv) != 1 and len( sys.argv ) < 4:
print_help( sys.argv[0] )
sys.exit( 1 )
if len(sys.argv) == 1:
# dojo was started without parameters
# so we need to add an input folder
input_dir = tempfile.mkdtemp()
# and a output folder
output_dir = tempfile.mkdtemp()
# and a free port
port = 1336
result = 0
import socket;
while result==0:
port += 1
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('127.0.0.1',port))
configured = False
else:
input_dir = sys.argv[1]
output_dir = sys.argv[2]
port = sys.argv[3]
configured = True
logic = ServerLogic()
logic.run( input_dir, output_dir, port, configured )