Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

amf3 read complete

  • Loading branch information...
commit 6ab3d158d152b97d5a9d738e84c997ea742bf917 1 parent 0cc401d
Nguyen Kien Trung authored
BIN  temp/client/bin-debug/main.swf
View
Binary file not shown
51 temp/server/amf0.erl
View
@@ -70,23 +70,40 @@ read_strict_array(Bin) ->
{ok, Size, BinAfterSize} = read_u32(Bin),
read_strict_array(BinAfterSize, 0, Size, []).
+read_date(Bin) ->
+ %% Just read, not use
+ {ok, _, BinAfterTimeZone} = read_u16(Bin),
+ {ok, TimeInMilli, NextBin} = read_number(BinAfterTimeZone),
+ %% convert to erlang date
+ Date = utils:milliseconds_to_date(TimeInMilli),
+ {ok, Date, NextBin}.
+
+read_long_string(Bin) ->
+ {bad, {"Not yet implemented", ?MODULE, ?LINE, Bin}}.
+
+read_xml(Bin) ->
+ {bad, {"Not yet implemented", ?MODULE, ?LINE, Bin}}.
+
+read_typed_object(Bin) ->
+ {bad, {"Not yet implemented", ?MODULE, ?LINE, Bin}}.
+
%% return {ok, value/Value, Rest} or {bad, Reason}
-read_object(<<?number_marker:8, Rest/binary>>) -> read_number(Rest);
-read_object(<<?boolean_marker:8, Rest/binary>>) -> read_boolean(Rest);
-read_object(<<?string_marker:8, Rest/binary>>) -> read_string(Rest);
-read_object(<<?object_marker:8, Rest/binary>>) -> {bad, {"Not supported",?MODULE,?LINE, Rest}};
-read_object(<<?movieclip_marker:8, Rest/binary>>) -> {bad, {"Reserved, not supported", Rest}};
-read_object(<<?null_marker:8, Rest/binary>>) -> {ok, null, Rest};
-read_object(<<?undefined_marker:8, Rest/binary>>) -> {bad, {"Not supported",?MODULE, ?LINE, Rest}};
-read_object(<<?reference_marker:8, Rest/binary>>) -> {bad, {"Not supported",?MODULE, ?LINE, Rest}};
-read_object(<<?ecma_array_marker:8, Rest/binary>>) -> {bad, {"Not supported",?MODULE, ?LINE, Rest}};
-read_object(<<?object_end_marker:8, Rest/binary>>) -> {bad, {"Not supported",?MODULE, ?LINE, Rest}};
-read_object(<<?strict_array_marker:8, Rest/binary>>) -> read_strict_array(Rest);
-read_object(<<?date_marker:8, Rest/binary>>) -> {bad, {"Not supported",?MODULE, ?LINE, Rest}};
-read_object(<<?long_string_marker:8, Rest/binary>>) -> {bad, {"Not supported",?MODULE, ?LINE, Rest}};
-read_object(<<?unsupported_marker:8, Rest/binary>>) -> {bad, {"Not supported",?MODULE, ?LINE, Rest}};
-read_object(<<?recordset_marker:8, Rest/binary>>) -> {bad, {"Reserved, not supported",?MODULE, ?LINE, Rest}};
-read_object(<<?xml_document_marker:8, Rest/binary>>) -> {bad, {"Not supported",?MODULE, ?LINE, Rest}};
-read_object(<<?typed_object_marker:8, Rest/binary>>) -> {bad, {"Not supported",?MODULE, ?LINE, Rest}};
+read_object(<<?number_marker:8, Rest/binary>>) -> read_number(Rest);
+read_object(<<?boolean_marker:8, Rest/binary>>) -> read_boolean(Rest);
+read_object(<<?string_marker:8, Rest/binary>>) -> read_string(Rest);
+read_object(<<?object_marker:8, Rest/binary>>) -> {bad, {"Not yet implemented", ?MODULE,?LINE, Rest}};
+read_object(<<?movieclip_marker:8, Rest/binary>>) -> {bad, {"Reserved, not supported", Rest}};
+read_object(<<?null_marker:8, Rest/binary>>) -> {ok, null, Rest};
+read_object(<<?undefined_marker:8, Rest/binary>>) -> {bad, {"Undefined marker", ?MODULE, ?LINE, Rest}};
+read_object(<<?reference_marker:8, Rest/binary>>) -> {bad, {"Not yet implemented", ?MODULE, ?LINE, Rest}};
+read_object(<<?ecma_array_marker:8, Rest/binary>>) -> {bad, {"Not yet implemented", ?MODULE, ?LINE, Rest}};
+read_object(<<?object_end_marker:8, Rest/binary>>) -> {bad, {"Unexpected object end", ?MODULE, ?LINE, Rest}};
+read_object(<<?strict_array_marker:8, Rest/binary>>) -> read_strict_array(Rest);
+read_object(<<?date_marker:8, Rest/binary>>) -> read_date(Rest);
+read_object(<<?long_string_marker:8, Rest/binary>>) -> read_long_string(Rest);
+read_object(<<?unsupported_marker:8, Rest/binary>>) -> {bad, {"Not yet implemented", ?MODULE, ?LINE, Rest}};
+read_object(<<?recordset_marker:8, Rest/binary>>) -> {bad, {"Reserved, not supported", ?MODULE, ?LINE, Rest}};
+read_object(<<?xml_document_marker:8, Rest/binary>>) -> read_xml(Rest);
+read_object(<<?typed_object_marker:8, Rest/binary>>) -> read_typed_object(Rest);
%% switch to AMF3
read_object(<<?avm_plus_object_marker:8, Rest/binary>>) -> amf3:read_object(Rest).
43 temp/server/amf3.erl
View
@@ -1,5 +1,7 @@
-module(amf3).
--export([read_object/1, reset/0, read_uint_29/1]).
+-author("trung@mdkt.org").
+% -export([read_object/1, reset/0, read_uint_29/1]).
+-compile(export_all).
-include("action_message.hrl").
-include("messages.hrl").
@@ -239,18 +241,39 @@ read_object_with_trait(Bin, TraitObj) when is_record(TraitObj, trait) ->
read_object_property(Bin, TraitObj#trait.properties, NewObject)
end.
+read_double(<<Value/float, Rest/binary>>) ->
+ {ok, Value, Rest}.
+
+read_date(Bin) ->
+ {ok, Ref, BinAfterRef} = read_uint_29(Bin),
+ case Ref band 1 of
+ 0 ->
+ {ok, DateRef} = read_object_reference(Ref bsr 1),
+ {ok, DateRef, BinAfterRef};
+ _ ->
+ {ok, TimeInMilli, NextBin} = read_double(BinAfterRef),
+ %% convert to erlang date
+ Date = utils:milliseconds_to_date(TimeInMilli),
+ _ = write_object_reference(Date),
+ {ok, Date, NextBin}
+ end.
+
+%% TODO use xmerl?
+read_xml(Bin) ->
+ {bad, "Not yet implemented", ?MODULE, ?LINE}.
+
%% Return {ok, value|Value, Rest} or {bad, Reason}
read_object(<<?undefined_marker:8, Rest/binary>>) -> {bad, {"Undefined marker ", Rest}};
-read_object(<<?null_marker:8, Rest/binary>>) -> {ok, null, Rest};
-read_object(<<?false_marker:8, Rest/binary>>) -> {ok, false, Rest};
-read_object(<<?true_marker:8, Rest/binary>>) -> {ok, true, Rest};
-read_object(<<?integer_marker:8, Rest/binary>>) -> read_uint_29(Rest);
-read_object(<<?double_marker:8, Rest/binary>>) -> {bad, {"Not yet implemented marker", Rest}};
+read_object(<<?null_marker:8, Rest/binary>>) -> {ok, null, Rest};
+read_object(<<?false_marker:8, Rest/binary>>) -> {ok, false, Rest};
+read_object(<<?true_marker:8, Rest/binary>>) -> {ok, true, Rest};
+read_object(<<?integer_marker:8, Rest/binary>>) -> read_uint_29(Rest);
+read_object(<<?double_marker:8, Rest/binary>>) -> read_double(Rest);
read_object(<<?string_marker:8, Rest/binary>>) -> read_string(Rest);
-read_object(<<?xml_doc_marker:8, Rest/binary>>) -> {bad, {"Not yet implemented marker", Rest}};
-read_object(<<?date_marker:8, Rest/binary>>) -> {bad, {"Not yet implemented marker", Rest}};
+read_object(<<?xml_doc_marker:8, Rest/binary>>) -> read_xml(Rest);
+read_object(<<?date_marker:8, Rest/binary>>) -> read_date(Rest);
read_object(<<?array_marker:8, Rest/binary>>) -> read_array(Rest);
-read_object(<<?object_marker:8, Rest/binary>>) ->
+read_object(<<?object_marker:8, Rest/binary>>) ->
%% io:fwrite("Reading Ref from ~p got value ", [Rest]),
{ok, Ref, BinAfterRef} = read_uint_29(Rest),
case Ref band 1 of
@@ -276,7 +299,7 @@ read_object(<<?object_marker:8, Rest/binary>>) ->
{ok, Obj, BinAfterObj}
end
end;
-read_object(<<?xml_marker:8, Rest/binary>>) -> {bad, {"Not yet implemented marker", Rest}};
+read_object(<<?xml_marker:8, Rest/binary>>) -> read_xml(Rest);
read_object(<<>>) ->
{ok, null, <<>>};
126 temp/server/deserializer.erl
View
@@ -1,63 +1,63 @@
--module(deserializer).
--author("trung@mdkt.org").
-
--export([read/1]).
-
--include("action_message.hrl").
-
-read_header(Bin) ->
- io:fwrite("Bin = ~n~p~n", [Bin]),
- {ok, HeaderName, BinAfterHeaderName} = amf0:read_string(Bin),
- {ok, MustUnderstand, BinAfterMustUnderstand} = amf0:read_u8(BinAfterHeaderName),
- {ok, _DataLen, DataBin} = amf0:read_u32(BinAfterMustUnderstand),
- %% Don't care about DataLen, just read
- %% {DataBin, NextBin} = split_binary(Rest2, DataLen),
- %% clear ETS tables
- amf0:reset(),
- {ok, Data, NextBin} = amf0:read_object(DataBin),
- Header = #header{headerName = HeaderName, mustUnderstand = case MustUnderstand of 0 -> false; _Other -> true end, data = Data},
- {ok, Header, NextBin}.
-
-read_headers(Bin, Count, Total, Acc) when Count == Total ->
- {ok, Acc, Bin};
-read_headers(Bin, Count, Total, Acc) ->
- {ok, Header, NextBin} = read_header(Bin),
- io:fwrite("Parsed header ~p : ~p~n", [Count + 1, Header]),
- read_headers(NextBin, Count + 1, Total, Acc ++ [Header]).
-
-read_body(Bin) ->
- {ok, TargetUri, BinAfterTargetUri} = amf0:read_string(Bin),
- {ok, ResponseUri, BinAfterResponseUri} = amf0:read_string(BinAfterTargetUri),
- {ok, _DataLen, DataBin} = amf0:read_u32(BinAfterResponseUri),
- %% Don't care about DataLen, just read
- %% {DataBin, NextBin} = split_binary(Rest2, DataLen),
- %% clear ETS tables
- amf0:reset(),
- {ok, Data, NextBin} = amf0:read_object(DataBin),
- Body = #body{targetUri = TargetUri, responseUri = ResponseUri, data=Data},
- {ok, Body, NextBin}.
-
-read_bodies(Bin, Count, Total, Acc) when Count == Total ->
- {ok, Acc, Bin};
-read_bodies(Bin, Count, Total, Acc) ->
- {ok, Body, NextBin} = read_body(Bin),
- read_bodies(NextBin, Count + 1, Total, Acc ++ [Body]).
-
-%% Read binary and translate to action_message record
-%% Msg = #action_message
-%% return {ok, Msg, Rest} or {bad, Reason}
-%% ideally, Rest must be <<>>
-read(<<?VERSION_1:16, Rest/binary>>) -> read_now(?VERSION_1, Rest);
-read(<<?VERSION_3:16, Rest/binary>>) -> read_now(?VERSION_3, Rest);
-read(_) ->
- {bad, "Not a valid binary or version not supported"}.
-
-read_now(Version, Bin) ->
- {ok, HeaderCount, BinAfterHeaderCount} = amf0:read_u16(Bin),
- io:fwrite("There are ~p header(s)~n", [HeaderCount]),
- {ok, Headers, BinAfterHeaders} = read_headers(BinAfterHeaderCount, 0, HeaderCount, []),
- {ok, BodyCount, BinAfterBodyCount} = amf0:read_u16(BinAfterHeaders),
- io:fwrite("There are ~p body(s)~n", [BodyCount]),
- {ok, Bodies, BinAfterBodies} = read_bodies(BinAfterBodyCount, 0, BodyCount, []),
- Msg = #action_message{version = Version, headers = Headers, bodies = Bodies},
- {ok, Msg, BinAfterBodies}.
+-module(deserializer).
+-author("trung@mdkt.org").
+
+-export([read/1]).
+
+-include("action_message.hrl").
+
+read_header(Bin) ->
+ io:fwrite("Bin = ~n~p~n", [Bin]),
+ {ok, HeaderName, BinAfterHeaderName} = amf0:read_string(Bin),
+ {ok, MustUnderstand, BinAfterMustUnderstand} = amf0:read_u8(BinAfterHeaderName),
+ {ok, _DataLen, DataBin} = amf0:read_u32(BinAfterMustUnderstand),
+ %% Don't care about DataLen, just read
+ %% {DataBin, NextBin} = split_binary(Rest2, DataLen),
+ %% clear ETS tables
+ amf0:reset(),
+ {ok, Data, NextBin} = amf0:read_object(DataBin),
+ Header = #header{headerName = HeaderName, mustUnderstand = case MustUnderstand of 0 -> false; _Other -> true end, data = Data},
+ {ok, Header, NextBin}.
+
+read_headers(Bin, Count, Total, Acc) when Count == Total ->
+ {ok, Acc, Bin};
+read_headers(Bin, Count, Total, Acc) ->
+ {ok, Header, NextBin} = read_header(Bin),
+ io:fwrite("Parsed header ~p : ~p~n", [Count + 1, Header]),
+ read_headers(NextBin, Count + 1, Total, Acc ++ [Header]).
+
+read_body(Bin) ->
+ {ok, TargetUri, BinAfterTargetUri} = amf0:read_string(Bin),
+ {ok, ResponseUri, BinAfterResponseUri} = amf0:read_string(BinAfterTargetUri),
+ {ok, _DataLen, DataBin} = amf0:read_u32(BinAfterResponseUri),
+ %% Don't care about DataLen, just read
+ %% {DataBin, NextBin} = split_binary(Rest2, DataLen),
+ %% clear ETS tables
+ amf0:reset(),
+ {ok, Data, NextBin} = amf0:read_object(DataBin),
+ Body = #body{targetUri = TargetUri, responseUri = ResponseUri, data=Data},
+ {ok, Body, NextBin}.
+
+read_bodies(Bin, Count, Total, Acc) when Count == Total ->
+ {ok, Acc, Bin};
+read_bodies(Bin, Count, Total, Acc) ->
+ {ok, Body, NextBin} = read_body(Bin),
+ read_bodies(NextBin, Count + 1, Total, Acc ++ [Body]).
+
+%% Read binary and translate to action_message record
+%% Msg = #action_message
+%% return {ok, Msg, Rest} or {bad, Reason}
+%% ideally, Rest must be <<>>
+read(<<?VERSION_1:16, Rest/binary>>) -> read_now(?VERSION_1, Rest);
+read(<<?VERSION_3:16, Rest/binary>>) -> read_now(?VERSION_3, Rest);
+read(_) ->
+ {bad, "Not a valid binary or version not supported"}.
+
+read_now(Version, Bin) ->
+ {ok, HeaderCount, BinAfterHeaderCount} = amf0:read_u16(Bin),
+ io:fwrite("There are ~p header(s)~n", [HeaderCount]),
+ {ok, Headers, BinAfterHeaders} = read_headers(BinAfterHeaderCount, 0, HeaderCount, []),
+ {ok, BodyCount, BinAfterBodyCount} = amf0:read_u16(BinAfterHeaders),
+ io:fwrite("There are ~p body(s)~n", [BodyCount]),
+ {ok, Bodies, BinAfterBodies} = read_bodies(BinAfterBodyCount, 0, BodyCount, []),
+ Msg = #action_message{version = Version, headers = Headers, bodies = Bodies},
+ {ok, Msg, BinAfterBodies}.
8 temp/server/flex_classes.hrl
View
@@ -1,4 +1,4 @@
--define(FC_ARRAYCOLLECTION, "flex.messaging.io.ArrayCollection").
--define(FC_OBJECTPROXY, "flex.messaging.io.ObjectProxy").
--define(FC_REMOTINGMESSAGE, "flex.messaging.messages.RemotingMessage").
--define(FC_COMMANDMESSAGE, "flex.messaging.messages.CommandMessage").
+-define(FC_ARRAYCOLLECTION, "flex.messaging.io.ArrayCollection").
+-define(FC_OBJECTPROXY, "flex.messaging.io.ObjectProxy").
+-define(FC_REMOTINGMESSAGE, "flex.messaging.messages.RemotingMessage").
+-define(FC_COMMANDMESSAGE, "flex.messaging.messages.CommandMessage").
16 temp/server/messages.hrl
View
@@ -2,14 +2,14 @@
-define(SUBSCRIBE_OPERATION, 0).
-define(UNSUBSCRIBE_OPERATION, 1).
-define(POLL_OPERATION, 2).
--define(CLIENT_SYNC_OPERATION, 3).
--define(CLIENT_PING_OPERATION, 4).
--define(CLUSTER_REQUEST_OPERATION, 5).
--define(LOGIN_OPERATION, 6).
--define(LOGOUT_OPERATION, 7).
--define(SUBSCRIPTION_INVALIDATE_OPERATION, 8).
--define(MULTI_SUBSCRIBE_OPERATION, 9).
--define(DISCONNECT_OPERATION, 20).
+-define(CLIENT_SYNC_OPERATION, 4).
+-define(CLIENT_PING_OPERATION, 5).
+-define(CLUSTER_REQUEST_OPERATION, 7).
+-define(LOGIN_OPERATION, 8).
+-define(LOGOUT_OPERATION, 9).
+-define(SUBSCRIPTION_INVALIDATE_OPERATION, 10).
+-define(MULTI_SUBSCRIBE_OPERATION, 11).
+-define(DISCONNECT_OPERATION, 12).
-define(UNKNOWN_OPERATION, 1000).
-record(abstract_message, {
186 temp/server/record_utils.erl
View
@@ -1,93 +1,93 @@
--module(record_utils).
--author("trung@mdkt.org").
--compile(export_all).
-
--include("messages.hrl").
--include("flex_classes.hrl").
-
-%% Setters for records defined in messages.hrl file
-%% return {ok, NewObject, {propertyName, NewValue}}
-set(Obj, clientId, Value) when is_record(Obj, abstract_message) ->
- NewObj = Obj#abstract_message{clientId = Value},
- {ok, NewObj, {clientId, Value}};
-set(Obj, destination, Value) when is_record(Obj, abstract_message) ->
- NewObj = Obj#abstract_message{destination = Value},
- {ok, NewObj, {destination, Value}};
-set(Obj, messageId, Value) when is_record(Obj, abstract_message) ->
- NewObj = Obj#abstract_message{messageId = Value},
- {ok, NewObj, {messageId, Value}};
-set(Obj, timestamp, Value) when is_record(Obj, abstract_message) ->
- NewObj = Obj#abstract_message{timestamp = Value},
- {ok, NewObj, {timestamp, Value}};
-set(Obj, timeToLive, Value) when is_record(Obj, abstract_message) ->
- NewObj = Obj#abstract_message{timeToLive = Value},
- {ok, NewObj, {timeToLive, Value}};
-set(Obj, headers, Value) when is_record(Obj, abstract_message) ->
- NewObj = Obj#abstract_message{headers = Value},
- {ok, NewObj, {headers, Value}};
-set(Obj, body, Value) when is_record(Obj, abstract_message) ->
- NewObj = Obj#abstract_message{body = Value},
- {ok, NewObj, {body, Value}};
-
-set(Obj, remoteUsername, Value) when is_record(Obj, rpc_message) ->
- NewObj = Obj#rpc_message{remoteUsername = Value},
- {ok, NewObj, {remoteUsername, Value}};
-set(Obj, remotePassword, Value) when is_record(Obj, rpc_message) ->
- NewObj = Obj#rpc_message{remotePassword = Value},
- {ok, NewObj, {remotePassword, Value}};
-set(Obj, parent, Value) when is_record(Obj, rpc_message) and is_record(Value, abstract_message) ->
- NewObj = Obj#rpc_message{parent = Value},
- {ok, NewObj, {parent, Value}};
-set(Obj, AbstractMessageProperty, Value) when is_record(Obj, rpc_message) and is_atom(AbstractMessageProperty) ->
- {ok, NewAbstractMessage, _} = set(Obj#rpc_message.parent, AbstractMessageProperty, Value),
- set(Obj, parent, NewAbstractMessage);
-
-set(Obj, parent, Value) when is_record(Obj, remoting_message) and is_record(Value, rpc_message) ->
- NewObj = Obj#remoting_message{parent = Value},
- {ok, NewObj, {parent, Value}};
-set(Obj, source, Value) when is_record(Obj, remoting_message) ->
- NewObj = Obj#remoting_message{source = Value},
- {ok, NewObj, {source, Value}};
-set(Obj, operation, Value) when is_record(Obj, remoting_message) ->
- NewObj = Obj#remoting_message{operation = Value},
- {ok, NewObj, {operation, Value}};
-set(Obj, parameters, Value) when is_record(Obj, remoting_message) ->
- NewObj = Obj#remoting_message{parameters = Value},
- {ok, NewObj, {parameters, Value}};
-set(Obj, RpcMessageProperty, Value) when is_record(Obj, remoting_message) and is_atom(RpcMessageProperty) ->
- {ok, NewRpcMessage, _} = set(Obj#remoting_message.parent, RpcMessageProperty, Value),
- set(Obj, parent, NewRpcMessage);
-
-set(Obj, parent, Value) when is_record(Obj, async_message) and is_record(Value, abstract_message) ->
- NewObj = Obj#async_message{parent = Value},
- {ok, NewObj, {parent, Value}};
-set(Obj, correlationId, Value) when is_record(Obj, async_message) ->
- NewObj = Obj#async_message{correlationId = Value},
- {ok, NewObj, {correlationId, Value}};
-set(Obj, correlationIdBytes, Value) when is_record(Obj, async_message) ->
- NewObj = Obj#async_message{correlationIdBytes = Value},
- {ok, NewObj, {correlationIdBytes, Value}};
-set(Obj, AbstractMessageProperty, Value) when is_record(Obj, async_message) and is_atom(AbstractMessageProperty) ->
- {ok, NewAbstractMessage, _} = set(Obj#async_message.parent, AbstractMessageProperty, Value),
- set(Obj, parent, NewAbstractMessage);
-
-set(Obj, parent, Value) when is_record(Obj, command_message) and is_record(Value, async_message) ->
- NewObj = Obj#command_message{parent = Value},
- {ok, NewObj, {parent, Value}};
-set(Obj, operation, Value) when is_record(Obj, command_message) ->
- NewObj = Obj#command_message{operation = Value},
- {ok, NewObj, {operation, Value}};
-set(Obj, AsyncMessageProperty, Value) when is_record(Obj, command_message) and is_atom(AsyncMessageProperty) ->
- {ok, NewAsyncMessage, _} = set(Obj#command_message.parent, AsyncMessageProperty, Value),
- set(Obj, parent, NewAsyncMessage).
-
-
-fc_to_record(?FC_REMOTINGMESSAGE) -> {ok, #remoting_message{}};
-fc_to_record(?FC_COMMANDMESSAGE) -> {ok, #command_message{}};
-fc_to_record(_) -> {ok, undefined}.
-
-%% Convert String to term, Str must be term-like string
-to_term(Str) ->
- {ok, Tokens, _} = erl_scan:string(Str),
- {ok, Term} = erl_parse:parse_term(Tokens ++ [{dot,1}]),
- Term.
+-module(record_utils).
+-author("trung@mdkt.org").
+-compile(export_all).
+
+-include("messages.hrl").
+-include("flex_classes.hrl").
+
+%% Setters for records defined in messages.hrl file
+%% return {ok, NewObject, {propertyName, NewValue}}
+set(Obj, clientId, Value) when is_record(Obj, abstract_message) ->
+ NewObj = Obj#abstract_message{clientId = Value},
+ {ok, NewObj, {clientId, Value}};
+set(Obj, destination, Value) when is_record(Obj, abstract_message) ->
+ NewObj = Obj#abstract_message{destination = Value},
+ {ok, NewObj, {destination, Value}};
+set(Obj, messageId, Value) when is_record(Obj, abstract_message) ->
+ NewObj = Obj#abstract_message{messageId = Value},
+ {ok, NewObj, {messageId, Value}};
+set(Obj, timestamp, Value) when is_record(Obj, abstract_message) ->
+ NewObj = Obj#abstract_message{timestamp = Value},
+ {ok, NewObj, {timestamp, Value}};
+set(Obj, timeToLive, Value) when is_record(Obj, abstract_message) ->
+ NewObj = Obj#abstract_message{timeToLive = Value},
+ {ok, NewObj, {timeToLive, Value}};
+set(Obj, headers, Value) when is_record(Obj, abstract_message) ->
+ NewObj = Obj#abstract_message{headers = Value},
+ {ok, NewObj, {headers, Value}};
+set(Obj, body, Value) when is_record(Obj, abstract_message) ->
+ NewObj = Obj#abstract_message{body = Value},
+ {ok, NewObj, {body, Value}};
+
+set(Obj, remoteUsername, Value) when is_record(Obj, rpc_message) ->
+ NewObj = Obj#rpc_message{remoteUsername = Value},
+ {ok, NewObj, {remoteUsername, Value}};
+set(Obj, remotePassword, Value) when is_record(Obj, rpc_message) ->
+ NewObj = Obj#rpc_message{remotePassword = Value},
+ {ok, NewObj, {remotePassword, Value}};
+set(Obj, parent, Value) when is_record(Obj, rpc_message) and is_record(Value, abstract_message) ->
+ NewObj = Obj#rpc_message{parent = Value},
+ {ok, NewObj, {parent, Value}};
+set(Obj, AbstractMessageProperty, Value) when is_record(Obj, rpc_message) and is_atom(AbstractMessageProperty) ->
+ {ok, NewAbstractMessage, _} = set(Obj#rpc_message.parent, AbstractMessageProperty, Value),
+ set(Obj, parent, NewAbstractMessage);
+
+set(Obj, parent, Value) when is_record(Obj, remoting_message) and is_record(Value, rpc_message) ->
+ NewObj = Obj#remoting_message{parent = Value},
+ {ok, NewObj, {parent, Value}};
+set(Obj, source, Value) when is_record(Obj, remoting_message) ->
+ NewObj = Obj#remoting_message{source = Value},
+ {ok, NewObj, {source, Value}};
+set(Obj, operation, Value) when is_record(Obj, remoting_message) ->
+ NewObj = Obj#remoting_message{operation = Value},
+ {ok, NewObj, {operation, Value}};
+set(Obj, parameters, Value) when is_record(Obj, remoting_message) ->
+ NewObj = Obj#remoting_message{parameters = Value},
+ {ok, NewObj, {parameters, Value}};
+set(Obj, RpcMessageProperty, Value) when is_record(Obj, remoting_message) and is_atom(RpcMessageProperty) ->
+ {ok, NewRpcMessage, _} = set(Obj#remoting_message.parent, RpcMessageProperty, Value),
+ set(Obj, parent, NewRpcMessage);
+
+set(Obj, parent, Value) when is_record(Obj, async_message) and is_record(Value, abstract_message) ->
+ NewObj = Obj#async_message{parent = Value},
+ {ok, NewObj, {parent, Value}};
+set(Obj, correlationId, Value) when is_record(Obj, async_message) ->
+ NewObj = Obj#async_message{correlationId = Value},
+ {ok, NewObj, {correlationId, Value}};
+set(Obj, correlationIdBytes, Value) when is_record(Obj, async_message) ->
+ NewObj = Obj#async_message{correlationIdBytes = Value},
+ {ok, NewObj, {correlationIdBytes, Value}};
+set(Obj, AbstractMessageProperty, Value) when is_record(Obj, async_message) and is_atom(AbstractMessageProperty) ->
+ {ok, NewAbstractMessage, _} = set(Obj#async_message.parent, AbstractMessageProperty, Value),
+ set(Obj, parent, NewAbstractMessage);
+
+set(Obj, parent, Value) when is_record(Obj, command_message) and is_record(Value, async_message) ->
+ NewObj = Obj#command_message{parent = Value},
+ {ok, NewObj, {parent, Value}};
+set(Obj, operation, Value) when is_record(Obj, command_message) ->
+ NewObj = Obj#command_message{operation = Value},
+ {ok, NewObj, {operation, Value}};
+set(Obj, AsyncMessageProperty, Value) when is_record(Obj, command_message) and is_atom(AsyncMessageProperty) ->
+ {ok, NewAsyncMessage, _} = set(Obj#command_message.parent, AsyncMessageProperty, Value),
+ set(Obj, parent, NewAsyncMessage).
+
+
+fc_to_record(?FC_REMOTINGMESSAGE) -> {ok, #remoting_message{}};
+fc_to_record(?FC_COMMANDMESSAGE) -> {ok, #command_message{}};
+fc_to_record(_) -> {ok, undefined}.
+
+%% Convert String to term, Str must be term-like string
+to_term(Str) ->
+ {ok, Tokens, _} = erl_scan:string(Str),
+ {ok, Term} = erl_parse:parse_term(Tokens ++ [{dot,1}]),
+ Term.
100 temp/server/ref_table.erl
View
@@ -1,50 +1,50 @@
--module(ref_table).
--author("trung@mdkt.org").
-
--compile(export_all).
-
-%% TODO make sure error handlings are in place
-
-%% delete all objects
-clear(TableName) ->
- try ets:delete_all_objects(TableName) of
- true ->
- _ = ets:insert(TableName, {counter, 0}),
- {ok}
- catch
- error:Error ->
- {bad, Error}
- end.
-
-%% Make sure the table exist, if not, create new one
-sure_exist(TableName) ->
- case ets:info(TableName) of
- undefined ->
- ets:new(TableName, [named_table]),
- _ = ets:insert(TableName, {counter, 0}),
- {ok, created};
- _ ->
- {ok, exists}
- end.
-
-%% wrapper of ets:insert, just make sure the table exists
-insert(TableName, Obj) ->
- _ = sure_exist(TableName),
- {ok, Count} = read(TableName, counter),
- io:fwrite("Inserting into ~p - index: ~p - value: ~p ~n", [TableName, Count, Obj]),
- _ = ets:insert(TableName, {Count, Obj}),
- _ = ets:insert(TableName, {counter, Count+1}),
- {ok, inserted, Count}.
-
-%% wrapper of ets:lookup
-read(TableName, Ref) ->
-%% io:fwrite("Read from ~p with index ~p got value ", [TableName, Ref]),
- case ets:lookup(TableName, Ref) of
- [] ->
- {bad, {not_found, TableName, Ref}};
- [{counter, Count}|_] ->
- {ok, Count};
- [{_Idx, Obj}|_] ->
-%% io:fwrite("~p~n", [Obj]),
- {ok, Obj}
- end.
+-module(ref_table).
+-author("trung@mdkt.org").
+
+-compile(export_all).
+
+%% TODO make sure error handlings are in place
+
+%% delete all objects
+clear(TableName) ->
+ try ets:delete_all_objects(TableName) of
+ true ->
+ _ = ets:insert(TableName, {counter, 0}),
+ {ok}
+ catch
+ error:Error ->
+ {bad, Error}
+ end.
+
+%% Make sure the table exist, if not, create new one
+sure_exist(TableName) ->
+ case ets:info(TableName) of
+ undefined ->
+ ets:new(TableName, [named_table]),
+ _ = ets:insert(TableName, {counter, 0}),
+ {ok, created};
+ _ ->
+ {ok, exists}
+ end.
+
+%% wrapper of ets:insert, just make sure the table exists
+insert(TableName, Obj) ->
+ _ = sure_exist(TableName),
+ {ok, Count} = read(TableName, counter),
+ io:fwrite("Inserting into ~p - index: ~p - value: ~p ~n", [TableName, Count, Obj]),
+ _ = ets:insert(TableName, {Count, Obj}),
+ _ = ets:insert(TableName, {counter, Count+1}),
+ {ok, inserted, Count}.
+
+%% wrapper of ets:lookup
+read(TableName, Ref) ->
+%% io:fwrite("Read from ~p with index ~p got value ", [TableName, Ref]),
+ case ets:lookup(TableName, Ref) of
+ [] ->
+ {bad, {not_found, TableName, Ref}};
+ [{counter, Count}|_] ->
+ {ok, Count};
+ [{_Idx, Obj}|_] ->
+%% io:fwrite("~p~n", [Obj]),
+ {ok, Obj}
+ end.
8 temp/server/utils.erl
View
@@ -0,0 +1,8 @@
+-module(utils).
+-author("trung@mdkt.org").
+-compile(export_all).
+
+milliseconds_to_date(Milliseconds) ->
+ BaseDate = calendar:datetime_to_gregorian_seconds({{1970,1,1},{0,0,0}}),
+ Seconds = BaseDate + (Milliseconds div 1000),
+ calendar:gregorian_seconds_to_datetime(Seconds).
Please sign in to comment.
Something went wrong with that request. Please try again.