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()
56 self.numblocks_sub = None
57 self.addresses_sub = {}
60 self._connection.close()
61 print "Terminating connection:", self.address[0]
71 raise Exception("Session was stopped")
73 return self._connection
75 def push_request(self, item):
76 self.request_queue.put(item)
78 def pop_request(self):
79 return self.request_queue.get()
81 def push_response(self, item):
82 self.response_queue.put(item)
84 def pop_response(self):
85 return self.response_queue.get()
87 def subscribe_to_numblocks(self,message_id):
89 self.numblocks_sub = message_id
91 def subscribe_to_address(self,address,message_id,status):
93 self.addresses_sub[address] = message_id,status
96 class TcpClientResponder(threading.Thread):
98 def __init__(self, shared, session):
100 self.session = session
101 threading.Thread.__init__(self)
104 while not self.shared.stopped() or self.session.stopped():
105 response = self.session.pop_response()
106 raw_response = json.dumps(response)
107 # Possible race condition here by having session
109 # I assume Python connections are thread safe interfaces
110 connection = self.session.connection()
112 connection.send(raw_response + "\n")
116 class TcpClientRequestor(threading.Thread):
118 def __init__(self, shared, session):
121 self.session = session
122 threading.Thread.__init__(self)
125 while not self.shared.stopped():
126 if not self.update():
131 data = self.receive()
144 return self.session.connection().recv(1024)
149 raw_buffer = self.message.find('\n')
153 raw_command = self.message[0:raw_buffer].strip()
154 self.message = self.message[raw_buffer + 1:]
155 if raw_command == 'quit':
159 command = json.loads(raw_command)
161 self.session.push_response(
162 {"error": "bad JSON", "request": raw_command})
166 # Try to load vital fields, and return an error if
168 message_id = command['id']
169 method = command['method']
171 # Return an error JSON in response.
172 self.session.push_response(
173 {"error": "syntax error", "request": raw_command})
175 self.session.push_request(command)
179 class TcpServer(threading.Thread):
181 def __init__(self, shared, processor, host, port):
183 self.processor = processor
185 threading.Thread.__init__(self)
191 print "TCP server started."
192 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
193 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
194 sock.bind((self.host, self.port))
196 while not self.shared.stopped():
197 session = Session(*sock.accept())
198 client_req = TcpClientRequestor(self.shared, session)
200 client_res = TcpClientResponder(self.shared, session)
202 self.processor.add_session(session)
207 self.lock = threading.Lock()
208 self._stopped = False
211 print "Stopping Stratum"
221 def start(self, processor):
223 # Bind shared to processor since constructor is user defined
224 processor.shared = shared
226 # Create various transports we need
227 transports = TcpServer(shared, processor, "176.31.24.241",50001),
228 for server in transports:
230 while not shared.stopped():
231 if raw_input() == "quit":
235 if __name__ == "__main__":
236 processor = Processor()