Пример #1
0
            public void CanMapPersonOneToPersonTwo()
            {
                IList <IAddress> adrList = new List <IAddress> {
                    new AddressOne {
                        Street = "Street One"
                    }
                };
                PersonOne source = new PersonOne {
                    Name = "A Name", Addresses = adrList
                };

                // I thought these mappings would be enough. I tried various others, without success.
                Mapper.CreateMap <PersonOne, PersonTwo>();
                Mapper.CreateMap <AddressOne, AddressTwo>();
                Mapper.CreateMap <AddressOne, IAddress>().ConvertUsing(Mapper.Map <AddressOne, AddressTwo>);
                Mapper.AssertConfigurationIsValid();
                var result = Mapper.Map <PersonOne, PersonTwo>(source);

                // These are ok.
                Assert.AreEqual(source.Name, result.Name);
                Assert.IsNotNull(result.Addresses);
                Assert.IsTrue(result.Addresses.Count == 1);
                Assert.AreEqual(source.Addresses[0].Street, result.Addresses[0].Street);

                // This is what I can't get to pass:
                result.Addresses[0].ShouldBeInstanceOf <AddressTwo>();

                // Expected: instance of <AutomapperTest.AddressTwo>
                // But was:  <AutomapperTest.AddressOne>
            }
Пример #2
0
        private void ThreadPingSetup()
        {
            pingConnection = new Thread(() =>
            {
                Ping pingSender = new Ping();


                while (true)
                {
                    List <Person> listP = PersonOne.ToList();
                    Thread.Sleep(1500);
                    foreach (Person p in listP)
                    {
                        PingReply reply = pingSender.Send(IPAddress.Parse(p.IP));
                        bool b          = true;
                        if (reply.Status != IPStatus.Success)
                        {
                            Dispatcher.Invoke(new Action(() =>
                            {
                                PersonOne.Remove(p);
                            }));
                        }
                    }
                }
            });
            pingConnection.Start();
        }
Пример #3
0
 private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
 {
     App.app.s = null;
     send_list.Clear();
     PersonOne.Clear();
     receivingAnnouncement.Abort();
 }
Пример #4
0
			public void CanMapPersonOneToPersonTwo()
			{
				IList<IAddress> adrList = new List<IAddress> { new AddressOne { Street = "Street One" } };
				var source = new PersonOne { Name = "A Name", Addresses = adrList };

				// I thought these mappings would be enough. I tried various others, without success.
                Mapper.Initialize(cfg =>
                {
                    cfg.CreateMap<PersonOne, PersonTwo>();
                    cfg.CreateMap<AddressOne, AddressTwo>();
                    cfg.CreateMap<AddressOne, IAddress>().ConvertUsing(Mapper.Map<AddressOne, AddressTwo>);
                });
				Mapper.AssertConfigurationIsValid();
				var result = Mapper.Map<PersonOne, PersonTwo>(source);

				// These are ok.
				source.Name.ShouldBe(result.Name);
				result.Addresses.ShouldNotBeNull();
				(result.Addresses.Count == 1).ShouldBeTrue();
				source.Addresses[0].Street.ShouldBe(result.Addresses[0].Street);

				// This is what I can't get to pass:
				result.Addresses[0].ShouldBeOfType<AddressTwo>();

				// Expected: instance of <AutomapperTest.AddressTwo>
				// But was:  <AutomapperTest.AddressOne>
			}
Пример #5
0
        private void insertDeleteSendList(ToSend ip, bool to_Insert)
        {
            Person p = searchIp(ip.ip);

            if (to_Insert)
            {
                send_list.Add(ip);
                p.tick = true;
            }
            else
            {
                foreach (ToSend i in send_list)
                {
                    if (i.ip.Equals(ip.ip))
                    {
                        if (i.index != null)
                        {
                            PersonOne.Remove(i.index);
                        }
                        p.tick = false;

                        send_list.Remove(i);
                        break;
                    }
                }
            }
        }
Пример #6
0
        private Person searchIp(string ipforSearch)
        {
            List <Person> listTemp = PersonOne.ToList();

            foreach (Person p in listTemp)
            {
                if (p.IP.Equals(ipforSearch))
                {
                    return(p);
                }
            }
            return(null);
        }
Пример #7
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            PersonOne personOne = new PersonOne()
            {
                Name = "小黑"
            };

            personOne.Show();

            Console.WriteLine();
            SuitPersonOne suitPersonOne = new SuitPersonOne()
            {
                Name = "小黑"
            };

            suitPersonOne.Show();
            Console.WriteLine();

            TShirtPersonOne shirtPersonOne = new TShirtPersonOne()
            {
                Name = "小黑"
            };

            shirtPersonOne.Show();
            Console.WriteLine();

            Console.WriteLine();
            Console.WriteLine("==============================");

            // 首先要有打扮的人
            AbstractPerson xiaoXin = new Person()
            {
                Name = "小新"
            };
            // 然后准备要穿的衣服
            Finery personWithSuit     = new Suit();     // 西装
            Finery personWithTie      = new Tie();      // 领带
            Finery personWithTrousers = new Trousers(); // 长裤
            Finery personWithLeather  = new Leather();  // 皮鞋

            // 最后按顺序把衣服穿上
            // 先穿裤子
            personWithTrousers.SetPerson(xiaoXin);
            // 再让穿裤子的小新穿西装
            personWithSuit.SetPerson(personWithTrousers);
            // 最后把鞋子领带都穿上
            personWithTie.SetPerson(personWithSuit);
            personWithLeather.SetPerson(personWithTie);

            // 照下镜子
            personWithLeather.Show();

            Console.WriteLine();
            Console.WriteLine("==============================");

            AbstractPerson xiaoHei = new Person()
            {
                Name = "小黑"
            };

            Finery personWithTShirt  = new TShirt();  // 大t恤
            Finery personWithSneaker = new Sneaker(); // 网鞋

            personWithTShirt.SetPerson(xiaoHei);
            personWithSneaker.SetPerson(personWithTShirt);

            personWithSneaker.Show();



            Console.ReadKey();
        }
Пример #8
0
        private void ThreadReceivingAnnSetup()
        {
            receivingAnnouncement = new Thread(() =>

            {
                //Creates a UdpClient for reading incoming data.
                UdpClient receivingUdpClient = new UdpClient(port);
                receivingUdpClient.JoinMulticastGroup(IPAddress.Parse("224.0.0.2"));
                //Creates an IPEndPoint to record the IP Address and port number of the sender.
                // The IPEndPoint will allow you to read datagrams sent from any source.
                IPEndPoint RemoteIpEndPoint        = new IPEndPoint(IPAddress.Any, port);
                receivingUdpClient.EnableBroadcast = true;
                receivingUdpClient.Client.SetIPProtectionLevel(IPProtectionLevel.Unrestricted);
                //receivingUdpClient.MulticastLoopback = false;
                try
                {
                    while (true)
                    {
                        int i        = 0;
                        byte[] name1 = null;
                        byte[] image = null;
                        int length   = 0;


                        // Blocks until a message returns on this socket from a remote host.

                        Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
                        string ipforSearch  = RemoteIpEndPoint.Address.ToString();
                        length = BitConverter.ToInt32(receiveBytes, i);
                        if (ipforSearch.Equals(IPAddress.Loopback.ToString()))
                        {
                            ipforSearch = App.app.broadcastCalculation().ToString();
                        }

                        bool found = false;
                        if (ipforSearch.Equals(IPAddress.Loopback.ToString()))
                        {
                            length = 0;
                        }
                        IPAddress myIp = App.app.broadcastCalculation();
                        foreach (IPAddress ip in App.app.myIp)
                        {
                            if (ip.Equals(RemoteIpEndPoint.Address))
                            {
                                found = true;
                            }
                        }

                        if (!Properties.Settings.Default.DiscoverySelf && found)
                        {
                            length = 0;
                        }
                        //if (!myIp.Equals(IPAddress.Parse(ipforSearch)) && found) length=0;
                        if (length == 0)
                        {
                            Ping pingSender   = new Ping();
                            IPAddress address = RemoteIpEndPoint.Address;
                            PingReply reply   = pingSender.Send(address);
                            bool b            = true;

                            if (reply.Status == IPStatus.Success)
                            {
                                b = true;
                            }
                            else
                            {
                                b = false;
                            }

                            Dispatcher.Invoke(new Action(() =>
                            {
                                List <Person> tmp = PersonOne.ToList();
                                foreach (Person p in tmp)
                                {
                                    if (p.IP.Equals(ipforSearch))
                                    {
                                        if (!p.tick || !b)
                                        {
                                            foreach (ToSend o in send_list.ToList())
                                            {
                                                if (o.ip.Equals(ipforSearch))
                                                {
                                                    insertDeleteSendList(o, false);
                                                }
                                            }
                                            PersonOne.Remove(p);
                                        }
                                        else
                                        {
                                            foreach (ToSend o in send_list.ToList())
                                            {
                                                if (o.ip.Equals(ipforSearch))
                                                {
                                                    if (o.bkgWorker == null)
                                                    {
                                                        insertDeleteSendList(o, false);
                                                        PersonOne.Remove(p);
                                                        break;
                                                    }

                                                    o.o     = PersonOne;
                                                    o.index = p;
                                                }
                                            }
                                        }
                                    }
                                }
                            }));
                            continue;
                        }
                        i    += 4;
                        name1 = new byte[length];


                        for (int j = 0; j < length; j++)
                        {
                            name1[j] = receiveBytes[i + j];
                        }
                        i     += length;
                        length = BitConverter.ToInt32(receiveBytes, i);

                        //  MemoryStream stream = receiveImage(RemoteIpEndPoint.Address);
                        person = searchIp(ipforSearch);



                        if (person != null)
                        {
                            if (person.tick)
                            {
                                continue;
                            }
                        }
                        if (person == null || person.change != length)
                        {
                            image = receiveImage(RemoteIpEndPoint.Address);

                            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
                            {
                                if (person != null)
                                {
                                    int index = PersonOne.IndexOf(person);
                                    bool tick = person.tick;
                                    PersonOne.RemoveAt(index);
                                    person        = new Person();
                                    person.Name   = Encoding.ASCII.GetString(name1);
                                    person.IP     = RemoteIpEndPoint.Address.ToString();
                                    person.Image  = ByteArraytoBitmap(image);//StreamtoBitmap(stream);// new BitmapImage(
                                    person.change = length;
                                    person.tick   = tick;
                                    PersonOne.Insert(index, person);
                                    return;
                                }
                                person        = new Person();
                                person.Name   = Encoding.ASCII.GetString(name1);
                                person.change = length;
                                person.tick   = false;
                                person.IP     = RemoteIpEndPoint.Address.ToString();
                                person.Image  = ByteArraytoBitmap(image);//StreamtoBitmap(stream);// new BitmapImage(Uri);
                                PersonOne.Add(person);
                            }));
                        }
                        else
                        {
                            if (Encoding.ASCII.GetString(name1).Equals(person.Name))
                            {
                                continue;
                            }
                            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
                            {
                                int index = PersonOne.IndexOf(person);
                                PersonOne.RemoveAt(index);
                                person.Name = Encoding.ASCII.GetString(name1);
                                PersonOne.Insert(index, person);
                            }));
                        }
                    }
                }
                catch (Exception)
                { }
                finally {
                    receivingUdpClient.Close();
                }
            });
        }
Пример #9
0
            public void CanMapPersonOneToPersonTwo()
            {
                IList<IAddress> adrList = new List<IAddress> { new AddressOne { Street = "Street One" } };
                PersonOne source = new PersonOne { Name = "A Name", Addresses = adrList };

                // I thought these mappings would be enough. I tried various others, without success.
                Mapper.CreateMap<PersonOne, PersonTwo>();
                Mapper.CreateMap<AddressOne, AddressTwo>();
                Mapper.CreateMap<AddressOne, IAddress>().ConvertUsing(Mapper.Map<AddressOne, AddressTwo>);
                Mapper.AssertConfigurationIsValid();
                var result = Mapper.Map<PersonOne, PersonTwo>(source);

                // These are ok.
                Assert.AreEqual(source.Name, result.Name);
                Assert.IsNotNull(result.Addresses);
                Assert.IsTrue(result.Addresses.Count == 1);
                Assert.AreEqual(source.Addresses[0].Street, result.Addresses[0].Street);

                // This is what I can't get to pass:
                result.Addresses[0].ShouldBeInstanceOf<AddressTwo>();

                // Expected: instance of <AutomapperTest.AddressTwo>
                // But was:  <AutomapperTest.AddressOne>
            }