Example #1
0
 public static chat.model.Message getMessage(ChatRequest request)
 {
     chat.model.User    sender   = new chat.model.User(request.Message.SenderId);
     chat.model.User    receiver = new chat.model.User(request.Message.ReceiverId);
     chat.model.Message message  = new chat.model.Message(sender, receiver, request.Message.Text);
     return(message);
 }
Example #2
0
        public static ChatRequest createSendMesssageRequest(chat.model.Message message)
        {
            proto.Message messageDTO = proto.Message.CreateBuilder().
                                       SetSenderId(message.Sender.Id)
                                       .SetReceiverId(message.Receiver.Id)
                                       .SetText(message.Text).Build();
            ChatRequest request = ChatRequest.CreateBuilder()
                                  .SetType(ChatRequest.Types.Type.SendMessage)
                                  .SetMessage(messageDTO).Build();

            return(request);
        }
Example #3
0
 public virtual void messageReceived(chat.model.Message message)
 {
     //MessageDTO mdto = DTOUtils.getDTO(message);
     Console.WriteLine("Message received  " + message);
     try
     {
         sendResponse(ProtoUtils.createNewMessageResponse(message));
     }
     catch (Exception e)
     {
         throw new ChatException("Sending error: " + e);
     }
 }
Example #4
0
        public static ChatResponse createNewMessageResponse(chat.model.Message message)
        {
            proto.Message messageDTO = proto.Message.CreateBuilder().
                                       SetSenderId(message.Sender.Id)
                                       .SetReceiverId(message.Receiver.Id)
                                       .SetText(message.Text).Build();

            ChatResponse response = ChatResponse.CreateBuilder()
                                    .SetType(ChatResponse.Types.Type.NewMessage)
                                    .SetMessage(messageDTO).Build();

            return(response);
        }
Example #5
0
        public static ChatRequest createSendMesssageRequest(chat.model.Message message)
        {
            proto.Message messageDTO = new proto.Message
            {
                SenderId   = message.Sender.Id,
                ReceiverId = message.Receiver.Id,
                Text       = message.Text
            };

            ChatRequest request = new ChatRequest {
                Type = ChatRequest.Types.Type.SendMessage, Message = messageDTO
            };

            return(request);
        }
Example #6
0
        public static ChatResponse createNewMessageResponse(chat.model.Message message)
        {
            proto.Message messageDTO = new proto.Message
            {
                SenderId   = message.Sender.Id,
                ReceiverId = message.Receiver.Id,
                Text       = message.Text
            };

            ChatResponse response = new ChatResponse {
                Type = ChatResponse.Types.Type.NewMessage, Message = messageDTO
            };

            return(response);
        }
Example #7
0
        private ChatResponse handleRequest(ChatRequest request)
        {
            ChatResponse response = null;

            ChatRequest.Types.Type reqType = request.Type;
            switch (reqType)
            {
            case ChatRequest.Types.Type.Login:
            {
                Console.WriteLine("Login request ...");
                //LoginRequest logReq =(LoginRequest)request;
                //UserDTO udto =logReq.User;
                chat.model.User user = ProtoUtils.getUser(request);
                try
                {
                    lock (server)
                    {
                        server.login(user, this);
                    }
                    return(ProtoUtils.createOkResponse());
                }
                catch (ChatException e)
                {
                    connected = false;
                    return(ProtoUtils.createErrorResponse(e.Message));
                }
            }

            case ChatRequest.Types.Type.Logout:
            {
                Console.WriteLine("Logout request");
                //LogoutRequest logReq =(LogoutRequest)request;
                //UserDTO udto =logReq.User;
                chat.model.User user = ProtoUtils.getUser(request);
                try
                {
                    lock (server)
                    {
                        server.logout(user, this);
                    }
                    connected = false;
                    return(ProtoUtils.createOkResponse());
                }
                catch (ChatException e)
                {
                    return(ProtoUtils.createErrorResponse(e.Message));
                }
            }

            case ChatRequest.Types.Type.SendMessage:
            {
                Console.WriteLine("SendMessageRequest ...");
                //SendMessageRequest senReq =(SendMessageRequest)request;
                //MessageDTO mdto =senReq.Message;
                chat.model.Message message = ProtoUtils.getMessage(request);
                try
                {
                    lock (server)
                    {
                        server.sendMessage(message);
                    }
                    return(ProtoUtils.createOkResponse());
                }
                catch (ChatException e)
                {
                    return(ProtoUtils.createErrorResponse(e.Message));
                }
            }

            case ChatRequest.Types.Type.GetLoggedFriends:
            {
                Console.WriteLine("GetLoggedFriends Request ...");
                //GetLoggedFriendsRequest getReq =(GetLoggedFriendsRequest)request;
                //UserDTO udto =getReq.User;
                chat.model.User user = ProtoUtils.getUser(request);  //DTOUtils.getFromDTO(udto);
                try
                {
                    chat.model.User[] friends;
                    lock (server)
                    {
                        friends = server.getLoggedFriends(user);
                    }
                    //UserDTO[] frDTO =DTOUtils.getDTO(friends);
                    return(ProtoUtils.createLoggedFriendsResponse(friends));
                }
                catch (ChatException e)
                {
                    return(ProtoUtils.createErrorResponse(e.Message));
                }
            }
            }
            return(response);
        }