i’m trying to create a bridge program in twisted.web that receives data from a web server and sends it to another server, thus i’m using 2 getPage applications that i have wrapped in a class for convenience, the class contains all the callbacks and the client “routine”.. 1)auth 2)receive data 3)send data, all this is done in a cyclic way and works perfectly in both clients!!
What i am planning to do now is to integrate the two, this would mean that i would have to make some callbacks outside the classes in order to process them.
client1<—>main<—>client2
How can i do this?
i’m using twisted getPage
i’ll post one of the two classes
class ChatService():
def __init__(self):
self.myServID= self.generatemyServID()
self.myServServer= "http://localhost"
## This is where the magic starts
reactor.callWhenRunning(self.mainmyServ)
reactor.run()
def generatemyServID(self):
a= ""
for x in range(60):
c= floor(random() * 61)
a += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"[int(c):int(c)+1]
return a
def sentMessage(self, data):
print "Message was sent successfully"
def sendMessage(self, mess):
s1= DeferredList([client.getPage(self.myServServer+"/chat/", headers={'Content-Type': 'application/x-www-form-urlencoded'}, method="POST", postdata="action=chat&user=%s&message=%s" % (self.myServID, mess)),])
s1.addCallback(self.sentMessage)
s1.addErrback(self.errMessage)
def recivedMessage(self,data):
chat= loads(data[0][1])
if chat['from'] != "JOINED" and chat['from'] != "TYPING" and chat['from'] != "Ben":
print "%s says: %s" % (chat['from'], decode(chat['chat']))
self.sendMessage("Hello")
# Restart Loop
self.loopChat()
def errMessage(self,e):
# print "An error occured receiving/sending the messages\n%s" % e
print "Still no connectiions, waiting..."
self.loopChat()
def loopChat(self):
s1= DeferredList([client.getPage(self.myServServer+"/chat/", headers={'Content-Type': 'application/x-www-form-urlencoded'}, method="POST", postdata="action=poll&user=%s&message=null" % self.myServID),])
s1.addCallback(self.recivedMessage)
s1.addErrback(self.errMessage)
def error(self,e):
print "An error occured\n%s" % e
def connectedtomyServService(self,data):
if data[0][0] == False:
print "Connection to myServ Service was impossible"
reactor.stop()
return
if loads(data[0][1])['action'] == 'join':
print "Connected to the server and joined chat"
print "Started chat loop"
self.loopChat()
else:
print "An Error Occured"
return
def mainmyServ(self):
# print "Client ID is: " + self.myServID
# Joining Chat
s1= DeferredList([client.getPage(self.myServServer+"/chat/", headers={'Content-Type': 'application/x-www-form-urlencoded'}, method="POST", postdata="action=join&user=%s&message=null" % self.myServID),])
s1.addCallback(self.connectedtomyServService)
s1.addErrback(self.error)
How can I make callbacks outside the class?
I hope I managed to express myself =D
Thnaks a lot
This sounds like a very common misunderstanding. As a result of the misunderstanding, the question, as asked, doesn’t make a lot of sense. So let’s forget about the question.
You already have some code that’s using Deferreds. Let’s start with
mainmyServ:First, you can get rid of the
DeferredList. Your list only has oneDeferredin it, so theDeferredListisn’t adding any value. You’ll get practically the same behavior like this, and your callbacks can be simplified by removing all of the[0][0]expressions.So this is a perfectly reasonable method which is calling a function that returns a Deferred and then adding a callback and an errback to that Deferred. Say you have another function, perhaps your overall main function:
What prevents the
mainfunction from adding more callbacks to theDeferredinmainmyServ? Only thatmainmyServdoesn’t bother to return it. So:Nothing special there, it’s just another
addCallback. All you were missing was a reference to theDeferred.You can set up your loop now, by having
doSomethingElsecall another method onChatService. If that other method returns aDeferred, thendoSomethingElsecan add a callback to it that callsmainmyServagain. And so on. There’s your loop, controlled “outside” of the class.