Commits

Martin Gergov committed 4ae186b

Maintenance for trailing spaces and logging

  • Participants
  • Parent commits 7e121e0

Comments (0)

Files changed (4)

 
 #) PyUDT
 #) twisted
-#) foolscap(for logging)
+
+
+=======
+ Notes
+=======
+
+#) Tests from twisted pass only in internet/ and protocol/,
+   for the others they just seem to "hang" (something to do
+   with flags, probably)

File examples/testudt.py

 
 class Echo(DatagramProtocol):
     def datagramReceived(self, data, (host, port)):
-        
         log.msg("received %r from %s:%d" % (data, host, port))
         self.transport.write(data, (host, port))
 
         self.echo = Echo()
         self.port = reactor.listenUDT(5000, self.echo)
         print self.port
-    
+
     def print_hello(self, res):
         print "Hello!"
         return "hello"

File udt4twisted/udt.py

-import sys 
-import time 
+import sys
+import time
 import struct
 import socket
 from zope.interface import implementer
 
 def find_key(dic, val):
     """
-    From http://www.daniweb.com/software-development/python/code/217019/search-a-python-dictionary-both-ways
+    From http://www.daniweb.com/software-development/python/ \
+    code/217019/search-a-python-dictionary-both-ways
     return the key of dictionary dic given the value
     """
     return [k for k, v in dic.iteritems() if v == val][0]
                  interface='localhost',
                  maxPacketSize=8192,
                  reactor=None,
-                 backlog=50):
+                 backlog=50,
+                 ):
         """
         Initialize with a numeric port to listen on.
         """
                             self.socketType,
                             AI_PASSIVE)
         s.setblocking(False)
+        #s.setsockopt(udt.
         #FIXME
         #fdesc._setCloseOnExec(s.UDTSOCKET.udtsocket)
         return s
         self.protocol.makeConnection(self)
 
 
-        
-
     def connect(self, host, port):
         """
         Connect to a remote host.
             if addr == None:
                 addr = self.addresses[fd.UDTSOCKET][1]
         except udt.UDTException as ue:
-            print ue
+            #print ue
             return ue
         except socket.error as se:
             pass
                 self.protocol.datagramReceived(data, addr)
             except:
                 log.err()
-                
     def write(self, datagram, addr=None, ttl=-1, inorder=True):
         """
         Write datagram to address.
 
 
 class Connector(Port):
-    
+
     def doConnect(self, fd=None):
         if not fd:
             self.socket = self.createInternetSocket()

File udt4twisted/udtepollreactor.py

                    timeout,
                    bindAddress):
         """
-        Connects a given ClientFactory to a remote host
+        Connects a given ClientFactory to a remote host.
+
+        @return: object which provides L{IListeningPort}.
         """
 
 @implementer(IReactorFDSet)
     _POLL_DISCONNECTED = (udt4.ECONNLOST | udt4.UDT_EPOLL_ERR)
     _POLL_IN = udt4.UDT_EPOLL_IN
     _POLL_OUT = udt4.UDT_EPOLL_OUT
-    
+
     def __init__(self):
         """
-        Initialize epoll object.
+        Initialize epoll object, udt library
+        and adds event trigger to clean the latter after shutdown.
         """
         udt4.startup()
         self._poller = udt4.UDTepoll()
         posixbase.PosixReactorBase.__init__(self)
         self.addSystemEventTrigger("after", "shutdown", udt4.cleanup)
 
-        
 
     def _add(self, xer, primary, other, selectables, event, antievent):
         """
         for another state (read -> read/write for example).
         """
         if isinstance(xer, udt.Port):
-            fd = xer.socket.UDTSOCKET
+            fd = xer.socket.UDTSOCKET.UDTSOCKET
         else:
             fd = xer.fileno()
         if fd not in primary:
             # this method in this file.
             if fd in other:
                 flags |= antievent
-                self._poller.modify(fd, flags)
-            else:
-                
+                #FIXME for now remove and add again
                 if isinstance(xer, udt.Port):
-                    print "ADDING U:", fd, "With flags:", flags
+                    self._poller.remove_usock(fd)
                     self._poller.add_usock(fd, flags)
                 else:
-                    print "ADDING S:", fd, "With flags:", flags
+                    self._poller.remove_ssock(fd)
                     self._poller.add_ssock(fd, flags)
+                #self._poller.modify(fd, flags)
+                log.msg("MODIFY:{0} with flags:{1}".format(fd, flags))
+            else:
+                if isinstance(xer, udt.Port):
+
+                    self._poller.add_usock(fd, flags)
+                else:
+                    self._poller.add_ssock(fd, flags)
+                log.msg("ADDING:{0} with flags:{1}".format(fd, flags))
 
             # Update our own tracking state *only* after the epoll call has
             # succeeded.  Otherwise we may get out of sync.
-            
-            if isinstance(xer, udt.Port):
-                selectables[fd.UDTSOCKET] = xer
-                primary[fd.UDTSOCKET] = 1
-            else:
-                selectables[fd] = xer
-                primary[fd] = 1
+            selectables[fd] = xer
+            primary[fd] = 1
 
     def addReader(self, reader):
         """
             return
         self._remove(writer, self._writes, self._reads, self._selectables,
                       self._POLL_OUT, self._POLL_IN)
-            
-            
+
     def _remove(self, xer, primary, other, selectables, event, antievent):
         """
         Private method for removing a descriptor from the event loop.
             fd = xer.socket.UDTSOCKET.UDTSOCKET
         else:
             fd = xer.fileno()
-         
         if fd == -1:
             for fd, fdes in selectables.items():
                 if xer is fdes:
                 return
         if fd in primary:
             if fd in other:
+                #FIXME for now remove and add again
                 flags = antievent
                 # See comment above modify call in _add.
-                self._poller.modify(fd, flags)
+                if isinstance(xer, udt.Port):
+                    self._poller.remove_usock(fd)
+                    self._poller.add_usock(fd, flags)
+                else:
+                    self._poller.remove_ssock(fd)
+                    self._poller.add_ssock(fd, flags)
+                #self._poller.modify(fd, flags)
+                log.msg("MODIFY:{0} with flags:{1}".format(fd, flags))
             else:
                 flags = event
                 if isinstance(xer, udt.Port):
-                    
                     del selectables[fd]
                 else:
                     del selectables[fd]
                 # See comment above _control call in _add.
                 if isinstance(xer, udt.Port):
-                    print "REMOVING:", fd, "With flags:", flags
                     self._poller.remove_usock(fd, flags)
                 else:
-                    #FIXME
-                    try:
-                        print "REMOVING:", fd, "With flags:", flags
-                        self._poller.remove_ssock(fd, flags)
-                    except TypeError:
-                        pass
+                    #FIXME Apparently flags are not parsed in this method
+                    self._poller.remove_ssock(fd)
+                log.msg("REMOVING:{0} with flags:{1}".format(fd, flags))
             del primary[fd]
-        
 
     def _handleSystemSocketSet(self, set, event):
         _drdw = self._doReadOrWrite
             except KeyError:
                 pass
             else:
-                print "FD:", fd, "SELECT:", selectable
                 log.callWithLogger(selectable, _drdw, selectable, fd,
                                    event)
     def _handleUDTReadSocketSet(self, set, event):
             try:
                 selectable = self._selectables[fd.UDTSOCKET]
                 socket, addr = accept(fd)
-                print "FD:", fd, "SELECT:", selectable
-                print socket, addr
+                log.msg("ACCEPT FD:{0}SELECT:{1}".format(fd, selectable))
                 selectable.addresses[socket.UDTSOCKET] = (socket, addr)
                 self._udtsockets[socket.UDTSOCKET] = selectable
                 socket = UdtSocket(_sock=socket)
 
 
     def _handleUDTWriteSocketSet(self, set, event):
-        # _drdw = self._doReadOrWrite
-        # for fd in set:
-        #     try:
-        #         selectable = self._selectables[fd]
-        #     except KeyError:
-        #         pass
-        #     else:
-        #         log.callWithLogger(selectable, _drdw, selectable, fd,
-        #                            event)
+        """
+        Has no job for now.
+        """
         pass
 
     tests = True
             if err.errno == errno.EINTR:
                 return
             raise
-        
         #handle system sockets
         sread, swrite = l[2:]
-        
         self._handleSystemSocketSet(sread, self._POLL_IN)
         self._handleSystemSocketSet(swrite, self._POLL_OUT)
 
         self._handleUDTReadSocketSet(uread, self._POLL_IN)
         self._handleUDTWriteSocketSet(uwrite, self._POLL_OUT)
 
-        
-
     def _doReadOrWrite(self, selectable, fd, event):
         """
         fd is available for read or write, do the work and raise errors if
                     if event & self._POLL_IN:
                         # Handle a read event.
                         if isinstance(fd, udt4.UDTSOCKET):
-                            log.msg("Hello from doRead")
                             why = selectable.doRead(fd)
                         else:
                             why = selectable.doRead()
                 if why[0] == EASYNCRCV:
                     #Reset monitoring if no data is available
                     self._poller.add_usock(fd, self._POLL_IN)
-                    
                 if why[0] == ECONNLOST:
                     del self._udtsockets[fd]
                 gc.collect()
                 return
-            print "dissconnect..."
             self._disconnectSelectable(selectable, why, inRead)
 
 
                   interface='',
                   maxPacketSize=8192,
                   backlog=50):
-        
+
         p = udt.Port(port, protocol, interface,
                      maxPacketSize, self, backlog)
         p.startListening()
         return p
-    
+
     def connectUDT(self,
                    host,
                    port,
         c.connect(host, port)
         return c
 
-        
+
 
     doIteration = doPoll