001/*----------------------------------------------------------------------------*/
002/* Copyright (c) FIRST 2017-2018. All Rights Reserved.                        */
003/* Open Source Software - may be modified and shared by FRC teams. The code   */
004/* must be accompanied by the FIRST BSD license file in the root directory of */
005/* the project.                                                               */
006/*----------------------------------------------------------------------------*/
007
008package edu.wpi.first.networktables;
009
010/**
011 * NetworkTables Remote Procedure Call.
012 */
013public final class RpcCall {
014  /** Constructor.
015   * This should generally only be used internally to NetworkTables.
016   * @param entry Entry
017   * @param call Call handle
018   */
019  public RpcCall(NetworkTableEntry entry, int call) {
020    m_entry = entry;
021    m_call = call;
022  }
023
024  /**
025   * Cancels the result if no other action taken.
026   */
027  public synchronized void free() {
028    if (m_call != 0) {
029      cancelResult();
030    }
031  }
032
033  /**
034   * Determines if the native handle is valid.
035   * @return True if the native handle is valid, false otherwise.
036   */
037  public boolean isValid() {
038    return m_call != 0;
039  }
040
041  /**
042   * Get the RPC entry.
043   * @return NetworkTableEntry for the RPC.
044   */
045  public NetworkTableEntry getEntry() {
046    return m_entry;
047  }
048
049  /**
050   * Get the call native handle.
051   * @return Native handle.
052   */
053  public int getCall() {
054    return m_call;
055  }
056
057  /**
058   * Get the result (return value).  This function blocks until
059   * the result is received.
060   * @return Received result (output)
061   */
062  public byte[] getResult() {
063    byte[] result = NetworkTablesJNI.getRpcResult(m_entry.getHandle(), m_call);
064    if (result.length != 0) {
065      m_call = 0;
066    }
067    return result;
068  }
069
070  /**
071   * Get the result (return value).  This function blocks until
072   * the result is received or it times out.
073   * @param timeout     timeout, in seconds
074   * @return Received result (output)
075   */
076  public byte[] getResult(double timeout) {
077    byte[] result = NetworkTablesJNI.getRpcResult(m_entry.getHandle(), m_call, timeout);
078    if (result.length != 0) {
079      m_call = 0;
080    }
081    return result;
082  }
083
084  /**
085   * Ignore the result.  This function is non-blocking.
086   */
087  public void cancelResult() {
088    NetworkTablesJNI.cancelRpcResult(m_entry.getHandle(), m_call);
089  }
090
091  private final NetworkTableEntry m_entry;
092  private int m_call;
093}