Changeset 5542

Show
Ignore:
Timestamp:
07/05/12 23:04:49 (7 years ago)
Author:
cmatthew
Message:

moved rpc code to its own file.

Location:
seattle/branches/nacl_repy/seattlelib
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • seattle/branches/nacl_repy/seattlelib/lind_server.mix

    r5523 r5542  
    3232 
    3333 
    34 # if not TRACE: 
    35 #   def log(arg): 
    36 #     """Turn off logging""" 
    37 #     pass 
     34if not TRACE: 
     35  def log(arg): 
     36    """Turn off logging""" 
     37    pass 
    3838 
    3939 
     
    6363    log("warning: " + message) 
    6464 
    65  
    66 class Response: 
    67   """ An Response to a RPC request. 
    68  
    69   <Purpose> 
    70  
    71   This object is responsible sending responses to specific system calls.  It is 
    72   loaded with data, then can build a reply struct which can then be sent back 
    73   to NaCl through an IMC channel. 
    74  
    75   The Response can be in one of two states, an Error response (for calls that 
    76   have failed somehow), or a success response. An error response has an error 
    77   code and a message for perror to display. A success has a return code and 
    78   optionally a data string. 
    79  
    80   TODO: error message is not yet displayed by perror. 
    81   """ 
    82   message = "No error message set" 
    83   data = "" 
    84  
    85   def __init__(self, response_type, is_error, return_code): 
    86     assert isinstance(response_type, str), "response type is not a string" 
    87     self.response_type = response_type 
    88     assert isinstance(is_error, bool), "is error is not a boolean" 
    89     self.is_error = is_error 
    90  
    91     if isinstance(return_code, str): 
    92       try: 
    93         self.return_code = curr_comp()[ERRNO][return_code] 
    94       except KeyError: 
    95         raise Exception("Return code is not a valid error number.") 
    96     elif isinstance(return_code, int): 
    97       self.return_code = return_code 
    98     else: 
    99       raise Exception("Invalid return code.") 
    100     assert isinstance(self.return_code, int), \ 
    101            "return code is not a int, or a stirng which mapped to an int" 
    102  
    103   def make_struct(self): 
    104     """ 
    105     <Purpose> 
    106  
    107     Get the representation of this Response in struct format.  A struct is a 
    108     string, which when sent to C can be cast as a struct and used nativly. 
    109     The struct format is iiii123s, wich is: 
    110     struct { 
    111         int message_len; 
    112         int magic_number; 
    113         int is_in_error; 
    114         int return_or_error_code; 
    115         char data_or_message[]; 
    116     }; 
    117  
    118     """ 
    119     reply = None 
    120     if self.is_error: 
    121       # message format <size, magic, is_error, code, message> 
    122       reply = struct_pack("<i<i<i<i" + str(len(self.message)) + "s", \ 
    123                           len(self.message) + 16, \ 
    124                           101010, 1, self.return_code,\ 
    125                           self.message) 
    126     else: 
    127       # message format <size, magic, is_error, code, data> 
    128       reply = struct_pack("<i<i<i<i" + str(len(self.data)) + "s", \ 
    129                           len(self.data) + 16, 101010, 0, self.return_code,\ 
    130                           self.data) 
    131     return reply 
    132  
    133   def __str__(self): 
    134     """Human readable format for debugging""" 
    135     if self.is_error: 
    136       type_str = "Error" 
    137     else: 
    138       type_str = "Success" 
    139     return type_str + " in " + self.response_type + " with code " + \ 
    140            str(self.return_code) + " with data of size " + str(len(self.data))\ 
    141            + " with data " + str(self.data) 
    142  
    143  
    144 def ErrorResponseBuilder(who, code, message): 
    145   """ Build an error response object. 
    146  
    147   <Arguments> 
    148    who: Which portion of the system or system call is making this reply. 
    149    code: Positive error code to return, or string from errno.h to resolve 
    150    to a number. 
    151  
    152    message: A detailed message about the error. 
    153    """ 
    154   r = Response(who, True, code) 
    155   r.message = message 
    156   return r 
    157  
    158  
    159 def SuccessResponseBuilder(who, code, data=None): 
    160   """Build an success response object. 
    161  
    162   <Arugments> 
    163      who: Which portion of the system or system call is making this reply. 
    164      code: the integer return code to return. 
    165      data: the data this call returns (as a string), if any. 
    166   """ 
    167   r = Response(who, False, code) 
    168   if data != None: 
    169     r.data = data 
    170   return r 
    171  
     65include lind_rpc.repy 
     66 
     67 
     68include lind_parsers.repy 
     69 
     70include lind_fs_constants.py 
    17271 
    17372def check_valid_fd_handle(num): 
    17473  assert isinstance(num, int) 
    175   assert (10 <= num <= MAX_FD), "invalid handle %d" % num 
    176  
    177  
    178 include lind_parsers.repy 
    179  
    180 include lind_fs_constants.py 
     74  assert (STARTINGFD <= num <= MAX_FD), "invalid handle %d" % num 
     75 
     76 
     77 
    18178 
    18279include lind_net_constants.py 
     
    438335 
    439336def _check_file(name): 
     337  if name.startswith("/"): 
     338    raise FileNotFoundError("Cannot open files from full path") 
     339 
    440340  if name.startswith("./"): 
    441341    cleanname = name[2:]