Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 162 lines (144 sloc) 6.589 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);
494591e @baphled Adding list users units and implementation code for the reimplementat…
authored
58 handle('GET',"/users/list",ContentType,_Post) ->
1c0f3d1 @baphled Implemented connect & disconnect functionality for CWIGA
authored
59 success(chatterl_mid_man:user_list(ContentType),ContentType);
494591e @baphled Adding list users units and implementation code for the reimplementat…
authored
60 handle(_,Path,ContentType,_) ->
a4c1476 @baphled Added units for reimplementing CWIGA along with the implementation code.
authored
61 Response = message_handler:get_response_body(ContentType,
62 message_handler:build_carrier("error", "Unknown command: " ++Path)),
63 {404, [{"Content-Type", ContentType}], list_to_binary(Response)}.
1c0f3d1 @baphled Implemented connect & disconnect functionality for CWIGA
authored
64
65 success(Response,ContentType) ->
66 {200, [{"Content-Type", ContentType}], list_to_binary(Response)}.
a4c1476 @baphled Added units for reimplementing CWIGA along with the implementation code.
authored
67 %%====================================================================
68 %% gen_server callbacks
69 %%====================================================================
70
71 %%--------------------------------------------------------------------
72 %% Function: init(Args) -> {ok, State} |
73 %% {ok, State, Timeout} |
74 %% ignore |
75 %% {stop, Reason}
76 %% Description: Initiates the server
77 %%--------------------------------------------------------------------
78 init([Port]) ->
79 process_flag(trap_exit, true),
80 mochiweb_http:start([{port, Port}, {loop, fun dispatch_requests/1}]),
81 erlang:monitor(process,mochiweb_http),
82 {ok, #state{}}.
83
84 %%--------------------------------------------------------------------
85 %% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
86 %% {reply, Reply, State, Timeout} |
87 %% {noreply, State} |
88 %% {noreply, State, Timeout} |
89 %% {stop, Reason, Reply, State} |
90 %% {stop, Reason, State}
91 %% Description: Handling call messages
92 %%--------------------------------------------------------------------
93 handle_call(stop, _From, State) ->
94 io:format("Processing shut down ~s~n", [?APP]),
95 {stop, normal, stopped, State};
96 handle_call(_Request, _From, State) ->
97 Reply = ok,
98 {reply, Reply, State}.
99
100 %%--------------------------------------------------------------------
101 %% Function: handle_cast(Msg, State) -> {noreply, State} |
102 %% {noreply, State, Timeout} |
103 %% {stop, Reason, State}
104 %% Description: Handling cast messages
105 %%--------------------------------------------------------------------
106 handle_cast(_Msg, State) ->
107 {noreply, State}.
108
109 %%--------------------------------------------------------------------
110 %% Function: handle_info(Info, State) -> {noreply, State} |
111 %% {noreply, State, Timeout} |
112 %% {stop, Reason, State}
113 %% Description: Handling all non call/cast messages
114 %%--------------------------------------------------------------------
115 handle_info({'DOWN', _Ref, _Process, {mochiweb_http, Host}, Reason}, State) ->
116 io:format("Unable to start mochiweb on ~s:~nReason: ~s~n",[Host,Reason]),
117 {stop,normal,State};
118 handle_info(_Info, State) ->
119 {noreply, State}.
120
121 %%--------------------------------------------------------------------
122 %% Function: terminate(Reason, State) -> void()
123 %% Description: This function is called by a gen_server when it is about to
124 %% terminate. It should be the opposite of Module:init/1 and do any necessary
125 %% cleaning up. When it returns, the gen_server terminates with Reason.
126 %% The return value is ignored.
127 %%--------------------------------------------------------------------
128 terminate(_Reason, _State) ->
129 io:format("Shutting down ChatterlWeb on: ~s...~n",[node(self())]),
130 mochiweb_http:stop(),
131 ok.
132
133 %%--------------------------------------------------------------------
134 %% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
135 %% Description: Convert process state when code is changed
136 %%--------------------------------------------------------------------
137 code_change(_OldVsn, State, _Extra) ->
138 {ok, State}.
139
140 %%--------------------------------------------------------------------
141 %%% Internal functions
142 %%--------------------------------------------------------------------
143
144 %%--------------------------------------------------------------------
145 %% @private
146 %% @doc
147 %%
148 %% Gets the content type, used to help CWIGA to determine what format
149 %% to respond in.
150 %% @spec get_content_type(Type) -> string()
151 %%
152 %% @end
153 %%--------------------------------------------------------------------
154 get_content_type(Type) ->
155 case Type of
156 ["json"] ->
157 ["text/json"];
158 ["xml"] ->
159 ["text/xml"];
160 _ -> ["text/json"]
161 end.
Something went wrong with that request. Please try again.