Changeset e530abd


Ignore:
Timestamp:
2010-12-20T20:51:49Z (13 years ago)
Author:
Philippe Crama <pcfeb0009@…>
Branches:
master
Children:
9c51166
Parents:
5a54ec8
Message:

Make skyped restart itself

Now skyped.py detects broken sockets and restarts itself waiting
for a new connection: this sould help with passing the tests
(see branch NetBSD-test-suite)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • skype/skyped.py

    r5a54ec8 re530abd  
    4242        if type != KeyboardInterrupt:
    4343                print_exception(type, value, tb)
    44         options.conn.close()
     44        if options.conn: options.conn.close()
    4545        # shut down client if it's running
    4646        try:
     
    5252sys.excepthook = eh
    5353
    54 def input_handler(fd, io_condition):
    55         global options
     54def input_handler(fd):
     55        global options
     56        global skype
    5657        if options.buf:
    5758                for i in options.buf:
    5859                        skype.send(i.strip())
    5960                options.buf = None
     61                return True
    6062        else:
    6163                try:
     
    6466                        dprint("Warning, receiving 1024 bytes failed (%s)." % s)
    6567                        fd.close()
     68                        options.conn = False
    6669                        return False
    6770                for i in input.split("\n"):
     
    7982
    8083def send(sock, txt):
    81         from time import sleep
    8284        count = 1
    8385        done = False
     
    8991                        count += 1
    9092                        dprint("Warning, sending '%s' failed (%s). count=%d" % (txt, s, count))
    91                         sleep(1)
     93                        time.sleep(1)
    9294        if not done:
    93                 options.conn.close()
     95                if options.conn: options.conn.close()
     96                options.conn = False
     97        return done
    9498
    9599def bitlbee_idle_handler(skype):
    96100        global options
     101        done = False
    97102        if options.conn:
    98103                try:
    99104                        e = "PING"
    100                         send(options.conn, "%s\n" % e)
     105                        done = send(options.conn, "%s\n" % e)
    101106                        dprint("... pinged Bitlbee")
    102107                except Exception, s:
    103108                        dprint("Warning, sending '%s' failed (%s)." % (e, s))
    104                         options.conn.close()
    105         return True
     109                        if options.conn: options.conn.close()
     110                        options.conn = False
     111                        done = False
     112        return done
    106113
    107114def server(host, port, skype):
     
    139146                dprint("Warning, receiving 1024 bytes failed (%s)." % s)
    140147                options.conn.close()
     148                options.conn = False
    141149                return False
    142150        if ret == 2:
     
    148156                dprint("Username and/or password WRONG.")
    149157                options.conn.send("PASSWORD KO\n")
     158                options.conn.close()
     159                options.conn = False
    150160                return False
    151161
     
    191201                        # everybody will be happy
    192202                        e = i.encode('UTF-8')
    193                         dprint('<< ' + e)
    194203                        if options.conn:
     204                                dprint('<< ' + e)
    195205                                try:
    196206                                        # I called the send function really_send
     
    198208                                except Exception, s:
    199209                                        dprint("Warning, sending '%s' failed (%s)." % (e, s))
    200                                         options.conn.close()
     210                                        if options.conn: options.conn.close()
     211                                        options.conn = False
     212                        else:
     213                                dprint('---' + e)
    201214
    202215        def send(self, msg_text):
    203216                if not len(msg_text) or msg_text == "PONG":
     217                        if msg_text == "PONG": options.last_bitlbee_pong = time.time()
    204218                        return
    205219                try:
     
    260274        timeout = 1; # in seconds
    261275        skype_ping_period = 5
    262         bitlbee_ping_period = 30
    263         skype_ping_start_time = time.time()
    264         bitlbee_ping_start_time = time.time()
    265         while 1:
     276        bitlbee_ping_period = 10
     277        bitlbee_pong_timeout = 30
     278        now = time.time()
     279        skype_ping_start_time = now
     280        bitlbee_ping_start_time = now
     281        options.last_bitlbee_pong = now
     282        in_error = []
     283        handler_ok = True
     284        while (len(in_error) == 0) and handler_ok and options.conn:
    266285                ready_to_read, ready_to_write, in_error = \
    267286                        select.select([options.conn], [], [], timeout)
    268287                now = time.time()
     288                handler_ok = True
    269289                if len(ready_to_read) == 1:
    270                         input_handler(ready_to_read.pop(), options)
     290                        handler_ok = input_handler(ready_to_read.pop())
    271291                        # don't ping bitlbee/skype if they already received data
    272292                        bitlbee_ping_start_time = now
    273293                        skype_ping_start_time = now
    274                 if now - skype_ping_period > skype_ping_start_time:
    275                         skype_idle_handler(skype)
     294                if (now - skype_ping_period > skype_ping_start_time) and handler_ok:
     295                        handler_ok = skype_idle_handler(skype)
    276296                        skype_ping_start_time = now
    277297                if now - bitlbee_ping_period > bitlbee_ping_start_time:
    278                         bitlbee_idle_handler(skype)
     298                        handler_ok = bitlbee_idle_handler(skype)
    279299                        bitlbee_ping_start_time = now
     300                        if options.last_bitlbee_pong:
     301                                if (now - options.last_bitlbee_pong) > bitlbee_pong_timeout:
     302                                        dprint("Bitlbee pong timeout")
     303                                        # TODO is following line necessary? Should there be a options.conn.unwrap() somewhere?
     304                                        # options.conn.shutdown()
     305                                        if options.conn: options.conn.close()
     306                                        options.conn = False
     307                                else:
     308                                        dprint("%f seconds since last PONG" % (now - options.last_bitlbee_pong))
     309                        else:
     310                                options.last_bitlbee_pong = now
     311        dprint("Serverloop done")
    280312
    281313if __name__=='__main__':
     
    347379        except Skype4Py.SkypeAPIError, s:
    348380                sys.exit("%s. Are you sure you have started Skype?" % s)
    349         server(options.host, options.port, skype)
     381        while 1:
     382                options.conn = False
     383                server(options.host, options.port, skype)
Note: See TracChangeset for help on using the changeset viewer.