[Assorted-commits] SF.net SVN: assorted:[1681] sandbox/trunk/src/misc
Brought to you by:
yangzhang
|
From: <yan...@us...> - 2010-06-12 06:03:17
|
Revision: 1681
http://assorted.svn.sourceforge.net/assorted/?rev=1681&view=rev
Author: yangzhang
Date: 2010-06-12 06:03:07 +0000 (Sat, 12 Jun 2010)
Log Message:
-----------
Added protobuf Python RPC demo
Added Paths:
-----------
sandbox/trunk/src/misc/protobuf/
sandbox/trunk/src/misc/protobuf/Makefile
sandbox/trunk/src/misc/protobuf/README
sandbox/trunk/src/misc/protobuf/clisrv.py
sandbox/trunk/src/misc/protobuf/simple.proto
Added: sandbox/trunk/src/misc/protobuf/Makefile
===================================================================
--- sandbox/trunk/src/misc/protobuf/Makefile (rev 0)
+++ sandbox/trunk/src/misc/protobuf/Makefile 2010-06-12 06:03:07 UTC (rev 1681)
@@ -0,0 +1,5 @@
+simple_pb2.py: simple.proto
+ protoc --python_out=. simple.proto
+.PHONY: clean
+clean:
+ rm -f simple_pb2.py
Added: sandbox/trunk/src/misc/protobuf/README
===================================================================
--- sandbox/trunk/src/misc/protobuf/README (rev 0)
+++ sandbox/trunk/src/misc/protobuf/README 2010-06-12 06:03:07 UTC (rev 1681)
@@ -0,0 +1 @@
+Demo of PB RPC implementation <http://code.google.com/p/protobuf-socket-rpc/>
Added: sandbox/trunk/src/misc/protobuf/clisrv.py
===================================================================
--- sandbox/trunk/src/misc/protobuf/clisrv.py (rev 0)
+++ sandbox/trunk/src/misc/protobuf/clisrv.py 2010-06-12 06:03:07 UTC (rev 1681)
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+
+from simple_pb2 import *
+import protobuf.server, protobuf, sys, time
+
+class QAImpl(QA):
+ def Ask(self, controller, query, done):
+ time.sleep(1)
+ done.run(Answer(text='hello!'))
+
+if sys.argv[1:] == ['server']:
+
+ # Start server
+ server = protobuf.server.SocketRpcServer(9876)
+ server.registerService(QAImpl())
+ server.run()
+
+else:
+
+ # Define callback
+ class Callback:
+ def run(self,response):
+ print "Received Response: %s" % response
+
+ # Create channel
+ channel = protobuf.channel.SocketRpcChannel('localhost',9876)
+ controller = channel.newController()
+
+ # Call service
+ service = QA_Stub(channel)
+ request = Question(text = 'hello?')
+ service.Ask(controller,request,Callback()) # blocks
+
+ # Check success
+ if controller.failed():
+ print "Rpc failed %s : %s" % (controller.error,controller.reason)
+
+# vim: et sw=2 ts=2
Added: sandbox/trunk/src/misc/protobuf/simple.proto
===================================================================
--- sandbox/trunk/src/misc/protobuf/simple.proto (rev 0)
+++ sandbox/trunk/src/misc/protobuf/simple.proto 2010-06-12 06:03:07 UTC (rev 1681)
@@ -0,0 +1,11 @@
+message Question {
+ optional string text = 1;
+}
+
+message Answer {
+ optional string text = 1;
+}
+
+service QA {
+ rpc Ask (Question) returns (Answer);
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|