Skip to content

Commit

Permalink
XML server configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
SharpShooter17 committed Nov 6, 2017
1 parent 7c98451 commit a540cd5
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 12 deletions.
Binary file added Chess Game.lnk
Binary file not shown.
Binary file added Server.lnk
Binary file not shown.
6 changes: 3 additions & 3 deletions src/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private synchronized Request getrequest(){

@SuppressWarnings("unchecked")
private void handleTheRequest(Request request){
System.out.println("Request");
//System.out.println("Request");
switch (request.getCodeRequest()){
case 500: //lobby list
SceneController.getSceneController().getLobbyViewController().addLobby(requestGetList(request));
Expand Down Expand Up @@ -149,7 +149,7 @@ private synchronized void addRequest(Request request){
private void readRequest() throws ClassNotFoundException, IOException{
if (this.socket.getInputStream().available() > 0) {
Request request = (Request) this.in.readObject();
System.out.println("Client get code: " + request.getCodeRequest() );
//System.out.println("Client get code: " + request.getCodeRequest() );
addRequest(request);
}
}
Expand All @@ -160,7 +160,7 @@ public void closeConnection(){
}

public void sendRequest(Request request){
System.out.println("Send request. Code: " + request.getCodeRequest());
//System.out.println("Send request. Code: " + request.getCodeRequest());
try {
this.out.reset();
this.out.writeObject(request);
Expand Down
18 changes: 15 additions & 3 deletions src/client/Controller.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
package client;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Controller {
private Client client;

public Controller() throws UnknownHostException {
client = new Client(InetAddress.getLocalHost(), 62000);
public Controller() throws IOException {
String ip = loadIpAddress();
client = new Client(InetAddress.getByName(ip), 62000);
}
public Client getClient() {
return client;
}
private String loadIpAddress() throws IOException{
File f = new File(this.getClass().getResource("data/config.txt").getFile());
BufferedReader in = new BufferedReader( new FileReader(f) );
String res = in.readLine();
in.close();

return res;
}
}
1 change: 1 addition & 0 deletions src/client/data/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
127.0.0.1
2 changes: 1 addition & 1 deletion src/server/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void run(){
this.close();
return;
} else {
System.out.println("Request code: " + request.getCodeRequest());
//System.out.println("Request code: " + request.getCodeRequest());
this.receiver.addRequest(request, this);
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/server/Listener.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import java.io.IOException;
import java.net.ServerSocket;

import javafx.application.Platform;

public class Listener {
public final static int SERVER_PORT = 62000;

private Server server;
Listener(){
server = new Server();
Expand All @@ -17,7 +13,7 @@ public void run(){
server.start();
try {
@SuppressWarnings("resource")
ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
ServerSocket serverSocket = new ServerSocket(ServerConfiguration.getInstance().getPort());
while (true) {
Client client = new Client(server, serverSocket.accept());
server.addClient(client);
Expand Down
45 changes: 45 additions & 0 deletions src/server/ServerConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package server;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

public class ServerConfiguration {
private static ServerConfiguration instance = null;

private int port;

private ServerConfiguration(){
this.load();
}

static ServerConfiguration getInstance(){
if (!(instance instanceof ServerConfiguration)){
instance = new ServerConfiguration();
}
return ServerConfiguration.instance;
}

private void load(){

try {
File fXmlFile = new File(this.getClass().getResource("data/config.xml").getFile());
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

this.port = Integer.parseInt( doc.getElementsByTagName("port").item(0).getTextContent() );

} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}

int getPort(){
return this.port;
}
}
1 change: 1 addition & 0 deletions src/server/data/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<port>62000</port>

0 comments on commit a540cd5

Please sign in to comment.