private void ConnectToMasterButtonClick(object sender, EventArgs e)
        {
            Surgery.Master.MyIPAddress = tb_ipAddress.Text;
            if (User.ConnectToMaster())
            {
                try
                {
                    TcpClient tCPClient = new TcpClient();
                    tCPClient.Connect(IPAddress.Parse(Surgery.Master.MyIPAddress), connectionPort);
                    SocketMessage sm = new SocketMessage(Surgery, User);
                    SocketData.SendTCPDataTo(tCPClient, SocketData.SerializeObject<SocketMessage>(sm));

                    //start listening for master's position
                    SocketData.IsListeningForData = true;
                    Thread t1 = new Thread(new ThreadStart(SocketData.ListenForData));
                    t1.IsBackground = true;
                    t1.Start();

                    VideoCapture.IsListeningForVideo = true;
                    Thread t2 = new Thread(new ThreadStart(VideoCapture.ListenForVideo));
                    t2.IsBackground = true;
                    t2.Start();

                    Markup.IsListeningForMarkup = true;
                    Thread t3 = new Thread(new ThreadStart(Markup.ListenForMarkup));
                    t3.IsBackground = true;
                    t3.Start();

                    Thread t4 = new Thread(new ThreadStart(listenForGrantReq));
                    t4.IsBackground = true;
                    t4.Start();

                    if (User.HasOmnis)
                    {
                        lbl_Connections.Text = "Connected to: ";
                        string dir = Path.Combine(System.IO.Directory.GetCurrentDirectory(), @"..\..\Content\pc.png");
                        Image pcImg = Image.FromFile(dir);
                        ToolStripItem newItem = new ToolStripButton("Master", pcImg, sendClientGrantReq, "btn_Master");
                        newItem.ToolTipText = tb_ipAddress.Text;
                        ss_Connections.Items.Add(newItem);

                        btn_ReqControl.Enabled = true;
                    }
                    else
                    {
                        lbl_Connections.Text = "Connected to: Master";
                    }

                    lbl_Connections.Visible = true;
                    gb_SendingLeft.Visible = true;
                    tb_SendingLeft.Visible = true;
                    gb_SendingRight.Visible = true;
                    tb_SendingRight.Visible = true;
                }
                catch (Exception ex)
                {
                    ShowError(ex.Message, ex.ToString());
                }
            }

            ConnectToMasterButton.Enabled = !User.ConnectedToMaster;
            tb_ipAddress.Enabled = !User.ConnectedToMaster;
            btn_zeroForces.Enabled = User.ConnectedToMaster;
            groupBox3.Enabled = User.ConnectedToMaster;
            tb_forces.Enabled = User.ConnectedToMaster;
            lbl_forceStrength.Enabled = User.ConnectedToMaster;
            trb_forceStrength.Enabled = User.ConnectedToMaster;
        }
 private void btn_ClearMarks_Click(object sender, EventArgs e)
 {
     Markup.MyMarkings.Clear();
     tmpPoints = new List<Point>();
     if (!User.IsMaster)
     {
         SocketMessage sm = new SocketMessage(Surgery, User);
         sm.ClearMarkingsReq = true;
         SocketData.SendUDPDataTo(IPAddress.Parse(Surgery.Master.MyIPAddress), SocketData.SerializeObject<SocketMessage>(sm));
     }
     else
         Markup.ClearMarkingsReq = true;
 }
        private void UnderlyingTimerTick(object sender, EventArgs e)
        {
            try
            {
                //Get current position
                OmniPosition currentPos = new OmniPosition();
                if( User.HasOmnis )
                {
                    currentPos = User.GetOmniPositions();
                }
                showOmniPositions(currentPos);
                if (User.IsInControl)
                {
                    Surgery.UserInControl = User;
                    if (telSurgeOnly && User.IsFrozen)
                    {
                        Surgery.InControlPosition = User.FrozenPosition;
                    }
                    else
                        Surgery.InControlPosition = currentPos;
                    if (!User.IsMaster)
                    {
                        //Only send data to Master while InControl
                        SocketData.SendUDPDataTo(IPAddress.Parse(Surgery.Master.MyIPAddress), SocketData.CreateMessageToSend());
                        messageCount++;
                    }
                    //Check for freeze button press
                    if (telSurgeOnly && this.User.CheckForFreeze(currentPos))
                        Freeze();
                    //Display Frozen status
                    if (User.IsFrozen)
                        tb_InControl.Text = "You are frozen.";
                    else
                        tb_InControl.Text = "You are in control!";
                }
                else
                {
                    if (User.IsMaster)
                    {
                        //check for emergency switch
                        if (this.User.CheckForEmergencySwitch(currentPos))
                            emergencySwitchControl();
                    }
                    //Check for following
                    this.User.CheckIfFollowing(currentPos);
                    if (User.IsFollowing)
                        User.OmniFollow(Surgery.InControlPosition);
                    else
                        User.SetOmniForce(new OmniPosition());
                }
                if (User.IsMaster)
                {
                    //Master always sends data
                    if (Surgery.ConnectedClients.Count > 0)
                        SocketData.MasterSendData();
                    //Check for "stay alive" from clients
                    foreach (User u in Surgery.ConnectedClients)
                    {
                        if (DateTime.Now.Subtract(u.LastHeardFrom).Seconds > 30)
                        {
                            disconnectClient(u);
                            break;
                        }
                    }
                }
                else
                {
                    //Send "stay alive to Master
                    if (User.ConnectedToMaster)
                    {
                        if (DateTime.Now.Subtract(User.LastHeardFrom).Seconds > 20)
                        {
                            TcpClient tCPClient = new TcpClient();
                            tCPClient.Connect(IPAddress.Parse(Surgery.Master.MyIPAddress), connectionPort);
                            SocketMessage sm = new SocketMessage(Surgery, User);
                            SocketData.SendTCPDataTo(tCPClient, SocketData.SerializeObject<SocketMessage>(sm));

                            User.LastHeardFrom = DateTime.Now;
                        }
                    }
                }
                //Update and show output data (RobotApp)
                if (Surgery.InControlPosition != null)
                {
                    OutputPosition = Surgery.InControlPosition;
                    showOutputPosition();
                }
            }
            catch (Exception ex)
            {
                UnderlyingTimer.Enabled = false;
                ShowError("An error occured during routine program timer. Application must be reset.", ex.Message + " => " + ex.ToString());
            }
        }
Esempio n. 4
0
 public bool ConnectToMaster()
 {
     TcpClient c = new TcpClient();
     try
     {
         c.Connect(IPAddress.Parse(Main.Surgery.Master.MyIPAddress), connectionPort);
     }
     catch (SocketException ex)
     {
         if (ex.SocketErrorCode == SocketError.TimedOut)
             Main.ShowError("Cannot connect to Master... Try Again.", ex.Message);
         else
             Main.ShowError(ex.Message, ex.ToString());
     }
     ConnectedToMaster = c.Connected;
     if (ConnectedToMaster)
     {
         SocketMessage sm = new SocketMessage(Main.Surgery, this);
         Main.SocketData.SendTCPDataTo(c, SocketData.SerializeObject<SocketMessage>(sm));
     }
     return ConnectedToMaster;
 }
Esempio n. 5
0
        private void disconnect(string ipAddress)
        {
            try
            {
                if (cb_isMaster.Checked)
                {
                    //if disconnected client was in control, take control
                    if (inControlIP.Equals(ipAddress))
                    {
                        inControlIsFrozen = true;
                        switchControl(true, GetMyIP());
                    }
                    connectedClients.Remove(ipAddress);
                    foreach (ToolStripItem itm in ss_Connections.Items)
                    {
                        if (itm.ToolTipText != null && itm.ToolTipText.Equals(ipAddress))
                        {
                            ss_Connections.Items.Remove(itm);
                            break;
                        }
                    }
                    if (connectedClients.Count < 1)
                    {
                        lbl_Connections.Text = "Connections: None";
                    }
                }
                else
                {
                    //send disconnect to Master
                    SocketMessage sm = new SocketMessage(myName, GetMyIP());
                    using (TcpClient c = new TcpClient())
                    {
                        c.Connect(IPAddress.Parse(tb_ipAddress.Text), connectionPort);
                        Stream s = c.GetStream();
                        string json = JsonConvert.SerializeObject(sm);
                        byte[] arry = Encoding.ASCII.GetBytes(json);
                        s.Write(arry, 0, arry.Length);
                    }
                }

                logMessage("Successfully disconnected.", "This machine was successfully disconnected with the surgery.", Logging.StatusTypes.Running);
            }
            catch (Exception ex)
            {
                ShowError(ex.Message, ex.ToString());
            }
        }
Esempio n. 6
0
        private void ConnectToMasterButtonClick(object sender, EventArgs e)
        {
            TcpClient c = new TcpClient();
            try
            {
                c.Connect(IPAddress.Parse(tb_ipAddress.Text), connectionPort);
            }
            catch (SocketException ex)
            {
                if (ex.SocketErrorCode == SocketError.TimedOut)
                    ShowError("Cannot connect to Master... Try Again.", ex.Message);
                else
                    ShowError(ex.Message, ex.ToString());
            }
            bool connected = c.Connected;
            if (connected)
            {
                try
                {
                    inControlIP = tb_ipAddress.Text;

                    SocketMessage sm = new SocketMessage(tb_InstanceName.Text, GetMyIP());
                    sm.hasOmnis = !cb_noOmnisAttached.Checked;
                    string json = JsonConvert.SerializeObject(sm);
                    byte[] arry = Encoding.ASCII.GetBytes(json);
                    Stream s = c.GetStream();
                    s.Write(arry, 0, arry.Length);

                    //start listening for master's position
                    isListeningForData = true;
                    Thread t1 = new Thread(new ThreadStart(listenForData));
                    t1.IsBackground = true;
                    t1.Start();

                    Thread t2 = new Thread(new ThreadStart(listenForVideo));
                    t2.IsBackground = true;
                    t2.Start();

                    Thread t3 = new Thread(new ThreadStart(sendMarkings));
                    t3.IsBackground = true;
                    t3.Start();

                    Thread t4 = new Thread(new ThreadStart(listenForGrantReq));
                    t4.IsBackground = true;
                    t4.Start();

                    if (!cb_noOmnisAttached.Checked)
                    {
                        lbl_Connections.Text = "Connected to: ";
                        string dir = Path.GetDirectoryName(exeFile);
                        Image pcImg = Image.FromFile(Path.Combine(dir, @"..\..\Content\pc.png"));
                        ToolStripItem newItem = new ToolStripButton("Master", pcImg, sendClientGrantReq, "btn_Master");
                        newItem.ToolTipText = tb_ipAddress.Text;
                        ss_Connections.Items.Add(newItem);

                        btn_ReqControl.Visible = true;
                    }
                    else
                    {
                        lbl_Connections.Text = "Connected to: Master";
                    }

                    lbl_Connections.Visible = true;
                    gb_SendingLeft.Visible = true;
                    tb_SendingLeft.Visible = true;
                    gb_SendingRight.Visible = true;
                    tb_SendingRight.Visible = true;
                }
                catch (Exception ex)
                {
                    ShowError(ex.Message, ex.ToString());
                }
            }

            ConnectToMasterButton.Enabled = !connected;
            tb_ipAddress.Enabled = !connected;
            btn_zeroForces.Enabled = connected;
            groupBox3.Enabled = connected;
            tb_forces.Enabled = connected;
            lbl_forceStrength.Enabled = connected;
            trb_forceStrength.Enabled = connected;
            btn_ReqControl.Enabled = connected;
        }
Esempio n. 7
0
 private void sendDataTo(IPAddress addr)
 {
     Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
     SocketMessage sm = new SocketMessage(myName, GetMyIP());
     try
     {
         if (isFrozen)
             sm.OmniPosition = frozenPosition;
         else
             sm.OmniPosition = inControlPosition;
         sm.isFrozen = isFrozen;
         string json = JsonConvert.SerializeObject(sm);
         byte[] arry = Encoding.ASCII.GetBytes(json);
         s.SendTo(arry, new IPEndPoint(addr, dataPort));
     }
     catch (Exception ex)
     {
         ShowError(ex.Message, ex.ToString());
     }
 }
Esempio n. 8
0
 private void masterSendData()
 {
     Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
     SocketMessage sm = new SocketMessage("Master", GetMyIP());
     try
     {
         if (isFrozen)
             sm.OmniPosition = frozenPosition;
         else
             sm.OmniPosition = inControlPosition;
         sm.isFrozen = isFrozen;
         string json = JsonConvert.SerializeObject(sm);
         byte[] arry = Encoding.ASCII.GetBytes(json);
         foreach (string a in connectedClients)
         {
             if (!a.Equals(inControlIP))
                 s.SendTo(arry, new IPEndPoint(IPAddress.Parse(a), dataPort));
         }
     }
     catch (Exception ex)
     {
         ShowError(ex.Message, ex.ToString());
     }
 }