Browse Source

n2o_async to n4u_async

221V 3 years ago
parent
commit
f881162a7b
7 changed files with 34 additions and 28 deletions
  1. 1 1
      README.md
  2. 1 1
      ebin/n4u.app
  3. 13 6
      include/n4u_api.hrl
  4. 5 5
      src/handlers/n4u_async.erl
  5. 3 4
      src/n4u_sup.erl
  6. 4 4
      src/protocols/n2o_file.erl
  7. 7 7
      src/wf.erl

+ 1 - 1
README.md

@@ -65,7 +65,7 @@ All Features in One snippet
 
 peer()    -> io_lib:format("~p",[wf:peer(?REQ)]).
 message() -> wf:js_escape(wf:html_encode(wf:q(message))).
-main()    -> #dtl{file="index",app=n2o_sample,bindings=[{body,body()}]}.
+main()    -> #dtl{file="index",app=n4u_sample,bindings=[{body,body()}]}.
 body() ->
     {Pid,_} = wf:async(fun(X) -> chat_loop(X) end),
     [ #panel{id=history}, #textbox{id=message},

+ 1 - 1
ebin/n4u.app

@@ -2,7 +2,7 @@
   {description, "N4U WebSocket Application Server"},
   {vsn, "4.4.20"},
   {applications, [kernel, stdlib, asn1, public_key, ssl, crypto, ranch, cowboy, fs, active, sh, gproc, nitro]},
-  {modules, [n2o, n4u_app, n4u_sup, n2o_async, n2o_xhr, n2o_cx, n2o_cowboy, n2o_multipart, n2o_static, n2o_stream, n2o_document, n2o_proto, n2o_relay, n2o_error, n2o_io, n2o_log, n2o_mq, n2o_pickle, n2o_query, n2o_secret, n2o_session, n2o_syn, n2o_client, n2o_file, n2o_heart, n2o_http, n2o_nitrogen, n2o_text, wf, wf_convert, wf_utils]},
+  {modules, [n2o, n4u_app, n4u_sup, n4u_async, n2o_xhr, n2o_cx, n2o_cowboy, n2o_multipart, n2o_static, n2o_stream, n2o_document, n2o_proto, n2o_relay, n2o_error, n2o_io, n2o_log, n2o_mq, n2o_pickle, n2o_query, n2o_secret, n2o_session, n2o_syn, n2o_client, n2o_file, n2o_heart, n2o_http, n2o_nitrogen, n2o_text, wf, wf_convert, wf_utils]},
   {registered, [n4u_sup]},
   {mod, {n4u_app, []}},
   {env, []}

+ 13 - 6
include/n4u_api.hrl

@@ -7,6 +7,7 @@
 -type wire_answer() :: undefined | list(tuple()).
 -type wiring() :: string() | tuple().
 
+
 % update
 
 -spec update(name(), render()) -> wire_answer().
@@ -16,24 +17,28 @@
 -spec insert_after(name(), render()) -> wire_answer().
 -spec remove(name()) -> wire_answer().
 
+
 % wire
 
 -spec wire(wiring()) -> wire_answer().
 
+
 % async
 
--spec async(name(),fun()) -> {pid(),{async,{name(),any()}}}.
--spec start(#handler{}) -> {pid(),{name(),any()}}.
--spec stop(name()) -> any().
--spec restart(name()) -> any().
--spec flush() -> any().
--spec flush(any()) -> any().
+%-spec async(name(),fun()) -> {pid(),{async,{name(),any()}}}.
+%-spec start(#handler{}) -> {pid(),{name(),any()}}.
+%-spec stop(name()) -> any().
+%-spec restart(name()) -> any().
+%-spec flush() -> any().
+%-spec flush(any()) -> any().
+
 
 % pickle
 
 -spec pickle(any()) -> binary().
 -spec depickle(binary()) -> any().
 
+
 % session
 
 -spec session(name()) -> any().
@@ -41,12 +46,14 @@
 -spec user() -> any().
 -spec user(any()) -> any().
 
+
 % mq
 
 -spec send(name(), any()) -> {ok,pid()}.
 -spec reg(name(), any()) -> defined | undefined | any().
 -spec unreg(name()) -> skip | undefined | any().
 
+
 % query
 
 -spec q(name()) -> any().

+ 5 - 5
src/handlers/n2o_async.erl → src/handlers/n4u_async.erl

@@ -1,11 +1,11 @@
--module(n2o_async).
+-module(n4u_async).
 -include_lib("n4u/include/n4u.hrl").
 -behaviour(gen_server).
 -export([start_link/1]).
 -export([init/1,handle_call/3,handle_cast/2,handle_info/2,terminate/2,code_change/3]).
 -compile([export_all, nowarn_export_all]).
 
-% n2o_async API
+% n4u_async API
 
 async(Fun) -> async(async,wf:temp_id(),Fun).
 async(Name, F) -> async(async,Name,F).
@@ -13,7 +13,7 @@ async(Class,Name,F) ->
     Key = key(),
     Handler = #handler{module=?MODULE,class=async,group=n2o,
                        name={Name,Key},config={F,?REQ},state=self()},
-    case n2o_async:start(Handler) of
+    case ?MODULE:start(Handler) of
         {error,{already_started,P}} -> init(P,Class,{Name,Key}), {P,{Class,{Name,Key}}};
         {P,X} when is_pid(P)        -> init(P,Class,X),          {P,{Class,X}};
         Else -> Else end.
@@ -21,7 +21,7 @@ async(Class,Name,F) ->
 init(Pid,Class,Name) when is_pid(Pid) -> wf:cache({Class,Name},Pid,infinity), send(Pid,{parent,self()}).
 send(Pid,Message) when is_pid(Pid) -> gen_server:call(Pid,Message);
 send(Name,Message) -> send(async,{Name,key()},Message).
-send(Class,Name,Message) -> gen_server:call(n2o_async:pid({Class,Name}),Message).
+send(Class,Name,Message) -> gen_server:call(?MODULE:pid({Class,Name}),Message).
 pid({Class,Name}) -> wf:cache({Class,Name}).
 key() -> n2o_session:session_id().
 restart(Name) -> restart(async,{Name,key()}).
@@ -31,7 +31,7 @@ flush() -> A=wf:actions(), wf:actions([]), get(parent) ! {flush,A}.
 flush(Pool) -> A=wf:actions(), wf:actions([]), wf:send(Pool,{flush,A}).
 stop(Name) -> stop(async,{Name,key()}).
 stop(Class,Name) ->
-    case n2o_async:pid({Class,Name}) of
+    case ?MODULE:pid({Class,Name}) of
         Pid when is_pid(Pid) ->
             #handler{group=Group} = Async = send(Pid,{get}),
             [ supervisor:F(Group,{Class,Name})||F<-[terminate_child,delete_child]],

+ 3 - 4
src/n4u_sup.erl

@@ -1,5 +1,4 @@
 -module(n4u_sup).
-
 -behaviour(supervisor).
 
 -include_lib("n4u/include/n4u.hrl").
@@ -14,7 +13,7 @@ ets_tables_opts() -> [ set, named_table, { keypos, 1 }, public ].
 
 start_link() ->
   A = supervisor:start_link({local, ?MODULE}, ?MODULE, []),
-  n2o_async:start(#handler{module=?MODULE, class=system, group=?MODULE, state=[], name="timer"}),
+  n4u_async:start(#handler{module=?MODULE, class=system, group=?MODULE, state=[], name="timer"}),
   A.
 
 
@@ -31,7 +30,7 @@ init([]) ->
 
 
 
-% proc handler for n2o_async:start on line 17
+% proc handler for n4u_async:start (line 16 of this module)
 timer_restart(Diff) ->
   {X, Y, Z} = Diff,
   erlang:send_after(1000 * (Z + 60 * Y + 60 * 60 * X), erlang:self(), {timer, ping}).
@@ -50,7 +49,7 @@ proc({timer, ping}, #handler{state=Timer}=Async) ->
     undefined -> skip;
     _ -> erlang:cancel_timer(Timer)
   end,
-  wf:info(?MODULE, "n2o Timer: ~p~n", [ping]),
+  wf:info(?MODULE, "n4u Timer: ~p~n", [ping]),
   (application:get_env(n2o, session, n2o_session)):invalidate_sessions(),
   wf:invalidate_cache(),
   {reply, ok, Async#handler{state=timer_restart(ping())}}.

+ 4 - 4
src/protocols/n2o_file.erl

@@ -34,14 +34,14 @@ info(#ftp{id=Link,sid= _Sid,filename= _FileName,status= <<"init">>,block=Block,o
     Offset2=case FileSize >= Offset of true -> FileSize; false -> 0 end,
     FTP2=FTP#ftp{block=Block2,offset=Offset2,filename=RelPath,data= <<>>},
 
-    n2o_async:stop(file,Link),
-    n2o_async:start(#handler{module=?MODULE,class=file,group=n2o,state=FTP2,name=Link}),
+    n4u_async:stop(file,Link),
+    n4u_async:start(#handler{module=?MODULE,class=file,group=n2o,state=FTP2,name=Link}),
 
     {reply,wf:format(FTP2),Req,State};
 
 info(#ftp{id=Link,sid= _Sid,filename= _FileName,status= <<"send">>}=FTP,Req,State) ->
     wf:info(?MODULE,"Info Send: ~p",[FTP#ftp{data= <<>>}]),
-    Reply=try gen_server:call(n2o_async:pid({file,Link}),FTP)
+    Reply=try gen_server:call(n4u_async:pid({file,Link}),FTP)
         catch _E:_R -> wf:error(?MODULE,"Info Error call the sync: ~p~n",[FTP#ftp{data= <<>>}]),
             FTP#ftp{data= <<>>,block=?STOP} end,
     wf:info(?MODULE,"reply ~p",[Reply#ftp{data= <<>>}]),
@@ -69,7 +69,7 @@ proc(#ftp{id=Link,sid=Sid,data=Data,filename= _FileName,status= <<"send">>,block
 		ok ->
             FTP2=FTP#ftp{data= <<>>,block=?STOP},
             wf:send(Sid,FTP2#ftp{status={event,stop},filename=RelPath}),
-			spawn(fun() -> n2o_async:stop(file,Link) end),
+			spawn(fun() -> n4u_async:stop(file,Link) end),
 			{stop,normal,FTP2,Async#handler{state=FTP2}} end;
 
 proc(#ftp{sid= _Sid,data=Data,block=Block}=FTP,

+ 7 - 7
src/wf.erl

@@ -62,13 +62,13 @@ wire(Actions) -> action_wire:wire(Actions).
 
 % Spawn async processes wf:async, wf:flush
 
-async(Function) -> n2o_async:async(Function).
-start(#handler{}=Handler) -> n2o_async:start(Handler).
-stop(Name) -> n2o_async:stop(Name).
-restart(Name) -> n2o_async:restart(Name).
-async(Name,Function) -> n2o_async:async(Name,Function).
-flush() -> n2o_async:flush().
-flush(Key) -> n2o_async:flush(Key).
+async(Function) -> n4u_async:async(Function).
+start(#handler{}=Handler) -> n4u_async:start(Handler).
+stop(Name) -> n4u_async:stop(Name).
+restart(Name) -> n4u_async:restart(Name).
+async(Name,Function) -> n4u_async:async(Name,Function).
+flush() -> n4u_async:flush().
+flush(Key) -> n4u_async:flush(Key).
 
 % Redirect and purge connection wf:redirect