X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=python%2Fovs%2Fjsonrpc.py;h=a054401b2783f30140170cba2902cceebe9a410a;hb=2c0d2b3bfab41a25becdda41b1cf6003a9ebfd00;hp=ecfe5f828fbfaba47e2a231acda309b751e13b5a;hpb=b2edc4e7d84dd315330a9466adb755af4148dd40;p=openvswitch diff --git a/python/ovs/jsonrpc.py b/python/ovs/jsonrpc.py index ecfe5f82..a054401b 100644 --- a/python/ovs/jsonrpc.py +++ b/python/ovs/jsonrpc.py @@ -1,4 +1,4 @@ -# Copyright (c) 2010, 2011 Nicira Networks +# Copyright (c) 2010, 2011, 2012 Nicira, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,7 +13,6 @@ # limitations under the License. import errno -import logging import os import ovs.json @@ -21,8 +20,12 @@ import ovs.poller import ovs.reconnect import ovs.stream import ovs.timeval +import ovs.util +import ovs.vlog + +EOF = ovs.util.EOF +vlog = ovs.vlog.Vlog("jsonrpc") -EOF = -1 class Message(object): T_REQUEST = 0 # Request. @@ -44,6 +47,7 @@ class Message(object): self.id = id _next_id = 0 + @staticmethod def _create_id(): this_id = Message._next_id @@ -131,7 +135,7 @@ class Message(object): msg_type = Message.T_REQUEST else: msg_type = Message.T_NOTIFY - + msg = Message(msg_type, method, params, result, error, id_) validation_error = msg.is_valid() if validation_error is not None: @@ -173,6 +177,7 @@ class Message(object): s.append("id=" + ovs.json.to_string(self.id)) return ", ".join(s) + class Connection(object): def __init__(self, stream): self.name = stream.name @@ -196,8 +201,8 @@ class Connection(object): self.output = self.output[retval:] else: if retval != -errno.EAGAIN: - logging.warn("%s: send error: %s" % (self.name, - os.strerror(-retval))) + vlog.warn("%s: send error: %s" % + (self.name, os.strerror(-retval))) self.error(-retval) break @@ -205,7 +210,7 @@ class Connection(object): if not self.status: self.stream.run_wait(poller) if len(self.output): - self.stream.send_wait() + self.stream.send_wait(poller) def get_status(self): return self.status @@ -217,7 +222,7 @@ class Connection(object): return len(self.output) def __log_msg(self, title, msg): - logging.debug("%s: %s %s" % (self.name, title, msg)) + vlog.dbg("%s: %s %s" % (self.name, title, msg)) def send(self, msg): if self.status: @@ -257,8 +262,8 @@ class Connection(object): return error, None else: # XXX rate-limit - logging.warning("%s: receive error: %s" - % (self.name, os.strerror(error))) + vlog.warn("%s: receive error: %s" + % (self.name, os.strerror(error))) self.error(error) return self.status, None elif not data: @@ -289,7 +294,7 @@ class Connection(object): self.wait(poller) self.recv_wait(poller) poller.block() - + def transact_block(self, request): id_ = request.id @@ -297,7 +302,10 @@ class Connection(object): reply = None while not error: error, reply = self.recv_block() - if reply and reply.type == Message.T_REPLY and reply.id == id_: + if (reply + and (reply.type == Message.T_REPLY + or reply.type == Message.T_ERROR) + and reply.id == id_): break return error, reply @@ -306,21 +314,21 @@ class Connection(object): self.parser = None if type(json) in [str, unicode]: # XXX rate-limit - logging.warning("%s: error parsing stream: %s" % (self.name, json)) + vlog.warn("%s: error parsing stream: %s" % (self.name, json)) self.error(errno.EPROTO) return msg = Message.from_json(json) if not isinstance(msg, Message): # XXX rate-limit - logging.warning("%s: received bad JSON-RPC message: %s" - % (self.name, msg)) + vlog.warn("%s: received bad JSON-RPC message: %s" + % (self.name, msg)) self.error(errno.EPROTO) return self.__log_msg("received", msg) return msg - + def recv_wait(self, poller): if self.status or self.input: poller.immediate_wake() @@ -332,7 +340,8 @@ class Connection(object): self.status = error self.stream.close() self.output = "" - + + class Session(object): """A JSON-RPC session with reconnection.""" @@ -348,10 +357,10 @@ class Session(object): """Creates and returns a Session that maintains a JSON-RPC session to 'name', which should be a string acceptable to ovs.stream.Stream or ovs.stream.PassiveStream's initializer. - + If 'name' is an active connection method, e.g. "tcp:127.1.2.3", the new session connects and reconnects, with back-off, to 'name'. - + If 'name' is a passive connection method, e.g. "ptcp:", the new session listens for connections to 'name'. It maintains at most one connection at any given time. Any new connection causes the previous one (if any) @@ -363,6 +372,9 @@ class Session(object): if ovs.stream.PassiveStream.is_valid_name(name): reconnect.set_passive(True, ovs.timeval.msec()) + if ovs.stream.stream_or_pstream_needs_probes(name): + reconnect.set_probe_interval(0) + return Session(reconnect, None) @staticmethod @@ -395,7 +407,7 @@ class Session(object): self.stream.close() self.stream = None self.seqno += 1 - + def __connect(self): self.__disconnect() @@ -421,8 +433,8 @@ class Session(object): if error == 0: if self.rpc or self.stream: # XXX rate-limit - logging.info("%s: new connection replacing active " - "connection" % self.reconnect.get_name()) + vlog.info("%s: new connection replacing active " + "connection" % self.reconnect.get_name()) self.__disconnect() self.reconnect.connected(ovs.timeval.msec()) self.rpc = Connection(stream) @@ -513,7 +525,7 @@ class Session(object): else: max_tries = self.reconnect.get_max_tries() return max_tries is None or max_tries > 0 - + def is_connected(self): return self.rpc is not None