Source

udt4twisted / examples / newclient.py

Full commit
import sys
sys.path.append("../")

from udt4twisted import udtepollreactor
udtepollreactor.install()

from twisted.python import log
from twisted.internet import reactor, defer
from twisted.internet.protocol import DatagramProtocol, Protocol, ClientFactory
from twisted.python import log
from udt4twisted import udt
log.startLogging(sys.stdout)

class Echo(Protocol):
    def connectionMade(self):
        print "CONN MADE!"
        self.transport.write("bomb!!!")

    def dataReceived(self, data):
        log.msg("received {0} from {1}".format(data, self.transport.getPeer()))
        self.transport.write("bomb!!!", 200, False)

    def connectionLost(self, reason):
        print "CONN LOST!"

class EchoFactory(ClientFactory):

    def startedConnecting(self, connector):
        print 'Started to connect.'

    def buildProtocol(self, addr):
        print 'Connected.'
        return Echo()

    def clientConnectionLost(self, connector, reason):
        print 'Lost connection.  Reason:', reason

    def clientConnectionFailed(self, connector, reason):
        print 'Connection failed. Reason:', reason

class Tester(object):
    port = None
    def connect_to_udt_port(self, res):
        self.echo = EchoFactory()
        self.port = reactor.connectUDT("127.0.0.1", 5000, self.echo, 5)
        print self.port

    def print_hello(self, res):
        print "Hello!"
        return "hello"

d = defer.Deferred()
test = Tester()

d.addCallback(test.print_hello)
d.addCallback(test.connect_to_udt_port)

reactor.callLater(0.3, d.callback, 1)
reactor.run()