Example #1
0
        private void bg_worker1_DoWork(object sender, DoWorkEventArgs e)
        {
            //Int32 port = 13000;
            Int32 port = 1223;
            //IPAddress localAddr = IPAddress.Parse("130.126.122.122");
            //IPAddress localAddr = IPAddress.Any;

            TcpListener server = null;
            //server = new TcpListener(localAddr, port);
            server = new TcpListener(port);

            // Start listening for client requests.
            server.Start();

            // Buffer for reading data
            int num_floats = 512; // number of floating point numbers in each packet
            int packet_length = num_floats * sizeof(float); // (in bytes)
            Byte[] bytes = new Byte[packet_length];
            TcpClient client = null;
            NetworkStream stream = null;
            write_to_msg_box("Waiting for connection: \n");
            Subj S = new Subj();

            while (true)
            {
                if (bg_worker1.CancellationPending == true)
                {
                    server.Stop();
                    Thread.Sleep(500);
                    break;
                }
                else
                {
                    try
                    {
                        //write_to_msg_box("In BG_Do_WORK\n");
                        client = server.AcceptTcpClient();
                        write_to_msg_box("Client Connected!!\n");
                        stream = client.GetStream();

                        S = new Subj(client, 0, all_subj.Count);

                        int i;
                        while ((i = stream.Read(bytes, 0, packet_length)) != 0)
                        {
                            // Convert byte array to float
                            float[] float_array = new float[num_floats];
                            for (int ii = 0; ii < float_array.Length; ii++)
                            {
                                float_array[ii] = BitConverter.ToSingle(bytes, ii * sizeof(float));
                                if (ii < 11)
                                    Console.Write(float_array[ii] + " ");
                            }
                            Console.Write("\n\n");

                            // Process float array and get estimated gaze point
                            Point_Obj est_gaze_pt = process_float_data(float_array);

                            // Log estimated gaze point
                            S.get_gaze_pts().Add(est_gaze_pt);

                            // Check if subject is distracted
                            S = check_if_distract(S, est_gaze_pt);
                            Console.WriteLine("NUM DISTRACT FRAMES: {0}", S.get_num_frames_distracted());
                            if (S.get_num_frames_distracted() > DISTRACT_WINDOW)
                            {
                                write_to_alert_box(1);
                            }
                            else
                            {
                                write_to_alert_box(0);
                            }
                        }

                        all_subj.Add(S);
                        stream.Close();
                        client.Close();
                        write_to_msg_box("Connection Closed");
                    }
                    catch (Exception EE)
                    {
                        Console.WriteLine("EXCEPTION: {0}", EE.Message);
                        all_subj.Add(S);
                        stream.Close();
                        client.Close();
                        write_to_msg_box("Connection Closed");
                    }

                    // Write Subj. to a file
                    File_IO F = new File_IO();
                    F.write_subj_to_file(S);
                }

            }
        }
Example #2
0
        private Subj check_if_distract(Subj S, Point_Obj est_gaze_pt)
        {
            //Console.WriteLine("INPUT PT: {0}", est_gaze_pt.ToString());
            est_gaze_pt.set_coord(-est_gaze_pt.get_x(), est_gaze_pt.get_y() + MONITOR_DIM_Y / 2, est_gaze_pt.get_z());

            int cur_num_frames_distracted = S.get_num_frames_distracted();
            if (est_gaze_pt.get_x() < -MONITOR_DIM_X / 2 || est_gaze_pt.get_x() > MONITOR_DIM_X / 2)
            {
                //Console.WriteLine("INCREMENT!");
                S.set_num_frames_distracted(++cur_num_frames_distracted);
            }
            else if (est_gaze_pt.get_y() < -MONITOR_DIM_Y / 2 || est_gaze_pt.get_y() > MONITOR_DIM_Y / 2)
            {
                //Console.WriteLine("INCREMENT!!");
                S.set_num_frames_distracted(++cur_num_frames_distracted);
            }
            else
            {
                //Console.WriteLine("RESET");
                S.set_num_frames_distracted(0);
            }

            //Console.WriteLine("NUM DISTRACT FRAMES PRIOR: {0}", S.get_num_frames_distracted());

            return S;
        }