Permalink
Browse files

added mochiweb server to capture amf requests

  • Loading branch information...
1 parent 14203ac commit c6b257cb5100f7ff9f771859be158b2a8009e8a7 @trung committed Mar 30, 2010
View
No changes.
No changes.
View
@@ -0,0 +1,29 @@
+{application, mochiweb,
+ [{description, "MochiMedia Web Server"},
+ {vsn, "0.01"},
+ {modules, [
+ mochihex,
+ mochijson,
+ mochijson2,
+ mochinum,
+ mochiweb,
+ mochiweb_app,
+ mochiweb_charref,
+ mochiweb_cookies,
+ mochiweb_echo,
+ mochiweb_headers,
+ mochiweb_html,
+ mochiweb_http,
+ mochiweb_multipart,
+ mochiweb_request,
+ mochiweb_response,
+ mochiweb_skel,
+ mochiweb_socket_server,
+ mochiweb_sup,
+ mochiweb_util,
+ reloader
+ ]},
+ {registered, []},
+ {mod, {mochiweb_app, []}},
+ {env, []},
+ {applications, [kernel, stdlib]}]}.
@@ -0,0 +1,5 @@
+<!DOCTYPE cross-domain-policy
+ SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
+<cross-domain-policy>
+ <allow-access-from domain="*.*" secure="false" />
+</cross-domain-policy>
View
@@ -0,0 +1,8 @@
+<html>
+<head>
+<title>It Worked</title>
+</head>
+<body>
+Erling Server (based on MochiWeb) is running.
+</body>
+</html>
View
@@ -0,0 +1,14 @@
+{application, erling_server,
+ [{description, "erling_server"},
+ {vsn, "0.01"},
+ {modules, [
+ erling_server,
+ erling_server_app,
+ erling_server_sup,
+ erling_server_web,
+ erling_server_deps
+ ]},
+ {registered, []},
+ {mod, {erling_server_app, []}},
+ {env, []},
+ {applications, [kernel, stdlib, crypto]}]}.
View
@@ -0,0 +1,30 @@
+%% @author author <author@example.com>
+%% @copyright YYYY author.
+
+%% @doc TEMPLATE.
+
+-module(erling_server).
+-author('author <author@example.com>').
+-export([start/0, stop/0]).
+
+ensure_started(App) ->
+ case application:start(App) of
+ ok ->
+ ok;
+ {error, {already_started, App}} ->
+ ok
+ end.
+
+%% @spec start() -> ok
+%% @doc Start the erling_server server.
+start() ->
+ erling_server_deps:ensure(),
+ ensure_started(crypto),
+ application:start(erling_server).
+
+%% @spec stop() -> ok
+%% @doc Stop the erling_server server.
+stop() ->
+ Res = application:stop(erling_server),
+ application:stop(crypto),
+ Res.
@@ -0,0 +1 @@
+
@@ -0,0 +1,22 @@
+%% @author author <author@example.com>
+%% @copyright YYYY author.
+
+%% @doc Callbacks for the erling_server application.
+
+-module(erling_server_app).
+-author('author <author@example.com>').
+
+-behaviour(application).
+-export([start/2,stop/1]).
+
+
+%% @spec start(_Type, _StartArgs) -> ServerRet
+%% @doc application start callback for erling_server.
+start(_Type, _StartArgs) ->
+ erling_server_deps:ensure(),
+ erling_server_sup:start_link().
+
+%% @spec stop(_State) -> ServerRet
+%% @doc application stop callback for erling_server.
+stop(_State) ->
+ ok.
@@ -0,0 +1,84 @@
+%% @author author <author@example.com>
+%% @copyright YYYY author.
+
+%% @doc Ensure that the relatively-installed dependencies are on the code
+%% loading path, and locate resources relative
+%% to this application's path.
+
+-module(erling_server_deps).
+-author('author <author@example.com>').
+
+-export([ensure/0, ensure/1]).
+-export([get_base_dir/0, get_base_dir/1]).
+-export([local_path/1, local_path/2]).
+-export([deps_on_path/0, new_siblings/1]).
+
+%% @spec deps_on_path() -> [ProjNameAndVers]
+%% @doc List of project dependencies on the path.
+deps_on_path() ->
+ F = fun (X, Acc) ->
+ ProjDir = filename:dirname(X),
+ case {filename:basename(X),
+ filename:basename(filename:dirname(ProjDir))} of
+ {"ebin", "deps"} ->
+ [filename:basename(ProjDir) | Acc];
+ _ ->
+ Acc
+ end
+ end,
+ ordsets:from_list(lists:foldl(F, [], code:get_path())).
+
+%% @spec new_siblings(Module) -> [Dir]
+%% @doc Find new siblings paths relative to Module that aren't already on the
+%% code path.
+new_siblings(Module) ->
+ Existing = deps_on_path(),
+ SiblingEbin = filelib:wildcard(local_path(["deps", "*", "ebin"], Module)),
+ Siblings = [filename:dirname(X) || X <- SiblingEbin,
+ ordsets:is_element(
+ filename:basename(filename:dirname(X)),
+ Existing) =:= false],
+ lists:filter(fun filelib:is_dir/1,
+ lists:append([[filename:join([X, "ebin"]),
+ filename:join([X, "include"])] ||
+ X <- Siblings])).
+
+
+%% @spec ensure(Module) -> ok
+%% @doc Ensure that all ebin and include paths for dependencies
+%% of the application for Module are on the code path.
+ensure(Module) ->
+ code:add_paths(new_siblings(Module)),
+ code:clash(),
+ ok.
+
+%% @spec ensure() -> ok
+%% @doc Ensure that the ebin and include paths for dependencies of
+%% this application are on the code path. Equivalent to
+%% ensure(?Module).
+ensure() ->
+ ensure(?MODULE).
+
+%% @spec get_base_dir(Module) -> string()
+%% @doc Return the application directory for Module. It assumes Module is in
+%% a standard OTP layout application in the ebin or src directory.
+get_base_dir(Module) ->
+ {file, Here} = code:is_loaded(Module),
+ filename:dirname(filename:dirname(Here)).
+
+%% @spec get_base_dir() -> string()
+%% @doc Return the application directory for this application. Equivalent to
+%% get_base_dir(?MODULE).
+get_base_dir() ->
+ get_base_dir(?MODULE).
+
+%% @spec local_path([string()], Module) -> string()
+%% @doc Return an application-relative directory from Module's application.
+local_path(Components, Module) ->
+ filename:join([get_base_dir(Module) | Components]).
+
+%% @spec local_path(Components) -> string()
+%% @doc Return an application-relative directory for this application.
+%% Equivalent to local_path(Components, ?MODULE).
+local_path(Components) ->
+ local_path(Components, ?MODULE).
@@ -0,0 +1,54 @@
+%% @author author <author@example.com>
+%% @copyright YYYY author.
+
+%% @doc Supervisor for the erling_server application.
+
+-module(erling_server_sup).
+-author('author <author@example.com>').
+
+-behaviour(supervisor).
+
+%% External exports
+-export([start_link/0, upgrade/0]).
+
+%% supervisor callbacks
+-export([init/1]).
+
+%% @spec start_link() -> ServerRet
+%% @doc API for starting the supervisor.
+start_link() ->
+ supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+%% @spec upgrade() -> ok
+%% @doc Add processes if necessary.
+upgrade() ->
+ {ok, {_, Specs}} = init([]),
+
+ Old = sets:from_list(
+ [Name || {Name, _, _, _} <- supervisor:which_children(?MODULE)]),
+ New = sets:from_list([Name || {Name, _, _, _, _, _} <- Specs]),
+ Kill = sets:subtract(Old, New),
+
+ sets:fold(fun (Id, ok) ->
+ supervisor:terminate_child(?MODULE, Id),
+ supervisor:delete_child(?MODULE, Id),
+ ok
+ end, ok, Kill),
+
+ [supervisor:start_child(?MODULE, Spec) || Spec <- Specs],
+ ok.
+
+%% @spec init([]) -> SupervisorTree
+%% @doc supervisor callback.
+init([]) ->
+ Ip = case os:getenv("MOCHIWEB_IP") of false -> "0.0.0.0"; Any -> Any end,
+ WebConfig = [
+ {ip, Ip},
+ {port, 8000},
+ {docroot, erling_server_deps:local_path(["priv", "www"])}],
+ Web = {erling_server_web,
+ {erling_server_web, start, [WebConfig]},
+ permanent, 5000, worker, dynamic},
+
+ Processes = [Web],
+ {ok, {{one_for_one, 10, 10}, Processes}}.
@@ -0,0 +1,45 @@
+%% @author author <author@example.com>
+%% @copyright YYYY author.
+
+%% @doc Web server for erling_server.
+
+-module(erling_server_web).
+-author('author <author@example.com>').
+
+-export([start/1, stop/0, loop/2]).
+
+%% External API
+
+start(Options) ->
+ {DocRoot, Options1} = get_option(docroot, Options),
+ Loop = fun (Req) ->
+ ?MODULE:loop(Req, DocRoot)
+ end,
+ mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]).
+
+stop() ->
+ mochiweb_http:stop(?MODULE).
+
+loop(Req, DocRoot) ->
+ "/" ++ Path = Req:get(path),
+ io:format("Path = [~p]", [Path]),
+ case Req:get(method) of
+ Method when Method =:= 'GET'; Method =:= 'HEAD' ->
+ case Path of
+ _ ->
+ Req:serve_file(Path, DocRoot)
+ end;
+ 'POST' ->
+ io:format("Body = [~p]", [Req:recv_body()]),
+ case Path of
+ _ ->
+ Req:not_found()
+ end;
+ _ ->
+ Req:respond({501, [], []})
+ end.
+
+%% Internal API
+
+get_option(Option, Options) ->
+ {proplists:get_value(Option, Options), proplists:delete(Option, Options)}.
Binary file not shown.
@@ -24,7 +24,7 @@
<channels>
<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
- <endpoint uri="http://localhost:8001/erling/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
+ <endpoint uri="http://localhost:8000/erling/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
</channel-definition>
</channels>
@@ -1,6 +1,6 @@
package vo
{
- [RemoteClass(alias="org.mdkt.entity.Addres")]
+ [RemoteClass(alias="org.mdkt.entity.Address")]
[Bindable]
public class Address extends BaseVO
{
View
@@ -0,0 +1,39 @@
+-module(uint29).
+-compile(export_all).
+
+encode_int29(I) when I >= -16#10000000, I < 0 ->
+ encode_uint29(16#20000000 + I);
+encode_int29(I) when I =< 16#0FFFFFFF ->
+ encode_uint29(I);
+encode_int29(_) ->
+ throw(badrange).
+
+encode_uint29(I) when I >= 16#00000000, I =< 16#0000007F ->
+ <<I>>;
+encode_uint29(I) when I >= 16#00000080, I =< 16#00003FFF ->
+ X1 = 16#80 bor (I bsr 7),
+ X2 = I band 16#7F,
+ <<X1, X2>>;
+encode_uint29(I) when I >= 16#00004000, I =< 16#001FFFFF ->
+ X1 = 16#80 bor (I bsr 14),
+ X2 = 16#80 bor (I bsr 7),
+ X3 = I band 16#7F,
+ <<X1, X2, X3>>;
+encode_uint29(I) when I >= 16#00200000, I =< 16#1FFFFFFF ->
+ X1 = 16#80 bor (I bsr 22),
+ X2 = 16#80 bor (I bsr 15),
+ X3 = 16#80 bor (I bsr 8),
+ X4 = I band 16#FF,
+ <<X1, X2, X3, X4>>;
+encode_uint29(_) ->
+ throw(badrange).
+
+decode_uint29(Data) ->
+ decode_uint29(Data, 0, 0).
+
+decode_uint29(<<1:1, Num:7, Data/binary>>, Result, N) when N < 3 ->
+ decode_uint29(Data, (Result bsl 7) bor Num, N + 1);
+decode_uint29(<<0:1, Num:7, Data/binary>>, Result, N) when N < 3 ->
+ {(Result bsl 7) bor Num, Data};
+decode_uint29(<<Byte, Data/binary>>, Result, _N) ->
+ {(Result bsl 8) bor Byte, Data}.

0 comments on commit c6b257c

Please sign in to comment.