Browse Source

n2o_nitrogen to n4u_nitrogen

221V 3 years ago
parent
commit
a4e4c79f99

+ 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, n4u_async, n2o_xhr, n4u_cx, n2o_cowboy, n2o_multipart, n2o_static, n2o_stream, n4u_document, n2o_proto, n2o_relay, n4u_error, n4u_io, n4u_log, n4u_mq, n2o_pickle, n2o_query, n2o_secret, n2o_session, n2o_syn, n4u_client, n2o_file, n4u_heart, n4u_http, n2o_nitrogen, n2o_text, wf, wf_convert, wf_utils]},
+  {modules, [n2o, n4u_app, n4u_sup, n4u_async, n2o_xhr, n4u_cx, n2o_cowboy, n2o_multipart, n2o_static, n2o_stream, n4u_document, n2o_proto, n2o_relay, n4u_error, n4u_io, n4u_log, n4u_mq, n2o_pickle, n2o_query, n2o_secret, n2o_session, n2o_syn, n4u_client, n2o_file, n4u_heart, n4u_http, n4u_nitrogen, n2o_text, wf, wf_convert, wf_utils]},
   {registered, [n4u_sup]},
   {mod, {n4u_app, []}},
   {env, []}

+ 1 - 1
src/endpoints/n2o_proto.erl

@@ -6,7 +6,7 @@
 formatter(O)-> case lists:keyfind(formatter,1,O) of {formatter,F} -> F; X -> X end.
 upack(D)    -> binary_to_term(D,[safe]).
 protocols() -> wf:config(n2o,protocols,[ n4u_heart,
-                                         n2o_nitrogen,
+                                         n4u_nitrogen,
                                          n2o_file,
                                          n4u_client,
                                          n4u_http ]).

+ 1 - 1
src/protocols/n2o_file.erl

@@ -18,7 +18,7 @@ info(#ftp{status={event,_}}=FTP, Req, State) ->
     Module=State#cx.module,
     Reply=try Module:event(FTP)
           catch E:R -> Error=wf:stack(E,R), wf:error(?MODULE,"Catch: ~p:~p~n~p",Error), Error end,
-    {reply,wf:format({io,n2o_nitrogen:render_actions(wf:actions()),Reply}),Req,State};
+    {reply,wf:format({io,n4u_nitrogen:render_actions(wf:actions()),Reply}),Req,State};
 
 info(#ftp{id=Link,sid= _Sid,filename= _FileName,status= <<"init">>,block=Block,offset=Offset,size= _TotalSize}=FTP,Req,State) ->
     Root=?ROOT,

+ 0 - 89
src/protocols/n2o_nitrogen.erl

@@ -1,89 +0,0 @@
--module(n2o_nitrogen).
--include_lib("n4u/include/n4u.hrl").
--compile([export_all, nowarn_export_all]).
-
-% Nitrogen pickle handler
-
-info({init,Rest},Req,State) ->
-    Module = State#cx.module,
-    InitActionsReply = case Rest of
-         <<>> -> try Elements = Module:main(),
-                     wf_render:render(Elements),
-                     {ok,[]}
-               catch X:Y -> Stack = wf:stack(X,Y),
-                            wf:error(?MODULE,"Event Main: ~p:~p~n~p", Stack),
-                            {error,Stack} end;
-          Binary -> Pid = wf:depickle(Binary),
-                    Pid ! {'N2O',self()},
-                    {ok,receive_actions(Req)} end,
-    case InitActionsReply of
-         {ok,InitActions} -> UserCx = try Module:event(init),
-                                     case wf:config(n2o,auto_session) of
-                              disabled -> skip;
-                                      _ -> n2o_session:ensure_sid([],?CTX,[]) end
-                                    catch C:E -> Error = wf:stack(C,E),
-                                                 wf:error(?MODULE,"Event Init: ~p:~p~n~p",Error),
-                                                 {stack,Error} end,
-                             Actions = render_actions(wf:actions()),
-                             {reply,wf:format({io,iolist_to_binary([InitActions,Actions]),<<>>}),
-                                    Req,wf:context(State,?MODULE,{init,UserCx})};
-           {error,E} ->  {reply,wf:format({io,<<>>,E}), Req, wf:context(State,?MODULE,{error,E})} end;
-
-info({pickle,_,_,_}=Event, Req, State) ->
-    wf:actions([]),
-    Result = try html_events(Event,State)
-           catch E:R -> Stack = wf:stack(E,R),
-                        wf:error(?MODULE,"Catch: ~p:~p~n~p", Stack),
-                        {io,render_actions(wf:actions()),Stack} end,
-    {reply,wf:format(Result),Req,wf:context(State,?MODULE,{pickle,Result})};
-
-info({flush,Actions}, Req, State) ->
-    wf:actions([]),
-    Render = iolist_to_binary(render_actions(Actions)),
-    wf:info(?MODULE,"Flush Message: ~tp",[Render]),
-    {reply,wf:format({io,Render,<<>>}),Req, State};
-
-info({direct,Message}, Req, State) ->
-    wf:actions([]),
-    Module = State#cx.module,
-    Result = try Res = Module:event(Message), {direct,Res}
-           catch E:R -> Stack = wf:stack(E, R),
-                        wf:error(?MODULE,"Catch: ~p:~p~n~p", Stack),
-                        {stack,Stack} end,
-    {reply,wf:format({io,render_actions(wf:actions()),<<>>}),Req, wf:context(State,?MODULE,Result)};
-
-info(Message,Req,State) -> {unknown,Message,Req,State}.
-
-% double render: actions could generate actions
-
-render_actions(Actions) ->
-    wf:actions([]),
-    First  = wf:render(Actions),
-    Second = wf:render(wf:actions()),
-    wf:actions([]),
-    [First,Second].
-
-% N2O events
-
-html_events({pickle,Source,Pickled,Linked}=Pickle, State) ->
-    wf:info(?MODULE,"Pickle: ~tp",[Pickle]),
-    Ev = wf:depickle(Pickled),
-    _Result = case Ev of
-         #ev{} -> render_ev(Ev,Source,Linked,State);
-         CustomEnvelop -> wf:error("Only #ev{} events for now: ~p",[CustomEnvelop]) end,
-    {io,render_actions(wf:actions()),<<>>}.
-
-render_ev(#ev{module=M,name=F,msg=P,trigger=T},_Source,Linked,State) ->
-    case F of
-         api_event -> M:F(P,Linked,State);
-         event -> lists:map(fun({K,V})-> put(K,wf:to_binary(V)) end,Linked), M:F(P);
-         _UserCustomEvent -> M:F(P,T,State) end.
-
-receive_actions(Req) ->
-    receive
-        {actions,A} -> n2o_nitrogen:render_actions(A);
-        _ -> receive_actions(Req)
-    after 200 ->
-         QS = element(14, Req),
-         wf:redirect(case QS of <<>> -> ""; _ -> "?" ++ wf:to_list(QS) end),
-         [] end.

+ 2 - 2
src/protocols/n4u_client.erl

@@ -11,7 +11,7 @@ info({client, Message}, Req, State) ->
     wf:error(?MODULE, "Catch: ~p:~p~n~p", [E, R, Stk]),
     [E, R, Stk]
   end,
-  {reply, wf:format({io, n2o_nitrogen:render_actions(erlang:get(actions)), Reply}), Req, State};
+  {reply, wf:format({io, n4u_nitrogen:render_actions(erlang:get(actions)), Reply}), Req, State};
 
 info({server, Message}, Req, State) ->
   wf:info(?MODULE, "Server Message: ~p", [Message]),
@@ -21,7 +21,7 @@ info({server, Message}, Req, State) ->
     wf:error(?MODULE, "Catch: ~p:~p~n~p", [E, R, Stk]),
     [E, R, Stk]
   end,
-  {reply, wf:format({io, n2o_nitrogen:render_actions(erlang:get(actions)), Reply}), Req, State};
+  {reply, wf:format({io, n4u_nitrogen:render_actions(erlang:get(actions)), Reply}), Req, State};
 
 info(Message, Req, State) -> {unknown, Message, Req, State}.
 

+ 1 - 1
src/protocols/n4u_http.erl

@@ -15,7 +15,7 @@ info(#http{} = Message, Req, State) ->
     wf:error(?MODULE, "Catch: ~p:~p~n~p", [E, R, Stk]),
     [E, R, Stk]
   end,
-  {reply, wf:format({io, n2o_nitrogen:render_actions(erlang:get(actions)), Reply}), Req, State};
+  {reply, wf:format({io, n4u_nitrogen:render_actions(erlang:get(actions)), Reply}), Req, State};
 
 info(Message, Req, State) -> {unknown, Message, Req, State}.
 

+ 139 - 0
src/protocols/n4u_nitrogen.erl

@@ -0,0 +1,139 @@
+-module(n4u_nitrogen).
+-include_lib("n4u/include/n4u.hrl").
+
+-export([info/3]).
+-export([render_actions/1, html_events/2, render_ev/4, receive_actions/1]).
+% todo compare here and in nitro - render_actions, html_events, render_ev, receive_actions
+
+
+% nitrogen pickle handler
+
+info({init, Rest}, Req, State) ->
+  Module = State#cx.module,
+  InitActionsReply = case Rest of
+    <<>> ->
+      try Elements = Module:main(),
+        nitro:render(Elements),
+        {ok, []}
+      catch E1:R1:Stk1 ->
+        wf:error(?MODULE, "main: E:R:Stk: ~p ~p~n ~p~n", [E1, R1, Stk1]),
+        {error, [E1, R1, Stk1]}
+      end;
+    
+    Binary ->
+      Pid = wf:depickle(Binary),
+      Pid ! {'N4U', erlang:self()},
+      {ok, receive_actions(Req)}
+  end,
+  
+  case InitActionsReply of
+    {ok, InitActions} ->
+      UserCx = try Module:event(init),
+        case application:get_env(n4u, auto_session, "") of
+          disabled -> skip;
+          _ -> n2o_session:ensure_sid([], ?CTX, [])
+        end
+      catch E2:R2:Stk2 ->
+        wf:error(?MODULE, "init: E:R:Stk: ~p ~p~n ~p~n", [E2, R2, Stk2]),
+        {stack, [E2, R2, Stk2]}
+      end,
+      Actions = render_actions(erlang:get(actions)),
+      {reply, wf:format({io, erlang:iolist_to_binary([InitActions, Actions]), <<>>}),
+              Req, n4u_cx:context(State, ?MODULE, {init, UserCx})};
+    
+    {error, E} ->
+      {reply, wf:format({io, <<>>, E}), Req,
+              n4u_cx:context(State, ?MODULE, {error, E})}
+  end;
+
+
+info({pickle, _, _, _} = Event, Req, State) ->
+  erlang:put(actions, []),
+  Result = try html_events(Event, State)
+  catch E:R:Stk ->
+    wf:error(?MODULE, "info pickle E:R:Stk: ~p ~p~n ~p~n", [E, R, Stk]),
+    {io, render_actions(erlang:get(actions)), [E, R, Stk]}
+  end,
+  {reply, wf:format(Result), Req,
+          n4u_cx:context(State, ?MODULE, {pickle, Result})};
+
+
+info({flush, Actions}, Req, State) ->
+  erlang:put(actions, []),
+  Render = erlang:iolist_to_binary(render_actions(Actions)),
+  wf:info(?MODULE, "Flush Message: ~tp~n", [Render]),
+  {reply, wf:format({io, Render, <<>>}), Req, State};
+
+
+info({direct, Message}, Req, State) ->
+  erlang:put(actions, []),
+  Module = State#cx.module,
+  Result = try Res = Module:event(Message),
+    {direct, Res}
+  catch E:R:Stk ->
+    wf:error(?MODULE, "info direct E:R:Stk: ~p ~p~n ~p~n", [E, R, Stk]),
+    {stack, [E, R, Stk]}
+  end,
+  {reply, wf:format({io, render_actions(erlang:get(actions)), <<>>}),
+          Req, n4u_cx:context(State, ?MODULE, Result)};
+
+
+info(Message, Req, State) -> {unknown, Message, Req, State}.
+
+
+% double render: actions could generate actions
+
+render_actions(Actions) ->
+  erlang:put(actions, []),
+  First  = nitro:render(Actions),
+  Second = nitro:render(erlang:get(actions)),
+  erlang:put(actions, []),
+  [First, Second].
+
+
+% N4U events
+
+html_events({pickle, Source, Pickled, Linked} = Pickle, State) ->
+  wf:info(?MODULE, "Pickle: ~tp~n", [Pickle]),
+  Ev = wf:depickle(Pickled),
+  _Result = case Ev of
+    #ev{} ->
+      render_ev(Ev, Source, Linked, State);
+    Custom_Envelop ->
+      wf:error("Only #ev{} events for now: ~p~n", [Custom_Envelop])
+  end,
+  {io, render_actions(erlang:get(actions)), <<>>}.
+
+
+render_ev(#ev{module = M, name = F, msg = P, trigger = T}, _Source, Linked, State) ->
+  case F of
+    api_event ->
+      M:F(P, Linked, State);
+    
+    event ->
+      lists:map(fun({K, V}) ->
+        erlang:put(K, nitro:to_binary(V))
+      end, Linked),
+      M:F(P);
+    
+    _UserCustomEvent ->
+      M:F(P, T, State)
+  end.
+
+
+receive_actions(Req) ->
+  receive
+    {actions, A} ->
+      n4u_nitrogen:render_actions(A);
+    _ ->
+      receive_actions(Req)
+  after 200 ->
+    #{qs := QS} = Req,
+    R = case QS of
+      <<"">> -> "";
+      _ -> "?" ++ nitro:to_list(QS)
+    end,
+    wf:redirect(R),
+    []
+  end.
+