From ed0a2c0c359e5ed87c378f533519d269022462ce Mon Sep 17 00:00:00 2001 From: yvesf Date: Thu, 28 May 2009 16:09:02 +0000 Subject: blah git-svn-id: http://xapek.org/svn/common/omegle@1032 d0e8fea9-7529-0410-93fb-d39fd5b9c1dd --- omegle.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'omegle.py') diff --git a/omegle.py b/omegle.py index fd62d45..8c5fdce 100644 --- a/omegle.py +++ b/omegle.py @@ -62,9 +62,13 @@ class OmegleChat(RESTClient): self.timer = Timer(self.events, poll_interval) self.logger = logging.getLogger(__name__ + "." + self.__class__.__name__ + name) - self.is_connected = False + self.is_confirmed = False #after getting a connection confirm + self.is_connected = False #after having self.id def start(self): + if self.is_connected: + self.logger.error("Already connected") + raise Exception("Already connected") resp = RESTClient.request(self, "POST", "/start", {}) body=resp.read() id=body.split("\"") @@ -73,22 +77,26 @@ class OmegleChat(RESTClient): self.logger.info("Connected id=%s" % self.id) #Poll events self.timer.start() + self.is_connected = True else: self.logger.error("Bad response: %s" % body) raise Exception("Bad response: %s" % body) def disconnect(self): - self.logger.debug("/disconnect") + self.logger.debug("disconnect()") self.is_connected = False self.timer.stop() - r = RESTClient.request(self,"POST", "/disconnect", urllib.urlencode({'id': self.id})) - body = r.read() - self.logger.debug("/disconnect sent. Read: %s" % body) - if body == "win": - pass - else: - self.is_connected = True #disconnect failed - raise Exception("/disconnect; Bad response: %s" % body) + if self.is_confirmed: + self.is_confirmed = False + r = RESTClient.request(self,"POST", "/disconnect", urllib.urlencode({'id': self.id})) + body = r.read() + self.logger.debug("/disconnect sent. Read: %s" % body) + if body == "win": + pass + else: + self.is_connected = True #disconnect failed + self.is_confirmed = True + raise Exception("/disconnect; Bad response: %s" % body) def events(self): """does use its own "HTTPConnection" because its called async from a thread""" @@ -141,9 +149,10 @@ class OmegleChat(RESTClient): elif data[0] == "gotMessage" and data.__len__() == 2: self.on_message(data[1]) elif data[0] == "connected": - self.is_connected = True + self.is_confirmed = True self.on_connect() elif data[0] == "strangerDisconnected": + self.is_confirmed = False self.is_connected = False self.timer.stop() self.on_disconnect() -- cgit v1.2.1