PageRenderTime 116ms CodeModel.GetById 108ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mochiweb_echo.erl

http://github.com/basho/mochiweb
Erlang | 59 lines | 28 code | 7 blank | 24 comment | 0 complexity | 2abd6a8fafa897243529b0580777b8d8 MD5 | raw file
 1%% @author Bob Ippolito <bob@mochimedia.com>
 2%% @copyright 2007 Mochi Media, Inc.
 3%%
 4%% Permission is hereby granted, free of charge, to any person obtaining a
 5%% copy of this software and associated documentation files (the "Software"),
 6%% to deal in the Software without restriction, including without limitation
 7%% the rights to use, copy, modify, merge, publish, distribute, sublicense,
 8%% and/or sell copies of the Software, and to permit persons to whom the
 9%% Software is furnished to do so, subject to the following conditions:
10%%
11%% The above copyright notice and this permission notice shall be included in
12%% all copies or substantial portions of the Software.
13%%
14%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17%% THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20%% DEALINGS IN THE SOFTWARE.
21
22%% @doc Simple and stupid echo server to demo mochiweb_socket_server.
23
24-module(mochiweb_echo).
25-author('bob@mochimedia.com').
26-export([start/0, stop/0, loop/1]).
27
28stop() ->
29    mochiweb_socket_server:stop(?MODULE).
30
31start() ->
32    mochiweb_socket_server:start([{link, false} | options()]).
33
34options() ->
35    [{name, ?MODULE},
36     {port, 6789},
37     {ip, "127.0.0.1"},
38     {max, 1},
39     {loop, {?MODULE, loop}}].
40
41loop(Socket) ->
42    case mochiweb_socket:recv(Socket, 0, 30000) of
43        {ok, Data} ->
44            case mochiweb_socket:send(Socket, Data) of
45                ok ->
46                    loop(Socket);
47                _ ->
48                    exit(normal)
49            end;
50        _Other ->
51            exit(normal)
52    end.
53
54%%
55%% Tests
56%%
57-ifdef(TEST).
58-include_lib("eunit/include/eunit.hrl").
59-endif.