Commit e36f83d9 authored by Xiaoying Pu's avatar Xiaoying Pu

conflict resolved

parents e96b04c3 0c195680
......@@ -18,6 +18,7 @@ public class TestUDPController {
UDPController cntrl = new UDPController();
cntrl.setUp();
cntrl.sendTrialAndRecvResponse("DAF");
//for (int i = 0; i < util.UDPConstants.TrialSet.length; i++) {
// not useable any more because of the interface change
// cntrl.sendTrialAndRecvResponse(i);
......
......@@ -58,6 +58,7 @@ public class UDPHandler {
String messageOut = "";
try {
// create socket
//TODO: Socket creation failec here
DatagramSocket socket = new DatagramSocket(port);
socket.setSoTimeout(udpTimeout);
socket.setReuseAddress(true);
......
......@@ -266,9 +266,9 @@ public class Decision {
public String getBreak() {
List<String> result = new ArrayList<>();
// type pause only needs three col's
// type pause only needs three col's
result.add(typePause);
result.add("Now take a break...");
result.add("break.jpg");
result.add("PAUSE");
return String.join(",", result);
}
......
......@@ -56,7 +56,11 @@ public class MouseTrackerFacade {
this.theController.endDecision();
System.out.println("Let's say the decision is done");
handleEvent(EventType.DECISION_FINISHED);
//handleEvent(EventType.DECISION_FINISHED);
}
public void endExperiment() {
this.theController.endEntireExperiment();
}
public void addListener(EventListener listener) {
......
......@@ -14,7 +14,7 @@ import util.WLState;
*/
public class PrimaryTaskController {
private final boolean turnUDPOn = true; // TODO what an ugly flag
private final boolean turnUDPOn = true;
private final PrimaryTaskModel theModel;
private UDPController udpHandle;
/**
......@@ -60,22 +60,32 @@ public class PrimaryTaskController {
}
public void startDecision(WLState state) {
Runnable task = () -> {
if (timeIsUp) {
timeIsUp = false;
}
while (!timeIsUp) {
String msg = this.theModel.getDecision(DecisionType.GETMIXATYPICAL, state.toString());
this.udpHandle.sendTrialAndRecvResponse(msg);
System.out.println("Decision started, waiting for response.. ");
}
while (!this.timeIsUp) {
String msg = this.theModel.getDecision(DecisionType.GETMIXATYPICAL, state.toString());
this.udpHandle.sendTrialAndRecvResponse(msg);
System.out.println("Decision started, waiting for response.. ");
}
//after some time, the facade interrupts and MouseTracker displays a break screen
// while the user recalls digits
// TODO: break also needs to wait for response message??????
String breakMsg = this.theModel.getDecision(DecisionType.GETBREAK, state.toString());
// Note: cannot use sendTrialAndRecvResponse because the recvfrom() will not hear back!!!!
this.udpHandle.sendBreakScreen(breakMsg);
};
new Thread(task).start();
}
//after some time, the facade interrupts and MouseTracker displays a break screen
// while the user recalls digits
// TODO: break also needs to wait for response message??????
String breakMsg = this.theModel.getDecision(DecisionType.GETBREAK, state.toString());
this.udpHandle.sendTrialAndRecvResponse(breakMsg);
public void endEntireExperiment() {
this.udpHandle.endExperiment();
}
public void endDecision() {
this.timeIsUp = false;
this.timeIsUp = true;
}
public void connect() {
......@@ -106,7 +116,7 @@ public class PrimaryTaskController {
// eventListenerList.stream().forEach((listener) -> {
// System.out.println("hey UDPListner is working\n");
// // TODO or type, right?
// // should have multiple MT trials run
// // should have multiple MT trials run
// // before telling the MouseTrackerFacade that it's done
// // like a counter here
// listener.run(EventType.END_MULTI_DECISION);
......
......@@ -2,6 +2,7 @@ package tracker;
import java.util.Arrays;
import network.UDPHandler;
import util.DecisionType;
import util.EventListener;
import util.EventType;
import util.Persistence;
......@@ -20,6 +21,11 @@ import static util.UDPConstants.UDP_TIMEOUT;
*/
public class UDPController {
/**
* As opposed to a list of listeners because UDPController only needs one
* anyways. Remember to initialize it upstream of the UDPController, i.e.,
* the PrimaryTaskController.
*/
private EventListener theListener;
private String message_received = "";
private String messageToSend = "";
......@@ -29,6 +35,7 @@ public class UDPController {
public UDPController() {
this.persisHandle = new Persistence("recv.csv", HEADER);
//setUp(); NOTE: should not be called at init
}
public void addListener(EventListener l) {
......@@ -85,6 +92,23 @@ public class UDPController {
System.out.println("<<<" + message_received);
}
/**
* Only used to make MouseTracker display break / pause screen
*
* @param stimuli related to {@link DecisionType.GETBREAK}
*/
public void sendBreakScreen(String stimuli) {
/*
STEP 4: MATLAB sends trial information
*/
messageToSend = INTERFACE_ID + "," + MESSAGE_ID + "," + stimuli;
UDPHandler.send(messageToSend, HOST, SEND_PORT);
MESSAGE_ID++;
System.out.println(">>>" + messageToSend);
}
/**
* Loop between Step 4-5 for every trial (Send Stimuli to MouseTracker and
* Receive Responses)
......@@ -112,7 +136,7 @@ public class UDPController {
Object[] data = UDPHandler.receive(HOST, RECEIVE_PORT, UDP_TIMEOUT, PACKET_LENGTH);
message_received = (String) data[0];
}
this.theListener.run(EventType.UDPMSG_FROM_MT);
//this.theListener.run(EventType.UDPMSG_FROM_MT);
// TODO: What is this doing????
String[] tempParts = message_received.split(",");
......
......@@ -41,7 +41,7 @@ public class Trial extends Thread {
this.theListener = new ResponseListener();
this.sFacade.addListener(theListener);
this.pFacade.addListener(theListener);
this.iterations = new AtomicInteger(0);
this.maxIteration = Configurator.getnTrials();
}
......@@ -52,22 +52,26 @@ public class Trial extends Thread {
@Override
public void run() {
while (true) {
ProtoAction ac = actionQueue.poll();
if (ac != null) {
if (ac.getType() == ActionType.QUIT_TRIAL) {
return;
Runnable task = () -> {
while (true) {
ProtoAction ac = actionQueue.poll();
if (ac != null) {
if (ac.getType() == ActionType.QUIT_TRIAL) {
ac.run();
return;
}
System.out.println("-------------------An action says that it's runnning");
System.out.println(ac.getType());
ac.run();
}
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Trial.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("-----------------------------I am running");
System.out.println(ac.getType());
ac.run();
}
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Trial.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
new Thread(task).start();
}
/**
......@@ -77,14 +81,18 @@ public class Trial extends Thread {
@Override
public void run(EventType type) {
System.out.println("Response Listener: " + type.toString());
switch (type) {
// Now the Trial should decide when the decision is done
// more of an action than response to listen to ...
case DECISION_FINISHED:
sFacade.startRecallingDigit();
pFacade.endDecision();
break;
case DIGITINPUT_FINISHED:
//TODO how does trial know??????????????
// TODO who calls the listener in this case?
// TODO: the counter may be happier if it lived here
actionQueue.add(new ShowDigitAction(ActionType.START_HWL_DIGIT));
break;
......@@ -120,6 +128,7 @@ public class Trial extends Thread {
@Override
public void run() {
pFacade.endExperiment();
}
}
......@@ -131,16 +140,20 @@ public class Trial extends Thread {
@Override
public void run() {
System.out.println("SLEEP starts at -------->" + System.currentTimeMillis() / 1000L);
int tDecisions = Configurator.gettDecisions();
try {
Thread.sleep(tDecisions);
} catch (InterruptedException ex) {
Logger.getLogger(Dm_task.class.getName()).log(Level.SEVERE, null, ex);
}
// WHY IS THIS LINE FULL
//pFacade.endDecision();
System.out.println("SLEEP ends at -------->" + System.currentTimeMillis() / 1000L);
pFacade.endDecision();
// own goal
theListener.run(EventType.DECISION_FINISHED);
}
}
......@@ -159,9 +172,12 @@ public class Trial extends Thread {
// check if there's yet another iteration
if (iterations.addAndGet(1) >= maxIteration) {
System.out.println("Interrupting show of digit task because enough trials have been run");
actionQueue.add(new QuitAction(ActionType.QUIT_TRIAL));
return;
}
System.out.println("====================AT Trial " + iterations.get() + "======================");
// how ugly is this
if (type == ActionType.START_HWL_DIGIT
|| type == ActionType.START_LWL_DIGIT) {
......@@ -175,6 +191,9 @@ public class Trial extends Thread {
sFacade.showDigit(util.WLState.LOW_WORKLOAD);
}
// start UDP to MouseTracker
// go look at the mousetracker!!!
actionQueue.add(new SleepForDecisions(ActionType.SLEEP_FOR_DECISIONS));
pFacade.startAndWait(curState);
}
int tTask = Configurator.gettSTask();
......@@ -184,12 +203,11 @@ public class Trial extends Thread {
} catch (InterruptedException ex) {
Logger.getLogger(Dm_task.class.getName()).log(Level.SEVERE, null, ex);
}
// immediately after
// immediately after
sFacade.endDigitShowing();
System.out.println("Digit should go away now");
// yooo end digit showing
// go look at the mousetracker!!!
actionQueue.add(new SleepForDecisions(ActionType.SLEEP_FOR_DECISIONS));
}
}
}
......@@ -21,8 +21,8 @@ public class UDPConstants {
*/
public static int NUM_TRIALS = 10;
public static int RECEIVE_PORT = 9223; // recv for this prgm, output port for MouseTracker
public static int SEND_PORT = 9001; // input port for mousetracker
public static int RECEIVE_PORT = 8001; // recv for this prgm, output port for MouseTracker
public static int SEND_PORT = 8000; // input port for mousetracker
public static String HOST = "127.0.0.1";// TO MouseTracker
public static int UDP_TIMEOUT = 20 * 1000; // in millisecond
public static int PACKET_LENGTH = 512;
......
......@@ -9,7 +9,7 @@ tSTask = 1000
tPause = 4000
tDecisions = 30000
tDecisions = 10000
nTrials = 10
nTrials = 3
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment