Commits

Ning Sun committed d90ec87

some work on codec

Comments (0)

Files changed (2)

 # --*-- Coding: utf-8 --*--
 
+import random
+
 import gevent
 import gevent.socket
 import gevent.event
 
 class VenusError(Exception): pass
 class ConnectError(VenusError): pass
+class InvalidMessageFormat(VenusError): pass
 
 class Connection(object):
     """ A connection to Venus Server
 
                     self.trans[header.transaction_id].set(message)
                 else:
-                    pass ### do something when invalid message received                    
+                    raise InvalidMessageFormat()
             except:
-                pass ### do something when error in reading data from sock
+                self.close()
+                self.open()
 
     def send(self, msg):        
         packet_id = self.trans_id
         self.trans_id+=1
         msg.set_trans_id(packet_id)
         self.trans[packet_id] = gevent.event.AsyncResult()
-        self.sock.send(data)
+        self.sock.send(msg.encode())
         ## wait for response
         response = self.trans[packet_id].get(self.read_timeout)
         del self.trans[packet_id]
         ## more default values
 
 class Client(object):
-    def __init__(self, addrs, config):
-        self.addrs = addrs
+    def __init__(self, config):
+        self.addrs = config.addrs
         self.config = config
         self.connections = map(lambda a: Connection(a, config), addrs)
         for conn in self.connections:
             conn.open()
 
+    def call(self, interface, params):
+        message = VenusRequest()
+        message.set_content_type(config.content_type)
+
+        connection = self.connections[random.randint(0, len(self.connections)-1)]
+        response = connection.send(message)
+        return response
+
 # vim: et sts=4 sw=4
 
 
 @message_type(0x02000001)
 class VenusRequest(VenusMessage):
-    def set_body(self, body):
-        self.body = body
-        self.body_size = len(body)
-        self.header.size = VenusHeader.HEADER_SIZE + self.body_size
+    def set_api_name(self,name):
+        self.api_name = name
 
+    def set_params(self, params):
+        pass #TODO
+
+    def set_source_client_id(self, client_id):
+        self.source_client_id = client_id
+
+    def set_source_ip_addr(self, ip):
+        self.source_ip_address = ip
+
+    def set_service_version(self, version):
+        self.service_version = int(version)
+
+    def encode(self):
+        scid_len = struct.pack(">i", len(self.source_client_id))
+        scip_len = struct.pack(">i", len(self.source_ip_address))
+        api_len = struct.pack(">i", len(self.name))
+        service_version = struct.pack(">i", self.service_version)
+        self.body = "".join([scid_len, self.source_client_id,
+                             scip_len, self.source_ip_address,
+                             api_len, self.api_name,
+                             service_version])
+        return super(VenusMessage, self).encode()
+        
 @message_type(0x02000002)
 class VenusResponse(VenusMessage):
     pass