Exemplo n.º 1
0
 public ConcreteChatServer(string ipString, int portNumber, int capacity)
 {
     ipEndPoint = new IPEndPoint(IPAddress.Parse(ipString), portNumber);
     chatSystem = new ServerChatSystem();
     handlers   = new List <IClientHandler>();
     maxUsers   = capacity;
     working    = false;
 }
    public void handleRequest(List <IClientHandler> allHandlers, IServerChatSystem chatSystem,
                              IClientHandler handlerThread, byte[] messageBytes)
    {
        Console.WriteLine("DEBUG: {0} request received", "add new conversation");
        //decoding request - first 4 bytes are length of proposed conversation name, next there is the name
        List <string> namesOfParticipants = new List <string>();
        int           index        = 0;
        int           stringLength = BitConverter.ToInt32(messageBytes, 0);

        index += 4;
        string proposedConversationName = Encoding.UTF8.GetString(messageBytes, index, stringLength);

        index += stringLength;
        //the rest of the request are some number of pairs: (4 bytes describing length of user name, name of user to add to conversation)
        while (index < messageBytes.Length)
        {
            stringLength = BitConverter.ToInt32(messageBytes, index);
            index       += 4;
            namesOfParticipants.Add(Encoding.UTF8.GetString(messageBytes, index, stringLength));
            index += stringLength;
        }

        Console.WriteLine("DEBUG: trying to add conversation");
        byte[] reply = new byte[1];
        lock (allHandlers)
        {
            Conversation newConversation =
                chatSystem.AddConversation(proposedConversationName, namesOfParticipants.ToArray());
            if (newConversation == null)
            {
                reply[0] = 0;                 //conversation could not be created
            }
            else
            {
                reply[0] = 1;
                ConversationUpdates conversationUpdates = newConversation.GetUpdates(DateTime.MinValue);
                byte[] msg = conversationUpdates.Serialize(new ConcreteSerializer()).ToArray();
                //if conversation created successfully, send it to all participating users that are currently connected
                foreach (var handler in allHandlers.FindAll(h => namesOfParticipants.Contains(h.HandledUserName)))
                {
                    handler.sendMessage(5, msg);
                }
            }
        }

        handlerThread.sendMessage(1, reply);
    }
        /// <summary>
        /// Class handling request to create new user.
        /// </summary>
        public void handleRequest(List <IClientHandler> allHandlers, IServerChatSystem chatSystem, IClientHandler handlerThread, byte[] messageBytes)
        {
            Console.WriteLine("DEBUG: {0} request received", "add new user");
            //decoding request - all bytes are proposed user name
            string proposedName = Encoding.UTF8.GetString(messageBytes);

            Console.WriteLine("DEBUG: trying to add new user");
            IUser newUser = null;

            lock (allHandlers)
            {
                newUser = chatSystem.AddNewUser(proposedName);
            }
            byte[] reply = new byte[1];
            reply[0] = (newUser == null) ? (byte)0 : (byte)1; //if user was not created (eg. user name taken) indicate failure
            handlerThread.sendMessage(1, reply);
        }
Exemplo n.º 4
0
    /// <summary>
    /// Class handling request to send info about particular user.
    /// </summary>
    public void handleRequest(List <IClientHandler> allHandlers, IServerChatSystem chatSystem,
                              IClientHandler handlerThread, byte[] messageBytes)
    {
        Console.WriteLine("DEBUG: {0} request received", "send message");
        //decoding request - first 4 bytes are id of conversation, next 4 are id of message to which we reply the rest are message content bytes
        Guid requestedUserId = new Guid(messageBytes);

        Console.WriteLine("DEBUG: trying to send message");
        byte[] reply = new byte[1];
        lock (allHandlers)
        {
            IUser requestingUser = chatSystem.GetUser(handlerThread.HandledUserName);
            IUser user           =
                chatSystem.FindUser(requestedUserId);
            if (user != null)
            {
                // Check if exists at least one common conversation of these two users
                IEnumerable <Conversation> commonConversations =
                    chatSystem.getConversationsOfUser(handlerThread.HandledUserName);
                bool canRespond =
                    commonConversations.Any(conversation => conversation.Users.Any(u => u.ID == requestedUserId));
                if (canRespond)
                {
                    reply[0] = 1;
                    byte[] msg = user.Serialize(new ConcreteSerializer()).ToArray();
                    handlerThread.sendMessage(7, msg);                     //sent message - type 7
                }
                else
                {
                    reply[0] = 0;
                }
            }
            else
            {
                reply[0] = 0;
            }
        }

        handlerThread.sendMessage(1, reply);
    }
Exemplo n.º 5
0
    /// <summary>
    /// Class handling request to send message to conversation.
    /// </summary>
    public void handleRequest(List <IClientHandler> allHandlers, IServerChatSystem chatSystem,
                              IClientHandler handlerThread, byte[] messageBytes)
    {
        Console.WriteLine("DEBUG: {0} request received", "send message");
        //decoding request - first 4 bytes are id of conversation, next 4 are id of message to which we reply the rest are message content bytes
        Message message = new Message(new MemoryStream(messageBytes), new ConcreteDeserializer());

        Console.WriteLine("DEBUG: trying to send message");
        byte[] reply = new byte[1];
        lock (allHandlers)
        {
            Message sentMessage =
                chatSystem.SendMessage(
                    message.ConversationId,
                    message.AuthorID,
                    message.TargetId,
                    message.Content,
                    DateTime.Now);
            if (sentMessage != null)
            {
                //if successful conversation id with serialized message are broadcasted to all connected users from this conversation
                var conversationId = sentMessage.ConversationId;
                reply[0] = 1;
                byte[]       msg          = sentMessage.Serialize(new ConcreteSerializer()).ToArray();
                Conversation conversation = chatSystem.FindConversation(conversationId);
                foreach (var handler in allHandlers.FindAll(h =>
                                                            conversation.Users.Any(u => u.Name == h.HandledUserName)))
                {
                    handler.sendMessage(6, msg);                     //sent message - type 6
                }
            }
            else
            {
                reply[0] = 0;
            }
        }

        handlerThread.sendMessage(1, reply);
    }
Exemplo n.º 6
0
        /// <summary>
        /// Class handling request to log in.
        /// </summary>
        public void handleRequest(List <IClientHandler> allHandlers, IServerChatSystem chatSystem,
                                  IClientHandler handlerThread, byte[] messageBytes)
        {
            Console.WriteLine("DEBUG: {0} request received", "logIn");
            //decoding request - all bytes are user name under which they want to log in
            string userName = Encoding.UTF8.GetString(messageBytes);

            Console.WriteLine("DEBUG: requested logIn");
            byte[] reply = new byte[1024 * 256];
            lock (allHandlers)
            {
                IUser user = chatSystem.GetUser(userName);
                if (handlerThread.HandledUserName != null || user == null ||
                    allHandlers.Exists(h => h.HandledUserName == userName))
                {
                    //if this client is already logged in or there is no user with this user name or this user is already logged in
                    reply[0] = 0;                     //indicate that log in failed
                }
                else
                {
                    reply[0] = 1;
                    handlerThread.HandledUserName = userName;
                    // Send serialized User, so that the client can know the user's name and id
                    var serializedUser = (chatSystem.GetUser(userName) as User)
                                         .Serialize(new ConcreteSerializer()).ToArray();
                    Array.Copy(serializedUser, 0, reply, 1, serializedUser.Length);
                    //if login successful, send to this client all conversations in which this user takes part.
                    foreach (var conversation in user.Conversations)
                    {
                        byte[] msg = conversation.Serialize(new ConcreteSerializer()).ToArray();
                        handlerThread.sendMessage(5, msg);
                    }
                }
            }
            handlerThread.sendMessage(1, reply);
        }
Exemplo n.º 7
0
 public void handleRequest(List <IClientHandler> allHandlers, IServerChatSystem chatSystem,
                           IClientHandler handlerThread, byte[] messageBytes)
 {
     Console.WriteLine("DEBUG: {0} request received", "leave conversation");
     //decoding request - first 4 bytes are id of conversation which the user wants to leave
     Guid   conversationId = new Guid(messageBytes[0..16]);
Exemplo n.º 8
0
 public void handleRequest(List <IClientHandler> allHandlers, IServerChatSystem chatSystem, IClientHandler handlerThread, byte[] messageBytes)
 {
     Console.WriteLine("DEBUG: unknown request receive");
 }
Exemplo n.º 9
0
 public void handleRequest(List <IClientHandler> allHandlers, IServerChatSystem chatSystem,
                           IClientHandler handlerThread, byte[] messageBytes)
 {
     Console.WriteLine("DEBUG: {0} request received", "add user to conversation");
     //decoding request - first 16 bytes are id of conversation to which the user is to be added, following bytes are user name
     Guid   conversationId = new Guid(messageBytes[0..16]);
 /// <summary>
 /// Class handling request to disconnect.
 /// </summary>
 public void handleRequest(List <IClientHandler> allHandlers, IServerChatSystem chatSystem, IClientHandler handlerThread, byte[] messageBytes)
 {
     Console.WriteLine("DEBUG: {0} request received", "disconnect");
     handlerThread.shutdown(); //shutdown the handler
 }