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 (Server Side).
012 */
013public final class RpcAnswer {
014  /** Entry handle. */
015  public final int entry;
016
017  /** Call handle. */
018  public int call;
019
020  /** Entry name. */
021  public final String name;
022
023  /** Call raw parameters. */
024  public final String params;
025
026  /** Connection that called the RPC. */
027  public final ConnectionInfo conn;
028
029  /** Constructor.
030   * This should generally only be used internally to NetworkTables.
031   * @param inst Instance
032   * @param entry Entry handle
033   * @param call Call handle
034   * @param name Entry name
035   * @param params Call raw parameters
036   * @param conn Connection info
037   */
038  public RpcAnswer(NetworkTableInstance inst, int entry, int call, String name, String params, ConnectionInfo conn) {
039    this.inst = inst;
040    this.entry = entry;
041    this.call = call;
042    this.name = name;
043    this.params = params;
044    this.conn = conn;
045  }
046
047  static final byte[] emptyResponse = new byte[] {};
048
049  /**
050   * Posts an empty response if one was not previously sent.
051   */
052  public synchronized void free() {
053    if (call != 0) {
054      postResponse(emptyResponse);
055    }
056  }
057
058  /**
059   * Determines if the native handle is valid.
060   * @return True if the native handle is valid, false otherwise.
061   */
062  public boolean isValid() {
063    return call != 0;
064  }
065
066  /**
067   * Post RPC response (return value) for a polled RPC.
068   * @param result  result raw data that will be provided to remote caller
069   */
070  public void postResponse(byte[] result) {
071    NetworkTablesJNI.postRpcResponse(entry, call, result);
072    call = 0;
073  }
074
075  /* Network table instance. */
076  private final NetworkTableInstance inst;
077
078  /* Cached entry object. */
079  NetworkTableEntry entryObject;
080
081  /**
082   * Get the entry as an object.
083   * @return NetworkTableEntry for the RPC.
084   */
085  NetworkTableEntry getEntry() {
086    if (entryObject == null) {
087      entryObject = new NetworkTableEntry(inst, entry);
088    }
089    return entryObject;
090  }
091}