Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/main/java/com/lucwo/fourcharm/server/ClientGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public ClientGroup() {
clientCollection = new HashMap<>();
}

/**
* Checks if the name of the client already exists.
*
* @param name the name that will be checked
* @return true if the name already exists, false if not
*/
public boolean clientNameExists(String name) {
return clientCollection.keySet().contains(name);
}
Expand Down Expand Up @@ -68,7 +74,6 @@ public Collection<ClientHandler> getClients() {
/**
* Handles the join command from a client. The implementation is left to Classes
* which extend ClientGroup.
*
* @param client The client which performed this command.
* @param pName Player name
* @param gNumber Group number
Expand All @@ -77,13 +82,24 @@ public Collection<ClientHandler> getClients() {
public abstract void join(ClientHandler client, String pName, int gNumber,
Set<Extension> exts) throws C4Exception;

/**
* Makes a move
* @param client the client that will make the move
* @param col the column the move is about
* @throws C4Exception
*/
public abstract void doMove(ClientHandler client, int col) throws C4Exception;


/**
* If the client is ready to start a game, this method will be called.
* @param client the client that is ready to start a game
* @throws C4Exception
*/
public abstract void ready(ClientHandler client) throws C4Exception;

/**
* Handles the removal of a client from the group.
*
* @param client The {@link com.lucwo.fourcharm.server.ClientHandler} which will has been removed from the group.
*/
public abstract void removeClientCallback(ClientHandler client);
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/lucwo/fourcharm/server/ClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public void setName(String naam) {

/**
* Gives the client.
*
* @return the client
*/
public CoreClient.Client getClient() {
Expand Down Expand Up @@ -112,6 +111,13 @@ public void doMove(int col) throws C4Exception {
group.doMove(this, col);
}

/**
* Gives an error.
*
* @param errorCode the code of the error
* @param message the message about the error
* @throws C4Exception
*/
@Override
public void error(int errorCode, String message) throws C4Exception {
Logger.getGlobal().info(errorCode + " " + message);
Expand All @@ -133,6 +139,9 @@ public void run() {
handleClient();
}

/**
* TODO: Javadoc schrijven voor handleClient()
*/
public void handleClient() {

final String m_name = "handleClient";
Expand Down
38 changes: 34 additions & 4 deletions src/main/java/com/lucwo/fourcharm/server/FourCharmServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,30 @@ public class FourCharmServer {
private ClientGroup preLobby;
private Collection<GameGroup> games;
private boolean running;
private int poort;

public FourCharmServer(int port) {
lobby = new LobbyGroup(this);
preLobby = new PreLobbyGroup(lobby, this);
games = new ArrayList<>();
running = true;
startServer(port);
poort = port;
}

public static void main(String[] args) {

new FourCharmServer(8080);
FourCharmServer server = new FourCharmServer(8080);
server.startServer();

}

/**
* Checks if there is already a client with the same name.
*
* @param name the name that will be checked
* @return true if there exists another client with the same name,
* false if there does not exist another client with the same name.
*/
public boolean hasClientWithName(String name) {
boolean nameExistsinGame = false;
for (ClientGroup cG : games) {
Expand All @@ -44,12 +53,16 @@ public boolean hasClientWithName(String name) {
return nameExistsinGame || lobby.clientNameExists(name);
}

public void startServer(int port) {
/**
* Starts the server.
*/
public void startServer() {

Logger.getGlobal().info("Starting Fourcharm server");


try {
ServerSocket ss = new ServerSocket(port);
ServerSocket ss = new ServerSocket(poort);
while (running) {
Socket sock = ss.accept();
ClientHandler client = new ClientHandler(sock);
Expand All @@ -66,18 +79,35 @@ public void startServer(int port) {

}

/**
* Adds a game to the GameGroup
*
* @param game the game that will be added
*/
public void addGame(GameGroup game) {
games.add(game);
}

/**
* Removes a game from the GameGroup whenever a game is finished,
* or when a game stopped.
* @param game the game that will be removed
*/
public void removeGame(GameGroup game) {
games.remove(game);
}

/**
* Makes sure the server will stop.
*/
public void stop() {
running = false;
}

/**
* Gives the specific lobby.
* @return the lobby you've asked for
*/
public ClientGroup getLobby() {
return lobby;
}
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/lucwo/fourcharm/server/GameGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ public void join(ClientHandler client, String pName, int gNumber, Set<Extension>
throw new InvalidCommandError("You are not allowed to use this command now.");
}

/**
* Makes a move for a specific client
*
* @param client the client that is about to make a move
* @param col the column the move will be about
* @throws C4Exception
*/
@Override
public void doMove(ClientHandler client, int col) throws C4Exception {

Expand Down Expand Up @@ -105,7 +112,6 @@ public void ready(ClientHandler client) throws C4Exception {

/**
* Handles the removal of a client from the group.
*
* @param client The {@link com.lucwo.fourcharm.server.ClientHandler} which will has been removed from the group.
*/
@Override
Expand All @@ -121,12 +127,18 @@ public void removeClientCallback(ClientHandler client) {
endGame();
}

/**
* Starts a new game.
*/
public void startGame() {

Thread gameThread = new Thread(game);
gameThread.start();
}

/**
* Ends a game that started before.
*/
private void endGame() {

String winnerName = null;
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/lucwo/fourcharm/server/LobbyGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public LobbyGroup(FourCharmServer theServer) {

/**
* ServerHandler wants to go from the PreLobbyGroup to the LobbyGroup.
*
* @param client The client which performed this command.
* @param pName Player name
* @param gNumber Group number
Expand All @@ -45,7 +44,6 @@ public void join(ClientHandler client, String pName, int gNumber, Set<Extension>

/**
* Makes a move if a client is in a game. This is not allowed in the LobbyGroup.
*
* @param client the client that wants to make a move.
* @param col the column number the clients wants to use to make a move
* @throws C4Exception throws InvalidCommandError because this command is not allowed
Expand All @@ -59,7 +57,6 @@ public void doMove(ClientHandler client, int col) throws C4Exception {
/**
* Sets the status of the player to ready. If there is another player ready as well,
* a new game will be started.
*
* @param client the client that wants to play a game
* @throws C4Exception
*/
Expand All @@ -82,7 +79,6 @@ public synchronized void ready(ClientHandler client) throws C4Exception {

/**
* Handles the removal of a client from the group.
*
* @param client The {@link com.lucwo.fourcharm.server.ClientHandler} which will has been removed from the group.
*/
@Override
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/lucwo/fourcharm/server/PreLobbyGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public void join(ClientHandler client, String pName, int gNumber,

/**
* Makes a move.
*
* @param client the client that makes a move
* @param col the column the move is about
* @throws C4Exception the client is not playing a game and is not allowed to make a move
Expand All @@ -68,7 +67,6 @@ public void doMove(ClientHandler client, int col) throws C4Exception {
/**
* Sets the clients status to 'ready' and starts a game if there is another client
* that is ready as well.
*
* @param client the client that wants to play a game
* @throws C4Exception the client is not allowed to play a game in the PreLobbyGroup
*/
Expand All @@ -80,7 +78,6 @@ public void ready(ClientHandler client) throws C4Exception {

/**
* Handles the removal of a client from the group.
*
* @param client The {@link com.lucwo.fourcharm.server.ClientHandler} which will has been removed from the group.
*/
@Override
Expand Down
115 changes: 115 additions & 0 deletions src/test/java/com/lucwo/fourcharm/server/ClientHandlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (c) 2015. Luce Sandfort and Wouter Timmermans
*/

package com.lucwo.fourcharm.server;

import nl.woutertimmermans.connect4.protocol.exceptions.InvalidCommandError;
import org.junit.Before;
import org.junit.Test;

import java.security.cert.Extension;
import java.util.Set;

import static org.junit.Assert.assertEquals;

public class ClientHandlerTest {

ClientHandler clientH;
ClientHandler clientH2;
ClientHandler clientH3;
ClientGroup clientLobbyGroup;
ClientGroup clientPreLobbyGroup;
// ClientGroup clientGameGroup;
FourCharmServer theServer;
Set<Extension> exts;



@Before
public void setUp() throws Exception {
clientH = new ClientHandler(null);
clientH2 = new ClientHandler(null);
clientH3 = new ClientHandler(null);
clientH.setName("Wouter");
clientPreLobbyGroup = new PreLobbyGroup(clientLobbyGroup, theServer);
clientLobbyGroup = new LobbyGroup(theServer);
clientH2.setClientGroup(clientLobbyGroup);
// clientGameGroup = new GameGroup(theServer, clientH, clientH3);
}

@Test
public void testGetName() throws Exception {
assertEquals("Test to see if GetName() works", "Wouter", clientH.getName());
}

@Test
public void testSetName() throws Exception {
clientH.setName("Luce");
assertEquals("Test to see if SetName() works", "Luce", clientH.getName());
}

@Test
public void testGetClient() throws Exception {

//TODO: getClient vergelijken met een client
// assertEquals("Test to see if getClient() works", , clientH3.getClient());

}

@Test
public void testGetClientGroup() throws Exception {
assertEquals("Test to see if getting a GlientGroup works", clientLobbyGroup, clientH2.getClientGroup());
}

@Test
public void testSetClientGroup() throws Exception {
clientH2.setClientGroup(clientPreLobbyGroup);
assertEquals("Test to see if setting a ClientGroup to pre lobby group works", clientPreLobbyGroup, clientH2.getClientGroup());
clientH2.setClientGroup(clientLobbyGroup);
assertEquals("Test to see if setting a ClientGroup to lobby group works", clientLobbyGroup, clientH2.getClientGroup());

//TODO: setclientgroup to gamegroup
/* clientH2.setClientGroup(clientGameGroup);
clientH3.setClientGroup(clientGameGroup);
assertEquals("Test to see ifs setting a clientGroup to game group works", clientGameGroup, clientH2.getClientGroup());*/
}

/* @Test
public void testJoin() throws Exception {
// clientPreLobbyGroup.join(clientH3, clientH3.getName(), 23, exts);


}

@Test
public void testReady() throws Exception {

clientLobbyGroup.ready(clientH3);
}

@Test
public void testDoMove() throws Exception {

//Game group?
}*/

@Test(expected = InvalidCommandError.class)
public void testError() throws Exception {
clientPreLobbyGroup.doMove(clientH3, 5);

}

@Test
public void run() throws Exception {

//TODO: run() test schrijven
handleClient();
}

@Test
public void handleClient() throws Exception {

//TODO: handleClient() test schrijven
}
}
Loading