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)
36 def process(self, session):
37 request = session.pop_request()
38 print "New request", request
39 # Execute and when ready, you call
40 # session.push_response(response)
44 def __init__(self, connection, address):
45 self._connection = connection
46 self.address = address
48 self.lock = threading.Lock()
50 self.request_queue = queue.Queue()
51 self.response_queue = queue.Queue()
54 self._connection.close()
55 print "Terminating connection:", self.address[0]
65 raise Exception("Session was stopped")
67 return self._connection
69 def push_request(self, item):
70 self.request_queue.put(item)
72 def pop_request(self):
73 return self.request_queue.get()
75 def push_response(self, item):
76 self.response_queue.put(item)
78 def pop_response(self):
79 return self.response_queue.get()
81 class TcpClientResponder(threading.Thread):
83 def __init__(self, shared, session):
85 self.session = session
86 threading.Thread.__init__(self)
89 while not self.shared.stopped() or self.session.stopped():
90 response = self.session.pop_response()
91 raw_response = json.dumps(response)
92 # Possible race condition here by having session
94 # I assume Python connections are thread safe interfaces
95 connection = self.session.connection()
97 connection.send(raw_response + "\n")
101 class TcpClientRequestor(threading.Thread):
103 def __init__(self, shared, session):
106 self.session = session
107 threading.Thread.__init__(self)
110 while not self.shared.stopped():
111 if not self.update():
116 data = self.receive()
129 return self.session.connection().recv(1024)
135 raw_buffer = self.message.find('\n')
139 raw_command = self.message[0:raw_buffer].strip()
140 self.message = self.message[raw_buffer + 1:]
141 if raw_command == 'quit':
145 command = json.loads(raw_command)
147 self.session.push_response(
148 {"error": "bad JSON", "request": raw_command})
152 # Try to load vital fields, and return an error if
154 message_id = command['id']
155 method = command['method']
157 # Return an error JSON in response.
158 self.session.push_response(
159 {"error": "syntax error", "request": raw_command})
161 self.session.push_request(command)
165 class TcpServer(threading.Thread):
167 def __init__(self, shared, processor):
169 self.processor = processor
171 threading.Thread.__init__(self)
175 print "TCP server started."
176 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
177 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
178 sock.bind(("localhost", 50001))
180 while not self.shared.stopped():
181 session = Session(*sock.accept())
182 client_req = TcpClientRequestor(self.shared, session)
184 client_res = TcpClientResponder(self.shared, session)
186 self.processor.add_session(session)
191 self.lock = threading.Lock()
192 self._stopped = False
195 print "Stopping Stratum"
205 def start(self, processor):
207 # Bind shared to processor since constructor is user defined
208 processor.shared = shared
210 # Create various transports we need
211 transports = TcpServer(shared, processor),
212 for server in transports:
214 while not shared.stopped():
215 if raw_input() == "quit":
219 if __name__ == "__main__":
220 processor = Processor()