Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions vxi11/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,13 @@ def recvrecord(sock):
# Client using TCP to a specific port

class RawTCPClient(Client):
def __init__(self, host, prog, vers, port):
def __init__(self, host, prog, vers, port, timeout=None):
Client.__init__(self, host, prog, vers, port)
self.connect()
self.connect(timeout)

def connect(self):
def connect(self, timeout=None):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.settimeout(timeout)
self.sock.connect((self.host, self.port))

def close(self):
Expand Down Expand Up @@ -285,12 +286,13 @@ def do_call(self):
# Client using UDP to a specific port

class RawUDPClient(Client):
def __init__(self, host, prog, vers, port):
def __init__(self, host, prog, vers, port, timeout=None):
Client.__init__(self, host, prog, vers, port)
self.connect()
self.connect(timeout)

def connect(self):
def connect(self, timeout=None):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.settimeout(timeout)
self.sock.connect((self.host, self.port))

def close(self):
Expand Down Expand Up @@ -337,8 +339,9 @@ def __init__(self, bcastaddr, prog, vers, port):
self.reply_handler = None
self.timeout = 30

def connect(self):
def connect(self, timeout=None):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.settimeout(timeout)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)

def set_reply_handler(self, reply_handler):
Expand Down Expand Up @@ -488,15 +491,15 @@ def callit(self, ca):

class TCPPortMapperClient(PartialPortMapperClient, RawTCPClient):

def __init__(self, host):
RawTCPClient.__init__(self, host, PMAP_PROG, PMAP_VERS, PMAP_PORT)
def __init__(self, host, timeout=None):
RawTCPClient.__init__(self, host, PMAP_PROG, PMAP_VERS, PMAP_PORT, timeout)
PartialPortMapperClient.__init__(self)


class UDPPortMapperClient(PartialPortMapperClient, RawUDPClient):

def __init__(self, host):
RawUDPClient.__init__(self, host, PMAP_PROG, PMAP_VERS, PMAP_PORT)
def __init__(self, host, timeout=None):
RawUDPClient.__init__(self, host, PMAP_PROG, PMAP_VERS, PMAP_PORT, timeout)
PartialPortMapperClient.__init__(self)


Expand All @@ -511,26 +514,26 @@ def __init__(self, bcastaddr):

class TCPClient(RawTCPClient):

def __init__(self, host, prog, vers, port=0):
def __init__(self, host, prog, vers, port=0, timeout=None):
if port == 0:
pmap = TCPPortMapperClient(host)
pmap = TCPPortMapperClient(host, timeout)
port = pmap.get_port((prog, vers, IPPROTO_TCP, 0))
pmap.close()
if port == 0:
raise RPCError('program not registered')
RawTCPClient.__init__(self, host, prog, vers, port)
RawTCPClient.__init__(self, host, prog, vers, port, timeout)


class UDPClient(RawUDPClient):

def __init__(self, host, prog, vers, port=0):
def __init__(self, host, prog, vers, port=0, timeout=None):
if port == 0:
pmap = UDPPortMapperClient(host)
pmap = UDPPortMapperClient(host, timeout)
port = pmap.get_port((prog, vers, IPPROTO_UDP, 0))
pmap.close()
if port == 0:
raise RPCError('program not registered')
RawUDPClient.__init__(self, host, prog, vers, port)
RawUDPClient.__init__(self, host, prog, vers, port, timeout)


class BroadcastUDPClient(Client):
Expand Down
31 changes: 16 additions & 15 deletions vxi11/vxi11.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,10 @@ def done(self):


class CoreClient(rpc.TCPClient):
def __init__(self, host, port=0):
def __init__(self, host, port=0, timeout=None):
self.packer = Packer()
self.unpacker = Unpacker('')
rpc.TCPClient.__init__(self, host, DEVICE_CORE_PROG, DEVICE_CORE_VERS, port)
rpc.TCPClient.__init__(self, host, DEVICE_CORE_PROG, DEVICE_CORE_VERS, port, timeout)

def create_link(self, id, lock_device, lock_timeout, name):
params = (id, lock_device, lock_timeout, name)
Expand Down Expand Up @@ -487,10 +487,10 @@ def destroy_intr_chan(self):


class AbortClient(rpc.TCPClient):
def __init__(self, host, port=0):
def __init__(self, host, port=0, timeout=None):
self.packer = Packer()
self.unpacker = Unpacker('')
rpc.TCPClient.__init__(self, host, DEVICE_ASYNC_PROG, DEVICE_ASYNC_VERS, port)
rpc.TCPClient.__init__(self, host, DEVICE_ASYNC_PROG, DEVICE_ASYNC_VERS, port, timeout)

def device_abort(self, link):
return self.make_call(DEVICE_ABORT, link,
Expand Down Expand Up @@ -572,7 +572,6 @@ def __init__(self, host, name = None, client_id = None, term_char = None):
self.abort_port = 0
self.link = None
self.max_recv_size = 0
self.max_read_len = 128*1024*1024
self.locked = False

def __del__(self):
Expand Down Expand Up @@ -607,7 +606,7 @@ def open(self):
return

if self.client is None:
self.client = CoreClient(self.host)
self.client = CoreClient(self.host, timeout=self.timeout)

self.client.sock.settimeout(self.timeout+1)
error, link, abort_port, max_recv_size = self.client.create_link(
Expand All @@ -627,21 +626,23 @@ def open(self):

def close(self):
"Close connection"
if self.link is None:
if self.link is None or self.client is None:
return

self.client.destroy_link(self.link)
self.client.close()
self.link = None
self.client = None
try:
self.client.destroy_link(self.link)
finally:
self.client.close()
self.link = None
self.client = None

def abort(self):
"Asynchronous abort"
if self.link is None:
self.open()

if self.abort_client is None:
self.abort_client = AbortClient(self.host, self.abort_port)
self.abort_client = AbortClient(self.host, self.abort_port, timeout=self.timeout)
self.abort_client.sock.settimeout(self.timeout)

error = self.abort_client.device_abort(self.link)
Expand Down Expand Up @@ -692,9 +693,9 @@ def read_raw(self, num=-1):
if self.link is None:
self.open()

read_len = self.max_read_len
if num > 0:
read_len = min(num, self.max_read_len)
read_len = self.max_recv_size
if num > 0 and num < self.max_recv_size:
read_len = num

flags = 0
reason = 0
Expand Down