Beispiel #1
0
def _handle_request(data):
  """
  <Purpose>
    Process advertisements. This method contains no networking operations, 
    and is largely ported from previous versions of the advertise server.

  <Arguments>
    data (string)
      The packet sent by the client in its (more or less) raw form. This is a 
      string which has yet to be run through serialized.repy's deserialize 
      algorithm. A port of serialize.repy's serialize and deserialize methods 
      has been made locally because this client is intended to be purely 
      written in python.

  <Exceptions>
    Quite a few are possible, these will be populated later.

  <Returns>
    A packet ready for sending to the client who issued the original request.

  <Side Effects>
    None
  """
  global puts_so_far
  global gets_so_far
  global query_times
  # Format of requesttuple: ('PUT'/'GET', key, value, TTLval
  requesttuple = serialize.serialize_deserializedata(data)

  if requesttuple[0] == 'PUT':
    puts_so_far += 1

    ############# START Tons of type checking
    try:
      (key, value, ttlval) = requesttuple[1:]
    except ValueError, e:
      _log_with_timestamp(' > ERROR: Incorrect format for request tuple: ' + str(requesttuple) + "\n")
      return

    if type(key) is not str:
      _log_with_timestamp(' > ERROR: Key type for PUT must be str, not' + str(type(key)) + "\n")
      return

    if type(value) is not str:
      _log_with_timestamp(' > ERROR: Value type must be str, not' + str(type(value)) + "\n")
      return

    if type(ttlval) is not int and type(ttlval) is not long:
      _log_with_timestamp(' > ERROR: TTL type must be int or long, not' + str(type(ttlval)) + "\n")
      return

    if ttlval <=0:
      _log_with_timestamp(' > ERROR: TTL must be positive, not ' + str(ttlval) + "\n")
      return
    ############# END Tons of type checking

    _insert_item(key, value, ttlval)
    _insert_item('%all', value, ttlval)

    return serialize.serialize_serializedata("OK")
Beispiel #2
0
def _handle_request(data):
    """
  <Purpose>
    Process advertisements. This method contains no networking operations, 
    and is largely ported from previous versions of the advertise server.

  <Arguments>
    data (string)
      The packet sent by the client in its (more or less) raw form. This is a 
      string which has yet to be run through serialized.repy's deserialize 
      algorithm. A port of serialize.repy's serialize and deserialize methods 
      has been made locally because this client is intended to be purely 
      written in python.

  <Exceptions>
    Quite a few are possible, these will be populated later.

  <Returns>
    A packet ready for sending to the client who issued the original request.

  <Side Effects>
    None
  """
    global puts_so_far
    global gets_so_far
    global query_times
    # Format of requesttuple: ('PUT'/'GET', key, value, TTLval
    requesttuple = serialize.serialize_deserializedata(data)

    if requesttuple[0] == 'PUT':
        puts_so_far += 1

        ############# START Tons of type checking
        try:
            (key, value, ttlval) = requesttuple[1:]
        except ValueError, e:
            _log_with_timestamp(
                ' > ERROR: Incorrect format for request tuple: ' +
                str(requesttuple) + "\n")
            return

        if type(key) is not str:
            _log_with_timestamp(' > ERROR: Key type for PUT must be str, not' +
                                str(type(key)) + "\n")
            return

        if type(value) is not str:
            _log_with_timestamp(' > ERROR: Value type must be str, not' +
                                str(type(value)) + "\n")
            return

        if type(ttlval) is not int and type(ttlval) is not long:
            _log_with_timestamp(' > ERROR: TTL type must be int or long, not' +
                                str(type(ttlval)) + "\n")
            return

        if ttlval <= 0:
            _log_with_timestamp(' > ERROR: TTL must be positive, not ' +
                                str(ttlval) + "\n")
            return
        ############# END Tons of type checking

        _insert_item(key, value, ttlval)
        _insert_item('%all', value, ttlval)

        return serialize.serialize_serializedata("OK")
Beispiel #3
0
            return

        if maxvals <= 0:
            log(' > ERROR: maxvals; Value type must be positive, not ' +
                str(maxvals) + "\n")
            return

        ############# END Tons of type checking

        readlist = []
        entries = _read_item(key, maxvals)

        for entry in entries:
            readlist.append(entry)

        return serialize.serialize_serializedata(("OK", readlist))

    return


def _tcp_query_handler_thread():
    """
  <Purpose>
    Handles all TCP requests that were previously received in
    pending_tcp_queries.  This is NOT threadsafe, and should only be
    run from one thread only.

    This is meant to be a persistent thread that continuously runs in
    the background.  Remember to set daemon mode, otherwise this thread
    can cause the process to run indefinitely even when the main thread
    is interrupted.
Beispiel #4
0
      log(' > ERROR: Maximum value type must be int or long, not' + str(type(maxvals)) + "\n")
      return

    if maxvals <=0:
      log(' > ERROR: maxvals; Value type must be positive, not ' + str(maxvals) + "\n")
      return

    ############# END Tons of type checking

    readlist = []
    entries = _read_item(key, maxvals)

    for entry in entries:
      readlist.append(entry)

    return serialize.serialize_serializedata(("OK", readlist))

  return


def _tcp_query_handler_thread():
  global query_times

  while True:
    try:
      query = pending_tcp_queries.get(block=True)
      response = _handle_request(query['request'])

      session.session_sendmessage(query['socket'], response)

      query['socket'].close()