Exemplo n.º 1
0
            private PRSMessage portdead(PRSMessage Request)
            {
                ManagedPort p = find_port(Request.port);

                p.reserved = false;
                Console.WriteLine("Marked port dead:" + p.port);
                return(PRSMessage.MakeRESPONSE(PRSMessage.Status.SUCCESS, Request.port));
            }
Exemplo n.º 2
0
            private PRSMessage lookup_port(PRSMessage Request)
            {
                ManagedPort p = find_service(Request.serviceName);//find the service in the list

                if (p == null)
                {
                    return(PRSMessage.MakeRESPONSE(PRSMessage.Status.SERVICE_NOT_FOUND, Request.port)); //if the service isnt in the list return not found
                }
                return(PRSMessage.MakeRESPONSE(PRSMessage.Status.SUCCESS, (ushort)p.port));             //if it is return success with port number
            }
Exemplo n.º 3
0
            List <ManagedPort> Ports;                                        //list of ports

            public PRSHandler(int S, int E, int T)                           //Constructor
            {
                startingClientPort = S;                                      //sets the starting clientport
                endingClientPort   = E;                                      //sets the ending clientport
                keepAlive          = T;                                      //sets the keep alive time
                Ports = new List <ManagedPort>();                            //creates a new list of ports
                for (int p = startingClientPort; p <= endingClientPort; p++) //fills the list
                {
                    ManagedPort mp = new ManagedPort();
                    mp.port     = p;
                    mp.reserved = false;

                    Ports.Add(mp);
                }
            }
Exemplo n.º 4
0
            private PRSMessage keep_alive(PRSMessage Request)
            {
                //check if the service and port are both valid and match
                ManagedPort n = find_service(Request.serviceName);//checks if the service is valid

                if (n == null)
                {
                    return(PRSMessage.MakeRESPONSE(PRSMessage.Status.SERVICE_NOT_FOUND, Request.port));
                }
                ManagedPort p = find_port(Request.port);//Checks if the port is valid

                if (p == null || p != n)
                {
                    return(PRSMessage.MakeRESPONSE(PRSMessage.Status.INVALID_ARG, Request.port));
                }
                p.lastAlive = DateTime.Now;
                return(PRSMessage.MakeRESPONSE(PRSMessage.Status.SUCCESS, Request.port));
            }
Exemplo n.º 5
0
            private PRSMessage close_port(PRSMessage Request)
            {
                //check if port and name are accurate
                ManagedPort n = find_service(Request.serviceName);//checks if the service is valid

                if (n == null)
                {
                    return(PRSMessage.MakeRESPONSE(PRSMessage.Status.SERVICE_NOT_FOUND, Request.port));
                }
                ManagedPort p = find_port(Request.port);//Checks if the port is valid

                if (p == null || p != n)
                {
                    return(PRSMessage.MakeRESPONSE(PRSMessage.Status.INVALID_ARG, Request.port));
                }
                //Actually close port
                p.serviceName = null;
                p.reserved    = false;
                return(PRSMessage.MakeRESPONSE(PRSMessage.Status.SUCCESS, Request.port));
            }
Exemplo n.º 6
0
            private PRSMessage request_port(PRSMessage Request)
            {
                //check if service in use
                ManagedPort sn = find_service(Request.serviceName);                                  //if the service is in the list

                if (sn != null)                                                                      //check to see if it was found in the list
                {
                    return(PRSMessage.MakeRESPONSE(PRSMessage.Status.SERVICE_IN_USE, Request.port)); //if it was its already in use
                }
                foreach (ManagedPort p in Ports)                                                     //for each port in the list
                {
                    if (!p.reserved && p.serviceName == null)                                        //check if its reserved
                    {
                        p.reserved    = true;                                                        //reserve it
                        p.serviceName = Request.serviceName;                                         //set the service name
                        p.lastAlive   = DateTime.Now;                                                //update the time
                        return(PRSMessage.MakeRESPONSE(PRSMessage.Status.SUCCESS, (ushort)p.port));  //return success with port number
                    }
                }
                return(PRSMessage.MakeRESPONSE(PRSMessage.Status.ALL_PORTS_BUSY, Request.port));
            }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            // TODO: interpret cmd line options

            /*
             * -p <service port>
             * -s <starting client port number>
             * -e <ending client port number>
             * -t <keep alive time in seconds>
             */

            int servicePort        = 30000;
            int startingClientPort = 40000;
            int endingClientPort   = 40099;
            int keepAlive          = 300;

            // initialize a collection of un-reserved ports to manage
            List <ManagedPort> ports = new List <ManagedPort>();

            for (int p = startingClientPort; p <= endingClientPort; p++)
            {
                ManagedPort mp = new ManagedPort();
                mp.port     = p;
                mp.reserved = false;

                ports.Add(mp);
            }

            // create the socket for receiving messages at the server
            Socket listeningSocket = new Socket(SocketType.Dgram, ProtocolType.Udp);

            Console.WriteLine("Listening socket created");

            // bind the socket to the server port
            listeningSocket.Bind(new IPEndPoint(IPAddress.Any, servicePort));
            Console.WriteLine("Listening socket bound to port " + servicePort.ToString());

            // listen for client messages
            bool done = false;

            while (!done)
            {
                try
                {
                    // receive a message from a client
                    IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
                    PRSMessage msg      = PRSCommunicator.ReceiveMessage(listeningSocket, ref remoteEP);

                    // handle the message
                    PRSMessage response = null;
                    switch (msg.msgType)
                    {
                    case PRSMessage.MsgType.REQUEST_PORT:
                        Console.WriteLine("Received REQUEST_PORT message");
                        response = Handle_REQUEST_PORT(msg);
                        break;

                    case PRSMessage.MsgType.STOP:
                        Console.WriteLine("Received STOP message");
                        done = true;
                        break;

                    default:
                        // TODO: handle unknown message type!
                        break;
                    }

                    if (response != null)
                    {
                        // send response message back to client
                        PRSCommunicator.SendMessage(listeningSocket, remoteEP, response);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception when receiving..." + ex.Message);
                }
            }

            // close the socket and quit
            Console.WriteLine("Closing down");
            listeningSocket.Close();
            Console.WriteLine("Closed!");

            Console.ReadKey();
        }