001package edu.wpi.first.wpilibj.networktables; 002 003import java.io.*; 004 005import edu.wpi.first.wpilibj.networktables2.*; 006import edu.wpi.first.wpilibj.networktables2.client.*; 007import edu.wpi.first.wpilibj.networktables2.server.*; 008import edu.wpi.first.wpilibj.networktables2.stream.*; 009import edu.wpi.first.wpilibj.networktables2.thread.*; 010import edu.wpi.first.wpilibj.networktables2.type.*; 011 012/** 013 * 014 * Represents a different modes that network tables can be configured in 015 * 016 * @author Mitchell 017 * 018 */ 019public abstract class NetworkTableMode { 020 021 /** 022 * A mode where Network tables will be a server on the specified port 023 */ 024 public static final NetworkTableMode Server = new NetworkTableMode("Server"){ 025 public NetworkTableNode createNode(String ipAddress, int port, NTThreadManager threadManager) throws IOException { 026 IOStreamProvider streamProvider = SocketStreams.newStreamProvider(port); 027 return new NetworkTableServer(streamProvider, new NetworkTableEntryTypeManager(), threadManager); 028 } 029 }; 030 /** 031 * A mode where network tables will be a client which connects to the specified host and port 032 */ 033 public static final NetworkTableMode Client = new NetworkTableMode("Client"){ 034 public NetworkTableNode createNode(String ipAddress, int port, NTThreadManager threadManager) throws IOException { 035 if(ipAddress==null) 036 throw new IllegalArgumentException("IP address cannot be null when in client mode"); 037 IOStreamFactory streamFactory = SocketStreams.newStreamFactory(ipAddress, port); 038 NetworkTableClient client = new NetworkTableClient(streamFactory, new NetworkTableEntryTypeManager(), threadManager); 039 client.reconnect(); 040 return client; 041 } 042 }; 043 private String name; 044 private NetworkTableMode(String name){ 045 this.name = name; 046 } 047 public String toString(){ 048 return name; 049 } 050 051 /** 052 * @param ipAddress the IP address configured by the user 053 * @param port the port configured by the user 054 * @param threadManager the thread manager that should be used for threads in the node 055 * @return a new node that can back a network table 056 * @throws IOException 057 */ 058 abstract NetworkTableNode createNode(String ipAddress, int port, NTThreadManager threadManager) throws IOException; 059}