001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.networktables;
006
007/** NetworkTables Remote Procedure Call (Server Side). */
008public final class RpcAnswer {
009  /** Entry handle. */
010  @SuppressWarnings("MemberName")
011  public final int entry;
012
013  /** Call handle. */
014  @SuppressWarnings("MemberName")
015  public int call;
016
017  /** Entry name. */
018  @SuppressWarnings("MemberName")
019  public final String name;
020
021  /** Call raw parameters. */
022  @SuppressWarnings("MemberName")
023  public final byte[] params;
024
025  /** Connection that called the RPC. */
026  @SuppressWarnings("MemberName")
027  public final ConnectionInfo conn;
028
029  /**
030   * Constructor. This should generally only be used internally to NetworkTables.
031   *
032   * @param inst Instance
033   * @param entry Entry handle
034   * @param call Call handle
035   * @param name Entry name
036   * @param params Call raw parameters
037   * @param conn Connection info
038   */
039  @SuppressWarnings("PMD.ArrayIsStoredDirectly")
040  public RpcAnswer(
041      NetworkTableInstance inst,
042      int entry,
043      int call,
044      String name,
045      byte[] params,
046      ConnectionInfo conn) {
047    this.m_inst = inst;
048    this.entry = entry;
049    this.call = call;
050    this.name = name;
051    this.params = params;
052    this.conn = conn;
053  }
054
055  static final byte[] emptyResponse = new byte[] {};
056
057  /*
058   * Finishes an RPC answer by replying empty if the user did not respond.
059   * Called internally by the callback thread.
060   */
061  void finish() {
062    if (call != 0) {
063      NetworkTablesJNI.postRpcResponse(entry, call, emptyResponse);
064      call = 0;
065    }
066  }
067
068  /**
069   * Determines if the native handle is valid.
070   *
071   * @return True if the native handle is valid, false otherwise.
072   */
073  public boolean isValid() {
074    return call != 0;
075  }
076
077  /**
078   * Post RPC response (return value) for a polled RPC.
079   *
080   * @param result result raw data that will be provided to remote caller
081   * @return true if the response was posted, otherwise false
082   */
083  public boolean postResponse(byte[] result) {
084    boolean ret = NetworkTablesJNI.postRpcResponse(entry, call, result);
085    call = 0;
086    return ret;
087  }
088
089  /* Network table instance. */
090  private final NetworkTableInstance m_inst;
091
092  /* Cached entry object. */
093  NetworkTableEntry m_entryObject;
094
095  /**
096   * Get the entry as an object.
097   *
098   * @return NetworkTableEntry for the RPC.
099   */
100  NetworkTableEntry getEntry() {
101    if (m_entryObject == null) {
102      m_entryObject = new NetworkTableEntry(m_inst, entry);
103    }
104    return m_entryObject;
105  }
106}