|
25 | 25 | %%% </pre> |
26 | 26 | -module(dev_router). |
27 | 27 | -export([routes/3, route/2, route/3, preprocess/3]). |
28 | | --export([match/3, is_relevant/3]). |
| 28 | +-export([match/3, is_relevant/3, register/3]). |
29 | 29 | -include_lib("eunit/include/eunit.hrl"). |
30 | 30 | -include("include/hb.hrl"). |
31 | 31 |
|
32 | 32 | -define(DEFAULT_RELAVANT_ROUTES, [ |
33 | 33 | #{ <<"template">> => <<"/.*~process@1.0/.*">> } |
34 | 34 | ]). |
35 | 35 |
|
| 36 | +%% A exposed register function that allows telling the current node to register |
| 37 | +%% a new route with a remote router node. This function should also be itempotent |
| 38 | +%% so that it can be called only once. |
| 39 | +register(_M1, M2, Opts) -> |
| 40 | + ?event(debug_pete, {register, {msg, M2}}), |
| 41 | + Registered = hb_opts:get(registered, false, Opts), |
| 42 | + case Registered of |
| 43 | + true -> |
| 44 | + {ok, #{ |
| 45 | + <<"status">> => 208, |
| 46 | + <<"message">> => <<"Node already registered.">> |
| 47 | + }}; |
| 48 | + false -> |
| 49 | + RouterNode = hb_ao:get(<<"peer-location">>, M2, not_found, Opts), |
| 50 | + Prefix = hb_ao:get(<<"prefix">>, M2, not_found, Opts), |
| 51 | + Price = hb_ao:get(<<"price">>, M2, not_found, Opts), |
| 52 | + Template = hb_ao:get(<<"template">>, M2, not_found, Opts), |
| 53 | + |
| 54 | + % Check if any required parameters are missing |
| 55 | + Missing = [ |
| 56 | + {<<"peer-location">>, RouterNode}, |
| 57 | + {<<"prefix">>, Prefix}, |
| 58 | + {<<"price">>, Price}, |
| 59 | + {<<"template">>, Template} |
| 60 | + ], |
| 61 | + |
| 62 | + MissingParams = [Param || {Param, Value} <- Missing, Value =:= not_found], |
| 63 | + |
| 64 | + case MissingParams of |
| 65 | + [] -> |
| 66 | + % All required parameters are present, proceed with registration |
| 67 | + Req = #{ |
| 68 | + <<"path">> => <<"/router~node-process@1.0/schedule">>, |
| 69 | + <<"method">> => <<"POST">>, |
| 70 | + <<"body">> => |
| 71 | + hb_message:commit( |
| 72 | + #{ |
| 73 | + <<"path">> => <<"register">>, |
| 74 | + <<"route">> => |
| 75 | + #{ |
| 76 | + <<"prefix">> => Prefix, |
| 77 | + <<"template">> => Template, |
| 78 | + <<"price">> => Price |
| 79 | + }, |
| 80 | + <<"body">> => M2 |
| 81 | + }, |
| 82 | + Opts |
| 83 | + ) |
| 84 | + }, |
| 85 | + case hb_http:post(RouterNode, Req, Opts) of |
| 86 | + {ok, _} -> |
| 87 | + hb_http_server:set_opts(Opts#{ registered => true }), |
| 88 | + {ok, <<"Route registered.">>}; |
| 89 | + {error, _} -> |
| 90 | + {error, <<"Failed to register route.">>} |
| 91 | + end; |
| 92 | + _ -> |
| 93 | + % Some parameters are missing, return error message |
| 94 | + ParamList = lists:foldl( |
| 95 | + fun(Param, Acc) -> |
| 96 | + case Acc of |
| 97 | + <<>> -> Param; |
| 98 | + _ -> <<Acc/binary, ", ", Param/binary>> |
| 99 | + end |
| 100 | + end, |
| 101 | + <<>>, |
| 102 | + MissingParams |
| 103 | + ), |
| 104 | + ErrorMsg = <<"Missing required parameters: ", ParamList/binary>>, |
| 105 | + {error, ErrorMsg} |
| 106 | + end |
| 107 | + end. |
| 108 | + |
36 | 109 | %% @doc Device function that returns all known routes. |
37 | 110 | routes(M1, M2, Opts) -> |
38 | 111 | ?event({routes_msg, M1, M2}), |
@@ -394,7 +467,10 @@ preprocess(Msg1, Msg2, Opts) -> |
394 | 467 | {_, Match} = match(#{ <<"routes">> => TemplateRoutes}, Req, Opts), |
395 | 468 | case Match of |
396 | 469 | no_matching_route -> |
397 | | - {error, <<"No matching route found">>}; |
| 470 | + {ok, #{ |
| 471 | + <<"status">> => 404, |
| 472 | + <<"message">> => <<"No matching template found in the given routes.">> |
| 473 | + }}; |
398 | 474 | _ -> |
399 | 475 | {ok, |
400 | 476 | [ |
|
0 commit comments