Datasets:
ArXiv:
License:
File size: 6,658 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
package battleship.Networking;
import battleship.DataPackage.Data;
import battleship.DataPackage.DataConverter;
import battleship.Logic.GameLogic;
import java.net.*;
import java.io.*;
import java.util.Enumeration;
import java.util.Vector;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Server implements Runnable {
private ServerSocket sSocket = null;
private GameLogic gameLogic;
private int clientID = 0;
private List<String>[] queueArray;
private boolean close = false;
public void addMessageToQueue(String message, int ID) {
if (ID != -1) {
queueArray[ID].add(message);
}
}
public void close(){
close = true;
try {
sSocket.close();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Server(int port) {
try {
queueArray = new Vector[2];
for (int i = 0; i < 2; ++i) {
queueArray[i] = new Vector<>();
}
sSocket = new ServerSocket(port);
gameLogic = new GameLogic();
Thread threadQueuePoll = new Thread(() -> {
while (!close) {
try {
Thread.sleep(10);
while (!gameLogic.messageQueue.isEmpty()) {
String BroadcastMessage = gameLogic.messageQueue.get(0);
gameLogic.messageQueue.remove(0);
if (BroadcastMessage != null) {
Data decoded = DataConverter.decode(BroadcastMessage);
int recipient = decoded.getRecipientID();
addMessageToQueue(BroadcastMessage, recipient);
}
}
} catch (InterruptedException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
threadQueuePoll.start();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
private void ServeClient() {
Thread thread = new Thread(() -> {
while (!close) {
try {
Socket socket = sSocket.accept();
BufferedReader bfr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
if (bfr.readLine().equals("PING")){
socket.close();
continue;
}
Integer ID = clientID++;
System.out.println("Someone joined the server with ID: " + ID);
int otherQueueID = (ID == 0) ? 1 : 0;
int ownQueueID = (ID == 0) ? 0 : 1;
addMessageToQueue(ID + "$ConnectionData$$" + ((ID.equals(0)) ? 1 : 0), otherQueueID);
bfw.write(ID.toString());
bfw.newLine();
bfw.flush();
Thread threadReader = new Thread(() -> {
while (!close) {
try {
String inMsg = bfr.readLine();
if (inMsg != null) {
if (inMsg.equals("$DisconnectData$$-1")){
int recipient = (ID == 0) ? 1 : 0;
inMsg = "-1$ChatData$The other player has left the game.$" + recipient;
}
gameLogic.processMessage(DataConverter.decode(inMsg));
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
break;
}
}
});
threadReader.start();
while (!close) {
Thread.sleep(10);
while (!queueArray[ownQueueID].isEmpty()) {
String message = queueArray[ownQueueID].get(0);
queueArray[ownQueueID].remove(0);
bfw.write(message);
bfw.newLine();
bfw.flush();
}
}
} catch (IOException ex) {
System.out.println("Player disconnected");
--clientID;
} catch (InterruptedException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
thread.start();
}
public static String getLocalIP() {
try {
Enumeration e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
NetworkInterface n = (NetworkInterface) e.nextElement();
Enumeration ee = n.getInetAddresses();
while (ee.hasMoreElements()) {
InetAddress i = (InetAddress) ee.nextElement();
if (i.getHostAddress().split("\\.")[0].equals("192")) {
return i.getHostAddress();
}
}
}
} catch (SocketException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
return "NO LOCAL IP FOUND";
}
public static boolean isServerAvailable(String ip, int port){
boolean isAvailable;
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), 1000);
isAvailable = true;
BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bfw.write("PING");
bfw.newLine();
bfw.flush();
socket.close();
}
catch (IOException ex) {
isAvailable = false;
}
return isAvailable;
}
@Override
public void run() {
ServeClient();
ServeClient();
ServeClient();
System.out.println(getLocalIP());
}
}
|