Exemple #1
0
 public static DataHandle getInstance()
 {
     if (instance == null)
     {
         instance = new DataHandle();
     }
     return(instance);
 }
Exemple #2
0
        public static void SSendNote(Socket other, Notes nt)
        {
            string msg = "NOTE_" + nt.id.ToString() + "_"
                         + nt.Tittle + "_"
                         + nt.user_id.ToString() + "_"
                         + nt.Content + "_"
                         + DataHandle.getInstance().getRecentAccount().username;

            other.Send(SerializeMsg(msg));
        }
Exemple #3
0
        public static void SSendReminder(Socket other, Reminders r)
        {
            string msg = "REMINDER_" + r.ID.ToString() + "_"
                         + r.Tittle + "_"
                         + r.Content + "_"
                         + r.Time + "_"
                         + r.Check.ToString() + "_"
                         + r.User_id.ToString() + "_"
                         + DataHandle.getInstance().getRecentAccount().username;

            other.Send(SerializeMsg(msg));
        }
Exemple #4
0
 private void Close()
 {
     if (others != null && others.Count != 0)
     {
         foreach (Socket other in others)
         {
             SendStatus("DISCARD CONNECTION_" + DataHandle.getInstance().getRecentAccount().username);
         }
         others.Clear();
         p.Clear();
     }
     else
     {
         SendStatus("DISCARD CONNECTION_" + DataHandle.getInstance().getRecentAccount().username);
     }
     clientStatus = -1;
     peer.Close();
 }
Exemple #5
0
        public int startClient(IPAddress svIP)
        {
            endpoint = new IPEndPoint(svIP, port);
            peer     = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            try
            {
                peer.Connect(endpoint);
                SendStatus("CREATE CONNECTION_" + DataHandle.getInstance().getRecentAccount().username);
            }
            catch
            {
                MessageBox.Show("Cannot connect to server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(0);
            }
            Thread listen = new Thread(CReceive);

            listen.IsBackground = true;
            listen.Start();
            clientStatus = 1;
            return(1);
        }
Exemple #6
0
        public void SReceive(Object dest)
        {
            Socket other = dest as Socket;

            try
            {
                while (true)
                {
                    byte[] data = new byte[1024 * 5000];
                    other.Receive(data);

                    string   msg    = (string)DeserializeMsg(data);
                    string[] tokens = msg.Split('_');
                    if (tokens[0] == "STATUS")
                    {
                        if (tokens[1] == "DISCARD CONNECTION")
                        {
                            p.Remove(tokens[2].ToString().Trim());
                            Form1.getInstance().networkSubWindow1.UpdateClientList(p, others);
                            Form1.getInstance().UpdateConnectionStatus("Connected to " + tokens[2].ToString().Trim());
                        }
                        else
                        {
                            p.Add(tokens[2]);
                            Form1.getInstance().networkSubWindow1.UpdateClientList(p, others);
                            Form1.getInstance().UpdateConnectionStatus("Connected to " + tokens[2].ToString().Trim());
                        }
                    }
                    else if (tokens[0] == "ACCOUNT")
                    {
                        Account a = new Account()
                        {
                            username = tokens[3].ToString().Trim(),
                            id       = Convert.ToInt32(tokens[1]),
                            password = tokens[2].ToString().Trim(),
                            created  = Convert.ToDateTime(tokens[4].ToString().Trim()),
                        };
                        Form1.getInstance().UpdateConnectionStatus("Received information from account " + tokens[3].ToString().Trim());

                        if (DataHandle.getInstance().doesAccountExit(a))
                        {
                            Form1.getInstance().UpdateConnectionStatus("Account " + tokens[3].ToString().Trim() + " has already existed");
                        }
                        else
                        {
                            DataHandle.getInstance().saveAccount(a);
                        }
                    }
                    else if (tokens[0] == "DETAILS")
                    {
                        PersonalDetail d = new PersonalDetail()
                        {
                            id          = Convert.ToInt32(tokens[1]),
                            account     = Convert.ToInt32(tokens[5]),
                            category    = tokens[4].ToString().Trim(),
                            subcategory = tokens[2].ToString().Trim(),
                            content     = tokens[3].ToString().Trim(),
                        };

                        DataHandle.getInstance().createDetail(d);
                    }
                    else if (tokens[0] == "REMINDER")
                    {
                        Reminders r = new Reminders()
                        {
                            ID      = Convert.ToInt32(tokens[1]),
                            Tittle  = tokens[2].ToString().Trim(),
                            Content = tokens[3].ToString().Trim(),
                            Time    = tokens[4].ToString().Trim(),
                            Check   = Convert.ToInt32(tokens[5]),
                            User_id = Convert.ToInt32(tokens[6]),
                        };
                        Form1.getInstance().UpdateConnectionStatus("Received reminder from account " + tokens[tokens.Length - 1]);

                        DataHandle.getInstance().CreateNewReminder(r);
                    }
                    else if (tokens[0] == "NOTE")
                    {
                        Notes n = new Notes()
                        {
                            id      = Convert.ToInt32(tokens[1]),
                            Tittle  = tokens[2].ToString().Trim(),
                            Content = tokens[4].ToString().Trim(),
                            user_id = Convert.ToInt32(tokens[3]),
                        };
                        Form1.getInstance().UpdateConnectionStatus("Received reminder from account " + tokens[tokens.Length - 1]);
                        DataHandle.getInstance().CreateNewNote(n);
                    }
                }
            }
            catch
            {
                others.Remove(other);
                other.Close();
            }
        }
Exemple #7
0
        public void CReceive()
        {
            try
            {
                while (true)
                {
                    byte[] data = new byte[1024 * 5000];
                    peer.Receive(data);

                    string   msg    = (string)DeserializeMsg(data);
                    string[] tokens = msg.Split('_');
                    if (tokens[0] == "STATUS")
                    {
                        if (tokens[1] == "DISCARD CONNECTION")
                        {
                            Form1.getInstance().UpdateConnectionStatus("Disconnected to account " + tokens[tokens.Length - 1]);
                            Close();
                        }
                        else
                        {
                            Form1.getInstance().UpdateConnectionStatus("Connected to account " + tokens[tokens.Length - 1]);
                        }
                        /*p.Add(tokens[2]);*/
                    }
                    else if (tokens[0] == "ACCOUNT")
                    {
                        Account a = new Account()
                        {
                            username = tokens[3].ToString().Trim(),
                            id       = Convert.ToInt32(tokens[1]),
                            password = tokens[2].ToString().Trim(),
                            created  = Convert.ToDateTime(tokens[4].ToString().Trim()),
                        };
                        DataHandle.getInstance().saveAccount(a);

/*                        Form1.getInstance().networkSubWindow1.UpdateClientList(p, others);
 */                 }
                    else if (tokens[0] == "DETAILS")
                    {
                        PersonalDetail d = new PersonalDetail()
                        {
                            id          = Convert.ToInt32(tokens[1]),
                            account     = Convert.ToInt32(tokens[5]),
                            category    = tokens[4].ToString().Trim(),
                            subcategory = tokens[2].ToString().Trim(),
                            content     = tokens[3].ToString().Trim(),
                        };
                        DataHandle.getInstance().createDetail(d);
                    }
                    else if (tokens[0] == "REMINDER")
                    {
                        Reminders r = new Reminders()
                        {
                            ID      = Convert.ToInt32(tokens[1]),
                            Tittle  = tokens[2].ToString().Trim(),
                            Content = tokens[3].ToString().Trim(),
                            Time    = tokens[4].ToString().Trim(),
                            Check   = Convert.ToInt32(tokens[5]),
                            User_id = Convert.ToInt32(tokens[6]),
                        };
                        DataHandle.getInstance().CreateNewReminder(r);
                    }
                    else if (tokens[0] == "NOTE")
                    {
                        Notes n = new Notes()
                        {
                            id      = Convert.ToInt32(tokens[1]),
                            Tittle  = tokens[2].ToString().Trim(),
                            Content = tokens[4].ToString().Trim(),
                            user_id = Convert.ToInt32(tokens[3]),
                        };
                        DataHandle.getInstance().CreateNewNote(n);
                    }
                }
            }
            catch
            {
                Close();
            }
        }