Esempio n. 1
0
 /// <summary>
 /// Là bản tin thực hiện thiết lập kết nối từ Front End đến Back End
 /// </summary>
 /// <param name="body"></param>
 /// <param name="serverIp"></param>
 /// <param name="port"></param>
 /// <returns></returns>
 public Task <ConnectResponeBody> ConnectAsync(string serverIp, int port, ConnectRequestBody body)
 {
     _serverIp    = serverIp;
     _connectBody = body;
     _port        = port;
     return(ConnectAsync());
 }
        // Main work of thread
        public async void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            try
            {
                // Create new connection
                byte[] byIVArray = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                var p = new APIClient(IPAdd, Port, Timeout, EncryptAES, byIVArray);

                // Loop for repeat
                for (round = 0; round < Loop; round++)
                {
                    // Loop for all step
                    for (int i = 0; i < listScenario.Count; i++)
                    {
                        switch (listScenario[i])
                        {
                            case "CONNECT":
                                // Get command parameter
                                int stationID = ComFunc.StringToInt32(objSettingConfig.allItems["CONNECTStation"]);
                                string serverIP = objSettingConfig.allItems["SocketServerIP"];
                                int serverPort = ComFunc.StringToInt32(objSettingConfig.allItems["SocketServerPort"]);
                                // Create command
                                ReportProgress("CONNECT to server ...");
                                var connectReq = new ConnectRequestBody(User, Pass, stationID);
                                // Send and Wait response
                                var re = await p.ConnectAsync(serverIP, serverPort, connectReq);
                                // Report status
                                ReportProgress("CONNECT result: " + re?.Status.ToString());
                                break;
                            case "SHAKE":
                                // Get command parameter
                                // Send command to server and wait response
                                ReportProgress("Send SHAKE to server ...");
                                var re2 = await p.ShakeAsync();
                                // Report status
                                ReportProgress("SHAKE result: " + re2?.Status.ToString());
                                break;
                            case "TERMINATE":
                                // Get command parameter
                                // Send command to server and Wait response
                                ReportProgress("Send TERMINATE to server ...");
                                var re3 = await p.TerminateAsync();
                                // Report status
                                ReportProgress("TERMINATE result: " + re3?.Status.ToString());
                                break;
                            case "CHECKIN":
                                // Get command parameter
                                string eTagCHECKIN = objSettingConfig.allItems["CHECKINEtag"];
                                int stationCHECKIN = ComFunc.StringToInt32(objSettingConfig.allItems["CHECKINStation"]);
                                int laneCHECKIN = ComFunc.StringToInt32(objSettingConfig.allItems["CHECKINLane"]);
                                // Send command to server and Wait response
                                ReportProgress("Send CHECKIN to server ...");
                                var checkin = new ChargeRequestBody(eTagCHECKIN, stationCHECKIN, laneCHECKIN);
                                var re4 = await p.CheckinAsync(checkin);
                                // Report status
                                ReportProgress("CHECKIN result: " + re4?.Status.ToString());
                                PlateRes = re4.Plate;
                                TicketRes = re4.TicketId;
                                break;
                            case "COMMIT":
                                // Get command parameter
                                string eTagCOMMIT = objSettingConfig.allItems["COMMITEtag"];
                                int stationCOMMIT = ComFunc.StringToInt32(objSettingConfig.allItems["COMMITStation"]);
                                int laneCOMMIT = ComFunc.StringToInt32(objSettingConfig.allItems["COMMITLane"]);
                                int imageCOMMIT = ComFunc.StringToInt32(objSettingConfig.allItems["COMMITImageCount"]);
                                // Send command to serve rand wait response
                                ReportProgress("Send COMMIT to server ...");
                                var transaction = new TransactionRequestBody(eTagCOMMIT, stationCOMMIT, laneCOMMIT, TicketRes, PlateStatus.Match, PlateRes, imageCOMMIT);
                                var re5 = await p.CommitAsync(transaction);
                                // Report status
                                ReportProgress("COMMIT result: " + re5?.Status.ToString());
                                break;
                            case "ROLLBACK":
                                // Get command parameter
                                string eTagROLLBACK = objSettingConfig.allItems["ROLLBACKEtag"];
                                int stationROLLBACK = ComFunc.StringToInt32(objSettingConfig.allItems["ROLLBACKStation"]);
                                int laneROLLBACK = ComFunc.StringToInt32(objSettingConfig.allItems["ROLLBACKLane"]);
                                int imageROLLBACK = ComFunc.StringToInt32(objSettingConfig.allItems["ROLLBACKImageCount"]);
                                // Send command to serve rand wait response
                                ReportProgress("Send ROLLBACK to server ...");
                                var rollback = new TransactionRequestBody(eTagROLLBACK, stationROLLBACK, laneROLLBACK, TicketRes, PlateStatus.Match, PlateRes, imageROLLBACK);
                                var re6 = await p.RollbackAsync(rollback);
                                // Report status
                                ReportProgress("ROLLBACK result: " + re6?.Status.ToString());
                                break;
                            case "CHARGE":
                                // Get command parameter
                                string eTagCHARGE = objSettingConfig.allItems["CHARGEEtag"];
                                int stationCHARGE = ComFunc.StringToInt32(objSettingConfig.allItems["CHECKINStation"]);
                                int laneCHARGE = ComFunc.StringToInt32(objSettingConfig.allItems["CHECKINLane"]);
                                // Send command to server and Wait response
                                ReportProgress("Send CHARGE to server ...");
                                var charge_req = new ChargeRequestBody(eTagCHARGE, stationCHARGE, laneCHARGE);
                                var re7 = await p.ChargeAsync(charge_req);
                                // Report status
                                break;
                            default:
                                ReportProgress("Error = Wrong Command");
                                break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // Report Error
                ReportProgress("Error = " + ex.Message);
            }

            // Report progress: complete
            ReportProgress("COMPLETED !");
        }
        /// <summary>
        /// This method is used to establish a Session Context with the server with specified user.
        /// </summary>
        /// <param name="userName">The UserName used to connect with server.</param>
        /// <param name="password">The password used to connect with server.</param>
        /// <param name="userDN">The UserESSDN used to connect with server.</param>
        /// <param name="cookies">Cookies used to identify the Session Context.</param>
        /// <param name="responseBody">The response body of the Connect request type.</param>
        /// <param name="webHeaderCollection">The web headers of the Connect request type.</param>
        /// <param name="httpStatus">The HTTP call response status.</param>
        /// <returns>The status code of the Connect request type.</returns>
        public uint Connect(string userName, string password, string userDN, ref CookieCollection cookies, out MailboxResponseBodyBase responseBody, ref WebHeaderCollection webHeaderCollection, out HttpStatusCode httpStatus)
        {
            responseBody = null;
            byte[] rgbAuxIn = new byte[] { };
            byte[] rawBuffer;

            // Prepare the connect request body.
            ConnectRequestBody connectRequestBody = new ConnectRequestBody();

            connectRequestBody.UserDN              = userDN;
            connectRequestBody.Flags               = ConstValues.ConnectionFlag;
            connectRequestBody.Cpid                = ConstValues.CodePageId;
            connectRequestBody.LcidString          = ConstValues.DefaultLocale;
            connectRequestBody.LcidSort            = ConstValues.DefaultLocale;
            connectRequestBody.AuxiliaryBufferSize = (uint)rgbAuxIn.Length;
            connectRequestBody.AuxiliaryBuffer     = rgbAuxIn;

            // Send the HTTP request and get the HTTP response.
            HttpWebResponse response = this.SendMAPIHttpRequest(userName, password, connectRequestBody, ServerEndpoint.MailboxServerEndpoint, cookies, webHeaderCollection, out rawBuffer);

            webHeaderCollection = response.Headers;
            httpStatus          = response.StatusCode;
            uint responseCode = AdapterHelper.GetFinalResponseCode(response.Headers["X-ResponseCode"]);

            if (httpStatus != HttpStatusCode.OK)
            {
                return(0);
            }

            // Read the HTTP response buffer and parse the response to correct format.
            CommonResponse commonResponse = CommonResponse.ParseCommonResponse(rawBuffer);

            if (responseCode == 0)
            {
                Site.Assert.IsNotNull(commonResponse.ResponseBodyRawData, "The response body should contains data.");
                uint statusCode = BitConverter.ToUInt32(commonResponse.ResponseBodyRawData, 0);
                if (statusCode == 0)
                {
                    // Connect succeeded when the StatusCode field equals zero.
                    ConnectSuccessResponseBody responseSuccess = ConnectSuccessResponseBody.Parse(commonResponse.ResponseBodyRawData);
                    responseBody = responseSuccess;

                    this.VerifyConnectSuccessResponseBody(responseSuccess);
                }

                this.VerifyHTTPS(response);
                this.VerifyAuthentication(response);
                this.VerifyAutoDiscover(httpStatus, ServerEndpoint.MailboxServerEndpoint);
                this.VerifyHTTPHeaders(response.Headers);
                this.VerifyAdditionalHeaders(commonResponse.AdditionalHeaders);
                this.VerifyConnectResponse(response);
                this.VerifyConnectOrBindResponse(response.Headers);
                this.VerifyRequestTypesForMailboxServerEndpoint(response.Headers, commonResponse);
                this.VerifyResponseMetaTags(commonResponse.MetaTags);
            }

            this.VerifyRespondingToAllRequestTypeRequests(response, commonResponse, responseCode);
            response.GetResponseStream().Close();
            AdapterHelper.SessionContextCookies = response.Cookies;
            cookies = response.Cookies;
            return(responseCode);
        }
        // Main work of thread
        public async void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            try
            {
                // Create new connection
                byte[] byIVArray = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                var    p         = new APIClient(IPAdd, Port, Timeout, EncryptAES, byIVArray);

                // Loop for repeat
                for (round = 0; round < Loop; round++)
                {
                    // Loop for all step
                    for (int i = 0; i < listScenario.Count; i++)
                    {
                        switch (listScenario[i])
                        {
                        case "CONNECT":
                            // Get command parameter
                            int    stationID  = ComFunc.StringToInt32(objSettingConfig.allItems["CONNECTStation"]);
                            string serverIP   = objSettingConfig.allItems["SocketServerIP"];
                            int    serverPort = ComFunc.StringToInt32(objSettingConfig.allItems["SocketServerPort"]);
                            // Create command
                            ReportProgress("CONNECT to server ...");
                            var connectReq = new ConnectRequestBody(User, Pass, stationID);
                            // Send and Wait response
                            var re = await p.ConnectAsync(serverIP, serverPort, connectReq);

                            // Report status
                            ReportProgress("CONNECT result: " + re?.Status.ToString());
                            break;

                        case "SHAKE":
                            // Get command parameter
                            // Send command to server and wait response
                            ReportProgress("Send SHAKE to server ...");
                            var re2 = await p.ShakeAsync();

                            // Report status
                            ReportProgress("SHAKE result: " + re2?.Status.ToString());
                            break;

                        case "TERMINATE":
                            // Get command parameter
                            // Send command to server and Wait response
                            ReportProgress("Send TERMINATE to server ...");
                            var re3 = await p.TerminateAsync();

                            // Report status
                            ReportProgress("TERMINATE result: " + re3?.Status.ToString());
                            break;

                        case "CHECKIN":
                            // Get command parameter
                            string eTagCHECKIN    = objSettingConfig.allItems["CHECKINEtag"];
                            int    stationCHECKIN = ComFunc.StringToInt32(objSettingConfig.allItems["CHECKINStation"]);
                            int    laneCHECKIN    = ComFunc.StringToInt32(objSettingConfig.allItems["CHECKINLane"]);
                            // Send command to server and Wait response
                            ReportProgress("Send CHECKIN to server ...");
                            var checkin = new ChargeRequestBody(eTagCHECKIN, stationCHECKIN, laneCHECKIN);
                            var re4     = await p.CheckinAsync(checkin);

                            // Report status
                            ReportProgress("CHECKIN result: " + re4?.Status.ToString());
                            PlateRes  = re4.Plate;
                            TicketRes = re4.TicketId;
                            break;

                        case "COMMIT":
                            // Get command parameter
                            string eTagCOMMIT    = objSettingConfig.allItems["COMMITEtag"];
                            int    stationCOMMIT = ComFunc.StringToInt32(objSettingConfig.allItems["COMMITStation"]);
                            int    laneCOMMIT    = ComFunc.StringToInt32(objSettingConfig.allItems["COMMITLane"]);
                            int    imageCOMMIT   = ComFunc.StringToInt32(objSettingConfig.allItems["COMMITImageCount"]);
                            // Send command to serve rand wait response
                            ReportProgress("Send COMMIT to server ...");
                            var transaction = new TransactionRequestBody(eTagCOMMIT, stationCOMMIT, laneCOMMIT, TicketRes, PlateStatus.Match, PlateRes, imageCOMMIT);
                            var re5         = await p.CommitAsync(transaction);

                            // Report status
                            ReportProgress("COMMIT result: " + re5?.Status.ToString());
                            break;

                        case "ROLLBACK":
                            // Get command parameter
                            string eTagROLLBACK    = objSettingConfig.allItems["ROLLBACKEtag"];
                            int    stationROLLBACK = ComFunc.StringToInt32(objSettingConfig.allItems["ROLLBACKStation"]);
                            int    laneROLLBACK    = ComFunc.StringToInt32(objSettingConfig.allItems["ROLLBACKLane"]);
                            int    imageROLLBACK   = ComFunc.StringToInt32(objSettingConfig.allItems["ROLLBACKImageCount"]);
                            // Send command to serve rand wait response
                            ReportProgress("Send ROLLBACK to server ...");
                            var rollback = new TransactionRequestBody(eTagROLLBACK, stationROLLBACK, laneROLLBACK, TicketRes, PlateStatus.Match, PlateRes, imageROLLBACK);
                            var re6      = await p.RollbackAsync(rollback);

                            // Report status
                            ReportProgress("ROLLBACK result: " + re6?.Status.ToString());
                            break;

                        case "CHARGE":
                            // Get command parameter
                            string eTagCHARGE    = objSettingConfig.allItems["CHARGEEtag"];
                            int    stationCHARGE = ComFunc.StringToInt32(objSettingConfig.allItems["CHECKINStation"]);
                            int    laneCHARGE    = ComFunc.StringToInt32(objSettingConfig.allItems["CHECKINLane"]);
                            // Send command to server and Wait response
                            ReportProgress("Send CHARGE to server ...");
                            var charge_req = new ChargeRequestBody(eTagCHARGE, stationCHARGE, laneCHARGE);
                            var re7        = await p.ChargeAsync(charge_req);

                            // Report status
                            break;

                        default:
                            ReportProgress("Error = Wrong Command");
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // Report Error
                ReportProgress("Error = " + ex.Message);
            }

            // Report progress: complete
            ReportProgress("COMPLETED !");
        }