Ejemplo n.º 1
0
        public void SendMessage(DataMessage message)
        {
            Console.WriteLine("Call SendMessage");
            int id = AddMessageIntoDatabase(message);

            message.Id       = id;
            message.Sender   = _allUsers.Find(x => x.Username == message.Sender.Username);
            message.Receiver = _allUsers.Find(x => x.Username == message.Receiver.Username);
            Console.WriteLine("sender: " + message.Sender.Username + "; to: " + message.Receiver.Username + "; message: " + message.Message);
            lock (_synNotReceivedMessage)
            {
                _notReceivedMessages.Add(message);
            }

            if (_onlineUsers.ContainsKey(message.Receiver))
            {
                bool isReceived = false;
                try
                {
                    isReceived = _onlineUsers[message.Receiver].ReceiveMessage(message);
                }
                catch
                {
                    Console.WriteLine("Cant find user online to send message");
                    _onlineUsers.Remove(message.Receiver);
                }
                if (isReceived)
                {
                    lock (_synNotReceivedMessage)
                    {
                        UpdateReceivedMessageIntoDatabase(message);
                        _notReceivedMessages.Remove(message);
                    }
                }
            }
            else
            {
                //cur User is offline
            }
            Console.WriteLine("exit SendMessage service");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Change Message status into Received
        /// Whose Id = message.id
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        private bool UpdateReceivedMessageIntoDatabase(DataMessage message)
        {
            bool res = false;

            string query = "UPDATE Messages " +
                           "SET [status] = @status " +
                           "WHERE id = @id";

            using (SqlConnection connection = new SqlConnection(_strConnection))
            {
                SqlCommand command = new SqlCommand(query, connection);
                command.Parameters.AddWithValue("@status", (int)DataMessageStatus.Received);
                command.Parameters.AddWithValue("@id", message.Id);

                if (connection.State != System.Data.ConnectionState.Open)
                {
                    connection.Open();
                }
                res = 1 == command.ExecuteNonQuery();
            }

            return(res);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Insert message into database
        /// return message id that automatic genarated
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        private int AddMessageIntoDatabase(DataMessage msg)
        {
            int    result = -1;
            string query  = "INSERT INTO Messages (sender, receiver, time_sent, [message], [status]) " +
                            "OUTPUT inserted.id " +
                            "VALUES(@sender, @receiver, @time_sent, @message, @status)";

            using (SqlConnection connection = new SqlConnection(_strConnection))
            {
                SqlCommand command = new SqlCommand(query, connection);
                command.Parameters.AddWithValue("@sender", msg.Sender.Username);
                command.Parameters.AddWithValue("@receiver", msg.Receiver.Username);
                command.Parameters.AddWithValue("@time_sent", msg.SentTime);
                command.Parameters.AddWithValue("@message", msg.Message);
                command.Parameters.AddWithValue("@status", (int)DataMessageStatus.Sent);

                if (connection.State != System.Data.ConnectionState.Open)
                {
                    connection.Open();
                }
                result = int.Parse(command.ExecuteScalar().ToString());
            }
            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Load all users from database into _allUserss
        /// Load relationship of users
        /// Create directory for storing avatar image
        /// Load unreceived messages
        /// </summary>
        public static void Init()
        {
            //Init _allUsers
            String query = "SELECT username, name, gender " +
                           "FROM Users";

            using (SqlConnection connection = new SqlConnection(_strConnection))
            {
                SqlCommand command = new SqlCommand(query, connection);
                if (connection.State != System.Data.ConnectionState.Open)
                {
                    connection.Open();
                }
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    User user = new User()
                    {
                        IsOnline = false,
                        Name     = (string)reader["name"],
                        Username = (string)reader["username"]
                    };
                    if (reader["gender"].ToString().Length > 0)
                    {
                        user.IsMale = (bool)reader["gender"];
                    }
                    _allUsers.Add(user);
                    _relationship.Add(user, new NodeRelationship(user));
                }
            }

            //Init _relationship
            query = "SELECT foo, bar, relation " +
                    "FROM Relations";
            using (SqlConnection connection = new SqlConnection(_strConnection))
            {
                SqlCommand command = new SqlCommand(query, connection);
                if (connection.State != System.Data.ConnectionState.Open)
                {
                    connection.Open();
                }
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    string foo          = reader["foo"].ToString();
                    string bar          = reader["bar"].ToString();
                    int    relationType = (int)reader["relation"];
                    if (relationType == (int)RelationshipType.Friend)
                    {
                        User fooUser = _allUsers.Find(x => x.Username == foo);
                        User barUser = _allUsers.Find(x => x.Username == bar);
                        _relationship[fooUser].Friends.Add(_relationship[barUser]);
                        _relationship[barUser].Friends.Add(_relationship[fooUser]);
                    }
                    else if (relationType == (int)RelationshipType.Pending)
                    {
                        User fooUser = _allUsers.Find(x => x.Username == foo);
                        User barUser = _allUsers.Find(x => x.Username == bar);
                        _relationship[fooUser].SentPendingRequest.Add(_relationship[barUser]);
                        _relationship[barUser].ReceivedPendingRequest.Add(_relationship[fooUser]);
                    }
                }
            }

            //Init directory
            DirectoryInfo dir = new DirectoryInfo(AVATAR_LOCATION);

            if (!dir.Exists)
            {
                dir.Create();
            }

            //load unreceived messages
            query = "SELECT id, sender, receiver, time_sent, message " +
                    "FROM Messages " +
                    "WHERE status = @NotReceivedStatus";
            using (SqlConnection connection = new SqlConnection(_strConnection))
            {
                SqlCommand command = new SqlCommand(query, connection);
                command.Parameters.AddWithValue("@NotReceivedStatus", (int)DataMessageStatus.Sent);

                if (connection.State != System.Data.ConnectionState.Open)
                {
                    connection.Open();
                }

                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    DataMessage msg = new DataMessage()
                    {
                        Id       = int.Parse(reader["id"].ToString()),
                        Message  = (string)reader["message"],
                        Receiver = _allUsers.Find(x => x.Username == (string)reader["receiver"]),
                        Sender   = _allUsers.Find(x => x.Username == (string)reader["sender"]),
                        SentTime = (DateTime)reader["time_sent"]
                    };
                    _notReceivedMessages.Add(msg);
                }
            }
        }