Had to redo the voteGetter: now it is handled via userInputListener and messageFormatter
This commit is contained in:
parent
bc136d7b68
commit
9299027bcd
@ -11,6 +11,8 @@ import java.io.*;
|
|||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
@ -24,6 +26,11 @@ public class Client {
|
|||||||
private BufferedWriter out;
|
private BufferedWriter out;
|
||||||
public ClientPinger clientPinger;
|
public ClientPinger clientPinger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the position of the client, gets refreshed everytime the client gets a vote request.
|
||||||
|
*/
|
||||||
|
int position = Integer.MAX_VALUE;
|
||||||
|
|
||||||
public Client(Socket socket) {
|
public Client(Socket socket) {
|
||||||
try {
|
try {
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
@ -59,7 +66,7 @@ public class Client {
|
|||||||
try {
|
try {
|
||||||
if (bfr.ready()) {
|
if (bfr.ready()) {
|
||||||
String msg = bfr.readLine();
|
String msg = bfr.readLine();
|
||||||
String formattedMSG = MessageFormatter.formatMsg(msg);
|
String formattedMSG = MessageFormatter.formatMsg(msg, position);
|
||||||
sendMsgToServer(formattedMSG);
|
sendMsgToServer(formattedMSG);
|
||||||
}
|
}
|
||||||
Thread.sleep(5);
|
Thread.sleep(5);
|
||||||
@ -73,44 +80,27 @@ public class Client {
|
|||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tells user to enter a position to vote for passenger at that position
|
* Tells user to enter a position to vote for passenger at that position
|
||||||
*/
|
*/
|
||||||
|
public void positionSetter(String msg) {
|
||||||
|
|
||||||
public void voteGetter(final String msg) {
|
LOGGER.info("Im in thread:" + Thread.currentThread());
|
||||||
/*TODO(Seraina): Find out what happens if there is no input*/
|
|
||||||
new Thread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
int msgIndex = msg.indexOf('$');
|
int msgIndex = msg.indexOf('$');
|
||||||
String position = msg.substring(0, msgIndex);
|
String pos = msg.substring(0, msgIndex);
|
||||||
String justMsg = msg.substring(msgIndex + 1);
|
|
||||||
Scanner input = new Scanner(System.in);
|
|
||||||
System.out.println(justMsg);
|
|
||||||
try {
|
try {
|
||||||
System.out.println("Please enter your vote");
|
position = Integer.parseInt(pos);
|
||||||
String msgInput = input.nextLine();
|
} catch (NumberFormatException e) {
|
||||||
int vote;
|
LOGGER.warn("Position got scrabbled on the way here");
|
||||||
try {
|
|
||||||
vote = Integer.parseInt(msgInput);
|
|
||||||
LOGGER.info("input is: " + vote);
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOGGER.warn(e.getMessage());
|
|
||||||
System.out.println("Invalid vote");
|
|
||||||
msgInput = String.valueOf(Integer.MAX_VALUE);
|
|
||||||
}
|
}
|
||||||
sendMsgToServer(Protocol.votedFor + "$" + position + "$" + msgInput);
|
String justMsg = msg.substring(msgIndex + 1);
|
||||||
LOGGER.debug(
|
|
||||||
"msg to server is: " + Protocol.votedFor + "$" + position + "$" + msgInput);
|
System.out.println(justMsg);
|
||||||
|
System.out.println("Please enter your vote");
|
||||||
|
|
||||||
Thread.sleep(5);
|
|
||||||
|
|
||||||
//LOGGER.debug("just checked next line");
|
//LOGGER.debug("just checked next line");
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -134,7 +124,7 @@ public class Client {
|
|||||||
} else { System.out.println("chatMsg is null"); throw new IOException();}
|
} else { System.out.println("chatMsg is null"); throw new IOException();}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
//e.printStackTrace();
|
//e.printStackTrace();
|
||||||
LOGGER.debug("Exception while trying to read message: " + e.getMessage());
|
LOGGER.warn("Exception while trying to read message: " + e.getMessage());
|
||||||
disconnectFromServer();
|
disconnectFromServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -45,13 +45,13 @@ public class JClientProtocolParser {
|
|||||||
case Protocol.serverRequestsGhostVote:
|
case Protocol.serverRequestsGhostVote:
|
||||||
LOGGER.debug("Ghost received Vote request");
|
LOGGER.debug("Ghost received Vote request");
|
||||||
System.out.println("Ghost Vote:");
|
System.out.println("Ghost Vote:");
|
||||||
c.voteGetter(msg.substring(6));
|
c.positionSetter(msg.substring(6));
|
||||||
//TODO(Seraina): How can be enforced, that clients won't vote otherwise? Trigger a methode here that listens to input
|
//TODO(Seraina): How can be enforced, that clients won't vote otherwise? Trigger a methode here that listens to input
|
||||||
break;
|
break;
|
||||||
case Protocol.serverRequestsHumanVote:
|
case Protocol.serverRequestsHumanVote:
|
||||||
LOGGER.debug("Human received Vote request");
|
LOGGER.debug("Human received Vote request");
|
||||||
System.out.println("Human Vote:");
|
System.out.println("Human Vote:");
|
||||||
c.voteGetter(msg.substring(6));
|
c.positionSetter(msg.substring(6));
|
||||||
//TODO(Seraina): How can be enforced, that clients won't vote otherwise? Trigger a methode here that listens to input
|
//TODO(Seraina): How can be enforced, that clients won't vote otherwise? Trigger a methode here that listens to input
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
@ -18,7 +18,7 @@ public class MessageFormatter {
|
|||||||
* @return the reformatted message in the form HEADR$msg
|
* @return the reformatted message in the form HEADR$msg
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public static String formatMsg(String msg) {
|
public static String formatMsg(String msg, int position) {
|
||||||
String header = ""; //header is first two characters
|
String header = ""; //header is first two characters
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
String s = ""; // just a friendly helper to save message in
|
String s = ""; // just a friendly helper to save message in
|
||||||
@ -59,10 +59,20 @@ public class MessageFormatter {
|
|||||||
stringBuilder.append(Protocol.listLobbies + "$");
|
stringBuilder.append(Protocol.listLobbies + "$");
|
||||||
s = ""; //Command has no parameters
|
s = ""; //Command has no parameters
|
||||||
break;
|
break;
|
||||||
|
case "/v":
|
||||||
|
try {
|
||||||
|
s = msg.substring(3);
|
||||||
|
LOGGER.debug("substring: " + s);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("invalid vote");
|
||||||
|
}
|
||||||
|
stringBuilder.append(Protocol.votedFor + "$" + position + "$");
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
s = msg;
|
s = msg;
|
||||||
}
|
}
|
||||||
stringBuilder.append(s);
|
stringBuilder.append(s);
|
||||||
|
LOGGER.debug(stringBuilder.toString());
|
||||||
return stringBuilder.toString();
|
return stringBuilder.toString();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user