mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
Initial checkin of unified hierarchy of WPILib 2015
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package edu.wpi.first.wpilibj.networktables2.stream;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import javax.microedition.io.*;
|
||||
|
||||
|
||||
/**
|
||||
* An object that will provide cRIO socket connections when a client connects to the server on the given port
|
||||
*
|
||||
* @author mwills
|
||||
*
|
||||
*/
|
||||
public class SocketConnectionServerStreamProvider implements IOStreamProvider{
|
||||
|
||||
private final ServerSocketConnection server;
|
||||
|
||||
/**
|
||||
* Create a new Stream provider that wraps a Socket Server on the given port
|
||||
* @param port
|
||||
* @throws IOException
|
||||
*/
|
||||
public SocketConnectionServerStreamProvider(final int port) throws IOException{
|
||||
server = (ServerSocketConnection) Connector.open("socket://:" + port);
|
||||
}
|
||||
|
||||
public IOStream accept() throws IOException {
|
||||
SocketConnection socket = (SocketConnection) server.acceptAndOpen();
|
||||
if(socket!=null){
|
||||
socket.setSocketOption(SocketConnection.LINGER, 0);
|
||||
return new SocketConnectionStream(socket);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
server.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package edu.wpi.first.wpilibj.networktables2.stream;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import javax.microedition.io.*;
|
||||
|
||||
/**
|
||||
* A socket connection on the cRIO
|
||||
*
|
||||
* @author mwills
|
||||
*
|
||||
*/
|
||||
public class SocketConnectionStream implements IOStream{
|
||||
|
||||
private final SocketConnection socket;
|
||||
private final InputStream is;
|
||||
private final OutputStream os;
|
||||
|
||||
/**
|
||||
* Create a new IOStream for a socket connection with the given host and port
|
||||
* @param host
|
||||
* @param port
|
||||
* @throws IOException
|
||||
*/
|
||||
public SocketConnectionStream(final String host, final int port) throws IOException {
|
||||
this((SocketConnection) Connector.open("socket://"+host+":"+port));
|
||||
}
|
||||
/**
|
||||
* Create a new IOStream for a socket connection
|
||||
* @param socket
|
||||
* @throws IOException
|
||||
*/
|
||||
public SocketConnectionStream(final SocketConnection socket) throws IOException {
|
||||
this.socket = socket;
|
||||
is = socket.openInputStream();
|
||||
os = socket.openOutputStream();
|
||||
}
|
||||
|
||||
|
||||
public InputStream getInputStream() {
|
||||
return is;
|
||||
}
|
||||
public OutputStream getOutputStream() {
|
||||
return os;
|
||||
}
|
||||
public void close() {
|
||||
try{
|
||||
is.close();
|
||||
} catch(IOException e){
|
||||
//just ignore and close the rest of the stream
|
||||
}
|
||||
try{
|
||||
os.close();
|
||||
} catch(IOException e){
|
||||
//just ignore and close the rest of the stream
|
||||
}
|
||||
try{
|
||||
socket.close();
|
||||
} catch(IOException e){
|
||||
//just ignore and assume socket is now closed
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package edu.wpi.first.wpilibj.networktables2.stream;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author mwills
|
||||
*
|
||||
*/
|
||||
public class SocketConnectionStreamFactory implements IOStreamFactory{
|
||||
|
||||
private final String host;
|
||||
private final int port;
|
||||
|
||||
/**
|
||||
* Create a new factory that will create socket connections with the given host and port
|
||||
* @param host
|
||||
* @param port
|
||||
* @throws IOException
|
||||
*/
|
||||
public SocketConnectionStreamFactory(final String host, final int port) throws IOException {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public IOStream createStream() throws IOException {
|
||||
return new SocketConnectionStream(host, port);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package edu.wpi.first.wpilibj.networktables2.stream;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Static factory for socket stream factories and providers
|
||||
*
|
||||
* @author Mitchell
|
||||
*
|
||||
*/
|
||||
public class SocketStreams {
|
||||
/**
|
||||
* Create a new IOStream factory
|
||||
* @param host
|
||||
* @param port
|
||||
* @return a IOStreamFactory that will create Socket Connections on the given host and port
|
||||
* @throws IOException
|
||||
*/
|
||||
public static IOStreamFactory newStreamFactory(final String host, final int port) throws IOException{
|
||||
return new SocketConnectionStreamFactory(host, port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new IOStream provider
|
||||
* @param port
|
||||
* @return an IOStreamProvider for a socket server on the given port
|
||||
* @throws IOException
|
||||
*/
|
||||
public static IOStreamProvider newStreamProvider(final int port) throws IOException {
|
||||
return new SocketConnectionServerStreamProvider(port);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package java.io;
|
||||
|
||||
public class BufferedInputStream extends InputStream{
|
||||
private final InputStream source;
|
||||
private byte[] buffer;
|
||||
private int pos;//the next position to be read
|
||||
private int maxPos;//the maximum valid position in the buffer + 1
|
||||
|
||||
public BufferedInputStream(InputStream source){
|
||||
this(source, 8192);
|
||||
}
|
||||
public BufferedInputStream(InputStream source, int size){
|
||||
this.source = source;
|
||||
buffer = new byte[size];
|
||||
pos = 0;
|
||||
maxPos = 0;
|
||||
}
|
||||
|
||||
|
||||
private void fillBuffer() throws IOException {
|
||||
int numRemaining = maxPos-pos;
|
||||
System.arraycopy(buffer, pos, buffer, 0, numRemaining);
|
||||
pos = 0;
|
||||
maxPos = numRemaining;
|
||||
int numRead = source.read(buffer, numRemaining, buffer.length-numRemaining);
|
||||
maxPos += numRead;
|
||||
}
|
||||
|
||||
public int read() throws IOException {
|
||||
if(pos<maxPos)
|
||||
return buffer[pos++]&0xFF;//cast to make sure not to return -1 and instead just return lowest byte of int
|
||||
fillBuffer();
|
||||
if(pos<maxPos)
|
||||
return buffer[pos++]&0xFF;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
public int available() throws IOException{
|
||||
return maxPos-pos+source.available();
|
||||
}
|
||||
|
||||
public void close() throws IOException{
|
||||
source.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package java.io;
|
||||
|
||||
public class BufferedOutputStream extends OutputStream{
|
||||
|
||||
private final byte[] buffer;
|
||||
private int pos;
|
||||
private final OutputStream out;
|
||||
|
||||
public BufferedOutputStream(OutputStream out){
|
||||
this(out, 8192);
|
||||
}
|
||||
public BufferedOutputStream(OutputStream out, int size){
|
||||
this.out = out;
|
||||
buffer = new byte[size];
|
||||
pos = 0;
|
||||
}
|
||||
private void flushBuffer() throws IOException{
|
||||
if(pos>0){
|
||||
out.write(buffer, 0, pos);
|
||||
pos = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void write(int b) throws IOException {
|
||||
buffer[pos++] = (byte)b;
|
||||
if(pos>=buffer.length)
|
||||
flushBuffer();
|
||||
}
|
||||
|
||||
public void write(byte b[], int off, int len) throws IOException {
|
||||
if(len>=buffer.length){
|
||||
flushBuffer();
|
||||
out.write(b, off, len);
|
||||
}
|
||||
else{
|
||||
if(len>=(buffer.length-pos))
|
||||
flushBuffer();
|
||||
|
||||
System.arraycopy(b, off, buffer, pos, len);
|
||||
pos += len;
|
||||
}
|
||||
}
|
||||
|
||||
public void flush() throws IOException{
|
||||
flushBuffer();
|
||||
out.flush();
|
||||
}
|
||||
|
||||
public void close() throws IOException{
|
||||
flushBuffer();
|
||||
out.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package edu.wpi.first.wpilibj.networktables2.system;
|
||||
|
||||
import edu.wpi.first.testing.*;
|
||||
import edu.wpi.first.wpilibj.networktables2.server.*;
|
||||
import edu.wpi.first.wpilibj.networktables2.stream.*;
|
||||
import edu.wpi.first.wpilibj.tables.ITable;
|
||||
import edu.wpi.first.wpilibj.tables.ITableListener;
|
||||
import java.io.*;
|
||||
|
||||
public class SystemTest extends TestClass {
|
||||
private static NetworkTableServer staticServer;
|
||||
static{
|
||||
try {
|
||||
staticServer = new NetworkTableServer(SocketStreams.newStreamProvider(1735));
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
NetworkTableServer server;
|
||||
public void before() throws IOException{
|
||||
server = staticServer;
|
||||
staticServer.getEntryStore().clearEntries();
|
||||
}
|
||||
public void after(){
|
||||
//server.close();
|
||||
}
|
||||
|
||||
public void testSimpleBidirectionalPut() throws Exception{
|
||||
sendMessage("server up");
|
||||
|
||||
waitMessage();//Client connected
|
||||
System.out.println("client sent");
|
||||
sleep(100);
|
||||
assertEquals("CValue1-1", server.getString("ClientString1"));
|
||||
assertEquals("CValue2-1", server.getString("ClientString2"));
|
||||
server.putString("ServerString1", "SValue1-1");
|
||||
server.putString("ServerString2", "SValue2-1");
|
||||
sendMessage("server sent");
|
||||
System.out.println("sent server");
|
||||
|
||||
waitMessage();//Client sent
|
||||
System.out.println("client sent");
|
||||
sleep(100);
|
||||
assertEquals("CValue1-2", server.getString("ClientString1"));
|
||||
assertEquals("CValue2-2", server.getString("ClientString2"));
|
||||
server.putString("ServerString1", "SValue1-2");
|
||||
server.putString("ServerString2", "SValue2-2");
|
||||
sendMessage("server sent");
|
||||
System.out.println("sent server");
|
||||
|
||||
waitMessage();//test complete
|
||||
}
|
||||
public void testRapidServerPutSingleKey() throws Exception{
|
||||
sendMessage("server up");
|
||||
|
||||
waitMessage();//Client connected
|
||||
|
||||
|
||||
for(int i = 0; i<10000; ++i)
|
||||
server.putString("ServerKey", "SValue"+i);
|
||||
|
||||
sleep(100);
|
||||
sendMessage("server done");
|
||||
|
||||
waitMessage();//test complete
|
||||
}
|
||||
public void testRapidServerPutMultiKey() throws Exception{
|
||||
sendMessage("server up");
|
||||
|
||||
waitMessage();//Client connected
|
||||
|
||||
|
||||
for(int i = 0; i<1000; ++i)
|
||||
for(int j = 0; j<100; ++j)
|
||||
server.putString("ServerKey"+j, "SValue"+i);
|
||||
|
||||
sleep(100);
|
||||
sendMessage("server done");
|
||||
|
||||
waitMessage();//test complete
|
||||
}
|
||||
public void testPeriodicServerPutMultiKey() throws Exception{
|
||||
sendMessage("server up");
|
||||
|
||||
waitMessage();//Client connected
|
||||
|
||||
|
||||
for(int i = 0; i<100; ++i){
|
||||
for(int j = 0; j<100; ++j)
|
||||
server.putString("ServerKey"+j, "SValue"+i);
|
||||
sleep(20);
|
||||
}
|
||||
sleep(100);
|
||||
|
||||
sendMessage("server done");
|
||||
|
||||
waitMessage();//test complete
|
||||
}
|
||||
public void testBiDirectionalStress() throws Exception{
|
||||
server.addTableListener(new ITableListener() {
|
||||
|
||||
public void valueChanged(ITable source, String key, Object value, boolean isNew) {
|
||||
int prefixIndex = key.indexOf("/client/");
|
||||
if(prefixIndex == 0) {
|
||||
String name = key.substring("/client/".length());
|
||||
server.putDouble("/server/" + name, ((Double) value).doubleValue());
|
||||
}
|
||||
}
|
||||
}, false);
|
||||
sendMessage("Server ready");
|
||||
waitMessage(); // Client connected
|
||||
|
||||
waitMessage(); // Client done
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user