All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.signal.libsignal.net.CdsiLookup Maven / Gradle / Ivy

There is a newer version: 0.62.0
Show newest version
//
// Copyright 2023 Signal Messenger, LLC.
// SPDX-License-Identifier: AGPL-3.0-only
//

package org.signal.libsignal.net;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import org.signal.libsignal.internal.CompletableFuture;
import org.signal.libsignal.internal.Native;
import org.signal.libsignal.internal.NativeHandleGuard;

class CdsiLookup implements NativeHandleGuard.Owner {
  public static CompletableFuture start(
      Network network, String username, String password, CdsiLookupRequest request)
      throws IOException, InterruptedException, ExecutionException {

    CdsiLookupRequest.NativeRequest nativeRequest = request.makeNative();
    try (NativeHandleGuard asyncRuntime = new NativeHandleGuard(network.getAsyncContext());
        NativeHandleGuard connectionManager =
            new NativeHandleGuard(network.getConnectionManager())) {

      return Native.CdsiLookup_new(
              asyncRuntime.nativeHandle(),
              connectionManager.nativeHandle(),
              username,
              password,
              nativeRequest.getHandle())
          .thenApply((Long nativeHandle) -> new CdsiLookup(nativeHandle, network));
    }
  }

  public CompletableFuture complete() {
    try (NativeHandleGuard asyncRuntime = new NativeHandleGuard(this.network.getAsyncContext());
        NativeHandleGuard self = new NativeHandleGuard(this)) {
      return Native.CdsiLookup_complete(asyncRuntime.nativeHandle(), self.nativeHandle())
          .thenApply(response -> (CdsiLookupResponse) response);
    }
  }

  public byte[] getToken() {
    return Native.CdsiLookup_token(this.nativeHandle);
  }

  @Override
  public long unsafeNativeHandleWithoutGuard() {
    return this.nativeHandle;
  }

  private CdsiLookup(long nativeHandle, Network network) {
    this.nativeHandle = nativeHandle;
    this.network = network;
  }

  private Network network;
  private long nativeHandle;

  @Override
  @SuppressWarnings("deprecation")
  protected void finalize() {
    Native.CdsiLookup_Destroy(this.nativeHandle);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy