Victor Vasiliev has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/52957


Change subject: Implement a simple text-based protocol.
......................................................................

Implement a simple text-based protocol.

This is helpful for languages without good WebSocket clients.

Change-Id: I0da9e1d597daa6b58bdddd46b321159dc68ec26c
---
M rcsub.py
1 file changed, 36 insertions(+), 13 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/rcsub 
refs/changes/57/52957/1

diff --git a/rcsub.py b/rcsub.py
index 92e35db..ccd93df 100644
--- a/rcsub.py
+++ b/rcsub.py
@@ -3,13 +3,14 @@
 import json
 
 from twisted.internet import reactor
-from twisted.internet.protocol import DatagramProtocol
+from twisted.internet.protocol import DatagramProtocol, ServerFactory
 from twisted.protocols.basic import LineReceiver
 from autobahn.websocket import WebSocketServerFactory, 
WebSocketServerProtocol, listenWS
 
 config = {
     'input_port' : 12719,
     'websocket_port' : 13719,
+    'text_port' : 14719,
     'max_channels' : 100,
 }
 
@@ -59,7 +60,9 @@
     def deliver(self, message):
         self.target.deliver(message)
 
-    def handleJSONCommand(self, command):
+    def handleJSONCommand(self, commandText):
+        command = json.loads(commandText)
+
         if '@' not in command:
             raise ProtocolError('No command specified')
 
@@ -72,7 +75,7 @@
         if command['@'] == 'list-subscriptions':
             return { "@" : "subscriptions", "channels" : self.channels }
 
-        return None
+        raise ProtocolError('Unknown command: %s' % command['@'])
 
 router = MessageRouter()
 
@@ -97,23 +100,43 @@
 
     def onMessage(self, data, is_binary):
         try:
-            message = json.loads(data)
-            
-            response = self.subscriber.handleJSONCommand(message)
-            if response:
-                self.message(response)
-            else:
-                raise ProtocolError('Unknown command: %s' % message['@'])
+            self.message( self.subscriber.handleJSONCommand(data) )
         except Exception as err:
             self.message({'@' : 'error'})
 
     def deliver(self, message):
         self.message(message)
 
-factory = WebSocketServerFactory( "ws://localhost:%i" % 
config['websocket_port'] )
-factory.protocol = WebSocketRCFeed
-listenWS(factory)
+class SimpleTextRCFeed(LineReceiver):
+    def connectionMade(self):
+        LineReceiver.connectionMade(self)
+        self.subscriber = Subscriber(self)
+
+    def onClose(self, wasClean, code, reason):
+        self.subscriber.unsubscribeAll()
+
+    def message(self, data):
+        self.transport.write(json.dumps(data) + "\r\n")
+
+    def lineReceived(self, line):
+        try:
+            print line
+            self.message( self.subscriber.handleJSONCommand(line) )
+        except Exception as err:
+            self.message({'@' : 'error'})
+
+    def deliver(self, message):
+        self.message(message)
+
+ws_factory = WebSocketServerFactory( "ws://localhost:%i" % 
config['websocket_port'] )
+ws_factory.protocol = WebSocketRCFeed
+listenWS(ws_factory)
+
+st_factory = ServerFactory()
+st_factory.protocol = SimpleTextRCFeed
+reactor.listenTCP(config['text_port'], st_factory)
 
 reactor.listenUDP(config['input_port'], MediaWikiRCInput())
+
 reactor.run()
 

-- 
To view, visit https://gerrit.wikimedia.org/r/52957
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0da9e1d597daa6b58bdddd46b321159dc68ec26c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/rcsub
Gerrit-Branch: master
Gerrit-Owner: Victor Vasiliev <vasi...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to