Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.polsl.java.kamil.zietek.lab4.network;
- import java.io.*;
- import java.net.*;
- import pl.polsl.java.kamil.zietek.lab4.model.Database;
- /**
- * The server class servicing a single connection
- *
- * @author Kamil Zietek
- * @version 4.0
- */
- class SingleService implements Runnable {
- /**
- * socket representing connection to the client
- */
- private final Socket socket;
- /**
- * buffered input character stream
- */
- private final BufferedReader inFromClient;
- /**
- * Formatted output character stream
- */
- private final PrintWriter outToClient;
- /**
- * Reference to database
- */
- private Database database;
- /**
- * The constructor of instance of the SingleService class.
- *
- * @param socket socket representing connection to the client
- * @param database reference to database
- * @throws IOException ioexception
- */
- public SingleService(Socket socket, Database database) throws IOException {
- this.socket = socket;
- this.database = database;
- outToClient = new PrintWriter(
- new BufferedWriter(
- new OutputStreamWriter(
- socket.getOutputStream())), true);
- inFromClient = new BufferedReader(
- new InputStreamReader(
- socket.getInputStream()));
- }
- /**
- * Realizes the service
- */
- @Override
- public void run() {
- try {
- System.out.println(new java.util.Date().toString());
- System.out.print("Client connected: ");
- System.out.println(Thread.currentThread().getName());
- boolean quit = false;
- while (!quit) {
- String str = inFromClient.readLine();
- str = str.toUpperCase();
- System.out.print(Thread.currentThread().getName());
- System.out.println(" sent command " + str);
- switch (str) {
- case "BUY_TICKET":
- buyTicketCommand(); //5
- break;
- case "CHECK_CONNECTION":
- outToClient.println("OK");
- break;
- case "CHECK_DATABASE":
- if (database == null) {
- outToClient.println("NO_DATABASE");
- } else {
- outToClient.println("OK");
- }
- break;
- case "DRAWING_RESULTS":
- drawingResultsCommand(); //6
- break;
- case "GET_NUMBER_OF_PLAYERS":
- outToClient.println("OK");
- outToClient.println(database.getNumberOfPlayers());
- break;
- case "GET_PLAYER_TICKET":
- getPlayerTicketCommand(); //4
- break;
- case "GET_PLAYERS":
- getPlayersCommand(); //3
- break;
- case "HELP": //1
- helpCommand();
- break;
- case "NEW_DRAWING":
- database.newDrawing();
- outToClient.println("OK");
- break;
- case "QUIT":
- quit = true;
- outToClient.println("OK");
- break;
- case "REGISTER":
- registerCommand(); //2
- break;
- default:
- System.out.println("Unknown command: " + str);
- outToClient.println("UNKNOWN_COMMAND");
- break;
- }
- }
- } catch (IOException | NullPointerException e) {
- System.err.println(e.getMessage());
- } finally {
- System.out.print("Client disconnected: ");
- System.out.println(Thread.currentThread().getName());
- try {
- socket.close();
- } catch (IOException e) {
- System.err.println(e.getMessage());
- }
- }
- }
- /**
- * List of available commands, sent to client
- */
- private void helpCommand() {
- outToClient.print("Available commands: ");
- outToClient.print("BUY_TICKET [ID of player]");
- outToClient.print("CHECK_CONNECTION, ");
- outToClient.print("CHECK_DATABASE, [Last drawing or all drawings]");
- outToClient.print("DRAWING_RESULTS, ");
- outToClient.print("GET_NUMBER_OF_PLAYERS, ");
- outToClient.print("GET_PLAYER_TICKET [ID of player], ");
- outToClient.print("GET_PLAYERS, "); //getPlayersCommand
- outToClient.print("HELP, ");
- outToClient.print("NEW_DRAWING, ");
- outToClient.print("QUIT, ");
- outToClient.print("REGISTER [Number of players, NAME, SURNAME and AGE for each player"); //registerCommand
- outToClient.println();
- }
- /**
- * Registration of players. First value after REGISTER command must be
- * integer with number of players
- */
- private void registerCommand() {
- if (database == null) {
- outToClient.println("OK");
- Database tmp = null;
- try {
- String str = inFromClient.readLine();
- System.out.print(Thread.currentThread().getName());
- System.out.println(" sent number of players to register: " + str);
- tmp = new Database(Integer.parseInt(str));
- for (int i = 0; i < tmp.getNumberOfPlayers(); i++) {
- outToClient.println("OK_GIVE_NAME");
- String name = inFromClient.readLine();
- outToClient.println("OK_GIVE_SURNAME");
- String surname = inFromClient.readLine();
- outToClient.println("OK_GIVE_AGE");
- int age = Integer.parseInt(inFromClient.readLine());
- tmp.addNewPlayer(i, name, surname, age);
- outToClient.println("OK_ADDED");
- }
- } catch (IOException | NumberFormatException ex) {
- outToClient.println("ERROR");
- System.out.print(Thread.currentThread().getName());
- System.out.println(" get error when registering players");
- }
- database = tmp;
- outToClient.println("REGISTER_SUCCESS");
- System.out.print(Thread.currentThread().getName());
- System.out.println(" succesfully registered players.");
- } else {
- outToClient.println("ERROR_ALREADY_REGISTERED");
- System.out.println("Already registered");
- }
- }
- /**
- * All players to client sender, first value after GET_PLAYERS command must
- * be ID of specified player
- */
- private void getPlayersCommand() {
- outToClient.println("OK");
- outToClient.println(database.getNumberOfPlayers());
- boolean finish = false;
- int i = 0;
- while (!finish) {
- try {
- String str = inFromClient.readLine();
- switch (str) {
- case "OK_GIVE_NAME":
- outToClient.println(database.getPlayers()[i].getName());
- break;
- case "OK_GIVE_SURNAME":
- outToClient.println(database.getPlayers()[i].getSurname());
- break;
- case "OK_GIVE_AGE":
- outToClient.println(database.getPlayers()[i].getAge());
- break;
- case "OK_GIVE_CREDITS":
- outToClient.println(database.getPlayers()[i].getCredits());
- break;
- case "OK_GET":
- i++;
- break;
- case "GET_PLAYERS_SUCCESS":
- finish = true;
- System.out.print(Thread.currentThread().getName());
- System.out.println(" succesfully sent players.");
- break;
- }
- } catch (IOException e) {
- System.err.println(e.getMessage());
- System.out.print(Thread.currentThread().getName());
- System.out.println(" get error when sending players");
- }
- }
- }
- /**
- * Specified player's ticket sender,
- */
- private void getPlayerTicketCommand() {//ex
- outToClient.println("OK");
- try {
- int i = Integer.parseInt(inFromClient.readLine());
- if (database.getPlayers()[i].getTicket().getResults()[0] == 0) {
- outToClient.println("NO_TICKET");
- } else {
- String ticket = null;
- for (int j = 0; j < database.getPlayers()[i].getTicket().getResults().length; j++) {
- if (j == 0) {
- ticket = database.getPlayers()[i].getTicket().getResults()[j] + ", ";
- } else {
- ticket += database.getPlayers()[i].getTicket().getResults()[j] + ", ";
- }
- }
- outToClient.println(ticket);
- }
- if (inFromClient.readLine().equals("GIVE_CREDITS")) {
- outToClient.println(database.getPlayers()[i].getCredits());
- } else {
- outToClient.println("ERROR");
- }
- } catch (IOException | NumberFormatException e) {
- System.err.println(e.getMessage());
- }
- }
- /**
- * Buy ticket for specified player
- */
- private void buyTicketCommand() {
- outToClient.println("OK");
- try {
- int i = Integer.parseInt(inFromClient.readLine());
- if (database.buyLotteryTicket(i)) {
- outToClient.println("OK_BOUGHT");
- } else {
- outToClient.println("ERROR_BUYING");
- }
- } catch (IOException | NumberFormatException e) {
- System.err.println(e.getMessage());
- }
- }
- /**
- * Send all drawing results to client
- */
- private void drawingResultsCommand() {
- outToClient.println("OK");
- String str, tmp;
- boolean finish = false;
- while (!finish) {
- try {
- str = inFromClient.readLine();
- switch (str) {
- case "GET_LAST_DRAWING":
- int[] results = database.getDrawings().get(database.getDrawings().size() - 1).getResults();
- tmp = "Results of drawing #" + database.getDrawings().size() + '\n';
- for (int res : results) {
- tmp += res + ", ";
- }
- tmp += '\n';
- for (int i = 0; i < database.getNumberOfPlayers(); i++) {
- int reward = database.checkPlayerReward(i);
- if (reward > 0) {
- tmp += "Player #" + (i + 1) + " hit " + reward + " numbers!\n";
- } else if (reward == 0) {
- tmp += "Player #" + (i + 1) + " hasn't hit any numbers!\n";
- } else if (reward < 0) {
- tmp += "Player #" + (i + 1) + " hasn't bought ticket to this drawing!\n";
- }
- }
- outToClient.println(tmp);
- outToClient.println("LAST_DRAWING_SENT");
- break;
- case "GET_ALL_DRAWINGS":
- tmp = "Results of all drawings:\n";
- for (int i = database.getDrawings().size() - 1; i >= 0; i--) {
- tmp += "Drawing #" + (i + 1) + ": ";
- for (int res : database.getDrawings().get(i).getResults()) {
- tmp += res + ", ";
- }
- tmp += '\n';
- }
- outToClient.println(tmp);
- outToClient.println("ALL_DRAWINGS_SENT");
- break;
- case "DRAWINGS_REFRESH_SUCCESS":
- finish = true;
- break;
- }
- } catch (IOException e) {
- System.err.println(e.getMessage());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement