X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=python%2Fovs%2Fstream.py;h=08c6293adf5a94f8b4d085b100fa849bfb641645;hb=4000658b1a893485c163d19f50b51fbe942765e4;hp=82d4557e953035636311ca14c0d777c9ae5684f2;hpb=8ba31bf16d03c449fbf40ec00d9a816b4ec05018;p=openvswitch diff --git a/python/ovs/stream.py b/python/ovs/stream.py index 82d4557e..08c6293a 100644 --- a/python/ovs/stream.py +++ b/python/ovs/stream.py @@ -1,4 +1,4 @@ -# Copyright (c) 2010, 2011 Nicira Networks +# Copyright (c) 2010, 2011, 2012 Nicira Networks # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,19 +13,20 @@ # limitations under the License. import errno -import logging import os import select import socket -import sys import ovs.poller import ovs.socket_util +import ovs.vlog + +vlog = ovs.vlog.Vlog("stream") + class Stream(object): """Bidirectional byte stream. Currently only Unix domain sockets are implemented.""" - n_unix_sockets = 0 # States. __S_CONNECTING = 0 @@ -44,10 +45,9 @@ class Stream(object): False.""" return name.startswith("unix:") - def __init__(self, socket, name, bind_path, status): + def __init__(self, socket, name, status): self.socket = socket self.name = name - self.bind_path = bind_path if status == errno.EAGAIN: self.state = Stream.__S_CONNECTING elif status == 0: @@ -74,18 +74,15 @@ class Stream(object): if not Stream.is_valid_name(name): return errno.EAFNOSUPPORT, None - Stream.n_unix_sockets += 1 - bind_path = "/tmp/stream-unix.%d.%d" % (os.getpid(), - Stream.n_unix_sockets) connect_path = name[5:] error, sock = ovs.socket_util.make_unix_socket(socket.SOCK_STREAM, - True, bind_path, + True, None, connect_path) if error: return error, None else: status = ovs.socket_util.check_connection_completion(sock) - return 0, Stream(sock, name, bind_path, status) + return 0, Stream(sock, name, status) @staticmethod def open_block((error, stream)): @@ -107,7 +104,7 @@ class Stream(object): stream.connect_wait(poller) poller.block() assert error != errno.EINPROGRESS - + if error and stream: stream.close() stream = None @@ -115,9 +112,6 @@ class Stream(object): def close(self): self.socket.close() - if self.bind_path is not None: - ovs.fatal_signal.unlink_file_now(self.bind_path) - self.bind_path = None def __scs_connecting(self): retval = ovs.socket_util.check_connection_completion(self.socket) @@ -146,7 +140,7 @@ class Stream(object): def recv(self, n): """Tries to receive up to 'n' bytes from this stream. Returns a (error, string) tuple: - + - If successful, 'error' is zero and 'string' contains between 1 and 'n' bytes of data. @@ -154,7 +148,7 @@ class Stream(object): - If the connection has been closed in the normal fashion or if 'n' is 0, the tuple is (0, ""). - + The recv function will not block waiting for data to arrive. If no data have been received, it returns (errno.EAGAIN, "") immediately.""" @@ -213,17 +207,18 @@ class Stream(object): def connect_wait(self, poller): self.wait(poller, Stream.W_CONNECT) - + def recv_wait(self, poller): self.wait(poller, Stream.W_RECV) - + def send_wait(self, poller): self.wait(poller, Stream.W_SEND) - + def __del__(self): # Don't delete the file: we might have forked. self.socket.close() + class PassiveStream(object): @staticmethod def is_valid_name(name): @@ -259,7 +254,7 @@ class PassiveStream(object): try: sock.listen(10) except socket.error, e: - logging.error("%s: listen: %s" % (name, os.strerror(e.error))) + vlog.err("%s: listen: %s" % (name, os.strerror(e.error))) sock.close() return e.error, None @@ -285,12 +280,12 @@ class PassiveStream(object): try: sock, addr = self.socket.accept() ovs.socket_util.set_nonblocking(sock) - return 0, Stream(sock, "unix:%s" % addr, None, 0) + return 0, Stream(sock, "unix:%s" % addr, 0) except socket.error, e: error = ovs.socket_util.get_exception_errno(e) if error != errno.EAGAIN: # XXX rate-limit - logging.debug("accept: %s" % os.strerror(error)) + vlog.dbg("accept: %s" % os.strerror(error)) return error, None def wait(self, poller): @@ -300,14 +295,11 @@ class PassiveStream(object): # Don't delete the file: we might have forked. self.socket.close() -def usage(name, active, passive, bootstrap): - print - if active: - print("Active %s connection methods:" % name) - print(" unix:FILE " - "Unix domain socket named FILE"); - - if passive: - print("Passive %s connection methods:" % name) - print(" punix:FILE " - "listen on Unix domain socket FILE") + +def usage(name): + return """ +Active %s connection methods: + unix:FILE Unix domain socket named FILE + +Passive %s connection methods: + punix:FILE Listen on Unix domain socket FILE""" % (name, name)