Пример #1
0
        private void DoButtonRemoveClick(object sender, EventArgs e)
        {
            TreeNode temNextNode = null;

            if (currentNode != null)
            {
                connectionsInfo.Remove(currentNode.Text);
                currentNode.Remove();
                // currentNode = temNextNode;
                // treeViewConnections.SelectedNode = currentNode;
                ShowForm();
            }
        }
        // Remove a Server from the data structures.
        public void RemoveServer(ServerModel ServerToRemove, IPAddress IpAddressToRemove)
        {
            // Iterate for each process in the server and remove it from the applications listed in the applicationList of the viewModel.
            foreach (ProcessModel proc in ServerToRemove.TheProcesses)
            {
                ServerToRemove.RemoveProcessFromApplications(proc);
                // The function already iterates for all the application in the list and checks if the application has still at least a server running it.
                // It also takes care of raising the propertyChanged.
            }

            // Remove it from the IpConnections.
            if (IpConnections.Contains(IpAddressToRemove))
            {
                System.Windows.Application.Current.Dispatcher.Invoke((Action) delegate
                {
                    IpConnections.Remove(IpAddressToRemove);
                    RaisePropertyChanged("IpConnections");
                });
            }
            // Remove it from the ServerConnectons.
            if (ServerConnections.Contains(ServerToRemove))
            {
                ServerConnections.Remove(ServerToRemove);
            }

            // Check that there is the entry correspondent witht the serverIpAddress.
            lock (syncListOfConnectionThreads)
            {
                if (_listOfConnectionThreads != null && _currentSelectedServer != null)
                {
                    if (_listOfConnectionThreads.ContainsKey(_currentSelectedServer.ServerIpAddress))
                    {
                        // Abort the thread running the connection.
                        _listOfConnectionThreads[_currentSelectedServer.ServerIpAddress].Abort();
                        // Remove the thread from the dictionary.
                        _listOfConnectionThreads.Remove(_currentSelectedServer.ServerIpAddress);
                    }
                }
            }

            // If there are other servers, select the first of them
            if (ServerConnections.Count() > 0)
            {
                // Select the first server.
                CurrentSelectedServer    = ServerConnections.First();
                CurrentSelectedIpAddress = CurrentSelectedServer.ServerIpAddress;
                CurrentSelectedProcess   = null;
                TheProcessesList         = CurrentSelectedServer.TheProcesses;
                // Update the GUI
                RaisePropertyChanged("TheProcessesList");
                RaisePropertyChanged("CurrentSelectedIpAddress");
                RaisePropertyChanged("CurrentSelectedServer");
                RaisePropertyChanged("CurrentSelectedProcess");
            }
            else
            {
                // There are no more connected servers.
                CurrentSelectedServer    = null;
                CurrentSelectedProcess   = null;
                CurrentSelectedIpAddress = null;
                TheProcessesList         = null;
                RaisePropertyChanged("TheProcessesList");
            }
        }
        public void AddMessageData(MessageDirection direction, Message message)
        {
            var md = new MessageData(direction, message);

            System.Diagnostics.Trace.WriteLine("Adding " + direction + " message " + message.ID + " " + message.Type);

            Messages.Insert(0, md);
            if (Messages.Count > MESSAGE_LOG_MAX)
            {
                Messages.RemoveAt(MESSAGE_LOG_MAX);
            }

            switch (message.Type)
            {
            case MessageType.STATUS_RESPONSE:
                //distinguish the status response between status of a server or a connection
                if (message.HasValue("ServerID"))
                {
                    //assign some general server properties
                    ServerID             = message.GetString("ServerID");
                    MaxConnections       = message.GetInt("MaxConnections");
                    ConnectionsCount     = message.GetInt("ConnectionsCount");
                    RemainingConnections = MaxConnections - ConnectionsCount;
                    ServerDetails        = String.Format("{0} @ {1}: {2} Connections made, {3} Remaining", ServerID, ServerConnectionString, ConnectionsCount, RemainingConnections);

                    //remove clients by first getting a list of all 'active' clients
                    var           clients     = message.GetList <String>("Connections");
                    List <String> clientNames = new List <String>();
                    foreach (var cs in clients)
                    {
                        var data       = cs.Split(' ');
                        var clientName = data[1];
                        if (clientName != null && clientName != String.Empty)
                        {
                            clientNames.Add(clientName);
                        }
                    }
                    //and then removing any clients not on that list
                    var clientsToRemove = new List <ClientData>();
                    foreach (ClientData cd in Clients)
                    {
                        if (!clientNames.Contains(cd.Name))
                        {
                            clientsToRemove.Add(cd);
                        }
                    }
                    foreach (ClientData cd in clientsToRemove)
                    {
                        System.Diagnostics.Trace.WriteLine("Removing client " + cd.Name);
                        Clients.Remove(cd);
                    }

                    //add/remove server connections
                    List <String>        scdIDs = new List <String>();
                    ServerConnectionData scd;
                    scd = AddServerConnectionData("PRIMARY", message.GetString("PrimaryConnection"));
                    scdIDs.Add(scd.ID);
                    foreach (var s in message.GetList <String>("SecondaryConnections"))
                    {
                        scd = AddServerConnectionData("SECONDARY", s);
                        scdIDs.Add(scd.ID);
                    }
                    foreach (var s in message.GetList <String>("Connections"))
                    {
                        scd = AddServerConnectionData("CLIENT", s);
                        scdIDs.Add(scd.ID);
                    }

                    //now we remove any dead connections
                    List <ServerConnectionData> scToRemove = new List <ServerConnectionData>();
                    foreach (ServerConnectionData sd in ServerConnections)
                    {
                        if (!scdIDs.Contains(sd.ID))
                        {
                            scToRemove.Add(sd);
                        }
                    }
                    foreach (ServerConnectionData sd in scToRemove)
                    {
                        System.Diagnostics.Debug.Print("Removing server connection " + sd.ID);
                        ServerConnections.Remove(sd);
                    }
                }
                else if (message.HasValue("Context"))
                {
                    AddClientData(message);
                }
                break;
            }
        }