Ejemplo n.º 1
0
        void PostReq()
        {
            Dbg.Log("Sending connection request.");

            string tmpFile = Path.GetTempFileName();

            m_exHandler = ReqExceptionHandler;

            using (new AutoReleaser(() => File.Delete(tmpFile)))
            {
                var netEngin = new NetEngin(Program.NetworkSettings);

                SetProgressMessage("Envoi des données au serveur...");

                netEngin.Download(tmpFile, Urls.ConnectionReqURL, true);
                List <HubCore.DLG.Message> msgs = DialogEngin.ReadConnectionsReq(tmpFile).ToList();
                m_msgID = msgs.Count == 0 ? 1 : msgs.Max(m => m.ID) + 1;

                var    ms      = new MemoryStream();
                byte[] ciBytes = m_clInfo.GetBytes();
                byte[] ceBytes = GetEnvironment().GetBytes();
                ms.Write(ciBytes, 0, ciBytes.Length);
                ms.Write(ceBytes, 0, ceBytes.Length);

                var msg = new HubCore.DLG.Message(m_msgID, 0, Message_t.NewConnection, ms.ToArray());
                msgs.Add(msg);

                DialogEngin.WriteConnectionsReq(tmpFile, msgs);
                netEngin.Upload(Urls.ConnectionReqURL, tmpFile, true);
                StartTimer();

                SetProgressMessage("Attente de la réponse du serveur...");
            }
        }
Ejemplo n.º 2
0
        void ProcessResp()
        {
            StopTimer();

            string tmpFile = Path.GetTempFileName();

            Dbg.Log($"Processing Response, attempts = {m_attemptsCount + 1}.");

            m_exHandler = RespExceptionHandler;

            var netEngin = new NetEngin(Program.NetworkSettings);

            using (new AutoReleaser(() => File.Delete(tmpFile)))
            {
                SetProgressMessage("Réception des données à partir du serveur...");

                try
                {
                    netEngin.Download(tmpFile, Urls.ConnectionRespURL, true);
                }
                catch (Exception ex)
                {
                    Dbg.Log(ex.Message);
                    DialogEngin.WriteConnectionsResp(tmpFile, Enumerable.Empty <HubCore.DLG.Message>());
                }

                IEnumerable <HubCore.DLG.Message> messages = DialogEngin.ReadConnectionsResp(tmpFile);
                HubCore.DLG.Message[]             msgs     = (from resp in messages
                                                              where resp.ReqID >= m_msgID
                                                              select resp).ToArray();

                HubCore.DLG.Message msg = msgs.Where(m => m.ReqID == m_msgID).SingleOrDefault();

                uint clID = msg == null ? 0 : BitConverter.ToUInt32(msg.Data, 0);

                if (msg != null && clID == m_clInfo.ClientID)
                {
                    switch (msg.MessageCode)
                    {
                    case Message_t.InvalidID:
                        Dbg.Log($"Got invalid ID! (ClientID = {m_clInfo.ClientID}).");

                        ClientInfo clInfo = ClientInfo.CreateClient(m_clInfo.ProfileID);
                        clInfo.ContaclEMail = m_clInfo.ContaclEMail;
                        clInfo.ContactName  = m_clInfo.ContactName;
                        clInfo.ContactPhone = m_clInfo.ContactPhone;
                        m_clInfo            = clInfo;

                        if (++m_attemptsCount >= SettingsManager.MaxConnectAttemps)
                        {
                            if (ShowMessage(MAX_ATTEMPTS_ERROR, MessageBoxButtons.YesNo) != DialogResult.Yes)
                            {
                                CloseDialog();
                                return;
                            }
                            else
                            {
                                m_attemptsCount = 0;
                            }
                        }

                        PostReq();
                        break;

                    case Message_t.Ok:
                        Dbg.Log("Client registered :-)!");


                        Program.Settings.ClientInfo = m_clInfo;
                        SetProgressMessage("Enregistrement terminé.");

                        //creation des fichier dlg
                        string dlgFile = SettingsManager.GetClientDialogFilePath(clID);
                        DialogEngin.WriteHubDialog(dlgFile, clID, Enumerable.Empty <HubCore.DLG.Message>());

                        try
                        {
                            netEngin.Upload(SettingsManager.GetClientDialogURL(clID), dlgFile, true);
                        }
                        catch (Exception ex)
                        {
                            Dbg.Log(ex.Message);
                        }

                        ShowMessage("Votre enregistrement est maintenant terminé. " +
                                    "Vous pouvez commencer à utiliser l’application.");

                        IsRegistered = true;
                        CloseDialog();
                        break;

                    case Message_t.InvalidProfile:
                        Dbg.Log($"Got invalid Profile! (ProfileID: = {m_clInfo.ProfileID}).");

                        ShowMessage(SRV_ERROR);
                        CloseDialog();
                        return;

                    case Message_t.Rejected:
                        Dbg.Log("Got reject connection!");
                        ShowMessage(REJECT_CONNCTION_ERROR);
                        CloseDialog();
                        return;

                    default:
                        Dbg.Log("Got invalid response!!!!");
                        Dbg.Assert(false);
                        break;
                    }
                }
                else if (msgs.Length > 0)
                {
                    Dbg.Log("Request message lost.");

                    if (++m_attemptsCount >= SettingsManager.MaxConnectAttemps)
                    {
                        if (ShowMessage(MAX_ATTEMPTS_ERROR, MessageBoxButtons.YesNo) != DialogResult.Yes)
                        {
                            CloseDialog();
                            return;
                        }
                        else
                        {
                            m_attemptsCount = 0;
                        }
                    }

                    PostReq();
                }
                else if (++m_attemptsCount >= SettingsManager.MaxConnectAttemps)
                {
                    Dbg.Log("Timeout.");

                    if (ShowMessage(MAX_ATTEMPTS_ERROR, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        StartTimer();
                        m_attemptsCount = 0;
                        PostReq();
                    }
                    else
                    {
                        CloseDialog();
                    }
                }
                else
                {
                    StartTimer();
                    SetProgressMessage("Attente de la réponse du serveur...");
                }
            }
        }
Ejemplo n.º 3
0
        void PostSetInfoMessage()
        {
            ClientInfo curClInfo = Program.Settings.ClientInfo;
            var        clInfo    = new ClientInfo(curClInfo.ClientID, curClInfo.ProfileID);

            clInfo.ContaclEMail = m_tbEmail.Text.Trim();
            clInfo.ContactName  = m_tbContact.Text.Trim();
            clInfo.ContactPhone = m_tbPhone.Text.Trim();

            byte[] msgData = clInfo.GetBytes();

            var dlg = new BusyDialog();

            Func <bool> postMessage = () =>
            {
                const int TIME_TO_SLEEP = 10 * 1000;
                int       nbAttempts    = 3;


                while (--nbAttempts >= 0)
                {
                    dlg.Message = "Envoi  des données vers le serveur...";
                    uint msgID = Program.DialogManager.SendMessage(Message_t.SetInfo, msgData);

                    if (msgID == 0)
                    {
                        continue;
                    }

                    dlg.Message = "En attente de la réponse du serveur...";
                    Thread.Sleep(TIME_TO_SLEEP);

                    dlg.Message = "Réception des données à partir du serveur...";
                    HubCore.DLG.Message resp = Program.DialogManager.ReceiveMessage(msgID);

                    Dbg.Assert(resp == null || resp.MessageCode == Message_t.Ok);

                    if (resp != null)
                    {
                        dlg.Message = "Transfert terminé.";
                        break;
                    }
                }

                return(nbAttempts >= 0);
            };

            Action <Task> onErr = t =>
            {
                dlg.Dispose();

                if (t.Exception != null)
                {
                    Dbg.Log(t.Exception.InnerException.Message);
                    Program.DialogManager.PostLog("Changement des informations utilisateur. Erreur lors de l'envoi: " +
                                                  t.Exception.InnerException.Message, true);
                }

                MessageBox.Show("Impossible de se connecter au serveur distant. Veuillez réessayer ultérieurement.",
                                null,
                                MessageBoxButtons.OK);
            };

            Action <Task <bool> > onSuccess = t =>
            {
                if (t.Result == false)
                {
                    onErr(t);
                }
                else
                {
                    Program.Settings.ClientInfo = clInfo;
                    ClientInfoChanged?.Invoke();
                    dlg.Dispose();
                }
            };

            var task = new Task <bool>(postMessage, TaskCreationOptions.LongRunning);

            task.OnSuccess(onSuccess);
            task.OnError(onErr);
            task.Start();
            dlg.ShowDialog();
        }