Exemple #1
0
 private int CallBackRoomRefresh(Data receive)
 {
     if (receive.Command == Data.DataType.GetDiscussionRoom)
     {
         DiscussionRoom room = (DiscussionRoom)receive.Other;
         if (room != null)
         {
             lastRefresh = DateTime.Now;
             actualRoom = room;
             this.Invoke((MethodInvoker)delegate()
             {
                 this.discussionRoomBindingSource.DataSource = actualRoom;
                 if (dataGridViewMessage.RowCount != 0)
                 {
                     dataGridViewMessage.CurrentCell = dataGridViewMessage.Rows[dataGridViewMessage.RowCount - 1].Cells[0];
                 }
             });
         }
     }
     else
     {
         //MessageBox.Show("ERROR!!!.");
     }
     return 0;
 }
Exemple #2
0
        private void button_Login_Click(object sender, EventArgs e)
        {
            Data loginRequest = new Data();
            loginRequest.Command = Data.DataType.Login;
            loginRequest.Text = txtUsername.Text;
            loginRequest.Other = txtPassword.Text;

            Program.callBackOnReceive.Enqueue(CallBackOnLogin);
            Program.client.Send(loginRequest);
        }
Exemple #3
0
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Data listUserRequest = new Data();
            listUserRequest.Command = Data.DataType.ListClient;
            Program.callBackOnReceive.Enqueue(CallBackListClient);
            Program.client.Send(listUserRequest);

            Data listRoomRequest = new Data();
            listRoomRequest.Command = Data.DataType.ListDiscussionRoom;
            Program.callBackOnReceive.Enqueue(CallBackListDiscussionRoom);
            Program.client.Send(listRoomRequest);
        }
Exemple #4
0
        /// <summary>
        /// Envoyer un message
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonSend_Click(object sender, EventArgs e)
        {
            string text = textBoxMessage.Text.Trim();

            //Envoi au serveur
            Data messageSendRequest = new Data();
            messageSendRequest.Command = Data.DataType.SendMessage;
            messageSendRequest.User = actualUser;
            messageSendRequest.Text = text;

            Program.client.Send(messageSendRequest);

            textBoxMessage.Clear();
        }
Exemple #5
0
        public void Send(Data msg)
        {
            byte[] byteData = Data.Serialize(msg);

            // TODO try adding a null byte
            if (byteData.Length == SocketState.BufferSize)
                throw new Exception("Won't be able to make difference between end of byte array and end of message.");
            else if (byteData.Length > SocketState.BufferSize)
            {
                throw new Exception("Message is too long. It is currently not supported.");
            }

            // Begin sending the data to the remote device.
            client.Client.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(SendCallback), client.Client);
        }
Exemple #6
0
        public void Send(Socket socket, Data msg)
        {
            byte[] byteData = Data.Serialize(msg);

            if (byteData.Length == SocketState.BufferSize)
                Console.WriteLine("Won't be able to make difference between end of byte array and end of message.");
            else if (byteData.Length > SocketState.BufferSize)
            {
                Console.WriteLine("Message is too long. It is currently not supported.");
            }
            else
            {
                socket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(this.SendCallback), socket);
            }
        }
        /// <summary>
        /// On ajoute la salle
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            room.Name = textBoxName.Text;
            room.Description = textBoxDescription.Text;

            if (String.IsNullOrEmpty(room.Name + room.Description))
            {
                MessageBox.Show("Il est nécessaire d'avoir un nom et une description.", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Data addRoomRequest = new Data();
            addRoomRequest.Command = Data.DataType.AddRoom;
            addRoomRequest.Other = room;
            Program.callBackOnReceive.Enqueue(CallBackAddRoom);
            Program.client.Send(addRoomRequest);
        }
Exemple #8
0
 private int CallBackOnLogin(Data received)
 {
     if (received.Command == Data.DataType.Login)
     {
         if (received.Text == "200")
         {
             this.Invoke((MethodInvoker)delegate() { this.Hide(); });
             HomeForm homeForm = new HomeForm(received.User);
             homeForm.ShowDialog();
             this.Invoke((MethodInvoker)delegate() { this.Show(); });
         }
         else if (received.Text == "401")
             MessageBox.Show("This user is already connected.");
         else if (received.Text == "404")
             MessageBox.Show("The username or password is incorrect.");
     }
     else
     {
         //MessageBox.Show("ERROR!!!.");
     }
     return 0;
 }
 private int CallBackAddRoom(Data received)
 {
     if (received.Command == Data.DataType.AddRoom)
     {
         if (received.Text == "201")
         {
             this.Invoke((MethodInvoker)delegate()
             {
                 this.DialogResult = System.Windows.Forms.DialogResult.OK;
                 this.Close();
             });
         }
         else
         {
             MessageBox.Show("Une salle existe déjà avec ce nom.", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
     else
     {
         //MessageBox.Show("ERROR!!!.");
     }
     return 0;
 }
Exemple #10
0
 private void HomeForm_FormClosed(object sender, FormClosedEventArgs e)
 {
     if (roomForm != null)
         roomForm.Close();
     Data disconnectRequest = new Data();
     disconnectRequest.Command = Data.DataType.Logout;
     disconnectRequest.Text = actualClient.UserName;
     Program.client.Send(disconnectRequest);
 }
Exemple #11
0
        /// <summary>
        /// Lorsque la fenêtre se ferme on enlève l'usager
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RoomForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Data leaveRoomRequest = new Data();
            leaveRoomRequest.Command = Data.DataType.LeaveRoom;
            leaveRoomRequest.Text = actualUser.UserName;

            Program.client.Send(leaveRoomRequest);
        }
Exemple #12
0
        private void refreshTimer_Tick(object sender, EventArgs e)
        {
            Data refreshRequest = new Data();
            refreshRequest.Command = Data.DataType.GetDiscussionRoom;
            refreshRequest.Text = actualUser.UserName;
            refreshRequest.Date = lastRefresh;

            Program.callBackOnReceive.Enqueue(CallBackRoomRefresh);
            Program.client.Send(refreshRequest);
        }
Exemple #13
0
 /// <summary>
 /// Double clique sur un utilisateur
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void listBoxUsers_DoubleClick(object sender, EventArgs e)
 {
     if (listBoxUsers.SelectedItem != null)
     {
         if (listBoxUsers.SelectedItem.ToString().Length != 0)
         {
             User selectedUser = listBoxUsers.SelectedItem as User;
             Data viewProfileRequest = new Data();
             viewProfileRequest.Command = Data.DataType.ViewProfile;
             viewProfileRequest.Text = selectedUser.UserName;
             Program.callBackOnReceive.Enqueue(CallBackViewProfile);
             Program.client.Send(viewProfileRequest);
         }
     }
 }
Exemple #14
0
        /// <summary>
        /// Lorsqu'on clic sur like ou dislike
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridViewMessage_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                Data data = (dataGridViewMessage.CurrentRow.DataBoundItem as Data);

                if (dataGridViewMessage.CurrentCell.ColumnIndex == 3)
                {
                    MessageBox.Show("Vous avez indiqué votre appréciation pour ce message.");
                    Data likeRequest = new Data();
                    likeRequest.Command = Data.DataType.Like;
                    likeRequest.User = data.User;
                    Program.client.Send(likeRequest);
                }
                else if (dataGridViewMessage.CurrentCell.ColumnIndex == 4)
                {
                    MessageBox.Show("Vous avez indiqué votre mécontentement pour ce message.");
                    dataGridViewMessage.CurrentCell.Style.BackColor = Color.Red;
                    dataGridViewMessage.Refresh();
                    Data dislikeRequest = new Data();
                    dislikeRequest.Command = Data.DataType.Dislike;
                    dislikeRequest.User = data.User;
                    Program.client.Send(dislikeRequest);
                }
                else if (dataGridViewMessage.CurrentCell.ColumnIndex == 5 && (actualUser.UserName == data.User.UserName))
                {
                    Data deleteMessageRequest = new Data();
                    deleteMessageRequest.Command = Data.DataType.DeleteMessage;
                    deleteMessageRequest.Text = actualUser.UserName;
                    deleteMessageRequest.Other = data.Id;
                    Program.client.Send(deleteMessageRequest);

                    //dataGridViewMessage.Rows.RemoveAt(e.RowIndex);
                    dataGridViewMessage.Cursor = Cursors.Arrow;
                }
            }
        }
Exemple #15
0
        static int ReceiveCallback(Tuple<Socket, Data> msg)
        {
            Socket client = msg.Item1;
            Data received = msg.Item2;
            Data response = new Data();

            User user;
            DiscussionRoom room;
            User updatedUser;

            switch (received.Command)
            {
                case Data.DataType.Login:
                    response.Command = Data.DataType.Login;
                    user = users.FirstOrDefault(x => x.UserName == received.Text
                                                && x.Password == (string)received.Other);
                    if (user != null)
                    {
                        if (!user.IsConnected)
                        {
                            user.IsConnected = true;
                            response.Text = "200";
                            response.User = user;
                            Console.WriteLine("User " + user.UserName + " logged in.");
                        }
                        else
                        {
                            // User already connected
                            response.Text = "401";
                            response.User = user;
                            Console.WriteLine("User " + user.UserName + " is already logged in.");
                        }
                    }
                    else
                    {
                        // Could not find user according to username + pwd
                        response.Text = "404";
                        Console.WriteLine("User " + received.Text + " doesn't exist with provided password.");
                    }
                    break;

                case Data.DataType.Logout:
                    //response.Command = Data.DataType.Logout;
                    user = users.FirstOrDefault(x => x.UserName == received.Text);
                    if (user != null)
                    {
                        foreach (DiscussionRoom r in rooms)
                            if (r.ClientList.Contains(user))
                                r.ClientList.Remove(user);
                        user.IsConnected = false;
                        Console.WriteLine("User " + user.UserName + " logged out.");
                        //response.Text = "ok";
                        //response.User = user;
                    }
                    //else
                    //{
                    //    response.Text = "reject";
                    //}
                    break;

                case Data.DataType.SendMessage:
                    user = users.FirstOrDefault(x => x.UserName == received.ClientUserName);
                    room = rooms.FirstOrDefault(x => x.ClientList.Any(y => y.UserName == user.UserName));
                    room.MessageList.Add(received);
                    room.LastModified = DateTime.Now;
                    user.NumMessages++;
                    Console.WriteLine("User " + user.UserName + " sent a message.");
                    break;

                case Data.DataType.GetDiscussionRoom:
                    response.Command = Data.DataType.GetDiscussionRoom;
                    user = users.FirstOrDefault(x => x.UserName == received.Text);
                    room = rooms.FirstOrDefault(x => x.ClientList.Any(y => y.UserName == user.UserName));
                    if(room.LastModified > received.Date)
                    {
                        response.Other = room;
                    }
                    break;

                case Data.DataType.Like:
                    user = users.FirstOrDefault(x => x.UserName == received.ClientUserName);
                    user.LikeNum++;
                    Console.WriteLine("User " + user.UserName + " gets a Like.");
                    break;

                case Data.DataType.Dislike:
                    user = users.FirstOrDefault(x => x.UserName == received.ClientUserName);
                    user.DislikeNum++;
                    Console.WriteLine("User " + user.UserName + " gets a Dislike.");
                    break;

                case Data.DataType.ViewProfile:
                    response.Command = Data.DataType.ViewProfile;
                    response.Other = users.FirstOrDefault(x => x.UserName == received.Text);
                    Console.WriteLine("GET " + received.Text + "'s profile.");
                    break;

                case Data.DataType.UpdateProfile:
                    response.Command = Data.DataType.UpdateProfile;
                    user = users.FirstOrDefault(x => x.UserName == received.Text);
                    updatedUser = received.User;
                    if (user.UserName != updatedUser.UserName
                        && users.Any(x => x.UserName == updatedUser.UserName))
                    {
                        response.Text = "401";
                        Console.WriteLine("User " + user.UserName + " profile modification refused.");
                    }
                    else
                    {
                        user.FirstName = updatedUser.FirstName;
                        user.LastName = updatedUser.LastName;
                        user.UserName = updatedUser.UserName;
                        user.Avatar = updatedUser.Avatar;
                        response.Text = "200";
                        Console.WriteLine("User " + user.UserName + "'s profile updated.");
                    }
                    break;

                case Data.DataType.ListClient:
                    response.Command = Data.DataType.ListClient;
                    response.Other = users;
                    Console.WriteLine("GET users list.");
                    break;

                case Data.DataType.ListDiscussionRoom:
                    response.Command = Data.DataType.ListDiscussionRoom;
                    response.Other = rooms;
                    Console.WriteLine("GET rooms list.");
                    break;

                case Data.DataType.EnterRoom:
                    response.Command = Data.DataType.EnterRoom;
                    user = users.FirstOrDefault(x => x.UserName == received.ClientUserName);
                    room = rooms.FirstOrDefault(x => x.Name == received.Text);
                    room.ClientList.Add(user);
                    response.Other = room;
                    room.LastModified = DateTime.Now;
                    Console.WriteLine("User " + user.UserName + " enters a room.");
                    break;

                case Data.DataType.AddRoom:
                    response.Command = Data.DataType.AddRoom;
                    room = (DiscussionRoom)received.Other;
                    if (rooms.Any(x => x.Name == room.Name))
                    {
                        response.Text = "400";
                        Console.WriteLine("Room creation canceled.");
                    }
                    else
                    {
                        response.Text = "201";
                        rooms.Add(room);
                        Console.WriteLine("Room '" + room.Name + "' created.");
                    }
                    break;

                case Data.DataType.LeaveRoom:
                    user = users.FirstOrDefault(x => x.UserName == received.Text);
                    room = rooms.FirstOrDefault(x => x.ClientList.Any(y => y.UserName == user.UserName));
                    room.ClientList.Remove(user);
                    room.LastModified = DateTime.Now;
                    Console.WriteLine("User " + user.UserName + " leaves room.");
                    break;

                case Data.DataType.DeleteMessage:
                    user = users.FirstOrDefault(x => x.UserName == received.Text);
                    room = rooms.FirstOrDefault(x => x.ClientList.Any(y => y.UserName == user.UserName));
                    room.MessageList.RemoveAll(x => x.Id == (int)received.Other);
                    room.LastModified = DateTime.Now;
                    Console.WriteLine("User " + user.UserName + " deletes a message.");
                    break;
            }

            if (response.Command != Data.DataType.Invalid)
            {
                server.Send(client, response);
            }

            return 0;
        }
Exemple #16
0
 public static byte[] Serialize(Data data)
 {
     using (var memoryStream = new MemoryStream())
     {
         (new BinaryFormatter()).Serialize(memoryStream, data);
         return memoryStream.ToArray();
     }
 }
 private int CallBackUpdateProfil(Data received)
 {
     if (received.Command == Data.DataType.UpdateProfile)
     {
         if (received.Text == "200")
         {
             this.Invoke((MethodInvoker)delegate()
             {
                 this.DialogResult = System.Windows.Forms.DialogResult.OK;
                 this.Close();
             });
         }
         else
         {
             MessageBox.Show("Le nom d'utilisateur est déjà prit.", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
             this.Invoke((MethodInvoker)delegate() { this.textBoxUserName.Text = oldClient.UserName; });
         }
     }
     else
     {
         //MessageBox.Show("ERROR!!!.");
     }
     return 0;
 }
Exemple #18
0
 /// <summary>
 /// Modification/affichage du profil et des stats
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void buttonProfil_Click(object sender, EventArgs e)
 {
     Data viewProfileRequest = new Data();
     viewProfileRequest.Command = Data.DataType.ViewProfile;
     viewProfileRequest.Text = actualClient.UserName;
     Program.callBackOnReceive.Enqueue(CallBackViewProfile);
     Program.client.Send(viewProfileRequest);
 }
Exemple #19
0
        private int CallBackEnterRoom(Data received)
        {
            if (received.Command == Data.DataType.EnterRoom)
            {
                DiscussionRoom room = (DiscussionRoom)received.Other;
                if (room == null)
                {
                    MessageBox.Show("Cet salle n'existe plus sous ce nom.", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    roomForm = new RoomForm(room, actualClient);
                    this.Invoke((MethodInvoker)delegate() { roomForm.Show(); });
                }

                Data listRoomRequest = new Data();
                listRoomRequest.Command = Data.DataType.ListDiscussionRoom;
                Program.callBackOnReceive.Enqueue(CallBackListDiscussionRoom);
                Program.client.Send(listRoomRequest);
            }
            else
            {
                //MessageBox.Show("ERROR!!!.");
            }
            return 0;
        }
Exemple #20
0
        private int CallBackViewProfile(Data received)
        {
            if (received.Command == Data.DataType.ViewProfile)
            {
                User user = (User)received.Other;
                if (user == null)
                {
                    MessageBox.Show("Cet utilisateur n'existe plus sous ce nom.", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    bool readOnly = user.UserName != actualClient.UserName;
                    UserProfilForm profilForm = new UserProfilForm(user, readOnly);
                    DialogResult result = DialogResult.None;
                    this.Invoke((MethodInvoker)delegate() { result = profilForm.ShowDialog(); });

                    if (!readOnly)
                    {
                        if (result == DialogResult.OK)
                        {
                            actualClient = user;
                        }
                        else
                        {
                            actualClient = profilForm.oldClient;
                        }

                        this.Invoke((MethodInvoker)delegate()
                        {
                            labelUserName.Text = actualClient.UserName;
                            labelAvatar.Image = (Bitmap)Resources.ResourceManager.GetObject(actualClient.Avatar);
                        });
                    }
                }

                Data listUserRequest = new Data();
                listUserRequest.Command = Data.DataType.ListClient;
                Program.callBackOnReceive.Enqueue(CallBackListClient);
                Program.client.Send(listUserRequest);
            }
            else
            {
                //MessageBox.Show("ERROR!!!.");
            }
            return 0;
        }
Exemple #21
0
 private int CallBackListDiscussionRoom(Data received)
 {
     if (received.Command == Data.DataType.ListDiscussionRoom)
     {
         this.Invoke((MethodInvoker)delegate()
         {
             listBoxChatRooms.DataSource = new List<DiscussionRoom>((List<DiscussionRoom>)received.Other);
         });
     }
     else
     {
         //MessageBox.Show("ERROR!!!.");
     }
     return 0;
 }
Exemple #22
0
 private int CallBackListClient(Data received)
 {
     if (received.Command == Data.DataType.ListClient)
     {
         this.Invoke((MethodInvoker)delegate()
         {
             listBoxUsers.DataSource = new List<User>((List<User>)received.Other);
         });
     }
     else
     {
         //MessageBox.Show("ERROR!!!.");
     }
     return 0;
 }
Exemple #23
0
 private int CallBackViewProfile(Data received)
 {
     if (received.Command == Data.DataType.ViewProfile)
     {
         User user = (User)received.Other;
         UserProfilForm profilForm = new UserProfilForm(user, true);
         this.Invoke((MethodInvoker)delegate() { profilForm.ShowDialog(); });
     }
     else
     {
         //MessageBox.Show("ERROR!!!.");
     }
     return 0;
 }
Exemple #24
0
        private void HomeForm_Load(object sender, EventArgs e)
        {
            //On va chercher la liste des Client
            List<User> clientList = new List<User>();
            listBoxUsers.DataSource = clientList;

            Data listUserRequest = new Data();
            listUserRequest.Command = Data.DataType.ListClient;
            Program.callBackOnReceive.Enqueue(CallBackListClient);
            Program.client.Send(listUserRequest);

            // On va chercher la liste des salles
            List<DiscussionRoom> roomList = new List<DiscussionRoom>();
            listBoxChatRooms.DataSource = roomList;
            listBoxChatRooms.DisplayMember = "Name";

            Data listRoomRequest = new Data();
            listRoomRequest.Command = Data.DataType.ListDiscussionRoom;
            Program.callBackOnReceive.Enqueue(CallBackListDiscussionRoom);
            Program.client.Send(listRoomRequest);
        }
Exemple #25
0
 /// <summary>
 /// Double clique sur une salle de discussion
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void listBoxChatRooms_DoubleClick(object sender, EventArgs e)
 {
     if (listBoxChatRooms.SelectedItem != null && (roomForm == null || !roomForm.Visible))
     {
         if (listBoxChatRooms.SelectedItem.ToString().Length != 0)
         {
             DiscussionRoom selectedRoom = listBoxChatRooms.SelectedItem as DiscussionRoom;
             Data getRoomRequest = new Data();
             getRoomRequest.Command = Data.DataType.EnterRoom;
             getRoomRequest.User = actualClient;
             getRoomRequest.Text = selectedRoom.Name;
             Program.callBackOnReceive.Enqueue(CallBackEnterRoom);
             Program.client.Send(getRoomRequest);
         }
     }
 }
 /// <summary>
 /// On enregistre les changements sur la BD
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void buttonConfirm_Click(object sender, EventArgs e)
 {
     if (!String.IsNullOrEmpty(textBoxUserName.Text))
     {
         Data updateProfilRequest = new Data();
         updateProfilRequest.Command = Data.DataType.UpdateProfile;
         updateProfilRequest.User = this.ActualClient;
         updateProfilRequest.Text = oldClient.UserName;
         Program.callBackOnReceive.Enqueue(CallBackUpdateProfil);
         Program.client.Send(updateProfilRequest);
     }
     else
     {
         MessageBox.Show("Le nom d'utilisateur ne doit pas être vide.","Erreur",MessageBoxButtons.OK,MessageBoxIcon.Error);
     }
 }