Kostas
Kostas

Reputation: 377

Python - access class method (using twisted)

I'm working with this example of an iPhone Chart Server and all is working as expected.

What I wanted to ask is if and how I can use message(self, message) outside the IphoneChat class...

For example if I have an event triggering every hour I want to be able to send everyone connected a message or if I want to take the server down to send a 'global' announcement, do I have to put all the code within the IphoneChat class?

The server.py is this:

from twisted.internet.protocol import Factory, Protocol
from twisted.internet import reactor

class IphoneChat(Protocol):

    def connectionMade(self):
    self.factory.clients.append(self)
    print "clients are ", self.factory.clients

    def connectionLost(self, reason):
    self.factory.clients.remove(self)

# define message handling...

    def dataReceived(self, data):
    a = data.split(':')
    print a
    if len(a) > 1:
        command = a[0]
        content = a[1]

        msg = ""

        if command == "iam":
            #msg = content + " has joined"
            msg = "test1"   

        elif command == "toggle":
            #msg = command + ": " + content
            msg = "test2"

        elif command == "msg":
            msg = command + ": " + content
            print msg

        for c in self.factory.clients:
            c.message(msg)

    def message(self, message):
        self.transport.write(message + '\n')

rt = pollTimer.RepeatedTimer(3, NotifyAllFunction)

factory = Factory()
factory.protocol = IphoneChat
factory.clients = []
reactor.listenTCP(6035, factory)
print "chat server started"
reactor.run()

Adding the polling module:

from threading import Timer

class RepeatedTimer(object):
    def __init__(self, interval, function, *args, **kwargs):
    self._timer     = None
    self.interval   = interval
    self.function   = function
    self.args       = args
    self.kwargs     = kwargs
    self.is_running = False
    self.start()

    def _run(self):
    self.is_running = False
    self.start()
    self.function(*self.args, **self.kwargs)

    def start(self):
    if not self.is_running:
        self._timer = Timer(self.interval, self._run)
        self._timer.start()
        self.is_running = True

    def stop(self):
    self._timer.cancel()
    self.is_running = False

Upvotes: 1

Views: 527

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251096

Let's say you registered a callback to be executed after some time, then you can simple access all the clients from factory.clients and send them a message using their .transport.write() method:

from twisted.internet import task

...
# Rest of the code
...

factory = Factory()
factory.protocol = IphoneChat
factory.clients = []

def broadcast(message):
    for client in factory.clients:
        client.transport.write(message + '\n')

event = task.LoopingCall(broadcast, 'Ping to all users')
event.start(60*60) # call every hour
reactor.listenTCP(6035, factory)
print "chat server started"
reactor.run()

Upvotes: 2

Related Questions