Пример #1
0
        public async Task Commit_async(AnsonMsg req, OnOk onOk, OnError onErr = null)
        {
            HttpServClient httpClient = new HttpServClient();
            AnsonMsg       msg        = await httpClient.Post(AnClient.ServUrl((Port)req.port), req);

            MsgCode code = msg.code;

            if (AnClient.console)
            {
                System.Console.Out.WriteLine(msg.ToString());
            }

            if (MsgCode.ok == code.code)
            {
                onOk.ok((AnsonResp)msg.Body(0));
            }
            else
            {
                if (onErr != null)
                {
                    onErr.err(code, ((AnsonResp)msg.Body(0)).Msg());
                }
                else
                {
                    System.Console.Error.WriteLine("code: {0}\nerror: {1}",
                                                   code, msg.ToString());
                }
            }
        }
Пример #2
0
        public string download(string uri, IPort port, DocsReq body, string localpath, string[] act = null)
        {
            if (port == null)
            {
                throw new AnsonException("AnsonMsg<DocsReq> needs port explicitly specified.");
            }

            // let header = Protocol.formatHeader(this.ssInf);
            body.Uri(uri);
            if (act != null && act.Length > 0)
            {
                header.act(act);
            }

            AnsonMsg msg = new AnsonMsg(port.name)
                           .Header(header)
                           .Body(body, uri);

            if (AnClient.verbose)
            {
                Utils.Logi(msg.ToString());
            }

            HttpServClient httpClient = new HttpServClient();

            return(httpClient.streamdown(AnClient.ServUrl(port), msg, localpath));
        }
Пример #3
0
        /// <summary>
        /// Submit transaction requist to jserv. This method is synchronized - not returned until callbacks been called.
        /// </summary>
        /// <param name="req"></param>
        /// <param name="onOk"></param>
        /// <param name="onErr"></param>
        public void CommitAsync(AnsonMsg req, OnOk onOk, OnError onErr = null, CancellationTokenSource waker = null)
        {
            Task t = Task.Run(async delegate
            {
                try
                {
                    HttpServClient httpClient = new HttpServClient();
                    AnsonMsg msg = await httpClient.Post(AnClient.ServUrl((Port)req.port), req);
                    MsgCode code = msg.code;

                    if (MsgCode.ok == code.code)
                    {
                        onOk.ok((AnsonResp)msg.Body(0));
                    }
                    else
                    {
                        if (onErr != null)
                        {
                            onErr.err(code, ((AnsonResp)msg.Body(0)).Msg());
                        }
                        else
                        {
                            Debug.WriteLine("Error: code: {0}\nerror: {1}",
                                            code, msg.ToString());
                        }
                    }
                }
                catch (Exception _) { }
                finally { if (waker != null)
                          {
                              waker.Cancel();
                          }
                }
            });
        }
Пример #4
0
        /// <summary>
        /// Login and return a client instance (with session managed by jserv).
        /// </summary>
        /// <param name="uid"></param>
        /// <paramref name="pswdPlain">password in plain</param>
        /// <param name="device"></param>
        /// <param name="onlogin"></param>
        /// <param name="err"></param>
        /// <throws>SQLException the request makes server generate wrong SQL.</throws>
        /// <throws>SemanticException Request can not parsed correctly</throws>
        /// <throws>GeneralSecurityException  other error</throws>
        /// <throws>Exception, most likely the network failed</throws>
        /// <return>null if failed, a SessionClient instance if login succeed.</return>
        public static async Task <SessionClient> Login(string uid, string pswdPlain, string device,
                                                       OnLogin onlogin, OnError err = null)
        {
            byte[] iv   = AESHelper.getRandom();
            string iv64 = AESHelper.Encode64(iv);

            if (uid == null || pswdPlain == null)
            {
                throw new SemanticException("user id and password can not be null.");
            }

            // string tk64 = AESHelper.Encrypt("-----------" + uid, pswdPlain, iv);
            string tk64 = AESHelper.Encrypt(uid, pswdPlain, iv);

            // formatLogin: {a: "login", logid: logId, pswd: tokenB64, iv: ivB64};
            // AnsonMsg<? extends AnsonBody> reqv11 = new AnsonMsg<AnQueryReq>(Port.session);;
            AnsonMsg reqv11 = AnSessionReq.formatLogin(uid, tk64, iv64, device);

            string         url        = ServUrl(new Port(Port.session));
            HttpServClient httpClient = new HttpServClient();

            SessionClient[] inst = new SessionClient[1];
            AnsonMsg        msg  = await httpClient.Post(url, reqv11).ConfigureAwait(false);

            MsgCode code = msg.code;

            if (code != null && MsgCode.ok == code.code)
            {
                // create a logged in client
                inst[0] = new SessionClient(((AnSessionResp)msg.Body()[0]).ssInf);
                if (onlogin != null)
                {
                    // onlogin.ok(new SessionClient(((AnSessionResp)msg.Body()[0]).ssInf));
                    onlogin.ok(inst[0]);
                }

                if (AnClient.console)
                {
                    Console.WriteLine(msg.ToString());
                }
            }
            else if (err != null)
            {
                err.err(new MsgCode(code.code), ((AnsonResp)msg.Body(0)).Msg());
            }
            else
            {
                throw new SemanticException(
                          "loging failed\ncode: {0}\nerror: {1}",
                          code, ((AnsonResp)msg.Body()[0]).Msg());
            }
            return(inst[0]);
        }
Пример #5
0
        public AnsonResp Commit(AnsonMsg req, ErrorCtx err = null)
        {
            HttpServClient  httpClient = new HttpServClient();
            Task <AnsonMsg> tmsg       = httpClient.Post(AnClient.ServUrl((Port)req.port), req);

            tmsg.Wait();
            AnsonMsg msg  = tmsg.Result;
            MsgCode  code = msg.code;

            if (MsgCode.ok != code.code)
            {
                if (err != null)
                {
                    err.onError(code, ((AnsonResp)msg.Body(0)).Msg());
                }
                else
                {
                    Debug.WriteLine("Error: code: {0}\nerror: {1}",
                                    code, msg.ToString());
                }
            }

            return((AnsonResp)msg.Body(0));
        }