protected override void Execute(object sender, System.ComponentModel.DoWorkEventArgs args)
        {
            lock (ClientConnectionList)
            {
                ClientConnectionList.Remove(ClientConnection);
            }

            if (ClientConnection.ClientID.IsAuthenticated)
            {
                UserLocation loc = new UserLocation(ClientConnection.ClientID.Username);
                lock (UserLocations)
                {
                    Int32 index = UserLocations.IndexOf(loc);
                    if (index > -1)
                    {
                        loc = UserLocations[index];
                        UserLocations.Remove(loc);
                        lock (ClientConnectionList)
                        {
                            ClientConnectionList.NotifyAllAuthenticatedClients(new RemoveGeoPointMessage(loc));
                        }
                        args.Result = ClientConnection.ClientID.Username + " disconnected from server, other clients has been notified";
                    }
                    else
                    {
                        args.Result = ClientConnection.ClientID.Username + " disconnected from server";
                    }
                }
            }
            else
            {
                args.Result = ClientConnection.RemoteEndPoint.ToString() + " disconnected from server";
            }
        }
        protected override void Execute(object sender, System.ComponentModel.DoWorkEventArgs args)
        {
            UserLocation loc = new UserLocation(GeoPointMessage.UserLocation.Username);
            lock (UserLocations)
            {
                Int32 index = UserLocations.IndexOf(loc);
                if (index > -1)
                {
                    UserLocations.RemoveAt(index);
                    UserLocations.Insert(index, GeoPointMessage.UserLocation);
                    lock (ClientConnectionList)
                    {
                        ClientConnectionList.NotifyAllAuthenticatedClientsExcept(ClientConnection, GeoPointMessage); // notify other observers
                    }
                }
                else
                {
                    UserLocations.Add(GeoPointMessage.UserLocation);
                    lock (ClientConnectionList)
                    {
                        ClientConnectionList.NotifyAllAuthenticatedClientsExcept(ClientConnection, GeoPointMessage); // notify other observers
                    }
                }
            }

            args.Result = ClientConnection.ClientID.Username + " broadcasted his location, other clients were notified";
        }
Ejemplo n.º 3
0
 public GeoPointMessage(UserLocation userLocation)
 {
     UserLocation = userLocation;
 }
Ejemplo n.º 4
0
        public override AbstractMessage Parse(String xml)
        {
            XmlTextReader reader = new XmlTextReader(new StringReader(xml));
            AbstractMessage returnee = null;

            String element = "";
            String username = "";
            Boolean isAccepted = false;
            GeoPoint geoPoint = null;
            Int32 longtitude = 0;
            Int32 latitude = 0;
            Int32 gender = 0x0A0; // please set to initial gender => male
            Int32 bgColour = 0x0B7; // colour => orange
            List<UserLocation> userLocations = null;
            UserLocation userLocation = null;

            try
            {
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.CDATA:
                            switch (element)
                            {
                                default:
                                    break;
                            }
                            break;

                        case XmlNodeType.Element:
                            element = reader.Name; // define last known element
                            switch (element)
                            {
                                case "UserLocations":
                                    userLocations = new List<UserLocation>();
                                    break;
                                default:
                                    break;
                            }
                            break;

                        case XmlNodeType.EndElement:
                            switch (reader.Name)
                            {
                                case "InitRequestMessage":
                                    returnee = new InitRequestMessage(username);
                                    break;
                                case "InitResponseMessage":
                                    returnee = new InitResponseMessage(isAccepted);
                                    break;
                                case "GeoPointMessage":
                                    returnee = new GeoPointMessage(userLocation);
                                    break;
                                case "MultipleGeoPointRequestMessage":
                                    returnee = new MultipleGeoPointRequestMessage();
                                    break;
                                case "MultipleGeoPointResponseMessage":
                                    returnee = new MultipleGeoPointResponseMessage(userLocations);
                                    break;
                                case "RemoveGeoPointMessage":
                                    returnee = new RemoveGeoPointMessage(userLocation);
                                    break;
                                case "UserLocation":
                                    if (userLocations != null)
                                    {
                                        userLocations.Add(new UserLocation(username, geoPoint, gender, bgColour));
                                    }
                                    else
                                    {
                                        userLocation = new UserLocation(username, geoPoint, gender, bgColour);
                                    }
                                    break;
                                case "GeoPoint":
                                    geoPoint = new GeoPoint(latitude, longtitude);
                                    break;
                                default:
                                    break;
                            }
                            break;
                        case XmlNodeType.Text:
                            switch (element)
                            {
                                case "Username":
                                    username = reader.Value;
                                    break;
                                case "IsAccepted":
                                    isAccepted = Boolean.Parse(reader.Value);
                                    break;
                                case "Latitude":
                                    latitude = Int32.Parse(reader.Value);
                                    break;
                                case "Longtitude":
                                    longtitude = Int32.Parse(reader.Value);
                                    break;
                                case "Gender":
                                    gender = int.Parse(reader.Value);
                                    break;
                                case "BgColour":
                                    bgColour = int.Parse(reader.Value);
                                    break;
                                default:
                                    break;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            catch
            {
                throw new XmlException("Unrecognized XML Format...");
            }
            reader.Close();
            if (returnee != null)
            {
                return returnee;
            }
            else
            {
                throw new XmlException("Unrecognized XML Format...");
            }
        }