Esempio n. 1
0
        public List <string> RequestFlightIDs()
        {
            try
            {
                IPHostEntry ipHostDetails    = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress   ipAddressDetails = ipHostDetails.AddressList[0];
                IPEndPoint  localEndPoint    = new IPEndPoint(ipAddressDetails, 4242);

                Socket sender = new Socket(ipAddressDetails.AddressFamily,
                                           SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    DataItem request        = new DataItem("RequestFlightList");
                    string   serialisedItem = DataItemSerialisation.GetSerialisedDataItem(request);
                    sender.Connect(localEndPoint);

                    // Send data request for Flight IDs to server
                    byte[] messageToSend = Encoding.ASCII.GetBytes(serialisedItem + "<EOF>");
                    int    byteSent      = sender.Send(messageToSend);

                    // Data buffer
                    byte[] messageReceived = new byte[4096];

                    // Recieve answer from server
                    int    byteRecv = sender.Receive(messageReceived);
                    string response = Encoding.ASCII.GetString(messageReceived, 0, byteRecv);

                    FlightItemList flightItemList = FlightItemListSerialisation.GetDataItem(response);


                    return(flightItemList.FlightIDs);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Unexpected exception : {0}", e.ToString());
                    return(new List <string>());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(new List <string>());
            }
        }
        /// <summary>
        /// Run a DataServer instance.
        /// </summary>
        public void Run()
        {
            IPHostEntry ipHostDetails    = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress   ipAddressDetails = ipHostDetails.AddressList[0];
            IPEndPoint  localEndPoint    = new IPEndPoint(ipAddressDetails, 4242);

            Socket listenerSocket = new Socket(ipAddressDetails.AddressFamily,
                                               SocketType.Stream, ProtocolType.Tcp);

            try
            {
                listenerSocket.Bind(localEndPoint);
                listenerSocket.Listen(10);

                while (true)
                {
                    WriteLine("Listening for data ");
                    Socket clientSocket = listenerSocket.Accept();

                    // Data buffer
                    byte[] bytes = new Byte[4096];
                    string data  = null;
                    // Get the data from the server
                    while (true)
                    {
                        int numberOfBytes = clientSocket.Receive(bytes);
                        data += Encoding.ASCII.GetString(bytes, 0, numberOfBytes);
                        if (data.IndexOf("<EOF>", StringComparison.Ordinal) > -1)
                        {
                            break;
                        }
                    }
                    string   serialisedXml = data.Substring(0, data.Length - 5);
                    DataItem dataItem      = DataItemSerialisation.GetDataItem(serialisedXml);
                    WriteLine("Text received -> {0} ", dataItem.package);
                    string[] packageItems = dataItem.package.Split(',');

                    if (packageItems[0] == "RequestFlightList")
                    //Chain of Responsibility
                    {
                        List <string> FlightIDList = new List <string>();

                        foreach (Flight flight in FlightList.Flights)
                        {
                            FlightIDList.Add(flight.FlightID);
                        }

                        FlightItemList response       = new FlightItemList(FlightIDList);
                        string         serialisedItem = FlightItemListSerialisation.GetSerialisedDataItem(response);
                        byte[]         message        = Encoding.ASCII.GetBytes(serialisedItem);

                        clientSocket.Send(message);
                        //Recieved and responds to quest for Flight list.
                    }
                    else if (packageItems[0] == "NoOfFlights")
                    {
                        DataItem response       = new DataItem((FlightList.Flights.Count - 1).ToString());
                        string   serialisedItem = DataItemSerialisation.GetSerialisedDataItem(response);

                        byte[] message = Encoding.ASCII.GetBytes(serialisedItem);

                        clientSocket.Send(message);
                        //Returns list of Flights
                    }
                    else if (packageItems[0] == "BookASeat")
                    {
                        bool     Successful = FlightList.Flights[Convert.ToInt32(packageItems[1])].BookSeat(packageItems[2]);
                        DataItem response   = new DataItem(" ");
                        if (Successful)
                        {
                            response = new DataItem("Seat Booked Successfully");
                        }
                        else
                        {
                            response = new DataItem("Seat Unavailable");
                        }
                        //Prevents seat being booked if already occupied.


                        string serialisedItem = DataItemSerialisation.GetSerialisedDataItem(response);

                        byte[] message = Encoding.ASCII.GetBytes(serialisedItem);

                        clientSocket.Send(message);
                    }
                    else if (packageItems[0] == "SeatPlan")
                    {
                        DataItem response       = new DataItem(FlightList.Flights[Convert.ToInt32(packageItems[1])].GetSeatingPlan());
                        string   serialisedItem = DataItemSerialisation.GetSerialisedDataItem(response);

                        byte[] message = Encoding.ASCII.GetBytes(serialisedItem);

                        clientSocket.Send(message);
                        //Sends seating plan to client
                    }
                    else if (packageItems[0] == "AvailableSeats")
                    {
                        DataItem response       = new DataItem(FlightList.Flights[Convert.ToInt32(packageItems[1])].GetAvailableSeats());
                        string   serialisedItem = DataItemSerialisation.GetSerialisedDataItem(response);

                        byte[] message = Encoding.ASCII.GetBytes(serialisedItem);

                        clientSocket.Send(message);
                        //Sends available seat list to Client.
                    }



                    clientSocket.Shutdown(SocketShutdown.Both);
                    clientSocket.Close();
                }
            }
            catch (Exception e)
            {
                WriteLine(e.ToString());
            }
        }