Lynn Rees avatar Lynn Rees committed 4355211

- pipe goes correct direction

Comments (0)

Files changed (4)

crossroads/low.py

             return getr(self, key)
         except AttributeError:
             try:
-                this = bigxsget(key.upper())
+                newkey = key.upper()
+                this = bigxsget(newkey)
                 if this in XS.TYPES:
-                    return self._setter(key.lower(), self._open, this)
+                    return self._setter(key.lower(), self._open, this, newkey)
             except AttributeError:
                 raise AttributeError(key)
 
             else:
                 raise xs.XSOperationError(msg)
 
-    def _open(self, stype, **options):
-        return Socket(self, stype, **options)
+    def _open(self, stype, key, **options):
+        return Socket(self, stype, key, **options)
 
     def close(self):
         rc = xs.term(self._ctx)
 
 class Socket(Setter):
 
-    def __init__(self, context, stype, **options):
+    def __init__(self, context, stype, newkey, **options):
         self.context = context
         self._type = stype
+        self._type_name = newkey
         self._socket = xs.socket(context._ctx, stype)
         options.update(context._options)
         _set = self._set
         except AttributeError:
             setr(self, key, value)
 
+    def __repr__(self):
+        return '{self._type_name}@{self._socket}'.format(self=self)
+
     def _opt_type(self, key):
         opt_key = bigxsget(key.upper())
         if opt_key in XS.INT:
             opt_type = c_int64
         elif opt_key in XS.BINARY:
             opt_type = c_char_p
+        else:
+            raise AttributeError(
+                '{0} is not a valid socket attribute'.format(key)
+            )
         return opt_key, opt_type
 
     def _get(self, key):
 
     def recvmsg(self, nowait=False):
         msg = MessageXS()
-        try:
-            msg.last_rc = self.last_rc = self._recvmsg(
-                msg.dest, XS.DONTWAIT if nowait else 0,
-            )
-            msg.more = self.rcvmore
-        finally:
-            msg.close()
+        msg.last_rc = self.last_rc = self._recvmsg(
+            msg.dest, XS.DONTWAIT if nowait else 0,
+        )
+        msg.more = self.rcvmore
         return msg
 
     def shutdown(self, sid):

crossroads/lowest.py

         self.errno = errno
 
     def __str__(self):
-        return self.strerror
+        return '{self.errno}: {self.strerror}'.format(self=self)
 
 
 def _check_nonzero(result, func, arguments):

crossroads/message.py

 
     def __init__(self, source=None):
         super(MessageXS, self).__init__()
-        self.dest = xs.msg_t()
-        if isinstance(source, getcls(self)):
+        if source is None:
+            self.dest = xs.msg_t()
+            self.src = None
+            self.rc = xs.msg_init(self.dest)
+            self.size = xs.msg_size(self.dest)
+        elif isinstance(source, getcls(self)):
             self.src = source.dest
-            self.rc = xs.msg_init(self.src)
-            self.size = sizeof(self.src)
-        elif source is None:
-            self.src = self.dest
-            self.rc = xs.msg_init(self.src)
-            self.size = sizeof(self.src)
+            self.dest = None
+            self.size = xs.msg_size(self.src)
         else:
-            self.src = bytes(source)
-            length = self.size = len(self.src)
-            self.rc = xs.msg_init_size(self.dest, length)
-            xs.memmove(xs.msg_data(self.dest), self.src, length)
+            self.src = xs.msg_t()
+            source = bytes(source)
+            length = self.size = len(source)
+            self.rc = xs.msg_init_size(self.src, length)
+            xs.memmove(xs.msg_data(self.src), source, length)
 
     def __len__(self):
         return self.size
         return value
 
     def close(self):
-        self.rc = xs.msg_close(self.dest)
+        if self.src is not None:
+            self.rc = xs.msg_close(self.src)
+        if self.dest is not None:
+            self.rc = xs.msg_close(self.dest)
+#
+#    __del__ = close
+

tests/test_low_level.py

         with xreq.bind_tcp('127.0.0.1:5560'), xrep.bind_tcp('127.0.0.1:5561'),\
            rep.connect_tcp('127.0.0.1:5560'), req.connect_tcp('127.0.0.1:5561'):
             # send a request
-            self.assertEqual(req.send('ABC', 3, True).last_rc, 3)
-            self.assertEqual(req.send('DEF', 3).last_rc, 3)
-            # pass the request through the device
-            msg = xrep.recvmsg()
-            self.assertEqual(msg.last_rc, 5)
-            sendmore = True if msg.more else False
-            self.assertEqual(xreq.sendmsg(msg, sendmore).last_rc, 5)
-#            msg = xrep.recvmsg()
-#            self.assertEqual(msg.last_rc, 0)
-#            rc = xreq.sendmsg(msg, True if msg.more else False).last_rc
-#            self.assertEqual(rc, 0)
-#            msg = xrep.recvmsg()
-#            self.assertEqual(msg.last_rc, 3)
-#            rc = xreq.sendmsg(msg, True if msg.more else False).last_rc
-#            self.assertEqual(rc, 3)
-#            msg = xrep.recvmsg()
-#            self.assertEqual(msg.last_rc, 3)
-#            rc = xreq.sendmsg(msg, True if msg.more else False).last_rc
-#            self.assertEqual(rc, 3)
-#            # receive the request
-#            buff = rep.recv(3)
-#            self.assertEqual(bytes(buff), b'ABC')
-#            self.assertEqual(buff.last_rc, 3)
-#            self.assertEqual(buff.more, 0)
-#            buff2 = rep.recv(3)
-#            self.assertEqual(buff2.last_rc, 3)
-#            self.assertEqual(bytes(buff2), b'DEF')
-#            self.assertEqual(buff2.more, 0)
-#            # send the reply
-#            self.assertEqual(rep.send('GHI', more=True).last_rc, 3)
-#            self.assertEqual(rep.send('JKL').last_rc, 3)
-#            # pass the reply through the device
-#            for i in lrange(4):
-#                msg = xrep.recvmsg()
-#                self.assertTrue(msg.last_rc >= 0)
-#                rc = xreq.sendmsg(msg, True if msg.more else False).last_rc
-#                self.assertTrue(rc >= 0)
-#            # receive the reply
-#            buff = req.recv(3)
-#            self.assertEqual(buff.last_rc, 3)
-#            self.assertEqual(bytes(buff), b'GHI')
-#            self.assertEqual(buff.more, 0)
-#            buff2 = req.recv(3)
-#            self.assertEqual(buff2.last_rc, 3)
-#            self.assertEqual(bytes(buff2), b'JKL')
-#            self.assertEqual(buff2.more, 0)
+            self.assertEqual(req.send(b'ABC', 3, True).last_rc, 3)
+            self.assertEqual(req.send(b'DEF', 3).last_rc, 3)
+            # pass the reply through the device
+            for i in lrange(4):
+                msg = xrep.recvmsg()
+                self.assertTrue(msg.last_rc >= 0)
+                rc = xreq.sendmsg(msg, True if msg.more else False).last_rc
+                self.assertTrue(rc >= 0)
+            self.assertEqual(msg.more, 0)
+            # receive the request
+            buff = rep.recv(3)
+            self.assertEqual(bytes(buff), b'ABC')
+            self.assertEqual(buff.last_rc, 3)
+            self.assertEqual(buff.more, 1)
+            buff = rep.recv(3)
+            self.assertEqual(buff.last_rc, 3)
+            self.assertEqual(bytes(buff), b'DEF')
+            self.assertEqual(buff.more, 0)
+            # send the reply
+            self.assertEqual(rep.send(b'GHI', 3, more=True).last_rc, 3)
+            self.assertEqual(rep.send(b'JKL', 3).last_rc, 3)
+            # pass the reply through the device
+            for i in lrange(4):
+                msg = xreq.recvmsg(nowait=True)
+                self.assertTrue(msg.last_rc >= 0)
+                rc = xrep.sendmsg(msg, True if msg.more else False).last_rc
+                self.assertTrue(rc >= 0)
+            # receive the reply
+            buff = req.recv(3)
+            self.assertEqual(buff.last_rc, 3)
+            self.assertEqual(bytes(buff), b'GHI')
+            self.assertEqual(buff.more, 1)
+            buff2 = req.recv(3)
+            self.assertEqual(buff2.last_rc, 3)
+            self.assertEqual(bytes(buff2), b'JKL')
+            self.assertEqual(buff2.more, 0)
 
 #    def test_resubscribe(self):
 #        from stuf.six import int2byte
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.