Commits

Martin Gergov committed f00af77

Really basic functionality works..testing and polish on the way.

Comments (0)

Files changed (5)

 import udt4
 from udt4.pyudt import UdtSocket
 import gc
+import random
+from time import sleep
 
 ok = True
 startup()
 poller = UDTepoll()
 s = UdtSocket(AF_INET, SOCK_DGRAM, AI_PASSIVE)
 print s.UDTSOCKET
-s.setblocking(False)
+#s.setblocking(False)
 s.bind(("127.0.0.1", 5000))
 poller.add_usock(s.UDTSOCKET, UDT_EPOLL_IN)
 s.listen(20)
 
+# try:
+#     for i in range(250):
+#         if ok:
+#             sock, addr = s.accept()
+#             print sock, addr
+#             sock.sendmsg("Hello0")
+#             ok = False
+#         else:
+#             mess = sock.recvmsg(1000)
+#             mod_mess = mess + str(i)
+#             print mess
+#             sock.sendmsg(mod_mess)
+        
+# except KeyboardInterrupt:
+#     exit("Bye!")
+
 try:
     while True:
         sets = poller.wait(True, True, 1000, False, False)
-        print sets
+        #print sets
         
         for socket in sets[0]:
-            print socket
-            # if (socket.UDTSOCKET == s.UDTSOCKET.UDTSOCKET):
-            #     sock, host = accept(socket)
-            #     print sock, host
-            #     sock = UdtSocket(_sock=sock)
-            #     sock.setblocking(False)
-            #     poller.add_usock(sock.UDTSOCKET, UDT_EPOLL_IN | UDT_EPOLL_ERR)
-            # else:
-            #     print "READ:", socket
-            #     try:
-            #         print recvmsg(socket.UDTSOCKET, 7)
-            #     except udt4.UDTException as ue:
-            #         print "ERR:",ue
-            #     poller.remove_usock(socket.UDTSOCKET, UDT_EPOLL_IN | UDT_EPOLL_ERR)
+            if (socket.UDTSOCKET == s.UDTSOCKET.UDTSOCKET):
+                sock, host = accept(socket)
+                print sock, host
+                sock = UdtSocket(_sock=sock)
+                sock.setblocking(False)
+                poller.add_usock(sock.UDTSOCKET, UDT_EPOLL_IN)
+                sendmsg(sock.UDTSOCKET, "Hello", 5)
+            else:
+                
+                try:
+                    mess = recvmsg(socket.UDTSOCKET, 100)
+                    print "READ:", socket,";", "RECV:", mess
+                    sendmsg(socket.UDTSOCKET, mess, len(mess))
+                except udt4.UDTException as ue:
+                    print "ERR:",ue
+                    poller.remove_usock(socket, UDT_EPOLL_IN)
+                    if ue[0] == 6002:
+                        poller.add_usock(socket, UDT_EPOLL_IN)
+            gc.collect()
                     
 except KeyboardInterrupt:
     exit("Bye!")

examples/client.py

 from socket import AF_INET, SOCK_DGRAM, AI_PASSIVE
 import udt4
 from udt4.pyudt import UdtSocket
+import gc
 
 # startup()
 # s = UdtSocket(AF_INET, SOCK_DGRAM, AI_PASSIVE)
 # print s.UDTSOCKET
 # #s.setblocking(True)
 # s.connect(("127.0.0.1", 5000))
-# s.sendmsg("Hello", None, -1, True)
-# print "entering loop..."
-# s.sendmsg("Pwwww", None, -1, True)
-# s.sendmsg("Hello", None, -1, True)
-# print "entering loop..."
-# s.sendmsg("Pwwww", None, -1, True)
-# s.sendmsg("Hello", None, -1, True)
-# print "entering loop..."
-# s.sendmsg("Pwwww", None, -1, True)
 # try:
 #     while True:
-#         print s.recvmsg(8)
+#         mess = s.recvmsg(1000)
+#         print mess
+#         s.sendmsg(mess)
+# except udt4.UDTException as ue:
+#     print ue
+#     exit("Bye!")
 # except KeyboardInterrupt:
 #     exit("Bye!")
 # cleanup()
+
 import sys
 sys.path.append("../")
 
 from twisted.internet import reactor, defer
 from twisted.internet.protocol import DatagramProtocol
 from twisted.python import log
+
 log.startLogging(sys.stdout)
 
 
 class UDTClient(DatagramProtocol):
 
-    def doSend(self):
+    def doSend1(self):
         self.transport.write("?>!", ("127.0.0.1", 5000))
 
     def datagramReceived(self, data, (host, port)):
         print "Received %r" % data
-        self.transport.write("?>!", ("127.0.0.1", 5000))
-
-
+        self.transport.write("bomb!@!!!!!", ("127.0.0.1", 5000))
 
 udtclient = UDTClient()
 reactor.connectUDT("127.0.0.1", 5000, udtclient)
-reactor.callWhenRunning(udtclient.doSend)
+reactor.callLater(1, udtclient.doSend1)
 reactor.run()
+
+# from udt4 import socket, listen, UDTepoll, bind, recvmsg, UDTException
+# from udt4 import startup, cleanup, accept, UDT_EPOLL_IN, UDT_EPOLL_OUT, UDT_EPOLL_ERR
+# from udt4 import EASYNCRCV, sendmsg
+# from socket import AF_INET, SOCK_DGRAM, AI_PASSIVE
+# import udt4
+# from udt4.pyudt import UdtSocket
+# from time import sleep 
+
+# startup()
+# s = UdtSocket(AF_INET, SOCK_DGRAM, AI_PASSIVE)
+# print s.UDTSOCKET
+# s.setblocking(False)
+# s.connect_ex(("127.0.0.1", 5000))
+# poller = UDTepoll()
+# poller.add_usock(s.UDTSOCKET, UDT_EPOLL_IN)
+
+# try:
+#     while True:
+#         sets = poller.wait(True, True, 1000, False, False)
+#         #print sets
+#         for socket in sets[0]:
+#             if (socket.UDTSOCKET == s.UDTSOCKET.UDTSOCKET):
+#                 print 
+#                 try:
+#                     mess = recvmsg(socket.UDTSOCKET, 200)
+#                     print "READ:", socket, ";RECV:", mess
+#                     sendmsg(socket.UDTSOCKET, mess, len(mess))
+#                 except udt4.UDTException as ue:
+#                     print "ERR:",ue
+#                     poller.remove_usock(socket, UDT_EPOLL_IN)
+#                     if ue[0] == 6002:
+#                         poller.add_usock(socket, UDT_EPOLL_IN)
+#             gc.collect()
+
+# except KeyboardInterrupt:
+#     exit("Bye!")
+# poller.remove_usock(s.UDTSOCKET)
+# cleanup()

examples/testudt.py

 log.startLogging(sys.stdout)
 
 class Echo(DatagramProtocol):
-
-    def startProtocol(self):
-        log.msg("CLIENT!!!")
-
     def datagramReceived(self, data, (host, port)):
+        
         log.msg("received %r from %s:%d" % (data, host, port))
         self.transport.write(data, (host, port))
 
 class Tester(object):
     port = None
     def listen_to_udt_port(self, res):
-        self.port = reactor.listenUDT(5000, Echo())
+        self.echo = Echo()
+        self.port = reactor.listenUDT(5000, self.echo)
         print self.port
     
     def print_hello(self, res):

udt4twisted/udt.py

 from   udt4 import pyudt
 from socket import AI_PASSIVE
 from errno import EWOULDBLOCK, EINTR, EMSGSIZE, ECONNREFUSED, EAGAIN
+import gc
 _sockErrReadIgnore = [EAGAIN, EINTR, EWOULDBLOCK]
 _sockErrReadRefuse = [ECONNREFUSED]
 
         s.setblocking(False)
         #FIXME
         #fdesc._setCloseOnExec(s.UDTSOCKET.udtsocket)
-        print "1"
         return s
 
+    def _connectDone(self):
+        self.connected = 1
+        logPrefix = self._getLogPrefix(self.protocol)
+        self.logstr = "%s,client" % logPrefix
+        self.startReading()
+        self.protocol.makeConnection(self)
+
+
+    def doConnect(self, fd=None):
+        if not fd:
+            # self.doWrite = self.doConnect
+            self.doRead = self.doConnect
+            self.socket = self.createInternetSocket()
+            self.fileno = self.socket.fileno
+            self.socket.connect_ex(self._connectedAddr)
+            self._connectDone()
+            return
+        
+        try:
+            print "recv from:", fd, " size:", udt.UDT_RCVDATE
+            #FIXME
+            data = udt.recvmsg(fd, self.maxPacketSize)
+            addr = self._connectedAddr
+        except udt.UDTException as ue:
+            #Reset socket monitoring
+            print ue
+            self.reactor._poller.remove_usock(self.socket.UDTSOCKET, self.reactor._POLL_IN)
+            if ue[0] == 6002:
+                self.reactor._poller.add_usock(self.socket.UDTSOCKET, self.reactor._POLL_IN)
+            if ue[0] == 2001:
+                self.stopListening()
+            gc.collect()
+            
+        else:
+            try:
+                self.protocol.datagramReceived(data, addr)
+            except:
+                log.err()
+
+        
+
     def connect(self, host, port):
         """
         Connect to a remote host.
             raise RuntimeError("already connected, reconnecting is not currently supported")
         if not abstract.isIPAddress(host):
             raise ValueError("please pass only IP addresses, not domain names")
-
-        self.createInternetSocket()
         self._connectedAddr = (host, port)
-        self.socket.connect((host, port))
-
+        self.doConnect()
 
     def _bindSocket(self):
         """
-        TODO
+        Bind socket to an address.
         """
         try:
             skt = self.createInternetSocket()
             skt.bind((self.interface, self.port))
-            print "2"
         except socket.error as le:
             raise error.CannotListenError(self.interface, self.port, le)
 
         """
         udp.Port.startListening(self)
         self.socket.listen(self.backlog)
-        print "3"
-
 
 
     def doRead(self, fd=None):
         try:
             print "recv from:", fd, " size:", udt.UDT_RCVDATE
             #FIXME
-            data = udt.recvmsg(fd, udt.UDT_RCVDATE)
+            data = udt.recvmsg(fd, self.maxPacketSize)
             addr = self.addresses[fd.UDTSOCKET][1]
         except udt.UDTException as ue:
             return ue
             I{integer port number}); can be C{None} in connected mode.
         """
         if self._connectedAddr:
-            print "Connected Address not implemented!"
-            # assert addr in (None, self._connectedAddr)
-            # try:
-            #     return self.socket.send(datagram)
-            # except socket.error as se:
-            #     no = se.args[0]
-            #     if no == EINTR:
-            #         return self.write(datagram)
-            #     elif no == EMSGSIZE:
-            #         raise error.MessageLengthError("message too long")
-            #     elif no == ECONNREFUSED:
-            #         self.protocol.connectionRefused()
-            #     else:
-            #         raise
-        else:
-            assert addr != None
-            if not addr[0].replace(".", "").isdigit() and addr[0] != "<broadcast>":
-                warnings.warn("Please only pass IPs to write(), not hostnames",
-                              DeprecationWarning, stacklevel=2)
-            try:
-                socket = [v[0] for k, v in self.addresses.iteritems() if  v[1] == addr][0]
-
-                return udt.sendmsg(socket, datagram, len(datagram))
-            # except socket.error as se:
+            print "Hello!"
+            self.socket.sendmsg(datagram)
+            return
+        assert addr != None
+        
+        if not addr[0].replace(".", "").isdigit() and addr[0] != "<broadcast>":
+            warnings.warn("Please only pass IPs to write(), not hostnames",
+                          DeprecationWarning, stacklevel=2)
+        try:
+            print "sending..."
+            socket = [v[0] for k, v in self.addresses.iteritems() if  v[1] == addr][0]
+            return udt.sendmsg(socket, datagram, len(datagram))
+        # except socket.error as se:
             #     no = se.args[0]
             #     if no == EINTR:
             #         return self.write(datagram, addr)
             #         return
             #     else:
             #         raise
-            except KeyError:
-                print "No key found!"
-            except udt.UDTException:
-                print "!!!"
+        except KeyError:
+            print "No key found!"
+        except udt.UDTException:
+            print "!!!"
         
 
 

udt4twisted/udtepollreactor.py

 from twisted.internet.epollreactor import EPollReactor, _ContinuousPolling
 from twisted.internet import threads
 import udt4
-from udt4 import pyudt, accept, sendmsg, recvmsg, UDT_RCVDATE
+from udt4 import pyudt, accept, sendmsg, recvmsg, UDT_RCVDATE, UDTException
 from udt4.pyudt import UdtSocket
 from udt4twisted import udt
 import socket as s
+import gc
 
 class IReactorUDT(Interface):
     """
         for fd in set:
             try:
                 selectable = self._selectables[fd.UDTSOCKET]
+                socket, addr = accept(fd)
                 print "FD:", fd, "SELECT:", selectable
-                socket, addr = accept(fd)
                 print socket, addr
                 selectable.addresses[socket.UDTSOCKET] = (socket, addr)
                 self._udtsockets[socket.UDTSOCKET] = selectable
                                        selectable,
                                        fd,
                                        event)
+            except UDTException as ue:
+                if ue[0] == 5006:
+                    log.callWithLogger(selectable,
+                                       _drdw,
+                                       selectable,
+                                       fd,
+                                       event)
             else:
                 pass
 
                 log.err()
         if why:
             if isinstance(fd, udt4.UDTSOCKET):
+                self._poller.add_usock(fd, self._POLL_IN)
                 if why[0] == 6002:
-                    self._poller.remove_usock(fd, self._POLL_IN | self._POLL_DISCONNECTED)
+                    self._poller.add_usock(fd, self._POLL_IN)
+                    gc.collect()
                 return
             self._disconnectSelectable(selectable, why, inRead)
 
                      maxPacketSize, self, backlog)
         p.startListening()
         return p
-        
-
-    doIteration = doPoll
-
+    
     def connectUDT(self,
                    host,
                    port,
                    protocol,
-                   bindAddress = (0, "127.0.0.1")):
+                   bindAddress = (0, "127.0.0.1"),
+                   maxPacketSize=8192):
 
         c = udt.Port(bindAddress[0], protocol, bindAddress[1],
                      maxPacketSize, self, 0)
         c.connect(host, port)
         return c
 
+        
+
+    doIteration = doPoll
+
+
 
 def install():
     """
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.