Skip to content
Snippets Groups Projects
ScheduleServer.java 2.73 KiB
Newer Older
  • Learn to ignore specific revisions
  • package com.example.scheduler_server;
    
    
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    
    /*
        Name: ScheduleServer
        Description: Establishes the connection to the database, and opens the  scheduling sever for scheduling clients
        to connect to.
     */
    
    public class ScheduleServer {
    
        private final Set<UserThread> userThreads = new HashSet<>(); // notify when there are changes
    
        /*
        Name: ScheduleServer
        Parameters:
            String ip: The IP address for the server to bind to.
            int port: The port the server will listen to.
            String dbURL: The URL of the database
            String dbUser: The username of the database.
            String dbPass: The password of the database.
        Description: Changes when the shift starts.
        Return: ScheduleServer
         */
    
        public ScheduleServer(String ip, int port, String dbURL, String dbUser, String dbPass) {
    
            this.port = port;
    
        }
    
        public void execute() {
    
            try (ServerSocket serverSocket = new ServerSocket(port, 50, InetAddress.getByName(ip))) {
    
                System.out.println("Server is listening on port " + port);
    
                while (true) {
                    Socket socket = serverSocket.accept();
    
                    System.out.println("New user connected");
    
                    UserThread newUser = new UserThread(socket, this, this.dbQuery);
    
                    userThreads.add(newUser);
                    newUser.start();
                }
            } catch (IOException error) {
                System.out.println("Error in the server :" + error.getMessage());
                error.printStackTrace();
            }
        }
    
    
        /*
        Name: Broadcast
        Parameters:
            String message: Message to be sent to all the clients.
        Description: Sends a message to all current online schedule clients.
        Return: void
         */
    
        void broadcast(String message) {
            for (UserThread aUser : userThreads) {
                aUser.sendMessage(message);
            }
        }
    
    
        /*
        Name: removeUser
        Parameters:
            UserThread aUser: The thread responsible for listening and sending message to a scheduling client.
        Description: Removes the connection of a scheduling client from the server.
        Return: void
         */
    
        void removeUser(UserThread aUser) {
            userThreads.remove(aUser);
        }
    
        public static void main(String[] args) {
    
            //Set "localhost" is working locally. Razer-Blade lan address (NOT static) is "172.16.1.99"
    
    ArktikHunter's avatar
    ArktikHunter committed
            ScheduleServer server = new ScheduleServer("localhost", 8989,
    
                    "jdbc:mysql://localhost:3306/ScheduleApp", "root", "password");
    
            server.execute();
        }
    }