Exemplo n.º 1
0
  protected boolean success() {
    HttpExchange exchange = connection.getExchange();
    if (exchange == null) return false;

    AtomicMarkableReference<Result> completion = exchange.responseComplete(null);
    if (!completion.isMarked()) return false;

    parser.reset();
    decoder = null;

    if (!updateState(State.RECEIVE, State.IDLE)) throw new IllegalStateException();

    exchange.terminateResponse();

    HttpResponse response = exchange.getResponse();
    List<Response.ResponseListener> listeners = exchange.getConversation().getResponseListeners();
    ResponseNotifier notifier = connection.getDestination().getResponseNotifier();
    notifier.notifySuccess(listeners, response);
    LOG.debug("Received {}", response);

    Result result = completion.getReference();
    if (result != null) {
      connection.complete(exchange, !result.isFailed());
      notifier.notifyComplete(listeners, result);
    }

    return true;
  }
Exemplo n.º 2
0
  protected boolean fail(Throwable failure) {
    HttpExchange exchange = connection.getExchange();
    // In case of a response error, the failure has already been notified
    // and it is possible that a further attempt to read in the receive
    // loop throws an exception that reenters here but without exchange;
    // or, the server could just have timed out the connection.
    if (exchange == null) return false;

    AtomicMarkableReference<Result> completion = exchange.responseComplete(failure);
    if (!completion.isMarked()) return false;

    parser.close();
    decoder = null;

    while (true) {
      State current = state.get();
      if (updateState(current, State.FAILURE)) break;
    }

    exchange.terminateResponse();

    HttpResponse response = exchange.getResponse();
    HttpConversation conversation = exchange.getConversation();
    ResponseNotifier notifier = connection.getDestination().getResponseNotifier();
    notifier.notifyFailure(conversation.getResponseListeners(), response, failure);
    LOG.debug("Failed {} {}", response, failure);

    Result result = completion.getReference();
    if (result != null) {
      connection.complete(exchange, false);

      notifier.notifyComplete(conversation.getResponseListeners(), result);
    }

    return true;
  }
 {
   ATOMIC_MARKABLE_REFERENCE.compareAndSet("abc", "abc2", false, true);
 }
Exemplo n.º 4
0
  /**
   * Takes a picture, blocking until the picture is complete, then returns the JPEG data for the
   * picture.
   *
   * @param context the context of the calling Activity or Service
   * @return byte array containing a JPEG-encoded image, or zero-length array on failure
   */
  public static synchronized byte[] takePhoto(final Context context, int width, int height) {

    // Set up a structure to hold the JPEG data
    final AtomicMarkableReference<byte[]> dataRef =
        new AtomicMarkableReference<byte[]>(null, false);

    // Set up a receiver that will fill in the data structure
    IntentFilter pictureFilter = new IntentFilter();
    pictureFilter.addAction(PICTURE_INTENT);
    final BroadcastReceiver receiver =
        new BroadcastReceiver() {

          @Override
          public void onReceive(Context context, Intent intent) {

            // Load this image data into the receiver
            byte[] data = intent.getByteArrayExtra(IMAGE_EXTRA);
            synchronized (dataRef) {
              dataRef.set(data, true);
              dataRef.notifyAll();
            }

            // Unregister this listener
            try {
              context.unregisterReceiver(this);
            } catch (IllegalArgumentException e) {
              Log.w(TAG, "PictureReceiver failed to unregister.", e);
            }
          }
        };
    context.registerReceiver(receiver, pictureFilter);

    // Try to schedule a picture to be taken immediately
    Intent intent = new Intent(context, AirboatCameraActivity.class);
    intent.setFlags(
        Intent.FLAG_ACTIVITY_NEW_TASK); // TODO: I don't know if this is the right flag here
    intent.putExtra(
        SAVE_EXTRA, true); // TODO: change this back at future point to disable always saving images
    intent.putExtra(QUALITY_EXTRA, 30);
    intent.putExtra(WIDTH_EXTRA, width);
    intent.putExtra(HEIGHT_EXTRA, height);
    context.startActivity(intent);

    // Wait for picture to be returned through atomic reference
    try {
      while (!dataRef.isMarked()) {
        synchronized (dataRef) {
          dataRef.wait();
        }
      }

      if (dataRef.getReference() != null) {
        return dataRef.getReference();
      } else {
        Log.w(TAG, "Camera activity returned null.");
        return new byte[0];
      }
    } catch (InterruptedException e) {
      Log.w(TAG, "Interrupted while taking photo", e);
      return new byte[0];
    }
  }