-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_server.py
127 lines (106 loc) · 3.35 KB
/
data_server.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
# -*- coding: utf-8 -*-
'''
Created on 2013-11-8
@author: samuelchen
'''
import SocketServer
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
import util
import os
import threading
from http_server import *
__version__ = '0.1'
__all__ = ['DataServer']
#@IServer
class DataServer(object):
'''
A tcp server for data transfer between peers.
'''
SUPPORT_PROTOCALS = ('tcp', 'http', 'udp')
DEFAULT_PORT = 37123
instance = None
thread = None
def __init__(self, port=DEFAULT_PORT, protocal='tcp', callbacks={}):
'''
Constructor.
*port*: int. service port for DataPeer
*protocal*: string. data transfer protocal. supports 'tcp', 'http', 'udp'
*callbacks*: map. callback functions to process the data your self.
'''
self.log = util.getLogger('DataServer(%d)' % port)
if not protocal in DataServer.SUPPORT_PROTOCALS:
self.log.error = '%s is not a supported data server protocal'
raise 'Not supported data transfer protocal'
pass
self.protocal = protocal
self.port = port
self.callbacks = callbacks
self.log.info('Data server (%s) created.' % protocal.upper())
def start(self, protocal=None):
'''
start data server
*protocal* : string. data transfer protocal. supports 'tcp', 'http', 'udp'
'''
if not protocal:
if not self.protocal: self.protocal = 'tcp'
else:
self.protocal = protocal
assert(self.protocal in DataServer.SUPPORT_PROTOCALS)
if self.protocal == 'tcp':
pass
elif self.protocal == 'http':
self.instance, self.thread = self._startHTTPServer()
elif self.protocal == 'udp':
pass
def stop(self):
if self.instance:
self.instance.shutdown()
def isAlive(self):
return self.thread and self.thread.isAlive()
def isBusy(self):
return self.instance.isBusy()
@property
def ip(self):
return self.instance.server_address[0]
def _startHTTPServer(self):
# callbacks = {
# 'resource' : self._on_resource,
# 'signature' : self._on_signature,
# }
svr = HTTPServer(('0.0.0.0', self.port), HTTPRequestHandler)
svr.init(callbacks=self.callbacks, chunked=True)
t = threading.Thread(target=svr.serve_forever)
t.daemon = True
t.start()
t.name = 'DataServer(%d)' % t.ident
return svr, t
# --------- http callbacks ----------
# def _on_resource(self, request, **kwargs):
# print '-'*60
# print request
# print '-'*60
# print kwargs
# print '-'*60
#
# ret = None
# self.log.debug(':: _on_resource')
# if 'resource' in self.callbacks:
# fn = self.callbacks['resource']
# if fn: ret = fn(request, **kwargs)
# return ret
#
# def _on_signature(self, **kwargs):
#
# """Simple signature for intranet download
# """
#
# ret = False
# self.log.info(':: _on_signature')
# if 'signature' in self.callbacks:
# fn = self.callbacks['signature']
# ret = fn(**kwargs)
# return ret
if __name__ == '__main__':
svr = DataServer(protocal='http')
svr.start()