working client
parent
6a559bc5ab
commit
49169c3bdd
|
@ -8,6 +8,8 @@ import java.io.IOException;
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class Client implements Runnable {
|
public class Client implements Runnable {
|
||||||
private GameDisplay view = new GameDisplay();
|
private GameDisplay view = new GameDisplay();
|
||||||
|
@ -15,26 +17,27 @@ public class Client implements Runnable {
|
||||||
private DatagramSocket clientSocket;
|
private DatagramSocket clientSocket;
|
||||||
private boolean running = true;
|
private boolean running = true;
|
||||||
private Thread receiveThread;
|
private Thread receiveThread;
|
||||||
|
private final HashMap<Long, Message.Sent> sentMessages = new HashMap<>();
|
||||||
|
private int lastMessageID = 0;
|
||||||
|
private final InetAddress serverAddress;
|
||||||
|
|
||||||
public Client(String address) throws IOException {
|
public Client(String address) throws IOException {
|
||||||
InetAddress serverAddress = InetAddress.getByName(address);
|
serverAddress = InetAddress.getByName(address);
|
||||||
clientSocket = new DatagramSocket();
|
clientSocket = new DatagramSocket();
|
||||||
receiveThread = new Thread(this);
|
receiveThread = new Thread(this);
|
||||||
receiveThread.start();
|
receiveThread.start();
|
||||||
|
|
||||||
|
sendMessage(new Message.Sent(PacketTable.CONNECT, 0, ++lastMessageID));
|
||||||
|
|
||||||
String prompt;
|
String prompt;
|
||||||
byte[] sendData = new byte[1024];
|
|
||||||
byte[] receiveData = new byte[1024];
|
|
||||||
while (running) {
|
while (running) {
|
||||||
if ((prompt = view.nextInput()) != null) {
|
if ((prompt = view.nextInput()) != null) {
|
||||||
if (!prompt.isEmpty() && prompt.charAt(0) == '6') break;
|
if (prompt.trim().isEmpty())
|
||||||
sendData = prompt.getBytes();
|
continue;
|
||||||
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
|
if (prompt.charAt(0) == '6')
|
||||||
clientSocket.send(sendPacket);
|
break;
|
||||||
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
|
|
||||||
clientSocket.receive(receivePacket);
|
|
||||||
String serverOutput = new String(receivePacket.getData()).trim();
|
|
||||||
System.out.println(">" + serverOutput);
|
|
||||||
view.printGameMenu();
|
view.printGameMenu();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,11 +57,34 @@ public class Client implements Runnable {
|
||||||
long clientID = stream.readLong();
|
long clientID = stream.readLong();
|
||||||
long messageID = stream.readLong();
|
long messageID = stream.readLong();
|
||||||
|
|
||||||
|
switch (packetID) {
|
||||||
|
case PacketTable.ACK:
|
||||||
|
Message.Sent message = sentMessages.get(messageID);
|
||||||
|
if (message == null)
|
||||||
|
throw new RuntimeException("Server message sync error!");
|
||||||
|
message.acknowledged();
|
||||||
|
sentMessages.remove(messageID);
|
||||||
|
System.out.println("Message acknowledged " + messageID);
|
||||||
|
break;
|
||||||
|
case PacketTable.DISCONNECT:
|
||||||
|
running = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void sendMessage(Message.Sent message){
|
||||||
|
sentMessages.put(message.getMessageID(), message);
|
||||||
|
byte[] data = message.getData().toByteArray();
|
||||||
|
DatagramPacket sendPacket = new DatagramPacket(data, data.length, serverAddress, Server.SERVER_PORT);
|
||||||
|
try {
|
||||||
|
clientSocket.send(sendPacket);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,10 +66,10 @@ public class Message {
|
||||||
* @param writer stream to write to
|
* @param writer stream to write to
|
||||||
* @param data byte array stream which contains the byte[] used in packet construction
|
* @param data byte array stream which contains the byte[] used in packet construction
|
||||||
*/
|
*/
|
||||||
public Sent(byte packetID, long clientID, long messageID, DataOutputStream writer, ByteArrayOutputStream data) {
|
public Sent(byte packetID, long clientID, long messageID) {
|
||||||
super(packetID, clientID, messageID);
|
super(packetID, clientID, messageID);
|
||||||
this.writer = writer;
|
this.data = new ByteArrayOutputStream();
|
||||||
this.data = data;
|
this.writer = new DataOutputStream(this.data);
|
||||||
timeSent = Time.getTime();
|
timeSent = Time.getTime();
|
||||||
// write the header to the stream, make sure you don't write into the stream before constructing this!
|
// write the header to the stream, make sure you don't write into the stream before constructing this!
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -94,9 +94,7 @@ public class Server implements Runnable {
|
||||||
processingThread = new Thread(this);
|
processingThread = new Thread(this);
|
||||||
processingThread.start();
|
processingThread.start();
|
||||||
|
|
||||||
ByteArrayOutputStream bstream = new ByteArrayOutputStream();
|
sendMessage(new Message.Sent(PacketTable.ACK, clientID, messageID));
|
||||||
DataOutputStream stream = new DataOutputStream(bstream);
|
|
||||||
sendMessage(new Message.Sent(PacketTable.ACK, clientID, messageID, stream, bstream));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleRequest(Message.Received request){
|
public void handleRequest(Message.Received request){
|
||||||
|
@ -129,8 +127,10 @@ public class Server implements Runnable {
|
||||||
while (running){
|
while (running){
|
||||||
// handle request processing without blocking the I/O thread
|
// handle request processing without blocking the I/O thread
|
||||||
requestLock.lock();
|
requestLock.lock();
|
||||||
Message.Received request = pendingRequests.remove();
|
if (!pendingRequests.isEmpty()) {
|
||||||
processRequest(request);
|
Message.Received request = pendingRequests.remove();
|
||||||
|
processRequest(request);
|
||||||
|
}
|
||||||
requestLock.unlock();
|
requestLock.unlock();
|
||||||
|
|
||||||
for (Map.Entry<Long, Message.Sent> message : sentMessages.entrySet()){
|
for (Map.Entry<Long, Message.Sent> message : sentMessages.entrySet()){
|
||||||
|
|
Loading…
Reference in New Issue