Skip to content

Commit

Permalink
better OwnetProxy init logic
Browse files Browse the repository at this point in the history
  • Loading branch information
miccoli committed Apr 10, 2014
1 parent c2b0d5d commit 30dc314
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 49 deletions.
2 changes: 0 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

document that every pathname has to by an ASCIIZ string in the wire

sending on the wire non ASCIIZ strings gives unpredictable results

FLG_ALIAS is apparently not working

on the dir() reply offset field seems to code some info (?)
2 changes: 1 addition & 1 deletion pyownet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

__version__ = '0.7.1.dev1'
__version__ = '0.7.1.dev2'
76 changes: 30 additions & 46 deletions pyownet/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,17 @@ def __init__(self, sockaddr, family=socket.AF_INET, verbose=False):
"establish a connection with server at sockaddr"

self.verbose = verbose

self.socket = socket.socket(family, socket.SOCK_STREAM)
self.socket.settimeout(_SCK_TIMEOUT)
self.socket.connect(sockaddr)

if self.verbose:
print(self.socket.getsockname(), '->', self.socket.getpeername())

def shutdown(self):
"shutdown connection"

self.socket.shutdown(socket.SHUT_RDWR)
self.socket.close()

Expand Down Expand Up @@ -336,6 +339,20 @@ def __init__(self, host='localhost', port=4304, flags=0,
printed on stdout.
"""

# save init args
self._hostport = (host, port)
self.flags = flags | FLG_OWNET
self.verbose = verbose

# default (empty) errcodes tuple
self.errmess = _errtuple()

#
# init logic:
# try to connect to the given owserver, send a MSG_NOP,
# and check answer
#

# resolve host name/port
try:
gai = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)
Expand All @@ -344,43 +361,26 @@ def __init__(self, host='localhost', port=4304, flags=0,

# gai is a list of tuples, search for the first working one
lasterr = None
for (family, _, _, _, sockaddr) in gai:
for (self._family, _, _, _, self._sockaddr) in gai:
try:
conn = OwnetConnection(sockaddr, family, verbose)
except socket.error as err:
self.ping()
except ConnError as err:
# not working, go over to next sockaddr
lasterr = err
else:
# ok, this is working, stop searching
break
else:
# no working (sockaddr, family) found: raise error
assert isinstance(lasterr, socket.error)
assert isinstance(lasterr, IOError)
raise ConnError(*lasterr.args)
# no working (sockaddr, family) found: reraise last exception
assert isinstance(lasterr, ConnError)
raise lasterr

# here we have an open connection, close for now
conn.shutdown()

# setup self attributes
self._sockaddr, self._family = sockaddr, family
self._hostport = (host, port) # for display only

self.verbose = verbose
self.flags = flags | FLG_OWNET

self.errmess = _errtuple()

# final sanity checks
# does the remote end speak the ownet protocol?
self.ping()

# fetch errcodes array from owserver
try:
self.errmess = _errtuple(
m for m in bytes2str(self.read(PTH_ERRCODES)).split(','))
except OwnetError:
# failed, leave the default defined above
# failed, leave the default empty errcodes defined above
pass

def __str__(self):
Expand All @@ -394,8 +394,7 @@ def sendmess(self, type, payload, flags=0, size=0, offset=0):
flags |= self.flags

try:
conn = OwnetConnection(self._sockaddr, self._family,
self.verbose)
conn = OwnetConnection(self._sockaddr, self._family, self.verbose)
ret, _, data = conn.req(type, payload, flags, size, offset)
conn.shutdown()
except IOError as err:
Expand All @@ -413,8 +412,7 @@ def present(self, path):
"returns True if there is an entity at path"

ret, data = self.sendmess(MSG_PRESENCE, str2bytez(path))
assert ret <= 0
assert len(data) == 0
assert ret <= 0 and len(data) == 0
if ret < 0:
return False
else:
Expand Down Expand Up @@ -458,26 +456,12 @@ def write(self, path, data):
ensure proper encoding.
"""

assert isinstance(data, bytes)
# fixme: check of path type delayed to str2bytez
if not isinstance(data, bytes):
raise TypeError("'data' argument must be of type 'bytes'")

ret, rdata = self.sendmess(MSG_WRITE, str2bytez(path)+data,
size=len(data))
assert len(rdata) == 0
if ret < 0:
raise OwnetError(-ret, self.errmess[-ret], path)

def _main():
# print sensors on localhost owserver
try:
proxy = OwnetProxy()
except ConnError:
print("No owserver on localhost")
return 1
print("directory on {0}:".format(proxy))
print("id".center(17), "type".center(7))
for sensor in proxy.dir(slash=False, bus=False):
stype = bytes2str(proxy.read(sensor + '/type'))
print(sensor.ljust(17), stype.ljust(7))
return 0

if __name__ == '__main__':
_main()
2 changes: 2 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def test_exceptions(self):
self.assertRaises(protocol.ConnError, protocol.OwnetProxy,
host='nonexistent.fake')
self.assertRaises(TypeError, self.proxy.dir, 1)
self.assertRaises(TypeError, self.proxy.write, '/', 1)
self.assertRaises(TypeError, self.proxy.write, 1, b'abc')

if __name__ == '__main__':
unittest.main()
Expand Down

0 comments on commit 30dc314

Please sign in to comment.