michael changes

main
mike 2023-04-15 18:23:30 -04:00
parent 49169c3bdd
commit a4eec6ecc6
3 changed files with 13 additions and 5 deletions

View File

@ -57,7 +57,7 @@ public class GameEngine implements Runnable {
adapter.attack(map); adapter.attack(map);
} }
private Map generateInitialMap(){ public Map generateInitialMap(){
return new Map(new CasaDeNarino(1, VillageHallStages.villageStages[0]), 30); return new Map(new CasaDeNarino(1, VillageHallStages.villageStages[0]), 30);
} }

View File

@ -16,5 +16,7 @@ public class PacketTable {
public static final byte ACK = 0x3; public static final byte ACK = 0x3;
// messageHeader, UTF8 String with length information (use DOS.writeUTF/DIS.readUTF) // messageHeader, UTF8 String with length information (use DOS.writeUTF/DIS.readUTF)
public static final byte MESSAGE = 0x4; public static final byte MESSAGE = 0x4;
// messageHeader, serial packets with map info
public static final byte MAP_DATA = 0x5;
} }

View File

@ -1,11 +1,15 @@
package ca.cosc3p91.a4.util.network; package ca.cosc3p91.a4.util.network;
import ca.cosc3p91.a4.game.GameEngine; import ca.cosc3p91.a4.game.GameEngine;
import ca.cosc3p91.a4.game.Map;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.rmi.ServerException; import java.rmi.ServerException;
import java.util.*;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
public class Server implements Runnable { public class Server implements Runnable {
@ -49,7 +53,7 @@ public class Server implements Runnable {
// the server must handle connection requests while the client's processing thread will handle all other messages // the server must handle connection requests while the client's processing thread will handle all other messages
if (packetID == PacketTable.CONNECT){ if (packetID == PacketTable.CONNECT){
clients.put(++clientAssignmentID, new ConnectedClient(socket, clientID, messageID, receivePacket.getAddress(), receivePacket.getPort())); clients.put(++clientAssignmentID, new ConnectedClient(socket, mainEngine, clientID, messageID, receivePacket.getAddress(), receivePacket.getPort()));
} else if (packetID == PacketTable.DISCONNECT) { } else if (packetID == PacketTable.DISCONNECT) {
if (client == null) if (client == null)
throw new ServerException("Client disconnected with invalid client id! (" + clientID + ")"); throw new ServerException("Client disconnected with invalid client id! (" + clientID + ")");
@ -85,12 +89,14 @@ public class Server implements Runnable {
private final long clientID; private final long clientID;
private volatile boolean running = true; private volatile boolean running = true;
private final Thread processingThread; private final Thread processingThread;
private final Map clientMap;
public ConnectedClient(DatagramSocket serverSocket, long clientID, long messageID, InetAddress address, int port){ public ConnectedClient(DatagramSocket serverSocket, GameEngine engine, long clientID, long messageID, InetAddress address, int port){
this.serverSocket = serverSocket; this.serverSocket = serverSocket;
this.address = address; this.address = address;
this.port = port; this.port = port;
this.clientID = clientID; this.clientID = clientID;
this.clientMap = engine.generateInitialMap();
processingThread = new Thread(this); processingThread = new Thread(this);
processingThread.start(); processingThread.start();
@ -133,7 +139,7 @@ public class Server implements Runnable {
} }
requestLock.unlock(); requestLock.unlock();
for (Map.Entry<Long, Message.Sent> message : sentMessages.entrySet()){ for (HashMap.Entry<Long, Message.Sent> message : sentMessages.entrySet()){
if (message.getValue().getTimeSinceSent().get() > MAX_PACKET_ACK_TIME_SECONDS) { if (message.getValue().getTimeSinceSent().get() > MAX_PACKET_ACK_TIME_SECONDS) {
System.out.println("The server did not process our message, did they receive it?"); System.out.println("The server did not process our message, did they receive it?");
// todo: resend message // todo: resend message