Implemented starting everything from NightTrainToBudapest class.
This commit is contained in:
@@ -7,6 +7,7 @@ import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ClientPinger;
|
||||
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.multiplayer.server.JServerProtocolParser;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.io.*;
|
||||
import java.net.UnknownHostException;
|
||||
@@ -31,7 +32,7 @@ public class Client {
|
||||
int position = Integer.MAX_VALUE;
|
||||
|
||||
|
||||
public Client(Socket socket) {
|
||||
public Client(Socket socket, String username) {
|
||||
try {
|
||||
this.socket = socket;
|
||||
this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
@@ -39,12 +40,16 @@ public class Client {
|
||||
|
||||
//sending the initial name to server.
|
||||
String systemName;
|
||||
try {
|
||||
systemName = System.getProperty("user.name");
|
||||
} catch (Exception e) {
|
||||
systemName = "U.N. Owen";
|
||||
if (username == null) {
|
||||
try {
|
||||
systemName = System.getProperty("user.name");
|
||||
} catch (Exception e) {
|
||||
systemName = "U.N. Owen";
|
||||
}
|
||||
if (systemName == null) systemName = "U.N. Owen";
|
||||
} else {
|
||||
systemName = username;
|
||||
}
|
||||
if (systemName == null) systemName = "U.N. Owen";
|
||||
sendMsgToServer(Protocol.clientLogin + "$" + systemName);
|
||||
|
||||
clientPinger = new ClientPinger(this, this.socket);
|
||||
@@ -183,7 +188,7 @@ public class Client {
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
String hostname;
|
||||
int port = 42069; //can be set via argument later if needed.
|
||||
int port = 1873;
|
||||
if (args.length < 1) {
|
||||
System.out.println("Enter the host's IP address (or type l for localhost)");
|
||||
hostname = sc.next();
|
||||
@@ -196,7 +201,7 @@ public class Client {
|
||||
Socket socket;
|
||||
try {
|
||||
socket = new Socket(hostname, 42069);
|
||||
Client client = new Client(socket);
|
||||
Client client = new Client(socket, null);
|
||||
client.chatListener();
|
||||
Thread cP = new Thread(client.clientPinger);
|
||||
cP.start();
|
||||
@@ -209,6 +214,22 @@ public class Client {
|
||||
|
||||
}
|
||||
|
||||
public static void main(InetAddress address, int port, String username) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
Socket socket;
|
||||
try {
|
||||
socket = new Socket(address, port);
|
||||
Client client = new Client(socket, username);
|
||||
client.chatListener();
|
||||
Thread cP = new Thread(client.clientPinger);
|
||||
cP.start();
|
||||
client.userInputListener(); //this one blocks.
|
||||
} catch (UnknownHostException e) {
|
||||
System.out.println("Invalid host IP");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public Socket getSocket() {
|
||||
return socket;
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
package ch.unibas.dmi.dbis.cs108.multiplayer.client;
|
||||
|
||||
public class Client_01 {
|
||||
public static void main(String[] args) {
|
||||
Client.main(args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package ch.unibas.dmi.dbis.cs108.multiplayer.client;
|
||||
|
||||
public class Client_02 {
|
||||
public static void main(String[] args) {
|
||||
Client.main(args);
|
||||
}
|
||||
}
|
||||
@@ -86,7 +86,7 @@ public class Protocol {
|
||||
public static final String clientQuitRequest = "QUITR";
|
||||
|
||||
/**
|
||||
* Client sends this message when they want to create a new game.
|
||||
* Client sends this message when they want to create a new lobby (& automatically join it).
|
||||
* Client issues this command in {@link ch.unibas.dmi.dbis.cs108.multiplayer.client.MessageFormatter}
|
||||
* using "/g".
|
||||
* First a lobby {@link Lobby} is created of which the requesting client is the admin of.
|
||||
@@ -94,7 +94,7 @@ public class Protocol {
|
||||
public static final String createNewLobby = "CRTLB";
|
||||
|
||||
/**
|
||||
* Represents a clients' request for a list of lobbies
|
||||
* Represents a clients' request for a list of lobbies, plus what players are in them.
|
||||
*/
|
||||
public static final String listLobbies = "LISTL";
|
||||
|
||||
|
||||
@@ -115,6 +115,7 @@ public class Lobby {
|
||||
*/
|
||||
public synchronized boolean addPlayer(ClientHandler client) {
|
||||
if (lobbyClients.size() < MAX_NO_OF_CLIENTS) {
|
||||
//todo: check that game hasn't started yet
|
||||
if (clientIsInLobby(client) == -1) {
|
||||
lobbyClients.add(client);
|
||||
ClientHandler.broadcastAnnouncementToAll(client.getClientUserName() + " has joined lobby " + this.getLobbyID());
|
||||
|
||||
@@ -14,7 +14,7 @@ public class Server {
|
||||
public static final Logger LOGGER = LogManager.getLogger();
|
||||
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
|
||||
|
||||
private static final int gamePort = 42069;
|
||||
private static int gamePort;
|
||||
private HashSet<ClientHandler> connectedClients = new HashSet<>();
|
||||
private ServerSocket serverSocket;
|
||||
Scanner sc = new Scanner(System.in);
|
||||
@@ -28,7 +28,7 @@ public class Server {
|
||||
*/
|
||||
public void startServer() {
|
||||
try {
|
||||
System.out.println("Port 42069 is open.");
|
||||
System.out.println("Port " + gamePort + " is open.");
|
||||
while (!serverSocket.isClosed()) {
|
||||
Socket socket = serverSocket.accept();
|
||||
ClientHandler nextClient = new ClientHandler(socket, socket.getInetAddress());
|
||||
@@ -53,6 +53,19 @@ public class Server {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ServerSocket serverSocket = null;
|
||||
gamePort = 1873;
|
||||
try {
|
||||
serverSocket = new ServerSocket(gamePort);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Server server = new Server(serverSocket);
|
||||
server.startServer();
|
||||
}
|
||||
|
||||
public static void main(int port) {
|
||||
gamePort = port;
|
||||
ServerSocket serverSocket = null;
|
||||
try {
|
||||
serverSocket = new ServerSocket(gamePort);
|
||||
|
||||
Reference in New Issue
Block a user