Added test server for testing
This commit is contained in:
@@ -33,12 +33,12 @@ public class FdaqEContainer implements EContainer {
|
||||
private Fdaq fdaq;
|
||||
private EventBus bus;
|
||||
|
||||
private FdaqEDescriptor request;
|
||||
private FdaqEDescriptor edescriptor;
|
||||
|
||||
@Inject
|
||||
public FdaqEContainer(FdaqEDescriptor edescriptor, EventBus ebus){
|
||||
this.bus = ebus;
|
||||
this.request = edescriptor;
|
||||
this.edescriptor = edescriptor;
|
||||
}
|
||||
|
||||
|
||||
@@ -54,7 +54,10 @@ public class FdaqEContainer implements EContainer {
|
||||
|
||||
fdaq = new Fdaq(bus, new FdaqConfiguration());
|
||||
|
||||
SerializerTXT serializer = new SerializerTXT(new File(request.getFileName()));
|
||||
File file = new File(edescriptor.getFileName());
|
||||
file.getParentFile().mkdirs(); // Create data base directory
|
||||
|
||||
SerializerTXT serializer = new SerializerTXT(file);
|
||||
serializer.setShowDimensionHeader(false);
|
||||
bus.register(serializer);
|
||||
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
/**
|
||||
*
|
||||
* Copyright 2013 Paul Scherrer Institute. All rights reserved.
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU Lesser General Public License as published by the Free
|
||||
* Software Foundation, either version 3 of the License, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but without any
|
||||
* warranty; without even the implied warranty of merchantability or fitness for
|
||||
* a particular purpose. See the GNU Lesser General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this code. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package ch.psi.fda.fdaq;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Simple socket server emulating the fdaq black box.
|
||||
*/
|
||||
public class FdaqServer {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(FdaqServer.class.getName());
|
||||
|
||||
private volatile boolean stop = false;
|
||||
|
||||
public void startKillSocket(int port) {
|
||||
try {
|
||||
ServerSocket serverSocket = new ServerSocket(port);
|
||||
Socket socket = serverSocket.accept();
|
||||
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
|
||||
DataInputStream in = new DataInputStream(socket.getInputStream());
|
||||
|
||||
byte[] requestBuffer = new byte[1 * 4]; // 2 times integers
|
||||
ByteBuffer bytebuffer = ByteBuffer.wrap(requestBuffer);
|
||||
bytebuffer.order(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
in.read(requestBuffer);
|
||||
|
||||
bytebuffer.getInt();
|
||||
|
||||
logger.info("Terminate sender");
|
||||
stop=true;
|
||||
|
||||
out.close();
|
||||
in.close();
|
||||
socket.close();
|
||||
serverSocket.close();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void startSenderSocket(int port) {
|
||||
stop = false;
|
||||
try {
|
||||
ServerSocket serverSocket = new ServerSocket(port);
|
||||
Socket socket = serverSocket.accept();
|
||||
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
|
||||
DataInputStream in = new DataInputStream(socket.getInputStream());
|
||||
|
||||
byte[] requestBuffer = new byte[2 * 4]; // 2 times integers
|
||||
ByteBuffer bytebuffer = ByteBuffer.wrap(requestBuffer);
|
||||
bytebuffer.order(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
in.read(requestBuffer);
|
||||
|
||||
bytebuffer.getInt(); // This will always be 26
|
||||
int nMessages = bytebuffer.getInt(); // Contains number of images to
|
||||
// receive
|
||||
|
||||
// System.out.println("Messages to send: "+nMessages);
|
||||
|
||||
for (int t = 0; t < nMessages; t++) {
|
||||
|
||||
if(stop){ // Terminate loop if kill socket got a request
|
||||
break;
|
||||
}
|
||||
// System.out.println(t+" "+0+" "+0+" "+ (-t));
|
||||
|
||||
byte[] dataBuffer = new byte[4 * 4]; // 4 times Integers
|
||||
ByteBuffer buffer = ByteBuffer.wrap(dataBuffer); // 4 times
|
||||
// Integers
|
||||
buffer.order(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
buffer.putInt(t);
|
||||
buffer.putInt(0);
|
||||
buffer.putInt(0);
|
||||
buffer.putInt(-t);
|
||||
|
||||
out.write(dataBuffer);
|
||||
out.flush();
|
||||
|
||||
Thread.sleep(10);
|
||||
}
|
||||
|
||||
out.close();
|
||||
in.close();
|
||||
socket.close();
|
||||
serverSocket.close();
|
||||
} catch (IOException | InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
|
||||
final FdaqConfiguration configuration = new FdaqConfiguration();
|
||||
final FdaqServer server = new FdaqServer();
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
server.startSenderSocket(configuration.getPort());
|
||||
}
|
||||
}).start();
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
server.startKillSocket(configuration.getKillPort());
|
||||
}
|
||||
}).start();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,11 +41,13 @@ public class SocketClient {
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
|
||||
// Socket echoSocket = new Socket("localhost", 9999);
|
||||
Socket echoSocket = new Socket("mchip015", 2233);
|
||||
// Socket echoSocket = new Socket("mchip015", 2233);
|
||||
Socket echoSocket = new Socket("localhost", 2233);
|
||||
DataOutputStream out = new DataOutputStream(echoSocket.getOutputStream());
|
||||
DataInputStream in = new DataInputStream(echoSocket.getInputStream());
|
||||
|
||||
int nMessages = 100;
|
||||
// int nMessages = 100;
|
||||
int nMessages = Integer.MAX_VALUE;
|
||||
|
||||
// struct fdaqbloc_in {int fnum;int nsample;};
|
||||
ByteBuffer bytebuffer = ByteBuffer.allocate(2*4); // 2 times Integers
|
||||
@@ -75,7 +77,7 @@ public class SocketClient {
|
||||
int c2 = (c>>16)&0xffff;
|
||||
int d = buffer.getInt();
|
||||
|
||||
System.out.println(a+ " " +b1+ " " +b2+ " " + c1+ " " +c2+ " " +d );
|
||||
System.out.println( a+ " " +b1+ " " +b2+ " " + c1+ " " +c2+ " " +d );
|
||||
}
|
||||
|
||||
out.close();
|
||||
|
||||
@@ -39,7 +39,8 @@ public class SocketClientStop {
|
||||
*/
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
|
||||
Socket echoSocket = new Socket("localhost", 9998);
|
||||
// Socket echoSocket = new Socket("localhost", 9998);
|
||||
Socket echoSocket = new Socket("localhost", 2234);
|
||||
DataOutputStream out = new DataOutputStream(echoSocket.getOutputStream());
|
||||
|
||||
ByteBuffer bytebuffer = ByteBuffer.allocate(1*4); // 2 times Integers
|
||||
|
||||
Reference in New Issue
Block a user