Upcall.ccn_upcall_res WriteCallback(IntPtr selfp, Upcall.ccn_upcall_kind kind, IntPtr info) { print("WriteCallback... " + kind); Upcall.ccn_upcall_res ret = Upcall.ccn_upcall_res.CCN_UPCALL_RESULT_OK; Egal.ccn_upcall_info Info = new Egal.ccn_upcall_info(); Info = (Egal.ccn_upcall_info)Marshal.PtrToStructure(info, typeof(Egal.ccn_upcall_info)); IntPtr h = Info.h; Egal.ccn_closure Selfp = new Egal.ccn_closure(); Selfp = (Egal.ccn_closure)Marshal.PtrToStructure(selfp, typeof(Egal.ccn_closure)); NormalStruct Data = new NormalStruct(); Data = (NormalStruct)Marshal.PtrToStructure(Selfp.data, typeof(NormalStruct)); switch (kind) { case Upcall.ccn_upcall_kind.CCN_UPCALL_FINAL: // Marshal.FreeHGlobal(selfp); // this again, will make Unity crash break; case Upcall.ccn_upcall_kind.CCN_UPCALL_INTEREST: PutContent(h, Data); // publish content ret = Upcall.ccn_upcall_res.CCN_UPCALL_RESULT_INTEREST_CONSUMED; break; case Upcall.ccn_upcall_kind.CCN_UPCALL_CONTENT: // repo first returns a content... // this is info of the repo... break; default: ret = Upcall.ccn_upcall_res.CCN_UPCALL_RESULT_ERR; break; } // print ("ref count: " + Selfp.refcount); // print ("WriteCallback returnning..." + ret); return(ret); }
void WriteToRepo(IntPtr h, System.String name, System.String content) { print("Writing " + name + " to repo: " + content); int res; IntPtr cb = Egal.ccn_charbuf_create(); IntPtr nm = Egal.ccn_charbuf_create(); IntPtr cmd = Egal.ccn_charbuf_create(); Egal.ccn_name_from_uri(nm, name); Egal.ccn_create_version(h, nm, VersioningFlags.CCN_V_NOW, 0, 0); NormalStruct Data = new NormalStruct(nm, cb, h, content.Length, content); IntPtr pData = Marshal.AllocHGlobal(Marshal.SizeOf(Data)); Marshal.StructureToPtr(Data, pData, true); IntPtr template = Egal.SyncGenInterest(IntPtr.Zero, 1, 4, -1, -1, IntPtr.Zero); Egal.ccn_closure Action = new Egal.ccn_closure(WriteCallback, pData, 0); IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(Action)); Marshal.StructureToPtr(Action, pnt, true); res = Egal.ccn_set_interest_filter(h, nm, pnt); // listen: interest res = Egal.ccn_charbuf_append_charbuf(cmd, nm); res = Egal.ccn_name_from_uri(cmd, "%C1.R.sw"); Egal.ccn_name_append_nonce(cmd); counter_for_run++; res = Egal.ccn_set_run_timeout(h, 0); Egal.ccn_express_interest(h, cmd, pnt, template); // express interest counter_for_run--; return; }
int RegisterInterestFilter(IntPtr h, string name) { int res = 0; IntPtr nm = Egal.ccn_charbuf_create(); res = Egal.ccn_name_from_uri(nm, name); if (res < 0) { return(res); } NormalStruct State = new NormalStruct(nm, IntPtr.Zero, h, 0, ""); IntPtr pState = Marshal.AllocHGlobal(Marshal.SizeOf(State)); Marshal.StructureToPtr(State, pState, true); Egal.ccn_closure Action = new Egal.ccn_closure(PublishState, pState, 0); IntPtr pAction = Marshal.AllocHGlobal(Marshal.SizeOf(Action)); Marshal.StructureToPtr(Action, pAction, true); res = Egal.ccn_set_interest_filter(h, nm, pAction); return(res); }
void WriteToRepo(System.String name, System.String content) { print ("Writing " + name + " to repo: " + content); int res; IntPtr h = GetHandle(); IntPtr cb = Egal.ccn_charbuf_create(); IntPtr nm = Egal.ccn_charbuf_create(); IntPtr cmd = Egal.ccn_charbuf_create(); Egal.ccn_name_from_uri(nm, name); Egal.ccn_create_version(h, nm, VersioningFlags.CCN_V_NOW, 0, 0); NormalStruct Data = new NormalStruct(nm, cb, h, content.Length, content); IntPtr pData = Marshal.AllocHGlobal(Marshal.SizeOf(Data)); Marshal.StructureToPtr(Data, pData, true); IntPtr template = Egal.SyncGenInterest(IntPtr.Zero, 1, 4, -1, -1, IntPtr.Zero); Egal.ccn_closure Action = new Egal.ccn_closure(WriteCallback, pData, 0); IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(Action)); Marshal.StructureToPtr(Action, pnt, true); res = Egal.ccn_set_interest_filter(h, nm, pnt); // listen: interest res = Egal.ccn_charbuf_append_charbuf(cmd, nm); res = Egal.ccn_name_from_uri(cmd, "%C1.R.sw"); Egal.ccn_name_append_nonce(cmd); Egal.ccn_express_interest(h, cmd, pnt, template); // express interest Egal.ccn_run(h,-1); Egal.ccn_destroy(ref h); }
Upcall.ccn_upcall_res WriteCallback(IntPtr selfp, Upcall.ccn_upcall_kind kind, IntPtr info) { print("WriteCallback... " + kind); Upcall.ccn_upcall_res ret = Upcall.ccn_upcall_res.CCN_UPCALL_RESULT_OK; Egal.ccn_upcall_info Info = new Egal.ccn_upcall_info(); Info = (Egal.ccn_upcall_info)Marshal.PtrToStructure(info, typeof(Egal.ccn_upcall_info)); IntPtr h = Info.h; Egal.ccn_closure Selfp = new Egal.ccn_closure(); Selfp = (Egal.ccn_closure)Marshal.PtrToStructure(selfp, typeof(Egal.ccn_closure)); NormalStruct Data = new NormalStruct(); Data = (NormalStruct) Marshal.PtrToStructure(Selfp.data, typeof(NormalStruct)); switch(kind) { case Upcall.ccn_upcall_kind.CCN_UPCALL_FINAL: // Marshal.FreeHGlobal(selfp); // this again, will make Unity crash break; case Upcall.ccn_upcall_kind.CCN_UPCALL_INTEREST: // print ("put content handle: " + h + ", content: " + Data.value); int res = PutContent(h, Data); if(res >= 0) { ret = Upcall.ccn_upcall_res.CCN_UPCALL_RESULT_INTEREST_CONSUMED; Egal.ccn_set_run_timeout(h, 0); } else print("put content error"); break; case Upcall.ccn_upcall_kind.CCN_UPCALL_CONTENT: // repo first returns a content... // this is info of the repo... break; default: ret = Upcall.ccn_upcall_res.CCN_UPCALL_RESULT_ERR; break; } // print ("ref count: " + Selfp.refcount); // print ("WriteCallback returnning..." + ret); return ret; }
static void ReadFromRepo(string dst) { print("Reading from the repo."); IntPtr ccn = GetHandle(); IntPtr nm = Egal.ccn_charbuf_create(); Egal.ccn_name_from_uri(nm,dst); Egal.ccn_create_version(ccn, nm, VersioningFlags.CCN_V_LOW, 0, 0); // without version, Unity crashes! //CCN_V_LOW might be right, might not! NormalStruct Data = new NormalStruct(nm, IntPtr.Zero, IntPtr.Zero, 0, ""); IntPtr pData = Marshal.AllocHGlobal(Marshal.SizeOf(Data)); Marshal.StructureToPtr(Data, pData, true); IntPtr template = Egal.SyncGenInterest(IntPtr.Zero, 1, 4, -1, -1, IntPtr.Zero); Egal.ccn_closure Action = new Egal.ccn_closure(ReadCallback, pData, 0); IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(Action)); Marshal.StructureToPtr(Action, pnt, true); Egal.ccn_express_interest(ccn,nm,pnt,template); Egal.ccn_run(ccn,-1); Egal.ccn_destroy(ref ccn); }
// ExpressInterest() // takes handle, name, callback and template public static int ExpressInterest(IntPtr ccn, string name, Egal.ccn_handler callback, IntPtr pData, IntPtr template) { IntPtr nm = Egal.ccn_charbuf_create(); Egal.ccn_name_from_uri(nm,name); Egal.ccn_closure Action = new Egal.ccn_closure(callback, pData, 0); IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(Action)); Marshal.StructureToPtr(Action, pnt, true); Egal.ccn_express_interest(ccn,nm,pnt,template); return 0; }