7 class Processor(threading.Thread):
11 self.lock = threading.Lock()
13 threading.Thread.__init__(self)
16 def add_session(self, session):
18 self.sessions.append(session)
21 if self.shared is None:
22 raise TypeError("self.shared not set in Processor")
23 while not self.shared.stopped():
24 # Deep copy entire sessions list and blank it
25 # This is done to minimise lock contention
27 sessions = self.sessions[:]
29 for session in sessions:
30 if not session.stopped():
31 # If session is still alive then re-add it back
32 # to our internal register
33 self.add_session(session)
40 def process(self, session):
41 request = session.pop_request()
42 print "New request", request
43 # Execute and when ready, you call
44 # session.push_response(response)
48 def __init__(self, connection, address):
49 self._connection = connection
50 self.address = address
52 self.lock = threading.Lock()
54 self.request_queue = queue.Queue()
55 self.response_queue = queue.Queue()
58 self._connection.close()
59 print "Terminating connection:", self.address[0]
69 raise Exception("Session was stopped")
71 return self._connection
73 def push_request(self, item):
74 self.request_queue.put(item)
76 def pop_request(self):
77 return self.request_queue.get()
79 def push_response(self, item):
80 self.response_queue.put(item)
82 def pop_response(self):
83 return self.response_queue.get()
85 class TcpClientResponder(threading.Thread):
87 def __init__(self, shared, session):
89 self.session = session
90 threading.Thread.__init__(self)
93 while not self.shared.stopped() or self.session.stopped():
94 response = self.session.pop_response()
95 raw_response = json.dumps(response)
96 # Possible race condition here by having session
98 # I assume Python connections are thread safe interfaces
99 connection = self.session.connection()
101 connection.send(raw_response + "\n")
105 class TcpClientRequestor(threading.Thread):
107 def __init__(self, shared, session):
110 self.session = session
111 threading.Thread.__init__(self)
114 while not self.shared.stopped():
115 if not self.update():
120 data = self.receive()
133 return self.session.connection().recv(1024)
138 raw_buffer = self.message.find('\n')
142 raw_command = self.message[0:raw_buffer].strip()
143 self.message = self.message[raw_buffer + 1:]
144 if raw_command == 'quit':
148 command = json.loads(raw_command)
150 self.session.push_response(
151 {"error": "bad JSON", "request": raw_command})
155 # Try to load vital fields, and return an error if
157 message_id = command['id']
158 method = command['method']
160 # Return an error JSON in response.
161 self.session.push_response(
162 {"error": "syntax error", "request": raw_command})
164 self.session.push_request(command)
168 class TcpServer(threading.Thread):
170 def __init__(self, shared, processor):
172 self.processor = processor
174 threading.Thread.__init__(self)
178 print "TCP server started."
179 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
180 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
181 sock.bind(("localhost", 50001))
183 while not self.shared.stopped():
184 session = Session(*sock.accept())
185 client_req = TcpClientRequestor(self.shared, session)
187 client_res = TcpClientResponder(self.shared, session)
189 self.processor.add_session(session)
194 self.lock = threading.Lock()
195 self._stopped = False
198 print "Stopping Stratum"
208 def start(self, processor):
210 # Bind shared to processor since constructor is user defined
211 processor.shared = shared
213 # Create various transports we need
214 transports = TcpServer(shared, processor),
215 for server in transports:
217 while not shared.stopped():
218 if raw_input() == "quit":
222 if __name__ == "__main__":
223 processor = Processor()