Hi All,
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