88from market .profile import Profile
99from keyutils .keys import KeyChain
1010from random import shuffle
11- from autobahn .twisted .websocket import WebSocketServerFactory , WebSocketServerProtocol
1211from protos .countries import CountryCode
1312from protos .objects import PlaintextMessage , Value , Listings
1413from protos import objects
1514from binascii import unhexlify
1615from dht .node import Node
16+ from twisted .internet .protocol import Protocol , Factory , connectionDone
1717
1818
19- class WSProtocol (WebSocketServerProtocol ):
19+ # pylint: disable=W0232
20+ class WSProtocol (Protocol ):
2021 """
2122 Handles new incoming requests coming from a websocket.
2223 """
2324
24- def onOpen (self ):
25+ def connectionMade (self ):
2526 self .factory .register (self )
2627
28+ def connectionLost (self , reason = connectionDone ):
29+ self .factory .unregister (self )
30+
2731 def get_vendors (self , message_id ):
2832 if message_id in self .factory .outstanding_vendors :
2933 queried = self .factory .outstanding_vendors [message_id ]
@@ -55,7 +59,7 @@ def handle_response(metadata, node):
5559 "nsfw" : metadata .nsfw
5660 }
5761 }
58- self .sendMessage (json .dumps (vendor , indent = 4 ), False )
62+ self .transport . write (json .dumps (vendor , indent = 4 ))
5963 queried .append (node .id )
6064 return True
6165 else :
@@ -92,7 +96,7 @@ def parse_profile(profile, node):
9296 "fee" : profile .moderation_fee
9397 }
9498 }
95- self .sendMessage (json .dumps (moderator , indent = 4 ), False )
99+ self .transport . write (json .dumps (moderator , indent = 4 ))
96100 else :
97101 m .delete_moderator (node .id )
98102 for mod in moderators :
@@ -152,7 +156,7 @@ def handle_response(listings, node):
152156 self .factory .mserver .get_image (node , l .thumbnail_hash )
153157 if not os .path .isfile (DATA_FOLDER + 'cache/' + listings .avatar_hash .encode ("hex" )):
154158 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 ))
156160 count += 1
157161 self .factory .outstanding_listings [message_id ].append (l .contract_hash )
158162 if count == 3 :
@@ -202,7 +206,7 @@ def respond(l, node):
202206 }
203207 for country in l .ships_to :
204208 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 ))
206210
207211 def parse_results (values ):
208212 if values is not None :
@@ -230,7 +234,7 @@ def parse_results(values):
230234 pass
231235 self .factory .kserver .get (keyword .lower ()).addCallback (parse_results )
232236
233- def onMessage (self , payload , isBinary ):
237+ def dataReceived (self , payload ):
234238 try :
235239 request_json = json .loads (payload )
236240 if isinstance (request_json , unicode ):
@@ -262,32 +266,26 @@ def onMessage(self, payload, isBinary):
262266 except Exception as e :
263267 print 'Exception occurred: %s' % e
264268
265- def connectionLost (self , reason ):
266- WebSocketServerProtocol .connectionLost (self , reason )
267- self .factory .unregister (self )
268-
269-
270- class WSFactory (WebSocketServerFactory ):
271269
272- """
273- Simple broadcast server broadcasting any message it receives to all
274- currently connected clients.
275- """
270+ class WSFactory (Factory ):
276271
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" ):
279273 self .mserver = mserver
280274 self .kserver = kserver
281275 self .db = mserver .db
282276 self .outstanding_listings = {}
283277 self .outstanding_vendors = {}
284- self .clients = []
278+ self .protocol = WSProtocol
285279 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 )
286286
287287 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 :
291289 self .clients .append (client )
292290
293291 def unregister (self , client ):
@@ -296,4 +294,6 @@ def unregister(self, client):
296294
297295 def push (self , msg ):
298296 for c in self .clients :
299- c .sendMessage (msg )
297+ c .transport .write (msg )
298+
299+
0 commit comments