From 5b31eb26d7ddfedf8e96c47f3dd6519c2521abdf Mon Sep 17 00:00:00 2001 From: Namdak Tonpa Date: Fri, 9 Oct 2020 04:41:55 +0300 Subject: [PATCH] 7.10.0 --- README.md | 1 - index.html | 5 +- man/n2o_cowboy2.htm | 53 --------------------- man/n2o_stream.htm | 105 ----------------------------------------- mix.exs | 2 +- src/n2o.app.src | 2 +- src/ws/n2o_cowboy.erl | 29 ++++++++---- src/ws/n2o_cowboy2.erl | 27 ----------- src/ws/n2o_stream.erl | 48 ------------------- 9 files changed, 25 insertions(+), 247 deletions(-) delete mode 100644 man/n2o_cowboy2.htm delete mode 100644 man/n2o_stream.htm delete mode 100644 src/ws/n2o_cowboy2.erl delete mode 100644 src/ws/n2o_stream.erl diff --git a/README.md b/README.md index 4822fce5..a54b5fde 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,6 @@ N2O Loop is directly connected and runned inside context of WebSocket handler. Usually in Erlang we use `syn` or `gproc` OTP message buses. As such buses are optional in MQTT setup we include bus drivers in WebSocket package. -* [n2o_stream](https://ws.n2o.dev/man/n2o_stream.htm) — COWBOY and XHR bridge * [n2o_ws](https://ws.n2o.dev/man/n2o_ws.htm) — N2O WebSocket Virtual Node * [n2o_heart](https://ws.n2o.dev/man/n2o_heart.htm) — PING protocol * [n2o_cowboy](https://ws.n2o.dev/man/n2o_cowboy.htm) — COWBOY API diff --git a/index.html b/index.html index 808805d1..d5724cd0 100644 --- a/index.html +++ b/index.html @@ -101,7 +101,7 @@

SYNOPSIS

The list of MQTT specific modules:

@@ -112,9 +112,8 @@

SYNOPSIS

handler that routes all incoming messages to N2O. The list of WebSocket specific modules:

diff --git a/man/n2o_cowboy2.htm b/man/n2o_cowboy2.htm deleted file mode 100644 index 79cdf92a..00000000 --- a/man/n2o_cowboy2.htm +++ /dev/null @@ -1,53 +0,0 @@ - - - - - - - - -COWBOY2 - - - - - - - - - - -
- -

COWBOY2

-
-
-
-
-

INTRO

-

The n2o_cowboy2 - module provides COWBOY2 API interface connectivity.

-
-
-

You may also want to read: - n2o_heart, - n2o_nitro. -

-
-
-
-
2005—2020 © Synrc Research Center
- - - diff --git a/man/n2o_stream.htm b/man/n2o_stream.htm deleted file mode 100644 index bec92961..00000000 --- a/man/n2o_stream.htm +++ /dev/null @@ -1,105 +0,0 @@ - - - - - - - - -STREAM - - - - - - - - - - -
- -

STREAM

-
-
-
-
-

INTRO

-

WebSocket n2o_stream - is implemented cowboy_websocket_handler behaviour for WS protocol - and cowboy_http_handler handler for XHR protocol. Both protocols are supported in single file. - This module is only for using N2O with cowboy web server.

-
-
-

API

-

The n2o_proto protocol loop is compatible with cowboy and emqttd versions of N2O. - The main purpose of this API is to embed N2O into your functional endpoint. - In other cases it's called automatically by configuration.

- -
-
The stream function is dedicated for cowboy version of n2o
- - websocket_info({ text | binary, binary() }, term(), term()) -> #reply{}. - -
- -
-
This function is called on message arrival
- - websocket_handle({ text | binary, binary() }, term(), term()) -> #reply{}. - -
- -
-
This function is called on init
- - websocket_init(term(), term(), term()) -> #reply{}. - -
- -
-
This function is called on terminate
- - websocket_terminate(term(), term(), term()) -> #reply{}. - -
- -
-
The info function is dedicated for HTTP XHR fallback
- - info(term(), term(), term()) -> #reply{}. - -
-
-
-

CONFIG

-
- - [{n2o,[{protocols,[n2o_nitro]}, - {filter,{n2o_proto,push}}]}]. - -
-
-
-

This module may refer to: - n2o, - n2o_pi. -

-
-
-
-
2005—2020 © Synrc Research Center
- - - diff --git a/mix.exs b/mix.exs index f12da534..313a2ef4 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule N2O.Mixfile do def application, do: [mod: {:n2o, []}, applications: []] def project do [ app: :n2o, - version: "7.8.3", + version: "7.10.0", description: "N2O MQTT TCP WebSocket", package: package(), deps: deps()] diff --git a/src/n2o.app.src b/src/n2o.app.src index f6666fa7..ee3aa608 100644 --- a/src/n2o.app.src +++ b/src/n2o.app.src @@ -1,6 +1,6 @@ {application,n2o, [{description,"N2O MQTT TCP WebSocket"}, - {vsn,"7.8.3"}, + {vsn,"7.10.0"}, {registered,[]}, {applications,[kernel,stdlib]}, {mod, {n2o, []}}]}. diff --git a/src/ws/n2o_cowboy.erl b/src/ws/n2o_cowboy.erl index feb9ca41..801a7432 100644 --- a/src/ws/n2o_cowboy.erl +++ b/src/ws/n2o_cowboy.erl @@ -1,16 +1,28 @@ -module(n2o_cowboy). -include_lib("n2o/include/n2o.hrl"). -description('N2O Cowboy HTTP Backend'). --export([init/3, handle/2, terminate/3, params/1, form/1, path/1, request_body/1, headers/1, header/3, +-export([init/2, websocket_init/1, websocket_handle/2, websocket_info/2, terminate/3, points/0]). +-export([params/1, form/1, path/1, request_body/1, headers/1, header/3, response/2, reply/2, cookies/1, cookie/2, cookie/3, cookie/5, delete_cookie/2, - peer/1, env/1, points/0, fix2/1, fix1/1]). --record(state, {headers, body}). + peer/1, env/1, fix2/1, fix1/1]). -% Cowboy HTTP Handler +% Cowboy 2 WebSocket API -init(_Transport, Req, _Opts) -> {ok, Req, #state{}}. -terminate(_Reason, _Req, _State) -> ok. -handle(Req, State) -> {ok, Req, State}. +init(Req,_Opts) -> {cowboy_websocket, Req, Req}. + +ws({ok,_,S}) -> {ok,S}; +ws({reply,{binary,Rep},_,S}) -> {reply,{binary,Rep},S}; +ws({reply,{json,Rep},_,S}) -> {reply,{binary,n2o_json:encode(Rep)},S}; +ws({reply,{bert,Rep},_,S}) -> {reply,{binary,n2o_bert:encode(Rep)},S}; +ws({reply,{text,Rep},_,S}) -> {reply,{text,Rep},S}; +ws({reply,{default,Rep},_,S})-> {reply,{binary,n2o:encode(Rep)},S}; +ws({reply,{Encoder,Rep},_,S})-> {reply,{binary,Encoder:encode(Rep)},S}; +ws(X) -> ?LOG_ERROR(#{unknown=>X}), {shutdown,[]}. + +websocket_init(S) -> ws(n2o_proto:init([],S,[],ws)). +websocket_handle(D,S) -> ws(n2o_proto:stream(D,[],S)). +websocket_info(D,S) -> ws(n2o_proto:info(D,[],S)). +terminate(M,R,S) -> ws(n2o_proto:info({direct,{exit,M}},R,S)). % Cowboy Bridge Abstraction @@ -40,10 +52,11 @@ static() -> { dir, fix1(code:priv_dir(application:get_env(n2o,app,review)))++"/s n2o() -> { dir, fix2(code:priv_dir(n2o)), mime() }. mime() -> [ { mimetypes, cow_mimetypes, all } ]. port() -> application:get_env(n2o,port,8000). + points() -> cowboy_router:compile([{'_', [ { "/n2o/[...]", cowboy_static, n2o() }, { "/app/[...]", cowboy_static, static() }, - { "/ws/[...]", n2o_stream, [] } ]}]). + { "/ws/[...]", n2o_cowboy, [] } ]}]). env(App) -> [{port, port()}, {certfile, code:priv_dir(App)++"/ssl/fullchain.pem"}, diff --git a/src/ws/n2o_cowboy2.erl b/src/ws/n2o_cowboy2.erl deleted file mode 100644 index 164e31b5..00000000 --- a/src/ws/n2o_cowboy2.erl +++ /dev/null @@ -1,27 +0,0 @@ --module(n2o_cowboy2). --description('N2O Cowboy2 WebSocket Backend'). --include_lib("n2o/include/n2o.hrl"). --export([init/2, websocket_init/1, websocket_handle/2, websocket_info/2, terminate/3, - points/0]). - -init(Req,_Opts) -> {cowboy_websocket, Req, Req}. - -ws({ok,_,S}) -> {ok,S}; -ws({reply,{binary,Rep},_,S}) -> {reply,{binary,Rep},S}; -ws({reply,{json,Rep},_,S}) -> {reply,{binary,n2o_json:encode(Rep)},S}; -ws({reply,{bert,Rep},_,S}) -> {reply,{binary,n2o_bert:encode(Rep)},S}; -ws({reply,{text,Rep},_,S}) -> {reply,{text,Rep},S}; -ws({reply,{default,Rep},_,S})-> {reply,{binary,n2o:encode(Rep)},S}; -ws({reply,{Encoder,Rep},_,S})-> {reply,{binary,Encoder:encode(Rep)},S}; -ws(X) -> ?LOG_ERROR(#{unknown=>X}), {shutdown,[]}. - -websocket_init(S) -> ws(n2o_proto:init([],S,[],ws)). -websocket_handle(D,S) -> ws(n2o_proto:stream(D,[],S)). -websocket_info(D,S) -> ws(n2o_proto:info(D,[],S)). -terminate(M,R,S) -> ws(n2o_proto:info({direct,{exit,M}},R,S)). - -points() -> cowboy_router:compile([{'_', [ - {"/ws/[...]", n2o_cowboy2, []}, - {"/n2o/[...]", cowboy_static, {dir, n2o_cowboy:fix2(code:priv_dir(n2o)), []}}, - {"/app/[...]", cowboy_static, {dir, n2o_cowboy:fix1(code:priv_dir( - application:get_env(n2o,app,review)))++"/static", []}} ]}]). diff --git a/src/ws/n2o_stream.erl b/src/ws/n2o_stream.erl deleted file mode 100644 index 9554070c..00000000 --- a/src/ws/n2o_stream.erl +++ /dev/null @@ -1,48 +0,0 @@ --module(n2o_stream). --description('N2O Cowboy WebSocket Backend'). --export([init/3,handle/2,info/3,terminate/3]). --export([websocket_init/3,websocket_handle/3,websocket_info/3,websocket_terminate/3]). - -init(T,R,O) -> upgrade(cowboy_req:header(<<"upgrade">>,R),{T,R,O}). -upgrade({undefined,R},{T,R,O}) -> xhr(n2o_proto:init(T,R,[{formatter,json}|O],xhr)); -upgrade({B,R},_) when is_binary(B) -> websocket(R,cowboy_bstr:to_lower(B)). -terminate(_,R,S) -> n2o_proto:terminate(R,S). - -% Lib - -body({ok,D,R2},S) -> xhr(n2o_proto:stream({type(D),D},R2,S)); -body(R,S) -> {ok,R,S}. -down(R) -> {shutdown,R,undefined}. - -type(<<"N2O,",_/binary>>) -> text; -type(<<"PING">>) -> text; -type(_) -> binary. - -% Cowboy XHR - -handle(R,S) -> body(cowboy_req:body(R),S). -info(M,R,S) -> xhr(n2o_proto:info(M,R,S)). - -reply(D,R,Code) -> {ok,R2}=cowboy_req:reply(Code,[],D,R), R2. - -xhr({ok,R,S}) -> {ok,R,S}; -xhr({reply,D,R,S}) -> {ok,reply(D,R,200),S}. - -% Cowboy WebSocket - -websocket(_,<<"websocket">>) -> {upgrade, protocol, cowboy_websocket}; -websocket(R,_) -> down(reply([],R,501)). - -websocket_info(I,R,S) -> ws(n2o_proto:info(I,R,S)). -websocket_handle(D,R,S) -> ws(n2o_proto:stream(D,R,S)). -websocket_init(T,R,_) -> ws(n2o_proto:init(T,R,[],ws)). -websocket_terminate(_,R,S) -> n2o_proto:terminate(R,S). - -ws({ok,R,S}) -> {ok,R,S,hibernate}; -ws({reply,{binary,Rep},R,S}) -> {reply,{binary,Rep},R,S,hibernate}; -ws({reply,{json,Rep},R,S}) -> {reply,{binary,n2o_json:encode(Rep)},R,S,hibernate}; -ws({reply,{bert,Rep},R,S}) -> {reply,{binary,n2o_bert:encode(Rep)},R,S,hibernate}; -ws({reply,{text,Rep},R,S}) -> {reply,{text,Rep},R,S,hibernate}; -ws({reply,{default,Rep},R,S})-> {reply,{binary,n2o:encode(Rep)},R,S,hibernate}; -ws({reply,{Encoder,Rep},R,S})-> {reply,{binary,Encoder:encode(Rep)},R,S,hibernate}; -ws(X) -> io:format("UNKNOWN: ~p~n",[X]). \ No newline at end of file