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. */ 008public final class RpcCall implements AutoCloseable { 009 /** 010 * Constructor. This should generally only be used internally to NetworkTables. 011 * 012 * @param entry Entry 013 * @param call Call handle 014 */ 015 public RpcCall(NetworkTableEntry entry, int call) { 016 m_entry = entry; 017 m_call = call; 018 } 019 020 /** Cancels the result if no other action taken. */ 021 @Override 022 public synchronized void close() { 023 if (m_call != 0) { 024 cancelResult(); 025 } 026 } 027 028 /** 029 * Determines if the native handle is valid. 030 * 031 * @return True if the native handle is valid, false otherwise. 032 */ 033 public boolean isValid() { 034 return m_call != 0; 035 } 036 037 /** 038 * Get the RPC entry. 039 * 040 * @return NetworkTableEntry for the RPC. 041 */ 042 public NetworkTableEntry getEntry() { 043 return m_entry; 044 } 045 046 /** 047 * Get the call native handle. 048 * 049 * @return Native handle. 050 */ 051 public int getCall() { 052 return m_call; 053 } 054 055 /** 056 * Get the result (return value). This function blocks until the result is received. 057 * 058 * @return Received result (output) 059 */ 060 public byte[] getResult() { 061 byte[] result = NetworkTablesJNI.getRpcResult(m_entry.getHandle(), m_call); 062 if (result.length != 0) { 063 m_call = 0; 064 } 065 return result; 066 } 067 068 /** 069 * Get the result (return value). This function blocks until the result is received or it times 070 * out. 071 * 072 * @param timeout timeout, in seconds 073 * @return Received result (output) 074 */ 075 public byte[] getResult(double timeout) { 076 byte[] result = NetworkTablesJNI.getRpcResult(m_entry.getHandle(), m_call, timeout); 077 if (result.length != 0) { 078 m_call = 0; 079 } 080 return result; 081 } 082 083 /** Ignore the result. This function is non-blocking. */ 084 public void cancelResult() { 085 NetworkTablesJNI.cancelRpcResult(m_entry.getHandle(), m_call); 086 } 087 088 private final NetworkTableEntry m_entry; 089 private int m_call; 090}