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)
134 raw_buffer = self.message.find('\n')
138 raw_command = self.message[0:raw_buffer].strip()
139 self.message = self.message[raw_buffer + 1:]
140 if raw_command == 'quit':
144 command = json.loads(raw_command)
146 self.session.push_response(
147 {"error": "bad JSON", "request": raw_command})
151 # Try to load vital fields, and return an error if
153 message_id = command['id']
154 method = command['method']
156 # Return an error JSON in response.
157 self.session.push_response(
158 {"error": "syntax error", "request": raw_command})
160 self.session.push_request(command)
164 class TcpServer(threading.Thread):
166 def __init__(self, shared, processor):
168 self.processor = processor
170 threading.Thread.__init__(self)
174 print "TCP server started."
175 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
176 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
177 sock.bind(("localhost", 50001))
179 while not self.shared.stopped():
180 session = Session(*sock.accept())
181 client_req = TcpClientRequestor(self.shared, session)
183 client_res = TcpClientResponder(self.shared, session)
185 self.processor.add_session(session)
190 self.lock = threading.Lock()
191 self._stopped = False
194 print "Stopping Stratum"
204 def start(self, processor):
206 # Bind shared to processor since constructor is user defined
207 processor.shared = shared
209 # Create various transports we need
210 transports = TcpServer(shared, processor),
211 for server in transports:
213 while not shared.stopped():
214 if raw_input() == "quit":
218 if __name__ == "__main__":
219 processor = Processor()