Ignore:
Timestamp:
04/07/12 08:48:34 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Implemented better connection and connection failure handling.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/pyuipc_sim.py

    r57 r59  
    637637#------------------------------------------------------------------------------
    638638
     639failOpen = False
     640
     641opened = False
     642
     643#------------------------------------------------------------------------------
     644
    639645def open(request):
    640646    """Open the connection."""
    641     return True
     647    global opened
     648    if failOpen:
     649        raise FSUIPCException(ERR_NOFS)
     650    elif opened:
     651        raise FSUIPCException(ERR_OPEN)
     652    else:
     653        time.sleep(0.5)
     654        opened = True
     655        return True
    642656
    643657#------------------------------------------------------------------------------
     
    645659def prepare_data(pattern, forRead = True):
    646660    """Prepare the given pattern for reading and/or writing."""
    647     return pattern
    648 
     661    if opened:
     662        return pattern
     663    else:
     664        raise FSUIPCException(ERR_OPEN)
     665       
    649666#------------------------------------------------------------------------------
    650667
    651668def read(data):
    652669    """Read the given data."""
    653     return [values.read(offset) for (offset, type) in data]
     670    print "opened", opened
     671    if opened:
     672        return [values.read(offset) for (offset, type) in data]
     673    else:
     674        raise FSUIPCException(ERR_OPEN)
    654675           
    655676#------------------------------------------------------------------------------
     
    657678def write(data):
    658679    """Write the given data."""
    659     for (offset, type, value) in data:
    660         values.write(offset, value)
     680    if opened:
     681        for (offset, type, value) in data:
     682            values.write(offset, value)
     683    else:
     684        raise FSUIPCException(ERR_OPEN)
    661685           
    662686#------------------------------------------------------------------------------
     
    664688def close():
    665689    """Close the connection."""
    666     pass
     690    global opened
     691    opened = False
    667692
    668693#------------------------------------------------------------------------------
     
    673698CALL_WRITE=2
    674699CALL_CLOSE=3
     700CALL_FAILOPEN=4
     701CALL_QUIT = 99
    675702
    676703RESULT_RETURNED=1
     
    715742                    elif call==CALL_WRITE:
    716743                        result = write(args[0])
     744                    elif call==CALL_CLOSE:
     745                        global opened
     746                        opened = False
     747                        result = None
     748                    elif call==CALL_FAILOPEN:
     749                        global failOpen
     750                        failOpen = args[0]
     751                        result = None
    717752                    else:
    718753                        break
     
    753788        """Write the given data."""
    754789        return self._call(CALL_WRITE, data)
     790
     791    def close(self):
     792        """Close the connection currently opened in the simulator."""
     793        return self._call(CALL_CLOSE, None)
     794
     795    def failOpen(self, really):
     796        """Enable/disable open failure in the simulator."""
     797        return self._call(CALL_FAILOPEN, really)
     798
     799    def quit(self):
     800        """Quit from the simulator."""
     801        data = cPickle.dumps((CALL_QUIT, None))
     802        self._socket.send(struct.pack("I", len(data)) + data)       
    755803
    756804    def _call(self, command, data):
     
    10241072        if line=="EOF":
    10251073            print
    1026             return True
     1074            return self.do_quit("")
    10271075        else:
    10281076            return super(CLI, self).default(line)
     
    11021150            return [key + "=" for key in self._valueHandlers if key.startswith(text)]
    11031151
     1152    def do_close(self, args):
     1153        """Close an existing connection so that FS will fail."""
     1154        try:
     1155            self._client.close()
     1156            print "Connection closed"
     1157        except Exception, e:
     1158            print >> sys.stderr, "Failed to close the connection: " + str(e)       
     1159       
     1160    def do_failopen(self, args):
     1161        """Enable/disable the failing of opens."""
     1162        try:
     1163            value = self.str2bool(args)
     1164            self._client.failOpen(value)
     1165            print "Opening will%s fail" % ("" if value else " not",)
     1166        except Exception, e:
     1167            print >> sys.stderr, "Failed to set open failure: " + str(e)       
     1168
     1169    def help_failopen(self, usage = False):
     1170        """Help for the failopen close"""
     1171        if usage: print "Usage:",
     1172        print "failopen yes|no"
     1173
     1174    def complete_failopen(self, text, line, begidx, endidx):
     1175        if text:
     1176            if "yes".startswith(text): return ["yes"]
     1177            elif "no".startswith(text): return ["no"]
     1178            else: return []
     1179        else:
     1180            return ["yes", "no"]
     1181       
    11041182    def do_quit(self, args):
    11051183        """Handle the quit command."""
     1184        self._client.quit()
    11061185        return True
    11071186
Note: See TracChangeset for help on using the changeset viewer.