Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 168 lines (150 sloc) 6.964 kB
a4c1476 @baphled Added units for reimplementing CWIGA along with the implementation code.
authored
1 %%----------------------------------------------------------------
2 %%% @author Yomi Colledge <yomi@boodah.net>
3 %%% @doc Web interface for Chatterl
4 %%%
5 %%% Chatterl Web Gateway, allowing Web based clients to interact
6 %%% with Chatterl over a RESTful API.
7 %%%
8 %%% Allows Chatterl to interface with any web-based interface
9 %%% Using JSON and XML, sending the requests off to the chatterl_serv
10 %%% module.
11 %%%
12 %%% All calls to CWIGA will only be allowed via a specified IP, which
13 %%% will be defined with the configuration file.
14 %%% @end
15 %%% @copyright 2008-2009 Yomi Colledge
16 %%%---------------------------------------------------------------
17 -module(cwiga).
18
19 -behaviour(gen_server).
20
21 %% API
22 -export([start_link/1,stop/0]).
23
24 -define(APP, "CWIGA").
25 %% gen_server callbacks
26 -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
27 terminate/2, code_change/3]).
28
29 -define(SERVER, ?MODULE).
30 -record(state, {}).
31
32 %%====================================================================
33 %% API
34 %%====================================================================
35 %%--------------------------------------------------------------------
36 %% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
37 %% Description: Starts the server
38 %%--------------------------------------------------------------------
39 start_link(Port) ->
40 gen_server:start_link({global, ?SERVER}, ?MODULE, [Port], []).
41
42 stop() ->
43 gen_server:call({global, ?SERVER}, stop, infinity).
44
45 dispatch_requests(Req) ->
46 %Path = Req:get(path),
47 [Path|Ext] = string:tokens(Req:get(path),"."),
48 Method = Req:get(method),
49 Post = Req:parse_post(),
50 io:format("~p request for ~p with post: ~p~n", [Method, Path, Post]),
51 Response = handle(Method, Path, get_content_type(Ext), Post),
52 Req:respond(Response).
53
1c0f3d1 @baphled Implemented connect & disconnect functionality for CWIGA
authored
54 handle('GET',"/users/connect/" ++ Client,ContentType,_Post) ->
55 success(chatterl_mid_man:connect(ContentType,Client),ContentType);
56 handle('GET',"/users/disconnect/" ++ Client,ContentType,_Post) ->
57 success(chatterl_mid_man:disconnect(ContentType,Client),ContentType);
2d32186 @baphled Added more get functionality to CWIGA, will finish off later.
authored
58 handle('GET',"/users/list/" ++ Group,ContentType,_Post) ->
59 success(chatterl_mid_man:user_list(ContentType,Group),ContentType);
494591e @baphled Adding list users units and implementation code for the reimplementat…
authored
60 handle('GET',"/users/list",ContentType,_Post) ->
1c0f3d1 @baphled Implemented connect & disconnect functionality for CWIGA
authored
61 success(chatterl_mid_man:user_list(ContentType),ContentType);
2d32186 @baphled Added more get functionality to CWIGA, will finish off later.
authored
62 handle('GET',"/users/poll/" ++ Client,ContentType,_Post) ->
63 success(chatterl_mid_man:user_poll(ContentType,Client),ContentType);
64 handle('GET',"/groups/list",ContentType,_Post) ->
65 success(chatterl_mid_man:group_list(ContentType),ContentType);
494591e @baphled Adding list users units and implementation code for the reimplementat…
authored
66 handle(_,Path,ContentType,_) ->
a4c1476 @baphled Added units for reimplementing CWIGA along with the implementation code.
authored
67 Response = message_handler:get_response_body(ContentType,
68 message_handler:build_carrier("error", "Unknown command: " ++Path)),
69 {404, [{"Content-Type", ContentType}], list_to_binary(Response)}.
1c0f3d1 @baphled Implemented connect & disconnect functionality for CWIGA
authored
70
71 success(Response,ContentType) ->
72 {200, [{"Content-Type", ContentType}], list_to_binary(Response)}.
a4c1476 @baphled Added units for reimplementing CWIGA along with the implementation code.
authored
73 %%====================================================================
74 %% gen_server callbacks
75 %%====================================================================
76
77 %%--------------------------------------------------------------------
78 %% Function: init(Args) -> {ok, State} |
79 %% {ok, State, Timeout} |
80 %% ignore |
81 %% {stop, Reason}
82 %% Description: Initiates the server
83 %%--------------------------------------------------------------------
84 init([Port]) ->
85 process_flag(trap_exit, true),
86 mochiweb_http:start([{port, Port}, {loop, fun dispatch_requests/1}]),
87 erlang:monitor(process,mochiweb_http),
88 {ok, #state{}}.
89
90 %%--------------------------------------------------------------------
91 %% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
92 %% {reply, Reply, State, Timeout} |
93 %% {noreply, State} |
94 %% {noreply, State, Timeout} |
95 %% {stop, Reason, Reply, State} |
96 %% {stop, Reason, State}
97 %% Description: Handling call messages
98 %%--------------------------------------------------------------------
99 handle_call(stop, _From, State) ->
100 io:format("Processing shut down ~s~n", [?APP]),
101 {stop, normal, stopped, State};
102 handle_call(_Request, _From, State) ->
103 Reply = ok,
104 {reply, Reply, State}.
105
106 %%--------------------------------------------------------------------
107 %% Function: handle_cast(Msg, State) -> {noreply, State} |
108 %% {noreply, State, Timeout} |
109 %% {stop, Reason, State}
110 %% Description: Handling cast messages
111 %%--------------------------------------------------------------------
112 handle_cast(_Msg, State) ->
113 {noreply, State}.
114
115 %%--------------------------------------------------------------------
116 %% Function: handle_info(Info, State) -> {noreply, State} |
117 %% {noreply, State, Timeout} |
118 %% {stop, Reason, State}
119 %% Description: Handling all non call/cast messages
120 %%--------------------------------------------------------------------
121 handle_info({'DOWN', _Ref, _Process, {mochiweb_http, Host}, Reason}, State) ->
122 io:format("Unable to start mochiweb on ~s:~nReason: ~s~n",[Host,Reason]),
123 {stop,normal,State};
124 handle_info(_Info, State) ->
125 {noreply, State}.
126
127 %%--------------------------------------------------------------------
128 %% Function: terminate(Reason, State) -> void()
129 %% Description: This function is called by a gen_server when it is about to
130 %% terminate. It should be the opposite of Module:init/1 and do any necessary
131 %% cleaning up. When it returns, the gen_server terminates with Reason.
132 %% The return value is ignored.
133 %%--------------------------------------------------------------------
134 terminate(_Reason, _State) ->
135 io:format("Shutting down ChatterlWeb on: ~s...~n",[node(self())]),
136 mochiweb_http:stop(),
137 ok.
138
139 %%--------------------------------------------------------------------
140 %% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
141 %% Description: Convert process state when code is changed
142 %%--------------------------------------------------------------------
143 code_change(_OldVsn, State, _Extra) ->
144 {ok, State}.
145
146 %%--------------------------------------------------------------------
147 %%% Internal functions
148 %%--------------------------------------------------------------------
149
150 %%--------------------------------------------------------------------
151 %% @private
152 %% @doc
153 %%
154 %% Gets the content type, used to help CWIGA to determine what format
155 %% to respond in.
156 %% @spec get_content_type(Type) -> string()
157 %%
158 %% @end
159 %%--------------------------------------------------------------------
160 get_content_type(Type) ->
161 case Type of
162 ["json"] ->
163 ["text/json"];
164 ["xml"] ->
165 ["text/xml"];
166 _ -> ["text/json"]
167 end.
Something went wrong with that request. Please try again.