Пример #1
0
        /* The GetFriendList MessageHandler
         * It handles messages of ADD_FRIEND_REQ type.
         */
        private static void AddFriend(RMessage message, TcpClient connection)
        {
            Console.WriteLine("AddFriend");
            uint thisUserId = ServerCore.GetIdByConnection(connection);
            UserData friend = UserConnector.GetUser((String)message.Data);
            UserData thisUser = UserConnector.GetUser(thisUserId);
            if(friend == null)
                return;
            if (friend.Id == thisUserId)
                return;
            List<FriendshipData> friendships = FriendshipConnector.GetFriendshipList(thisUserId);
            foreach(FriendshipData friendshipIt in friendships)
                if (friendshipIt.Former == friend.Id || friendshipIt.Latter == friend.Id)
                    return;
            FriendshipData friendship = new FriendshipData(thisUserId, friend.Id, false);
            FriendshipConnector.AddFriendship(friendship);

            ClientWorker formerWorker = ServerCore.GetWorkerById(friendship.Former);
            ClientWorker latterWorker = ServerCore.GetWorkerById(friendship.Latter);

            RMessage replyMessageFormer = new RMessage(MessageType.ADD_FRIEND_REPLY, friend);
            RMessage replyMessageLatter = new RMessage(MessageType.ADD_FRIEND_REPLY, thisUser);
            RMessage replyMessage = new RMessage(MessageType.ADD_FRIENDSHIP_REPLY, friendship);

            if (formerWorker != null)
            {
                formerWorker.SendMessage(replyMessageFormer);
                formerWorker.SendMessage(replyMessage);
            }
            if (latterWorker != null) {
                latterWorker.SendMessage(replyMessageLatter);
                latterWorker.SendMessage(replyMessage);
            }
        }
Пример #2
0
 /* The PartnerInfoRequested MessageHandler
  * It handles messages of PARTNER_INFO_REQUEST type.
  */
 private static void PartnerInfoRequested(RMessage message, TcpClient connection)
 {
     Console.WriteLine("PartnerInfoRequested");
     UserData partner = UserConnector.GetUserById((uint)message.Data);
     RMessage replyMessage = new RMessage(MessageType.PARTNER_INFO_REPLY,partner);
     ServerCore.GetWorkerByConnection(connection).SendMessage(replyMessage);
 }
Пример #3
0
 /* The GetDomainQuestions MessageHandler
  * It handles messages of GET_DOMAIN_QUESTIONS_REQUEST type.
  */
 private static void GetDomainQuestions(RMessage message, TcpClient connection)
 {
     Console.WriteLine("GetDomainQuestions");
     List<QuestionData> questions = QuestionConnector.GetDomainQuestions((DomainData)message.Data);
     RMessage replyMessage = new RMessage(MessageType.GET_DOMAIN_QUESTIONS_REPLY, questions);
     ServerCore.GetWorkerByConnection(connection).SendMessage(replyMessage);
 }
Пример #4
0
 public void AddMessage(RMessage message)
 {
     lock (mutex)
     {
         timeout[message] = TIMEOUT_TICKS;
         messageQueue.Add(message);
     }
 }
Пример #5
0
 /* The GetAllDomains MessageHandler
  * It handles messages of GET_ALL_DOMAINS_REQUEST type.
  */
 private static void GetAllDomains(RMessage message, TcpClient connection)
 {
     Console.WriteLine("GetAllDomains request");
     RMessage replyMessage =
         new RMessage(
             MessageType.GET_ALL_DOMAINS_REPLY,
             DomainConnector.GetAllDomains());
     ServerCore.GetWorkerByConnection(connection).SendMessage(replyMessage);
 }
Пример #6
0
 /* The CheckUserDomainValidation MessageHandler
  * It handles messages of GET_USER_CERTIFIED_DOMAINS_REQUEST type.
  */
 private static void CheckUserDomainValidation(RMessage message, TcpClient connection)
 {
     Console.WriteLine("GetUserDomainValidation");
     List<Object> resultContent = new List<Object>();
     resultContent.Add((Object)((UserData)message.Data));
     resultContent.Add((Object)DomainConnector.GetUserValidations(((UserData)message.Data).Id));
     RMessage replyMessage = new RMessage(MessageType.GET_USER_CERTIFIED_DOMAINS_REPLY, resultContent);
     ServerCore.GetWorkerByConnection(connection).SendMessage(replyMessage);
 }
Пример #7
0
 public static void SendMessage(TcpClient connection, RMessage message)
 {
     NetworkStream writer = connection.GetStream();
     byte[] buffer = Serial.ObjectToByteArray(message);
     UInt32 length = (UInt32)(buffer.Length);
     writer.Write(Serial.ObjectToByteArray(length), 0, Serial.ObjectToByteArray(length).Length);
     writer.Flush();
     writer.Write(buffer,0,buffer.Length);
     writer.Flush();
 }
Пример #8
0
 /* The GetUserInfo MessageHandler
  * It handles messages of GET_USER_INFO_REQUEST type.
  */
 private static void GetUserInfo(RMessage message, TcpClient connection)
 {
     UserData user = (UserData)message.Data;
     user = UserConnector.GetUserById(user.Id);
     List<Object> list = new List<Object>();
     list.Add(user);
     list.Add(DomainConnector.GetAllDomains()); // all domains
     list.Add(DomainConnector.GetUserValidations(user.Id)); // domains for which the user has certificates
     RMessage replyMessage = new RMessage(MessageType.GET_USER_INFO_REPLY, list);
     ServerCore.GetWorkerByConnection(connection).SendMessage(replyMessage);
 }
Пример #9
0
 private static void AddAnswer(RMessage message, TcpClient connection)
 {
     Console.WriteLine("ChangeQuestionStatus");
     AnswerData answer = (AnswerData)message.Data;
     LogConnector.AddAnswer(answer);
     UserConnector.UpdateUserRank(answer.Owner, Ranking.ADD_ANSWER);
     foreach (ClientWorker workersIterator in ServerCore.GetAllWorkers())
     {
         workersIterator.SendMessage(new RMessage(MessageType.ADD_ANSWER_REPLY, answer));
     }
 }
Пример #10
0
 /* The GetAllDomainsOrdered MessageHandler
  * It handles messages of GET_ALL_DOMAINS_ORDERED_REQUEST type.
  */
 private static void GetAllDomainsOrdered(RMessage message, TcpClient connection)
 {
     uint uid = ServerCore.GetIdByConnection(connection);
     List<DomainData> validations = DomainConnector.GetUserValidations(uid);
     List<DomainData> nonValidations = DomainConnector.GetUserNonValidations(uid);
     validations.AddRange(nonValidations);
      RMessage replyMessage =
         new RMessage(
             MessageType.GET_ALL_DOMAINS_ORDERED_REPLY,
             validations);
     ServerCore.GetWorkerByConnection(connection).SendMessage(replyMessage);
 }
Пример #11
0
        public static void SendMessage(TcpClient connection, RMessage message)
        {
            NetworkStream writer = connection.GetStream();

            byte[] buffer = Serial.ObjectToByteArray(message);
            UInt32 length = (UInt32)(buffer.Length);

            writer.Write(Serial.ObjectToByteArray(length), 0, Serial.ObjectToByteArray(length).Length);
            writer.Flush();
            writer.Write(buffer, 0, buffer.Length);
            writer.Flush();
        }
Пример #12
0
 public static void OnNewMessage(RMessage msg, TcpClient connection)
 {
     Dictionary<MessageHandler, TestTriggerCondition> copy_hash;
     lock (hashMutex)
     {
         copy_hash =
             new Dictionary<MessageHandler, TestTriggerCondition>(hash);
     }
     foreach (KeyValuePair<MessageHandler, TestTriggerCondition> entry in copy_hash)
         if (entry.Value(msg))
             entry.Key(msg,connection);
 }
Пример #13
0
 public void SendMessage(RMessage message)
 {
     try
     {
         WireMessage.SendMessage(connection, message);
     }
     catch (Exception)
     {
         // The connection is down, we destroy this ClientWorker
         CloseConnection();
         chatThread.Abort();
     }
 }
Пример #14
0
 /* The CheckResourceVote MessageHandler
  * It handles messages of RANK_CHECK_RESOURCE_VOTE_REQUEST type.
  */
 private static void CheckResourceVote(RMessage message, TcpClient connection)
 {
     Console.WriteLine("CheckResourceVote");
     UserData usr = (UserData)(((List<Object>)message.Data)[0]);
     ResourceData res = (ResourceData)(((List<Object>)message.Data)[1]);
     ResourceVoteData resVote = ResourceVoteConnector.CheckResourceVote(usr, res);
     List<Object> replyContent = new List<Object>();
     replyContent.Add(usr);
     replyContent.Add(res);
     replyContent.Add(resVote);
     RMessage replyMessage = new RMessage(MessageType.RANK_CHECK_RESOURCE_VOTE_REPLY, replyContent);
     ServerCore.GetWorkerByConnection(connection).SendMessage(replyMessage);
 }
Пример #15
0
 /* The GetFriendList MessageHandler
  * It handles messages of ADD_SAY_REQUEST type.
  */
 private static void AddSay(RMessage message, TcpClient connection)
 {
     Console.WriteLine("AddSay");
     SayData say = (SayData)(message.Data);
     uint id = say.Id;
     say.Id = ServerCore.GetIdByConnection(connection);
     if (ServerCore.GetWorkerById(id) != null)
     {
         RMessage replyMessage = new RMessage(MessageType.ADD_SAY_REPLY, say);
         ServerCore.GetWorkerById(id).SendMessage(replyMessage);
     }
     UserConnector.UpdateUserRank(say.Id, Ranking.ADD_SAY);
 }
Пример #16
0
        /* The SignIn MessageHandler
         * It handles messages of SIGN_IN_REQUEST type.
         */
        private static void SignIn(RMessage message, TcpClient connection)
        {
            RMessage replyMessage;

            UserData user1 = (UserData)message.Data;
             	UserData user2 = UserConnector.GetUser(user1.Username);

            // Check if the password match
            if (user2 != null && user2.Password != user1.Password)
                user2 = null;

            // If the credentials do not match, refuse the authentification
            if (user2 == null)
            {
                ServerCore.GetWorkerByConnection(connection).SendMessage(
                    new RMessage(MessageType.SIGN_IN_REPLY, null));
                return;
            }

            // If the user can authenticate, disconnect another user connected
            // with the same username
            if (user2 != null)
            {
                TcpClient previousConnection = ServerCore.GetConnectionById(user2.Id);
                if (previousConnection != null)
                {
                    ServerCore.RemoveIdConnectionMapping(user2.Id, previousConnection);
                    replyMessage =
                        new RMessage(
                            MessageType.SIGN_OUT_REPLY,
                            user2);
                    ServerCore.GetWorkerByConnection(previousConnection)
                        .SendMessage(replyMessage);
                }
            }

            List<Object> payload = new List<Object>();
            payload.Add(user2); // user data
            payload.Add(DomainConnector.GetAllDomains()); // all domains
            payload.Add(DomainConnector.GetUserValidations(user2.Id)); // domains for which the user has certificates
            replyMessage =
                new RMessage(
                    MessageType.SIGN_IN_REPLY,
                    payload);
            ServerCore.GetWorkerByConnection(connection).SendMessage(replyMessage);

            // If the user connected successfully, store the connection mapping
            if (user2 != null)
                ServerCore.AddIdConnectionMapping(user2.Id, connection);
        }
Пример #17
0
 public static void FireCallback(RMessage msg)
 {
     lock (clb_mutex)
     {
         Dictionary<CallbackFunction, TestTriggerCondition> copy_hash;
         lock (mutex)
         {
             copy_hash =
                 new Dictionary<CallbackFunction, TestTriggerCondition>(hash);
         }
         foreach (KeyValuePair<CallbackFunction, TestTriggerCondition> entry in copy_hash)
             if (entry.Value(msg))
                 entry.Key(msg);
     }
 }
Пример #18
0
        /* The AddReference MessageHandler
         * It handles messages of ADD_REFERENCE_REQUEST type.
         */
        private static void AddReference(RMessage message, TcpClient connection)
        {
            Console.WriteLine("AddReference");
            List<UInt32> ids = (List<UInt32>)message.Data;

            QuestionConnector.AddReference(ids);

            RMessage replyMessage = new RMessage(MessageType.ADD_QUESTION_REFERENCE_REPLY, null);
            //ServerCore.GetWorkerByConnection(connection).SendMessage(replyMessage);

            List<ClientWorker> allWorkers = ServerCore.GetAllWorkers();
            foreach (ClientWorker workersIterator in allWorkers)
            {
                workersIterator.SendMessage(replyMessage);
            }
        }
Пример #19
0
        /* The CreateNewUser MessageHandler
         * It handles messages of CREATE_NEW_USER_REQUEST type.
         */
        private static void CreateNewUser(RMessage message, TcpClient connection)
        {
            Console.WriteLine("CreateNewUser request");

            List<object> items = (List<object>)message.Data;
            UserData user = (UserData)items[0];
            List<DomainData> domains = (List<DomainData>)items[1];
            List<int> scores = (List<int>)items[2];

            double initialRank = 0;
            for (int i = 0; i < domains.Count; ++i)
                initialRank += GetRank(scores[i]);

            UserConnector.AddUser(user.Username, user.Password, user.Email, initialRank);
            user = UserConnector.GetUser(user.Username);

            foreach (DomainData item in domains)
                ValidationConnector.AddValidation(user.Id, item.ID);
        }
Пример #20
0
        /* The AddQuestion MessageHandler
         * It handles messages of ADD_QUESTION_REQUEST type.
         */
        private static void AddQuestion(RMessage message, TcpClient connection)
        {
            Console.WriteLine("AddQuestion");
            List<Object> data = (List<Object>)message.Data;
            QuestionData question0 = (QuestionData)data[0];
            List<DomainData> domains = (List<DomainData>)data[1];

            QuestionConnector.AddQuestion(question0);
            UserConnector.UpdateUserRank(question0.Owner, Ranking.ADD_QUESTION);

            UInt32 id = (UInt32)(Int64Connector.GetLastInsertId());
            QuestionData question = QuestionConnector.GetQuestion(id);

            QuestionConnector.AddDomainQuestion(id, domains);

            List<ClientWorker> allWorkers = ServerCore.GetAllWorkers();
            foreach (ClientWorker workersIterator in allWorkers)
            {
                workersIterator.SendMessage(new RMessage(MessageType.ADD_QUESTION_REPLY, question));
            }
        }
Пример #21
0
 /* The TriggerRule for the GetQuestion MessageHandler
  * The message must be of GET_ALL_QUESTIONS_REQUEST type to be
  * handled by this MessageHandler.
  */
 private static Boolean GetQuestionRule(RMessage message)
 {
     return message.Type == MessageType.GET_QUESTION_REQUEST;
 }
Пример #22
0
 /* The TriggerRule for the AddQuestion MessageHandler
  * The message must be of ADD_QUESTION_REQUEST type to be
  * handled by this MessageHandler.
  */
 private static Boolean AddQuestionRule(RMessage message)
 {
     return message.Type == MessageType.ADD_QUESTION_REQUEST;
 }
Пример #23
0
        /* The AddUserVote MessageHandler
         * It handles messages of ADD_USER_VOTE_REQUEST type.
         */
        private static void AddUserVote(RMessage message, TcpClient connection)
        {
            Console.WriteLine("AddUserVote");
            // Get the vote data from the message
            UserVoteData vote = (UserVoteData)message.Data;

            // Update the voter's rank
            UserVoteData rating = UserVoteConnector.CheckUserVote(vote);
            if (rating == null)
                UserConnector.UpdateUserRank(vote.Id_voter_user, Ranking.VOTE_USER);

            // Update the votee's rank
            UserConnector.VoteUser(vote);

            // Send the vote back to the client
            RMessage replyMessage = new RMessage(MessageType.ADD_USER_VOTE_REPLY, vote);
            ServerCore.GetWorkerByConnection(connection).SendMessage(replyMessage);

            // Send the updated user rank to all clients
            UserData user = UserConnector.GetUserById(vote.Id_votee_user);
            replyMessage = new RMessage(MessageType.RANK_USER_UPDATED, user);
            List<ClientWorker> allWorkers = ServerCore.GetAllWorkers();
            foreach (ClientWorker workersIterator in allWorkers)
            {
                workersIterator.SendMessage(replyMessage);
            }
        }
Пример #24
0
 /* The TriggerRule for the AddReference MessageHandler
  * The message must be of ADD_REFERENCE_REQUEST type to be
  * handled by this MessageHandler.
  */
 private static Boolean AddReferenceRule(RMessage message)
 {
     return message.Type == MessageType.ADD_QUESTION_REFERENCE_REQUEST;
 }
Пример #25
0
 private static Boolean AddAnswerRule(RMessage message)
 {
     return message.Type == MessageType.ADD_ANSWER_REQUEST;
 }
Пример #26
0
 /* The TriggerRule for the GetUserById MessageHandler
  * The message must be of GET_USER_BY_ID_REQUEST type to be
  * handled by this MessageHandler.
  */
 private static Boolean GetUserByIdRule(RMessage message)
 {
     return message.Type == MessageType.GET_USER_BY_ID_REQUEST;
 }
Пример #27
0
 /* The GetUserById MessageHandler
  * It handles messages of GET_USER_BY_ID_REQUEST type.
  */
 private static void GetUserById(RMessage message, TcpClient connection)
 {
     Console.WriteLine("GetUserById");
     UserData user = UserConnector.GetUser((UInt32)message.Data);
     RMessage replyMessage = new RMessage(MessageType.GET_USER_BY_ID_REPLY, user);
     ServerCore.GetWorkerByConnection(connection).SendMessage(replyMessage);
 }
Пример #28
0
 /* The TriggerRule for the GetResources MessageHandler
  * The message must be of GET_QUESTION_RESOURCES_REQUEST type to be
  * handled by this MessageHandler.
  */
 private static Boolean GetResourcesRule(RMessage message)
 {
     return message.Type == MessageType.GET_QUESTION_DOMAINS_RESOURCES_REQUEST;
 }
Пример #29
0
        /* The GetResources MessageHandler
         * It handles messages of GET_QUESTION_RESOURCES_REQUEST type.
         */
        private static void GetResources(RMessage message, TcpClient connection)
        {
            Console.WriteLine("GetResources");
            List<DomainData> domains = (List<DomainData>)message.Data;
            List<ResourceData> resources = new List<ResourceData>();

            for (int i = 0; i < domains.Count; i++)
            {
                List<ResourceData> res = ResourceConnector.GetDomainResources(domains[i]);
                for (int j = 0; j < res.Count; j++)
                    resources.Add(res[j]);
            }

            RMessage replyMessage = new RMessage(MessageType.GET_QUESTION_DOMAINS_RESOURCES_REPLY, resources);
            ServerCore.GetWorkerByConnection(connection).SendMessage(replyMessage);
        }
Пример #30
0
 /* The TriggerRule for the GetQuestionDomains MessageHandler
  * The message must be of GET_QUESTION_DOMAINS_REQUEST type to be
  * handled by this MessageHandler.
  */
 private static Boolean GetQuestionDomainsRule(RMessage message)
 {
     return message.Type == MessageType.GET_QUESTION_DOMAINS_REQUEST;
 }
Пример #31
0
        /* The GetQuestionResources MessageHandler
         * It handles messages of GET_QUESTION_RESOURCES_REQUEST type.
         */
        private static void GetQuestionResources(RMessage message, TcpClient connection)
        {
            Console.WriteLine("GetQuestionResources");
            QuestionData question = (QuestionData)message.Data;
            List<ResourceData> resources = ResourceConnector.GetQuestionResources(question.Id);

            RMessage replyMessage = new RMessage(MessageType.GET_QUESTION_RESOURCES_REPLY, resources);
            ServerCore.GetWorkerByConnection(connection).SendMessage(replyMessage);
        }