8
8
from market .profile import Profile
9
9
from keyutils .keys import KeyChain
10
10
from random import shuffle
11
- from autobahn .twisted .websocket import WebSocketServerFactory , WebSocketServerProtocol
12
11
from protos .countries import CountryCode
13
12
from protos .objects import PlaintextMessage , Value , Listings
14
13
from protos import objects
15
14
from binascii import unhexlify
16
15
from dht .node import Node
16
+ from twisted .internet .protocol import Protocol , Factory , connectionDone
17
17
18
18
19
- class WSProtocol (WebSocketServerProtocol ):
19
+ # pylint: disable=W0232
20
+ class WSProtocol (Protocol ):
20
21
"""
21
22
Handles new incoming requests coming from a websocket.
22
23
"""
23
24
24
- def onOpen (self ):
25
+ def connectionMade (self ):
25
26
self .factory .register (self )
26
27
28
+ def connectionLost (self , reason = connectionDone ):
29
+ self .factory .unregister (self )
30
+
27
31
def get_vendors (self , message_id ):
28
32
if message_id in self .factory .outstanding_vendors :
29
33
queried = self .factory .outstanding_vendors [message_id ]
@@ -55,7 +59,7 @@ def handle_response(metadata, node):
55
59
"nsfw" : metadata .nsfw
56
60
}
57
61
}
58
- self .sendMessage (json .dumps (vendor , indent = 4 ), False )
62
+ self .transport . write (json .dumps (vendor , indent = 4 ))
59
63
queried .append (node .id )
60
64
return True
61
65
else :
@@ -92,7 +96,7 @@ def parse_profile(profile, node):
92
96
"fee" : profile .moderation_fee
93
97
}
94
98
}
95
- self .sendMessage (json .dumps (moderator , indent = 4 ), False )
99
+ self .transport . write (json .dumps (moderator , indent = 4 ))
96
100
else :
97
101
m .delete_moderator (node .id )
98
102
for mod in moderators :
@@ -152,7 +156,7 @@ def handle_response(listings, node):
152
156
self .factory .mserver .get_image (node , l .thumbnail_hash )
153
157
if not os .path .isfile (DATA_FOLDER + 'cache/' + listings .avatar_hash .encode ("hex" )):
154
158
self .factory .mserver .get_image (node , listings .avatar_hash )
155
- self .sendMessage (json .dumps (listing_json , indent = 4 ), False )
159
+ self .transport . write (json .dumps (listing_json , indent = 4 ))
156
160
count += 1
157
161
self .factory .outstanding_listings [message_id ].append (l .contract_hash )
158
162
if count == 3 :
@@ -202,7 +206,7 @@ def respond(l, node):
202
206
}
203
207
for country in l .ships_to :
204
208
listing_json ["listing" ]["ships_to" ].append (str (CountryCode .Name (country )))
205
- self .sendMessage (json .dumps (listing_json , indent = 4 ), False )
209
+ self .transport . write (json .dumps (listing_json , indent = 4 ))
206
210
207
211
def parse_results (values ):
208
212
if values is not None :
@@ -230,7 +234,7 @@ def parse_results(values):
230
234
pass
231
235
self .factory .kserver .get (keyword .lower ()).addCallback (parse_results )
232
236
233
- def onMessage (self , payload , isBinary ):
237
+ def dataReceived (self , payload ):
234
238
try :
235
239
request_json = json .loads (payload )
236
240
if isinstance (request_json , unicode ):
@@ -262,32 +266,26 @@ def onMessage(self, payload, isBinary):
262
266
except Exception as e :
263
267
print 'Exception occurred: %s' % e
264
268
265
- def connectionLost (self , reason ):
266
- WebSocketServerProtocol .connectionLost (self , reason )
267
- self .factory .unregister (self )
268
-
269
-
270
- class WSFactory (WebSocketServerFactory ):
271
269
272
- """
273
- Simple broadcast server broadcasting any message it receives to all
274
- currently connected clients.
275
- """
270
+ class WSFactory (Factory ):
276
271
277
- def __init__ (self , url , mserver , kserver , only_ip = "127.0.0.1" , debug = False , debugCodePaths = False ):
278
- WebSocketServerFactory .__init__ (self , url , debug = debug , debugCodePaths = debugCodePaths )
272
+ def __init__ (self , mserver , kserver , only_ip = "127.0.0.1" ):
279
273
self .mserver = mserver
280
274
self .kserver = kserver
281
275
self .db = mserver .db
282
276
self .outstanding_listings = {}
283
277
self .outstanding_vendors = {}
284
- self .clients = []
278
+ self .protocol = WSProtocol
285
279
self .only_ip = only_ip
280
+ self .clients = []
281
+
282
+ def buildProtocol (self , addr ):
283
+ if addr .host != self .only_ip and self .only_ip != "0.0.0.0" :
284
+ return
285
+ return Factory .buildProtocol (self , addr )
286
286
287
287
def register (self , client ):
288
- if client .transport .getPeer ().host != self .only_ip and self .only_ip != "0.0.0.0" :
289
- client .transport .loseConnection ()
290
- elif client not in self .clients :
288
+ if client not in self .clients :
291
289
self .clients .append (client )
292
290
293
291
def unregister (self , client ):
@@ -296,4 +294,6 @@ def unregister(self, client):
296
294
297
295
def push (self , msg ):
298
296
for c in self .clients :
299
- c .sendMessage (msg )
297
+ c .transport .write (msg )
298
+
299
+
0 commit comments