Ejemplo n.º 1
0
        public static void Logout(byte[] data, IPEndPoint ip, Server server)
        {
            string message = "";

            BasicReqPacket logoutPacket = Packet.Unpack <BasicReqPacket>(data);

            server.DebugInfo("Logut request recieved.");
            server.DebugInfo("LogoutReq Packet: " + logoutPacket.ToString());

            ClientStatus current = server.GetClient(logoutPacket.Username);

            if (ClientsManagement.CheckBasics(current, ClientStatus.Status.Disconnected, logoutPacket.Alea, out message))
            {
                server.DebugInfo("Logout: Correct logut from " + logoutPacket.Username);
                server.DebugInfo(logoutPacket.Username + " now is disconnected.");
                if (server.ClientsOnChat.Any(d => d.Key == current.Client.Username))
                {
                    server.ClientsOnChat.Remove(current.Client.Username);
                }
                current.Disconnect();

                // Return ack
                server.Udp.SendMessage(new AckErrorPacket(PacketTypes.LogoutAck, "Logged out correctly.").Pack(), ip);
            }
            else
            {
                server.DebugInfo("Logout: Incorrect logut.");
                // Send error
                message = "Logout error: " + message;
                server.Udp.SendError(message, ip);
            }
        }
Ejemplo n.º 2
0
        public static void Alive(byte[] data, IPEndPoint ip, Server server)
        {
            string message = "";

            BasicReqPacket aPacket = Packet.Unpack <BasicReqPacket>(data);

            server.DebugInfo("Alive inf recieved.");
            server.DebugInfo("AliveInf Packet: " + aPacket.ToString());

            ClientStatus current = server.GetClient(aPacket.Username);

            if (ClientsManagement.CheckBasics(current, ClientStatus.Status.Disconnected, aPacket.Alea, out message))
            {
                // Save the last alive
                current.LastAlive = DateTime.Now;
                // Send ack
                server.Udp.SendMessage(new AckErrorPacket(PacketTypes.AliveAck, "Alive correct").Pack(), ip);
            }
            else
            {
                server.DebugInfo("Alive: Incorrect alive from " + aPacket.Username);
                server.DebugInfo(aPacket.Username + " now is disconnected.");

                if (server.ClientsOnChat.Any(d => d.Key == current.Client.Username))
                {
                    server.ClientsOnChat.Remove(current.Client.Username);
                }
                // Disconnect the client
                current.Disconnect();
                // Send error
                server.Udp.SendError(message, ip);
            }
        }
Ejemplo n.º 3
0
        public static void DeleteAccount(byte[] data, IPEndPoint ip, Server server)
        {
            string message = "";

            BasicReqPacket delPacket = Packet.Unpack <BasicReqPacket>(data);

            server.DebugInfo("Delete account request recieved.");
            server.DebugInfo("DeleteAccountReq Packet: " + delPacket.ToString());

            ClientStatus current = server.GetClient(delPacket.Username);

            if (ClientsManagement.CheckBasics(current, ClientStatus.Status.Disconnected, delPacket.Alea, out message))
            {
                server.DebugInfo("Delete Account: Account was " + delPacket.Username + " correctly deleted.");

                // Send the ack first
                server.Udp.SendMessage(new AckErrorPacket(PacketTypes.DeleteAccountAck, "Account deleted correctly.").Pack(), ip);

                // Delete client from database
                server.DeleteCLientFromDataBase(delPacket.Username);
            }
            else
            {
                server.DebugInfo("Delete account: Acount can't be deleted.");
                // Send error
                message = "Delete error: " + message;
                server.Udp.SendError(message, ip);
            }
        }
Ejemplo n.º 4
0
        public static void Register(byte[] data, IPEndPoint ip, Server server)
        {
            Models.Client current = null;   //< Who is doing the request
            string        message = "";

            // Unpack the packet in the correct format
            ProfilePacket regPacket = Packet.Unpack <ProfilePacket>(data);

            server.DebugInfo("Register request recieved.");
            server.DebugInfo("RegisterReq Packet: " + regPacket.ToString());
            // Send a package depending on registration success
            if (ClientsManagement.RegisterClient(regPacket, ip, ref current, out message))
            {
                // Send RegisterAck if all data is correct
                server.Udp.SendMessage(new AckErrorPacket(
                                           PacketTypes.RegisterAck,
                                           "Congratulations now you are registered.").Pack(),
                                       ip);

                server.DebugInfo(regPacket.FirstName + " " + regPacket.LastName + " has been registered as " + regPacket.Username);
                server.Clients.Add(new ClientStatus(current));
            }
            else
            {
                // Send the error
                server.DebugInfo("Register: " + regPacket.FirstName + " " + regPacket.LastName + " request not accepted");
                server.Udp.SendError(message, ip);
            }
        }
        public static void SendContactRequests(byte[] data, Server server, IPEndPoint ip)
        {
            string message = "";

            // Unpack the petition
            BasicReqPacket cPacket = Packet.Unpack <BasicReqPacket>(data);

            List <Models.ContactRequest> sent     = null;
            List <Models.ContactRequest> recieved = null;


            server.DebugInfo("Contact requests list requested by " + cPacket.Username);
            server.DebugInfo(cPacket.ToString());

            if (ClientsManagement.ContactRequestsList(cPacket,
                                                      server.GetClient(cPacket.Username),
                                                      ref sent,
                                                      ref recieved,
                                                      out message))
            {
                // List filled correctly
                server.Udp.SendMessage(new ContactReqListPacket(PacketTypes.ContactAck,
                                                                cPacket.Alea,
                                                                sent,
                                                                recieved).Pack(),
                                       ip);

                server.DebugInfo("Contact list requests sended correctly to + " + cPacket.Username);
            }
            else
            {
                server.DebugInfo("Contact requests list error: " + message);
                server.Udp.SendError(message, ip);
            }
        }
        public static void ClientsQuery(byte[] data, Server server, IPEndPoint ip)
        {
            string message = "";

            // Unpack the petition
            Packets.ClientQueryPacket cPacket = Packet.Unpack <ClientQueryPacket>(data);

            List <Models.Client> queryResult = null;

            server.DebugInfo("Client query  list requested by " + cPacket.Username);
            server.DebugInfo(cPacket.ToString());

            if (ClientsManagement.GetClientsQueryResult(cPacket, server.GetClient(cPacket.Username),
                                                        ref queryResult, cPacket.Query, out message))
            {
                // List filled correctly
                server.Udp.SendMessage(new ClientQueryPacket(PacketTypes.ClientsQueryAck,
                                                             cPacket.Alea,
                                                             queryResult).Pack(),
                                       ip);

                server.DebugInfo("Client list query requests sended correctly to + " + cPacket.Username);
            }
            else
            {
                server.DebugInfo("Client list query requests error: " + message);
                server.Udp.SendError(message, ip);
            }
        }
        public static void AnswerContactRequest(byte[] data, Server server, IPEndPoint ip, bool ack)
        {
            string message = "";

            // Unpack the petition
            ContactReqPacket cPacket = Packet.Unpack <ContactReqPacket>(data);

            ClientStatus current = server.GetClient(cPacket.To);

            if (ClientsManagement.AckOrRegContactReq(cPacket, current, ack, out message))
            {
                server.DebugInfo("Answer Contact request: " + (ack
                                                                ? "Request accepted correctly."
                                                                : "Request refused correctly."));

                if (ack)
                {
                    server.DebugInfo("New Contact added: " + cPacket.From + " - " + cPacket.To);
                }

                // Send the ack
                server.Udp.SendMessage(new AckErrorPacket(PacketTypes.ContactAck, "Answered correctly").Pack(), ip);
            }
            else
            {
                server.DebugInfo("Answer Contact request: Incorrect contact request.");
                // Send error
                server.Udp.SendError(message, ip);
            }
        }
Ejemplo n.º 8
0
        public static void ReadyChatClient(byte[] data, TcpClient client, Server server)
        {
            string message = "";
            // Unpack the data
            BasicReqPacket packet = Packet.Unpack <BasicReqPacket>(data);

            // Get the client who is requesting
            ClientStatus c = server.GetClient(packet.Username);

            // Check the basics
            if (ClientsManagement.CheckBasics(c, ClientStatus.Status.Disconnected, packet.Alea, out message))
            {
                // Add client to dicctionary with his respective tcpclient
                server.ClientsOnChat.Add(c.Client.Username, client);

                server.DebugInfo("Client Ready for chat: " + c.Client.Username + " is ready for chatting.");

                // Send ack
                server.Tcp.SendMessage(new AckErrorPacket(PacketTypes.ReadyChatAck, "Now you are ready for chat").Pack(), client);
            }
            else
            {
                server.DebugInfo("Client Ready for chat error: " + message);
                server.Tcp.SendError(message, client);
            }
        }
Ejemplo n.º 9
0
        public static void Login(byte[] data, IPEndPoint ip, Server server)
        {
            Models.Client current = null;   //< Who is doing the request

            // Unpack
            LoginReqPacket logPacket = Packet.Unpack <LoginReqPacket>(data);

            server.DebugInfo("Login request recieved.");
            server.DebugInfo("LoginReq Packet: " + logPacket.ToString());


            if (ClientsManagement.LoginClient(logPacket, ref current, out string message))
            {
                if (server.GetClient(current.Username).Stat == ClientStatus.Status.Logged)
                {
                    server.DebugInfo("Login: User is already logged");
                    message = "USer is already logged";
                    server.Udp.SendError(message, ip);
                    return;
                }

                // Client login
                server.DebugInfo("Client " + current.ToString() + " is now logged in.");
                string alea = Server.GenerateAlea();
                server.SetStatus(logPacket.Username, ClientStatus.Status.Logged, alea, DateTime.Now);

                // Send the profile info of the database to client
                server.DebugInfo("Sending profile info to " + logPacket.Username + ".");

                // Return user profile with the ack
                server.Udp.SendMessage(new ProfilePacket(
                                           PacketTypes.LoginAck,
                                           alea,  // New alea generated
                                           current.FirstName,
                                           current.LastName,
                                           current.Age,
                                           current.PhoneNumber,
                                           current.Gender,
                                           current.Username,
                                           current.Password,
                                           current.Email,
                                           server.GetContacts(server.GetClient(current.Username)),
                                           server.GetUnreadMessages(server.GetClient(current.Username)),
                                           server.GetAgendaEvents(current.Username),
                                           server.GetSocialNetworks(current.Username)
                                           ).Pack(), ip);

                // Mark messages as read
                server.MarkReadMessages(server.GetClient(current.Username));
            }
            else
            {
                server.DebugInfo("Login: Request not accepted");
                server.Udp.SendError(message, ip);
            }
        }
Ejemplo n.º 10
0
 public WCFBasisService()
 {
     InitializeComponent();
     
     var clientsRepository = new ClientsRepository();
     var clientsNotifications = new NotificationFactory(clientsRepository);
     var clientsManagement = new ClientsManagement(clientsRepository);
     var actionsHandler = new ServiceActionsHandler(clientsManagement, clientsNotifications);
     
     _wcfServiceHost = new WcfServiceHost(new ServiceContract(actionsHandler));
 }
Ejemplo n.º 11
0
        public ClientEditForm(int initialId, string initialName, string initialPhone, string initialEmail, string initialAnnotation)
        {
            InitializeComponent();

            _client = ClientsManagement.GetInstance();

            id = initialId;

            ClientEditForm_NameTextBox.Text       = name = initialName;
            ClientEditForm_PhoneTextBox.Text      = phone = initialPhone;
            ClientEditForm_EmailTextBox.Text      = email = initialEmail;
            ClientEditForm_AnnotationTextBox.Text = annotation = initialAnnotation;
        }
Ejemplo n.º 12
0
        public MainForm()
        {
            InitializeComponent();

            MessageBoxManager.Yes = Messages.Yes;
            MessageBoxManager.No  = Messages.No;
            MessageBoxManager.Register();

            _client = ClientsManagement.GetInstance();
            _device = DevicesManagement.GetInstance();
            _repair = RepairsManagement.GetInstance();

            clientBindingSource.DataSource = SerwisContext.GetContext().Clients.ToList();

            deviceBindingSource.DataSource = SerwisContext.GetContext().Devices.ToList();

            repairBindingSource.DataSource = SerwisContext.GetContext().Repairs.ToList();
        }
Ejemplo n.º 13
0
        public static void ProfileUpdate(byte[] data, IPEndPoint ip, Server server)
        {
            ProfilePacket pPacket = Packet.Unpack <ProfilePacket>(data);

            server.DebugInfo("Update profile recieved.");
            server.DebugInfo("Update profile Packet: " + pPacket.ToString());

            ClientStatus current = server.GetClient(pPacket.Username);

            if (ClientsManagement.UpdateProfile(pPacket, ref current, out string message))
            {
                // Profile updated
                server.DebugInfo("Update Profile: " + pPacket.Username + "'s profile updated.");

                // Return user profile with the ack
                server.Udp.SendMessage(new ProfilePacket(
                                           PacketTypes.ProfileUpdateAck,
                                           current.Alea,  // New alea generated
                                           current.Client.FirstName,
                                           current.Client.LastName,
                                           current.Client.Age,
                                           current.Client.PhoneNumber,
                                           current.Client.Gender,
                                           current.Client.Username,
                                           current.Client.Password,
                                           current.Client.Email,
                                           server.GetContacts(current),
                                           server.GetUnreadMessages(current),
                                           server.GetAgendaEvents(current.Client.Username),
                                           server.GetSocialNetworks(current.Client.Username)
                                           ).Pack(), ip);

                server.MarkReadMessages(current);
            }
            else
            {
                server.DebugInfo("Profile Update: Request not accepted");
                server.Udp.SendError(message, ip);
            }
        }
        public static void NewContactRequest(byte[] data, Server server, IPEndPoint ip)
        {
            string message = "";

            // Unpack the petition
            ContactReqPacket cPacket = Packet.Unpack <ContactReqPacket>(data);

            ClientStatus current = server.GetClient(cPacket.From);

            if (ClientsManagement.NewContactRequest(cPacket, current, out message))
            {
                server.DebugInfo(cPacket.From + " made a contact request to " + cPacket.To);

                // Send the ack
                server.Udp.SendMessage(new AckErrorPacket(PacketTypes.ContactAck, "New contact request added to database").Pack(), ip);
            }
            else
            {
                server.DebugInfo("New Contact Request: Incorrect contact request.");
                // Send error
                server.Udp.SendError(message, ip);
            }
        }
Ejemplo n.º 15
0
        public static void SendMessage(byte[] data, TcpClient client, Server server)
        {
            try
            {
                string message = "";

                // Unpack the data
                MessagePacket packet = Packet.Unpack <MessagePacket>(data);

                server.DebugInfo("SendMessageReq Recieved: " + packet.ToString());

                // Who is making the query
                ClientStatus c = server.GetClient(packet.From.Username);

                // Check te basics again and again
                if (ClientsManagement.CheckBasics(c, ClientStatus.Status.Disconnected, packet.Alea, out message))
                {
                    // Check if destination exists
                    if (!server.Clients.Any(d => d.Client.Username == packet.To.Username))
                    {
                        server.DebugInfo("Send Message Error: " + "Destination doesn't exist");
                        server.Tcp.SendError("Destination doesn't exist", client);
                        return;
                    }

                    // Store the message on database
                    using (var db = new Models.ServerDatabase())
                    {
                        // Create the message
                        Models.Message m = new Models.Message()
                        {
                            From    = db.Clients.Single(n => n.Username == c.Client.Username),
                            To      = db.Clients.Single(n => n.Username == packet.To.Username),
                            Date    = packet.Date,
                            Content = packet.Content
                        };

                        // Check if destination is on chat
                        if (server.ClientsOnChat.Any(d => d.Key.Equals(packet.To.Username)))
                        {
                            // If exist save the message and send him as well as readed message
                            m.Read = true;
                            server.DebugInfo("Message from : " + packet.From.Username + " sended to: " + packet.To.Username);
                            // Send the message to destination changing the type of package
                            packet.Type = (byte)PacketTypes.SendMessageAck;
                            server.Tcp.SendMessage(packet.Pack(), server.ClientsOnChat[packet.To.Username]);
                        }
                        else
                        {
                            m.Read = false;
                        }


                        db.Messages.InsertOnSubmit(m);
                        db.SubmitChanges();
                    }
                }
                else
                {
                    server.DebugInfo("Send Message Error: " + message);
                    server.Tcp.SendError(message, client);
                }
            }
            catch (SqlException)
            {
                server.DebugInfo("Send Message Error: database error.");
                server.Tcp.SendError("Database error", client);
            }
        }