forked from urakubo/Dojo-standalone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDojoStandalone.py
206 lines (144 loc) · 4.82 KB
/
DojoStandalone.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
#!/usr/bin/env python
#
# DOJO Image Server
#
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import json
import os
import socket
import sys
import tempfile
import signal
import tornado
import tornado.websocket
import tornado.httpserver
from threading import Thread
import asyncio
from os import path, pardir
main_dir = path.abspath(path.dirname(sys.argv[0])) # Dir of main
icon_dir = path.join(main_dir, "icons")
sys.path.append(os.path.join(main_dir, "Filesystem"))
from Params import Params
# import SaveChanges
sys.path.append(main_dir)
import _dojo
if os.name == 'nt':
try:
import win32api
except Exception:
print('No win32api module.')
#def doSaneThing(sig, func=None):
# print "Here I am"
# raise KeyboardInterrupt
#win32api.SetConsoleCtrlHandler(doSaneThing, 1)
#
# default handler
#
class DojoHandler(tornado.web.RequestHandler):
def initialize(self, logic):
self.__logic = logic
# @tornado.web.asynchronous
# @tornado.gen.coroutine
def get(self, uri):
self.__logic.handle(self)
# @tornado.web.asynchronous
# @tornado.gen.coroutine
def post(self, uri):
self.__logic.handle(self)
class ServerLogic:
def func(self, loop):
loop.stop()
def __init__( self ):
pass
def run( self, u_info ):
# self, mojo_dir, tmp_dir, /// out_dir, dojoserver
# register two data sources
self.__segmentation = _dojo.Segmentation( u_info.files_path , u_info.tmpdir, self)
self.__image = _dojo.Image( u_info.files_path , u_info.tmpdir)
# and the controller
self.__controller = _dojo.Controller( u_info, self.__segmentation.get_database(), self ) ####
# and the viewer
self.__viewer = _dojo.Viewer()
# and the controller
if self.__segmentation:
db = self.__segmentation.get_database()
else:
db = None
self.__controller = _dojo.Controller( u_info, db, self )
# and the setup
self.__setup = _dojo.Setup(self, u_info.files_path, u_info.tmpdir)
if getattr(sys, 'frozen', False):
print('Run on pyinstaller.')
path_gfx = os.path.normpath(os.path.join(main_dir, "../..", "_web/gfx"))
path_stl = os.path.normpath(os.path.join(main_dir, "../..", "_web_stl"))
else:
print('Run on live python.')
path_gfx = os.path.join(main_dir, "_web/gfx")
path_stl = os.path.join(main_dir, "_web_stl")
print('path_gfx: ',path_gfx)
print('path_stl: ',path_stl)
# running live
####
asyncio.set_event_loop(u_info.worker_loop)
dojo = tornado.web.Application([
(r'/dojo/gfx/(.*)', tornado.web.StaticFileHandler, {'path': path_gfx}),
(r'/dojo/stl/(.*)', tornado.web.StaticFileHandler, {'path': path_stl}),
(r'/ws', _dojo.Websockets, dict(controller=self.__controller)),
(r'/(.*)', DojoHandler, dict(logic=self))
],debug=True,autoreload=True) # (r'/dojo/gfx/(.*)', tornado.web.StaticFileHandler, {'path': '/dojo/gfx'})
# dojo.listen(u_info.port, max_buffer_size=1024*1024*150000)
server = tornado.httpserver.HTTPServer(dojo)
server.listen(u_info.port)
print('*'*80)
print('*', 'DOJO RUNNING')
print('*')
print('*', 'open', '[ http://' + u_info.ip + ':' + str(u_info.port) + '/dojo/ ] ')
print('*'*80)
tornado.ioloop.IOLoop.instance().start()
server.stop()
# def sig_handler(signum, frame):
# IOLoop.current().add_callback_from_signal(receiver.shutdown)
print("Tornado web server stops.")
return
##
## IOLoop.instance().stop()
## return
##
def stop():
asyncio.asyncio_loop.stop()
server.stop()
# thread.join()
def get_image(self):
return self.__image
def get_segmentation(self):
return self.__segmentation
def get_controller(self):
return self.__controller
def handle( self, r ):
content = None
# 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('Sayonara..!!')
output = {}
output['origin'] = 'SERVER'
sys.exit(0)