twisted web getPage, 2 clients in 2 classes, manage events between the two
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 "Cli开发者_运维问答ent 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
How can I make callbacks outside the class?
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
:
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)
First, you can get rid of the DeferredList
. Your list only has one Deferred
in it, so the DeferredList
isn'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.
def mainmyServ(self):
# print "Client ID is: " + self.myServID
# Joining Chat
s1= 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)
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:
def main():
service = ChatService()
service.mainmyServ()
What prevents the main
function from adding more callbacks to the Deferred
in mainmyServ
? Only that mainmyServ
doesn't bother to return it. So:
def mainmyServ(self):
# print "Client ID is: " + self.myServID
# Joining Chat
s1= 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)
return s1
def main():
service = ChatService()
d = service.mainmyServ()
d.addCallback(doSomethingElse)
Nothing special there, it's just another addCallback
. All you were missing was a reference to the Deferred
.
You can set up your loop now, by having doSomethingElse
call another method on ChatService
. If that other method returns a Deferred
, then doSomethingElse
can add a callback to it that calls mainmyServ
again. And so on. There's your loop, controlled "outside" of the class.
精彩评论