示例#1
0
        public void RegisterInstance(Instance instance)
        {
            if (Instances.SingleOrDefault(i => i.IpAddress.Equals(instance.IpAddress)) != null)
                //instance already registered
                //added as an additional guard after user reported Sequence error with use of
                //SingleOrDefault in ApplyMachineInformation()
                return;

            string nodeText = instance.MachineName;
            bool isThisPc = instance.MachineName.Equals(Environment.MachineName);

            TreeNode node;

            if (isThisPc)
            {
                ThisPCInstance = instance;
                nodeText = ThisPCText;
                node = treeView1.Nodes[0].Nodes.Insert(0, instance.IpAddress, nodeText);
            }
            else
            {
                node = treeView1.Nodes[0].Nodes.Add(instance.IpAddress, nodeText);
            }

            if (isThisPc)
            {
                node.Tag = 1;
                node.ImageIndex = 4;
                node.SelectedImageIndex = 4;
            }
            else
            {
                node.Tag = null;
                node.ImageIndex = 1;
                node.SelectedImageIndex = 1;
            }

            Instances.Add(instance);

            instanceUpdateDates[instance] = DateTime.Now;

            SortTree();

            treeView1.Nodes[0].ExpandAll();

            if (isThisPc)
                treeView1.SelectedNode = node;
        }
示例#2
0
        private void ProcessReceived(IPEndPoint source, byte[] bytes)
        {
            //only response to discovery on the local network since we transfer fingerprints
            if ((source.AddressFamily != AddressFamily.InterNetwork) &&
                (source.AddressFamily != AddressFamily.InterNetworkV6))
                return;

            string jsonData = Encoding.ASCII.GetString(bytes);

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            Data.Packet packet = serializer.Deserialize<Data.Packet>(jsonData);

            if (packet.Verb.Equals(Verbs.Online))
            {
                string ipAddress = source.Address.ToString();
                if (!instances.Any(i => i.IpAddress.Equals(ipAddress)))
                {
                    Data.Instance instance = new Data.Instance 
                    { 
                        IpAddress = ipAddress, 
                        MachineName = packet.MachineName, 
                        Fingerprint = packet.Fingerprint 
                    };

                    //lock for thread-safety - collection may be modified
                    lock (instances)
                    {
                        instances.Add(instance);
                    }

                    Sender.Send(source.Address, Verbs.Online, fingerprint);

                    if (InstanceOnline != null)
                        InstanceOnline(this, new InstanceChangedArgs { Instance = instance });
                }
            }
            else if (packet.Verb.Equals(Verbs.Offline))
            {
                string ipAddress = source.Address.ToString();

                //lock for thread-safety - collection may be modified
                lock (instances)
                {
                    Data.Instance instance = instances.SingleOrDefault(i => i.IpAddress.Equals(ipAddress));
                    if (instance != null)
                    {
                        instances.Remove(instance);

                        if (InstanceOffline != null)
                            InstanceOffline(this, new InstanceChangedArgs { Instance = instance });
                    }
                }
            }
        }
示例#3
0
        private void ProcessReceived(IPEndPoint source, byte[] bytes)
        {
            //only response to discovery on the local network since we transfer fingerprints
            if ((source.AddressFamily != AddressFamily.InterNetwork) &&
                (source.AddressFamily != AddressFamily.InterNetworkV6))
            {
                return;
            }

            string jsonData = Encoding.ASCII.GetString(bytes);

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            Data.Packet packet = serializer.Deserialize <Data.Packet>(jsonData);

            if (packet.Verb.Equals(Verbs.Online))
            {
                string ipAddress = source.Address.ToString();
                if (!instances.Any(i => i.IpAddress.Equals(ipAddress)))
                {
                    Data.Instance instance = new Data.Instance
                    {
                        IpAddress   = ipAddress,
                        MachineName = packet.MachineName,
                        Fingerprint = packet.Fingerprint
                    };

                    //lock for thread-safety - collection may be modified
                    lock (instances)
                    {
                        instances.Add(instance);
                    }

                    Sender.Send(source.Address, Verbs.Online, fingerprint);

                    if (InstanceOnline != null)
                    {
                        InstanceOnline(this, new InstanceChangedArgs {
                            Instance = instance
                        });
                    }
                }
            }
            else if (packet.Verb.Equals(Verbs.Offline))
            {
                string ipAddress = source.Address.ToString();

                //lock for thread-safety - collection may be modified
                lock (instances)
                {
                    Data.Instance instance = instances.SingleOrDefault(i => i.IpAddress.Equals(ipAddress));
                    if (instance != null)
                    {
                        instances.Remove(instance);

                        if (InstanceOffline != null)
                        {
                            InstanceOffline(this, new InstanceChangedArgs {
                                Instance = instance
                            });
                        }
                    }
                }
            }
        }
示例#4
0
 private static string GetMachineName(Instance instance)
 {
     string result = instance.MachineName;
     bool isThisPc = result.Equals(Environment.MachineName);
     if (isThisPc)
         result = ThisPCText;
     return result;
 }
示例#5
0
 public void UnregisterInstance(Instance instance)
 {
     treeView1.Nodes[0].Nodes.RemoveByKey(instance.IpAddress);
     Instances.Remove(instance);
     instanceMachines.Remove(instance);
     RefreshNetworkTotals();
 }
示例#6
0
        private void ProcessReceived(IPEndPoint source, byte[] bytes)
        {
            //only response to discovery on the local network since we transfer fingerprints
            if ((source.AddressFamily != AddressFamily.InterNetwork) &&
                (source.AddressFamily != AddressFamily.InterNetworkV6))
            {
                return;
            }

            string jsonData = Encoding.ASCII.GetString(bytes);

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            Data.Packet packet = serializer.Deserialize <Data.Packet>(jsonData);

            if (packet.Verb.Equals(Verbs.Online))
            {
                //lock for thread-safety, otherwise the dupe check below may not be sound
                lock (instances)
                {
                    //search by MachineName and Fingerprint - these are unique while IP address may not be
                    //reasoning - the same machine may have multiple IP addresses as discovery supports multiple interfaces
                    if (!instances.Any(i => i.MachineName.Equals(packet.MachineName) && (i.Fingerprint == packet.Fingerprint)))
                    {
                        Data.Instance instance = new Data.Instance
                        {
                            IpAddress   = source.Address.ToString(),
                            MachineName = packet.MachineName,
                            Fingerprint = packet.Fingerprint
                        };

                        instances.Add(instance);

                        Sender.Send(source.Address, Verbs.Online, fingerprint);

                        if (InstanceOnline != null)
                        {
                            InstanceOnline(this, new InstanceChangedArgs {
                                Instance = instance
                            });
                        }
                    }
                }
            }
            else if (packet.Verb.Equals(Verbs.Offline))
            {
                string ipAddress = source.Address.ToString();

                //lock for thread-safety - collection may be modified
                lock (instances)
                {
                    //search by MachineName and Fingerprint - these are unique while IP address may not be
                    //reasoning - the same machine may have multiple IP addresses as discovery supports multiple interfaces
                    Data.Instance instance = instances.SingleOrDefault(i => i.MachineName.Equals(packet.MachineName) && (i.Fingerprint == packet.Fingerprint));
                    if (instance != null)
                    {
                        instances.Remove(instance);

                        if (InstanceOffline != null)
                        {
                            InstanceOffline(this, new InstanceChangedArgs {
                                Instance = instance
                            });
                        }
                    }
                }
            }
        }
示例#7
0
        private void ProcessReceived(IPEndPoint source, byte[] bytes)
        {
            //only response to discovery on the local network since we transfer fingerprints
            if ((source.AddressFamily != AddressFamily.InterNetwork) &&
                (source.AddressFamily != AddressFamily.InterNetworkV6))
                return;

            string jsonData = Encoding.ASCII.GetString(bytes);

            Data.Packet packet = JsonConvert.DeserializeObject<Data.Packet>(jsonData);

            if (packet.Verb.Equals(Verbs.Online))
            {
                //lock for thread-safety, otherwise the dupe check below may not be sound
                lock (instances)
                {
                    //search by MachineName and Fingerprint - these are unique while IP address may not be
                    //reasoning - the same machine may have multiple IP addresses as discovery supports multiple interfaces
                    if (!instances.Any(i => i.MachineName.Equals(packet.MachineName) && (i.Fingerprint == packet.Fingerprint)))
                    {
                        Data.Instance instance = new Data.Instance 
                        { 
                            IpAddress = source.Address.ToString(), 
                            MachineName = packet.MachineName, 
                            Fingerprint = packet.Fingerprint 
                        };

                        instances.Add(instance);

                        Sender.Send(source.Address, Verbs.Online, fingerprint);

                        if (InstanceOnline != null)
                            InstanceOnline(this, new InstanceChangedArgs { Instance = instance });
                    }
                }
            }
            else if (packet.Verb.Equals(Verbs.Offline))
            {
                string ipAddress = source.Address.ToString();

                //lock for thread-safety - collection may be modified
                lock (instances)
                {
                    //search by MachineName and Fingerprint - these are unique while IP address may not be
                    //reasoning - the same machine may have multiple IP addresses as discovery supports multiple interfaces
                    Data.Instance instance = instances.SingleOrDefault(i => i.MachineName.Equals(packet.MachineName) && (i.Fingerprint == packet.Fingerprint));
                    if (instance != null)
                    {
                        instances.Remove(instance);

                        if (InstanceOffline != null)
                            InstanceOffline(this, new InstanceChangedArgs { Instance = instance });
                    }
                }
            }
        }