Changeset 55:7c8a720c2906 in livinglogic.python.tipimaid

Show
Ignore:
Timestamp:
12/23/08 14:14:25 (10 years ago)
Author:
Nikolas Tautenhahn <nik@…>
Branch:
default
Message:

added flush_buffer-method, more comments

Files:
2 modified

Legend:

Unmodified
Added
Removed
  • liaalh_sender.py

    • Property exe set to *
    r54 r55  
    33 
    44import sys, socket, select, signal, datetime, os 
    5  
     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 :-( 
    66def intify(s, defaultint=0): 
    77    try: 
     
    1111 
    1212class Sender(object): 
    13     def __init__(self, ip, port, buffertime=0, backuppath=None, stream=sys.stdin): 
     13    def __init__(self, ip, port, buffertime=0, backuppath=None, stream=sys.stdin, progname="liaalh_sender"): # TODO: Put the right name in here 
    1414        self.ip = ip 
    1515        self.port = port 
    1616        self.stream = stream 
     17        self.progname = progname 
    1718        self.check_every = datetime.timedelta(seconds=10) 
    1819        if buffertime > 3: 
     
    2425        self.s = None 
    2526        self.f = None 
    26         if backuppath: 
     27        if backuppath: # test if we may write to the backuppath-directory 
    2728            backuppath = backuppath.rstrip(os.path.sep) 
    2829            try: 
     
    9192                self.s = None 
    9293                self.last_check_connection = self.startedbuffering = datetime.datetime.now() 
     94                sys.stderr.write("%s (%s): Socket connection lost, starting to log to buffer and/or recovery file" % (self.progname, self.startedbuffering.isoformat())) 
    9395                self.send_tempfile(line) 
    9496            signal.alarm(0) 
     
    122124                            (l, t) = (len(oldline), 0) 
    123125                            while t < l: # it might be that only a part of line gets transmitted 
    124                                 t += self.s.send(oldline[t:]) 
    125126                                try: 
     127                                    t += self.s.send(oldline[t:]) 
    126128                                    signal.alarm(2) # wait 2 seconds for server to notify us that it got the message 
    127129                                    ret = self.s.recv(1024) 
     
    135137                                    return # our socket died while we were transmitting buffered items - go back to "normal" behaviour 
    136138                                signal.alarm(0) 
    137                         else: # some buffered entries come to late, maybe because it took so long to wait for the server's response. 
     139                        else: # some buffered entries come too late, maybe because it took so long to wait for the server's response. 
    138140                            self.s.close() # make sure that we go to this method again to write away all buffered content which is too old 
    139141                            self.s = None # that's why we throw away our socket 
    140142                            self.buffer = self.buffer[i:] # and the content which was transmitted to the server in time 
    141143                            return 
     144                    sys.stderr.write("%s (%s): Socket connection established again, all data could be sent to the server" % (self.progname, self.last_check_connection.isoformat())) 
     145                    self.buffer = [] # if we arrive here everything was sent successfully 
     146 
     147    def flush_buffer(self): 
     148        if not self.s: 
     149            self.create_socket() 
     150            self.last_check_connection = datetime.datetime.now() 
     151        if self.s: 
     152            for (i, oldline) in enumerate(self.buffer): 
     153                if datetime.datetime.now() - self.startedbuffering < self.buffertime: # make sure we are still in buffertime 
     154                    (l, t) = (len(oldline), 0) 
     155                    while t < l: # it might be that only a part of line gets transmitted 
     156                        try: 
     157                            t += self.s.send(oldline[t:]) 
     158                            signal.alarm(2) # wait 2 seconds for server to notify us that it got the message 
     159                            ret = self.s.recv(1024) 
     160                            if ret == "": 
     161                                raise socket.timeout 
     162                            signal.alarm(0) 
     163                        except (socket.timeout, socket.error), exc: 
     164                            signal.alarm(0) 
     165                            self.s.close() 
     166                            self.s = None 
     167                            self.buffer = self.buffer[i:] # the first i items were transmitted successfully 
     168                            break 
     169                    else: 
     170                        continue # if we didn't break in the while loop, continue for loop 
     171                    break # otherwise break the for loop, too 
     172                else: # some buffered entries come too late, maybe because it took so long to wait for the server's response. 
     173                    self.buffer = self.buffer[i:] 
     174                    break 
     175        if self.f: 
     176            if self.f.name != os.path.join(self.backuppath, datetime.datetime.now().strftime("%Y%m%d_recovery.log")): 
     177                self.f.close() 
     178                self.f = open(os.path.join(self.backuppath, datetime.datetime.now().strftime("%Y%m%d_recovery.log")), "a", 1) 
     179        else: 
     180            self.f = open(os.path.join(self.backuppath, datetime.datetime.now().strftime("%Y%m%d_recovery.log")), "a", 1) 
     181        for oldline in self.buffer: 
     182            self.f.write(oldline) 
     183        self.buffer = [] 
     184        self.f.close() 
     185        self.f = None 
    142186 
    143187    def send(self): 
     
    151195                self.s.close() 
    152196        except Exception, exc: 
    153             if self.f: 
    154                 self.f.close() 
    155             if self.s: 
    156                 self.s.close() 
     197            if self.buffer: 
     198                self.flush_buffer() 
    157199            raise 
    158200 
     
    168210        sys.stderr.flush() 
    169211        return 1 
    170     S = Sender(args[0], intify(args[1], None), intify(options.buffertime, 0), options.backuppath) 
     212    S = Sender(args[0], intify(args[1], None), intify(options.buffertime, 0), options.backuppath, p.get_prog_name()) 
    171213    S.send() 
    172214 
  • liaalh_server.py

    • Property exe set to *