Exemplo n.º 1
0
 public PresenceInformer(Auth auth)
 {
     m_auth = auth;
     m_account = auth.Account;
     m_account.Id = auth.Account.Id.ToString();
     m_informerThread = new Thread(Ping);
     m_shouldStop = true;
     m_shouldVanish = false;
     m_informerThread.Start();
 }
Exemplo n.º 2
0
 public SyncHandler(Auth auth)
 {
     stopwatch = new Stopwatch();
     m_auth = auth;
     m_account = auth.Account;
     m_account.Id = auth.Account.Id.ToString();
     m_timerThread = new Thread(SendSyncRequestCycler);
     m_shouldStop = true;
     m_timerThread.Start();
     m_shouldVanish = false;
 }
Exemplo n.º 3
0
        public int Logout()
        {
            try
            {
                if (m_account != null)
                {
                    LogHelper.Debug("Try Log out");
                    //note these two threads below may call this logout, so after they are done, check m_account != null
                    if(m_connectXMPPThread != null)
                        m_connectXMPPThread.Join();
                    if (m_validateThread != null)
                        m_validateThread.Join();
                    if (m_presenceInformer != null)
                    {
                        m_presenceInformer.RequestStopPing();
                        m_presenceInformer.Vanish();
                        m_presenceInformer = null;
                    }
                    if (m_syncRequestHandler != null)
                    {
                        m_syncRequestHandler.StopSending();
                        m_syncRequestHandler.Vanish();
                        m_syncRequestHandler = null;
                    }
                    //should be disconnected after m_presenceInformer and m_syncRequestHandler stopped
                    if (m_xmppCH != null && m_xmppCH.IsOnline)
                    {
                        m_xmppCH.Disconnect();
                        m_xmppCH = null;
                    }
                    //set member variable that might be needed by other variables at last
                    if (m_account != null)
                    {
                        string token = m_account.Token;

                        m_account = null;
                        m_user = null;
                        m_isAuthorized = false;

                        //once m_isAuthorized is false, the account is offline
                        MessageBox.Show("Now offline!");

                        string[] paraName = new string[] { "user_id", "token", "client_version" };
                        string[] paraValue = new string[] { m_account.Id, token, ClientInfo.VERSION };
                        string xmlResponse = RestComm.SendRestRequest(RestComm.URL_ACCOUNT_LOGOUT, paraName, paraValue);

                        if (xmlResponse != null)
                        {
                            LogHelper.Debug("Log out successfully");
                            return (int)Status.SUCCESS;
                        }
                        else
                        {
                            MessageBox.Show("No account found or server's dead!");
                            LogHelper.Debug("Log out failure: No account found");
                            //m_account.Status = "Offline";
                            //m_user.Status = "Offline";
                            return (int)Status.FAILURE;
                        }
                    }
                }
                MessageBox.Show("Already offline!");
                LogHelper.Debug("Log out failure: Already offline");
                return (int)Status.FAILURE;
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return (int)Status.EXCEPTION;
            }
        }
Exemplo n.º 4
0
 public CompanyFileValidator(Auth auth)
 {
     m_isValidated = false;
     m_auth = auth;
     m_account = auth.Account;
 }
Exemplo n.º 5
0
        public int Login()
        {
            if (!m_isAuthorized)
            {
                try
                {
                    LogHelper.Debug("Try Log in");
                    m_account = new Account(RetrieveToken("TOKEN"));
                    m_user = new User("test", "123");
                    //m_isNewAccount = false;
                    //m_isNewToken = false;
                    //m_isCompanyFileValidated = false;

                    string syncMarker = null;
                    string token = m_account.Token;
                    string companyFile = null;
                    string username = m_user.Username;
                    string password = m_user.Password;
                    string[] paraName = new string[] { "username", "password", "token", "client_version" };
                    string[] paraValue = new string[] { username, password, token, ClientInfo.VERSION };

                    string xmlResponse = RestComm.SendRestRequest(RestComm.URL_ACCOUNT_LOGIN, paraName, paraValue);

                    //if no response from server
                    if (xmlResponse == null)
                    {
                        m_isAuthorized = false;
                        m_account = null;
                        m_user = null;
                        MessageBox.Show("No response from server!");
                        LogHelper.Debug("Log in failure: No response from server");
                        return (int)Status.FAILURE;
                    }

                    string error = XMLParser.ExtractValueByTagName(xmlResponse, "error");

                    if (error == null || error.Length == 0)
                    {
                        companyFile = XMLParser.ExtractValueByTagName(xmlResponse, "company-file");
                        token = XMLParser.ExtractValueByTagName(xmlResponse, "token");

                        if (companyFile == null || companyFile.Length == 0)
                        {
                            //this accout has no company file record on the server side, thus is a new account
                            m_isNewAccount = true;
                            m_isNewToken = true;
                            m_neverSynced = true;
                            //a new account does not need validation
                            m_isCompanyFileValidated = true;
                            //new account needs full sync
                            m_needsFullSync = true;
                        }
                        else
                        {
                            m_isNewAccount = false;
                            syncMarker = XMLParser.ExtractValueByTagName(xmlResponse, "sync-marker");
                            if (syncMarker == null || syncMarker.Length == 0)
                                m_neverSynced = true;
                            else
                                m_neverSynced = false;
                            if (token.Equals(m_account.Token))
                            {
                                m_isNewToken = false;
                                m_isCompanyFileValidated = true;
                            }
                            else
                            {
                                m_isNewToken = true;
                                m_isCompanyFileValidated = false;
                            }
                        }

                        //once a new token is generated, the user needs to locate the company file
                        if (m_isNewToken)
                        {
                            companyFile = SelectCompanyFileDialog();
                            StroeToken("TOKEN", token);
                        }

                        LogHelper.Debug(String.Format("Comapny File: {0}", companyFile));

                        if (companyFile == null || companyFile.Length == 0)
                        {
                            m_isAuthorized = false;
                            m_account = null;
                            m_user = null;
                            MessageBox.Show("Invalid Company File!");
                            LogHelper.Debug("Log in failure: Invalid Company File");
                            return (int)Status.FAILURE;
                        }
                        else
                        {
                            //note that if it's the first time login, the company file must be assigned from the location provided by the user
                            m_account.AssignAccount(xmlResponse);
                            if (m_isNewAccount)
                                m_account.CompanyFile = companyFile;

                            m_isAuthorized = true;
                            MessageBox.Show("Login Success!");
                            LogHelper.Debug(String.Format("Login Success"));

                            //the company file and token will stroed in the local register on the client side
                            m_account.Status = "Online";
                            m_user.Status = "Online";
                            m_account.Token = token;
                            m_user.Token = token;
                            return (int)Status.SUCCESS;
                        }
                    }
                    else
                    {
                        m_isAuthorized = false;
                        m_account = null;
                        m_user = null;
                        MessageBox.Show(error);
                        LogHelper.Debug(String.Format("Log in failure: {0}", error));
                        return (int)Status.FAILURE;
                    }
                }
                catch (System.Net.WebException ex)
                {
                    //Http connection lost
                    LogHelper.Error(ex);
                    MessageBox.Show(ex.Message);
                    Thread logoutThread = new Thread(this.LogoutWrapper);
                    logoutThread.Start();
                    return (int)Status.EXCEPTION;
                }
                catch (Exception ex)
                {
                    m_isAuthorized = false;
                    m_account = null;
                    m_user = null;
                    LogHelper.Error(ex);
                    MessageBox.Show(ex.Message);
                    return (int)Status.EXCEPTION;
                }
            }
            else
            {
                MessageBox.Show("Already online!");
                LogHelper.Debug("Alredy online");
                //the company file and token will stored in the local register on the client side
                m_account.Status = "Online";
                m_user.Status = "Online";
                return (int)Status.FAILURE;
            }
        }