Esempio n. 1
0
        /**
         * This method manage the execution of a client request.
         */
        private void manageCommand()
        {
            String cmd = s.receiveString();

            try
            {
                switch (cmd)
                {
                case "getPort\0":
                    Console.WriteLine(cmd);
                    String macaddr = s.receiveString();
                    Console.WriteLine(macaddr);

                    String macToCheck = macaddr.Substring(0, macaddr.Length - 1);

                    int port = startingport + progressiveport;
                    progressiveport++;

                    Boolean ok = db.insertClient(macToCheck, port);
                    if (!ok)
                    {
                        Console.WriteLine("Error: Impossible to save new client in db!");
                        sendPort(-1);
                        return;
                    }

                    sendPort(port);

                    // This method manage the comunication between this client and the server.
                    ClientManager client = new ClientManager(macaddr, port);
                    client.start();

                    Console.WriteLine("Client: " + macaddr + " on port: " + port + " has ended!\n");
                    break;

                default:
                    Console.WriteLine("Error: received unknown request!\nCommand: " + cmd + "\n");
                    break;
                }
            }
            catch (Exception e) {
                Console.WriteLine("Error: manage commeand " + e.Message + "\n");

                if (s != null)
                {
                    s.Close();
                }
            }
        }