diff --git a/.gitignore b/.gitignore index 862398998..1542348d0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.a *.6 +*.pyc .settings src/inquirer/inquirer src/rater/rater diff --git a/src/inquirer/Makefile b/src/inquirer/Makefile index ec3db1559..fd5687720 100644 --- a/src/inquirer/Makefile +++ b/src/inquirer/Makefile @@ -4,5 +4,6 @@ TARG=inquirer GOFILES=\ inquirer.go\ registration/registration.go\ + responder/responder.go\ include $(GOROOT)/src/Make.cmd diff --git a/src/inquirer/inquirer.go b/src/inquirer/inquirer.go index a6485cd78..0d2bc8e7a 100644 --- a/src/inquirer/inquirer.go +++ b/src/inquirer/inquirer.go @@ -19,32 +19,41 @@ func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "") } -func callRater() { - var reply string +func callRater(key string) (reply string) { err := errors.New("") //not nil value for err != nil { client:= <-raterList.Balancer - err = client.Call("Storage.Get", "test", &reply) + err = client.Call("Storage.Get", key, &reply) if err != nil { log.Printf("Got en error from rater: %v", err) } } - fmt.Println(fmt.Sprintf("Result: %v", reply)) + log.Print(fmt.Sprintf("Result: %v", reply)) + return } -func testCallRater(){ +func testCallRater(key string){ for { - callRater() + callRater(key) time.Sleep(1 * time.Second) } } func main() { raterList = NewRaterList() - go testCallRater() + //go testCallRater("test") rpc.Register(raterList) rpc.HandleHTTP() - http.HandleFunc("/", handler) + + responder := new(Responder) + srvr := rpc.NewServer() + srvr.Register(responder) + f1 := func(w http.ResponseWriter, req *http.Request) { + srvr.ServeHTTP(w, req) + } + http.HandleFunc("/rpc", f1) + + http.HandleFunc("/", handler) log.Print("The server is listening...") http.ListenAndServe(":2000", nil) } diff --git a/src/inquirer/responder/responder.go b/src/inquirer/responder/responder.go new file mode 100644 index 000000000..c254f832c --- /dev/null +++ b/src/inquirer/responder/responder.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" +) + +type Responder int + +func (r *Responder) Get(args string, replay *string) error { + *replay = fmt.Sprintf("{'response': %s}", callRater(args)) + return nil +} + diff --git a/src/test.go b/src/test.go new file mode 100644 index 000000000..c699cedb9 --- /dev/null +++ b/src/test.go @@ -0,0 +1,18 @@ +package main + +import ( + "net/rpc" + "fmt" +) + + +func main(){ + client, _ := rpc.DialHTTPPath("tcp", "localhost:2000", "/rpc") + var reply string + for i:= 0; i < 5 * 10e6; i++ { + client.Call("Responder.Get", "test", &reply) + } + fmt.Println(reply) +} + + diff --git a/src/test/Makefile b/src/test/Makefile new file mode 100644 index 000000000..5e37857e6 --- /dev/null +++ b/src/test/Makefile @@ -0,0 +1,6 @@ +include $(GOROOT)/src/Make.inc + +TARG=test +GOFILES=test.go + +include $(GOROOT)/src/Make.cmd diff --git a/src/test/sj.py b/src/test/sj.py new file mode 100644 index 000000000..4e9acdf07 --- /dev/null +++ b/src/test/sj.py @@ -0,0 +1,149 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by the +# Free Software Foundation; either version 3, or (at your option) any later +# version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. + +"Pythonic simple JSON RPC Client implementation" + +__author__ = "Mariano Reingart (reingart@gmail.com)" +__copyright__ = "Copyright (C) 2011 Mariano Reingart" +__license__ = "LGPL 3.0" +__version__ = "0.04" + + +import urllib +from xmlrpclib import Transport, SafeTransport +from cStringIO import StringIO +import random +import sys +try: + import gluon.contrib.simplejson as json # try web2py json serializer +except ImportError: + try: + import json # try stdlib (py2.6) + except: + import simplejson as json # try external module + + +class JSONRPCError(RuntimeError): + "Error object for remote procedure call fail" + def __init__(self, code, message): + self.code = code + self.message = message + def __unicode__(self): + return u"%s: %s" % (self.code, self.message) + def __str__(self): + return self.__unicode__().encode("ascii","ignore") + + +class JSONDummyParser: + "json wrapper for xmlrpclib parser interfase" + def __init__(self): + self.buf = StringIO() + def feed(self, data): + self.buf.write(data) + def close(self): + return self.buf.getvalue() + + +class JSONTransportMixin: + "json wrapper for xmlrpclib transport interfase" + + def send_content(self, connection, request_body): + connection.putheader("Content-Type", "application/json") + connection.putheader("Content-Length", str(len(request_body))) + connection.endheaders() + if request_body: + connection.send(request_body) + # todo: add gzip compression + + def getparser(self): + # get parser and unmarshaller + parser = JSONDummyParser() + return parser, parser + + +class JSONTransport(JSONTransportMixin, Transport): + pass + +class JSONSafeTransport(JSONTransportMixin, SafeTransport): + pass + + +class ServerProxy(object): + "JSON RPC Simple Client Service Proxy" + + def __init__(self, uri, transport=None, encoding=None, verbose=0): + self.location = uri # server location (url) + self.trace = verbose # show debug messages + self.exceptions = True # raise errors? (JSONRPCError) + self.timeout = None + self.json_request = self.json_response = '' + + type, uri = urllib.splittype(uri) + if type not in ("http", "https"): + raise IOError, "unsupported JSON-RPC protocol" + self.__host, self.__handler = urllib.splithost(uri) + + if transport is None: + if type == "https": + transport = JSONSafeTransport() + else: + transport = JSONTransport() + self.__transport = transport + self.__encoding = encoding + self.__verbose = verbose + + def __getattr__(self, attr): + "pseudo method that can be called" + return lambda *args: self.call(attr, *args) + + def call(self, method, *args): + "JSON RPC communication (method invocation)" + + # build data sent to the service + request_id = random.randint(0, sys.maxint) + data = {'id': request_id, 'method': method, 'params': args, } + request = json.dumps(data) + + # make HTTP request (retry if connection is lost) + response = self.__transport.request( + self.__host, + self.__handler, + request, + verbose=self.__verbose + ) + + # store plain request and response for further debugging + self.json_request = request + self.json_response = response + + # parse json data coming from service + # {'version': '1.1', 'id': id, 'result': result, 'error': None} + response = json.loads(response) + + if response['id'] != request_id: + raise JSONRPCError(0, "JSON Request ID != Response ID") + + self.error = response.get('error', {}) + if self.error and self.exceptions: + raise JSONRPCError(self.error.get('code', 0), self.error.get('message', '')) + + return response.get('result') + + +if __name__ == "__main__": + # basic tests: + location = "http://www.web2py.com.ar/webservices/sample/call/jsonrpc" + client = ServerProxy(location, verbose='--verbose' in sys.argv,) + print client.add(1, 2) + + + diff --git a/src/test/test b/src/test/test new file mode 100755 index 000000000..4a2609a9f Binary files /dev/null and b/src/test/test differ diff --git a/src/test/test.go b/src/test/test.go new file mode 100644 index 000000000..cbc8ee007 --- /dev/null +++ b/src/test/test.go @@ -0,0 +1,16 @@ +package main + +import ( + "net/rpc" + "fmt" +) + + +func main(){ + client, _ := rpc.DialHTTPPath("tcp", "localhost:2000", "/rpc") + var reply string + client.Call("Responder.Get", "test", &reply) + fmt.Println(reply) +} + + diff --git a/src/test/test.py b/src/test/test.py new file mode 100644 index 000000000..6bf609938 --- /dev/null +++ b/src/test/test.py @@ -0,0 +1,5 @@ +import simplejsonrpc as jsonrpc + +server = jsonrpc.Server("http://localhost:2000/rpc") +print dir(server) +#print server.Get("test") diff --git a/src/test/test2.py b/src/test/test2.py new file mode 100644 index 000000000..9ee8e217a --- /dev/null +++ b/src/test/test2.py @@ -0,0 +1,5 @@ +from sj import * +URL = "http://127.0.0.1:2000/rpc" +service = ServerProxy(URL, verbose=True) +print service.call("Responder.Get", "test") +print service.ResponseGet("test")