Beispiel #1
0
        private void HandleClientComm(object client)
        {
            TcpClient clientSocket = (TcpClient)client;

            if (!blnServerRunning)
            {
                clientSocket.Close();

                return;
            }

            // Obtain a stream object for reading and writing
            NetworkStream clientStream = clientSocket.GetStream();

            // Object via protocol buffer
            while (true)
            {
                // Blocks until a client sends a message
                Console.WriteLine("clientThread: working...");
                //try
                //{
                    // Get data over TCP
                    byte[] byteTrimmed = ProtoBuffer.TCPHandler.RecieveData(clientSocket, clientStream);

                    theForm.Invoke(theForm.dLogCallBack, new object[] { "Received path planning request over the network.\n\n" });

                    // Construct Path Planning Request object from byte array
                    PathPlanningRequest curRequest = ByteArrayToRequest(byteTrimmed);

                    // Add request to queue
                    theForm.Invoke(theForm.dSubmitToRequestQueueCallBack, new object[] {curRequest});

                    // Do the path planning
                    PathPlanningHandler newHandler = new PathPlanningHandler(curRequest);
                    newHandler.Run();
                    double curEfficiency = newHandler.GetAvgEfficiency();
                    double curRunTime = newHandler.GetAvgRunTime();
                    List<Point> curPath = newHandler.GetPath();
                    newHandler = null;
                    theForm.Invoke(theForm.dLogCallBack, new object[] { curRequest.GetLog() });

                    // Convert result to byte array
                    byte[] curE = BitConverter.GetBytes(curEfficiency);
                    byte[] curR = BitConverter.GetBytes(curRunTime);
                    // Convert to int array first
                    int[] temp = new int[curPath.Count * 2];
                    for (int i = 0; i < curPath.Count; i++)
                    {
                        temp[i * 2] = curPath[i].X;
                        temp[i * 2 + 1] = curPath[i].Y;
                    }
                    // Convert int array to byte array
                    byte[] curP = new byte[temp.Length * sizeof(int)];
                    Buffer.BlockCopy(temp, 0, curP, 0, curP.Length);
                    // Combine them together
                    byte[] result = new byte[curE.Length + curR.Length + curP.Length];
                    Array.Copy(curE, 0, result, 0, curE.Length);
                    Array.Copy(curR, 0, result, curE.Length, curR.Length);
                    Array.Copy(curP, 0, result, curE.Length + curR.Length, curP.Length);
                    byte[] outStream = ProtoBuffer.TCPHandler.AddDataSizeHeader(result);

                    // Remove request from queue
                    theForm.Invoke(theForm.dRemoveFromRequestQueueCallBack, new object[] { 0 });

                    // Send server reponse
                    clientStream.Write(outStream, 0, outStream.Length);
                    clientStream.Flush();

                    theForm.Invoke(theForm.dLogCallBack, new object[] { "Path length " + curPath.Count + " planning result sent back to client.\n\n" });

                    break;
                //}
                //catch (Exception e)
                //{
                //    // A socket error has occured
                //    // theForm.Log(e.Message);
                //    System.Windows.Forms.MessageBox.Show(e.Message);
                //    break;
                //}
            }

            clientStream.Close();
            clientSocket.Close();
            Console.WriteLine("clientThread: terminating gracefully.");
        }
Beispiel #2
0
        // Method to process path planning request queue
        private void btnExecute_Click(object sender, EventArgs e)
        {
            // Eventually have a monitor thread initiate path planning tasks.
            // For now, just call from here directly.
            while (myServer.GetServerQueue().Count > 0)
            {
                // Do the path planning
                PathPlanningHandler newHandler = new PathPlanningHandler(myServer.GetServerQueue()[0].GetRequest());
                newHandler.Run();
                newHandler = null;

                // Log activities
                Log("Path planning using " + myServer.GetServerQueue()[0].GetRequest().AlgToUse.ToString() +
                    " algorithm completed successfully.\n\n");
                Log(myServer.GetServerQueue()[0].GetRequest().GetLog());

                // TODO Send activity log back to TestModuleForm

                // Remove it from server queue
                myServer.GetServerQueue().RemoveAt(0);

                // Remove it from listbox queue
                lstQueue.Items.RemoveAt(0);
                lstRequestQueue.RemoveAt(0);
            }
        }