Changeset 82:22eec11ad852 in livinglogic.python.tipimaid for liaalh_sender.py

Show
Ignore:
Timestamp:
02/03/09 16:26:49 (10 years ago)
Author:
Nik Tautenhahn <nik@…>
Branch:
default
Message:

use socket.settimeout() instead of raising socket.timeout()s manually

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • liaalh_sender.py

    r81 r82  
    33 
    44import sys, socket, select, signal, datetime, os 
    5 # TODO: What happens if we have to send in multiple chunks and bad things happen after one but before the last chunk? ATM this would result in liaalh-hiccup :-( 
     5 
    66def intify(s, defaultint=0): 
    77    try: 
     
    4141                raise 
    4242            self.backuppath = backuppath 
     43        socket.setdefaulttimeout(5) # applies only to *new* sockets so we have to set it here 
    4344        self.create_socket() 
    4445        self.last_check_connection = datetime.datetime.now() 
    45         signal.signal(signal.SIGALRM, self.timeout) 
     46#       signal.signal(signal.SIGALRM, self.timeout) 
    4647        signal.signal(signal.SIGHUP, self.sighandler) 
    4748        signal.signal(signal.SIGTERM, self.sighandler) 
     
    8788            t += self.s.send(line[t:]) 
    8889            try: 
    89                 signal.alarm(5) # wait 5 seconds for server to notify us that it got the message 
    9090                ret = self.s.recv(1024) 
    9191                if ret == "": 
    9292                    raise socket.timeout 
    9393            except (socket.timeout, socket.error), exc: 
    94                 signal.alarm(0) 
    9594                self.s.close() 
    9695                self.s = None 
     
    9897                sys.stderr.write("%s (%s): Socket connection lost, starting to log to buffer and/or recovery file\n" % (self.progname, self.startedbuffering.isoformat())) 
    9998                self.send_tempfile(line) 
    100             signal.alarm(0) 
    10199 
    102100    def send_tempfile(self, line): 
     
    132130                                try: 
    133131                                    t += self.s.send(oldline[t:]) 
    134                                     signal.alarm(2) # wait 2 seconds for server to notify us that it got the message 
    135132                                    ret = self.s.recv(1024) 
    136133                                    if ret == "": 
    137134                                        raise socket.timeout 
    138135                                except (socket.timeout, socket.error), exc: 
    139                                     signal.alarm(0) 
    140136                                    self.s.close() 
    141137                                    self.s = None 
     
    143139                                    sys.stderr.write("%s (%s): Socket connection died again, while sending buffered data\n" % (self.progname, datetime.datetime.now().isoformat())) 
    144140                                    return # our socket died while we were transmitting buffered items - go back to "normal" behaviour 
    145                                 signal.alarm(0) 
    146141                        else: # some buffered entries come too late, maybe because it took so long to wait for the server's response. 
    147142                            self.s.close() # make sure that we go to this method again to write away all buffered content which is too old 
     
    163158                        try: 
    164159                            t += self.s.send(oldline[t:]) 
    165                             signal.alarm(2) # wait 2 seconds for server to notify us that it got the message 
    166160                            ret = self.s.recv(1024) 
    167161                            if ret == "": 
    168162                                raise socket.timeout 
    169                             signal.alarm(0) 
    170163                        except (socket.timeout, socket.error), exc: 
    171                             signal.alarm(0) 
    172164                            self.s.close() 
    173165                            self.s = None 
     
    226218    p.add_option("-p", "--backuppath", dest="backuppath", type="string", action="store", help="Directory where recovery logs should be stored if the network connection dies", default=None) 
    227219    (options, args) = p.parse_args() 
    228     if len(args) != 2 or intify(args[1], None) is None or options.buffertime== 0 or options.backuppath is None: 
     220    if len(args) != 2 or intify(args[1], None) is None or options.buffertime == 0 or options.backuppath is None: 
    229221        p.print_usage(sys.stderr) 
    230222        sys.stderr.write("%s: Please specify ip, port, a buffertime and a path for local backups!\n" % p.get_prog_name()) # required options... not so nice but necessary