/// <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(); } } }); }
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()); } } }
public AlbumClientier getSettings(OnOk onOk, OnError onErr) { Task.Run(() => { try { AnsonHeader header = client .Header() .UsrAct("album.c#", "profile", "r/settings", "load profile"); AlbumReq req = new AlbumReq(uri); req.A(A.getPrefs); AnsonMsg q = client .UserReq(uri, new AlbumPort(AlbumPort.album), null, req) .Header(header); client.CommitAsync(q, onOk, onErr); } catch (Exception e) { onErr.err(new MsgCode(MsgCode.exIo), string.Format("%s\n%s", e.GetType().Name, e.Message)); } }); return(this); }
/// <summary> /// Asynchronously synchronize photos /// </summary> /// <param name="photos"></param> /// <param name="user"></param> /// <param name="onOk"></param> /// <param name="onErr"></param> /// <exception cref="SemanticException"></exception> /// <exception cref="SemanticException"></exception> /// <exception cref="IOException"></exception> /// <exception cref="AnsonException"></exception> public void asyncPhotos(List <IFileDescriptor> photos, SessionInf user, OnOk onOk, OnError onErr) { DocsResp resp = null; try { AnsonHeader header = client.Header().act("album.c#", "synch", "c/photo", "multi synch"); List <DocsResp> reslts = new List <DocsResp>(photos.Count); foreach (IFileDescriptor p in photos) { AlbumReq req = new AlbumReq() .createPhoto(p, user); AnsonMsg q = client.UserReq(uri, new AlbumPort(AlbumPort.album), req) .Header(header); Task <AnsonResp> tresp = (Task <AnsonResp>)client.Commit_async(q, null, onErr); tresp.Wait(); reslts.Add((DocsResp)tresp.Result); } onOk.ok(resp); } catch (Exception e) { onErr.err(new MsgCode(MsgCode.exIo), string.Format("%s, %s", e.GetType().Name, e.Message)); } }
/// <summary> /// Asynchronously query synchronizing records. /// </summary> /// <param name="files"></param> /// <param name="page"></param> /// <param name="onOk"></param> /// <param name="onErr"></param> /// <returns>this</returns> public AlbumClientier asyncQuerySyncs(IList <IFileDescriptor> files, SyncingPage page, OnOk onOk, OnError onErr) { Task.Run(() => { DocsResp resp = null; try { AnsonHeader header = client.Header().act("album.c#", "query", "r/states", "query sync"); List <DocsResp> reslts = new List <DocsResp>(files.Count); AlbumReq req = (AlbumReq) new AlbumReq().Syncing(page).A(A.selectSyncs); for (int i = page.start; i < page.end & i < files.Count; i++) { IFileDescriptor p = files[i]; req.querySync(p); } AnsonMsg q = client.UserReq(uri, new AlbumPort(AlbumPort.album), req) .Header(header); resp = (DocsResp)client.Commit(q, errCtx); reslts.Add(resp); onOk.ok(resp); } catch (Exception e) { onErr.err(new MsgCode(MsgCode.exIo), uri, new string[] { e.GetType().Name, e.Message }); } }); return(this); }
/// <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]); }
public AlbumClientier asyncVideos(IList <IFileDescriptor> videos, SessionInf user, OnProcess onProc, OnOk onOk, OnError onErr) { Task.Run(() => { try { IList <DocsResp> reslts = syncVideos(videos, user, onProc); DocsResp resp = new DocsResp(); resp.Data()["results"] = reslts; onOk.ok(resp); } catch (Exception e) { onErr.err(new MsgCode(MsgCode.exIo), uri, new string[] { e.GetType().Name, e.Message }); } }); return(this); }