Permalink
Browse files

switch most things to binaries instead of strings or atoms

  • Loading branch information...
1 parent c88f368 commit cd40fbf6f47ea6b95ae60d3d610834a76d731fe7 @gleber committed Apr 24, 2012
Showing with 66 additions and 91 deletions.
  1. +26 −31 src/acl.erl
  2. +8 −9 src/envelope.erl
  3. +4 −5 src/exat.erl
  4. +11 −14 src/http_server.erl
  5. +14 −26 src/mtp.erl
  6. +1 −4 src/sl.erl
  7. +1 −1 src/sl_parser.erl
  8. +1 −1 src/sl_parser.yrl
View
@@ -27,16 +27,14 @@
hexlify/1, inform/1, parse_message/1, propose/1,
query_if/1, query_ref/1, refuse/1, reject_proposal/1, reply/3,
request/1, sendacl/1, sl0_getcontent/1, sl0_getslot/2,
- sl0_parse/1, sl0_parsecontent/1, unhexlify/1]).
+ sl0_parse/1, sl0_parsecontent/1, unhexlify/1, normalize/1]).
-author('csanto@diit.unict.it').
-include("acl.hrl").
-include("fipa_ontology.hrl").
--define(ACL_REPRESENTATION, acl_erl_native).
-
erl_to_sl0([], Acc) ->
lists:flatten(lists:reverse([")" | Acc]));
erl_to_sl0([{Slotname, SlotValue} | Tail], Acc) ->
@@ -63,22 +61,17 @@ erlang_to_sl0(ErlangSentence) ->
%%%
sl0_parse(M) ->
- S = aclscanner:scan(M),
- T = aclscanner:tokenize(S),
- aclparser:parse(T).
+ {ok, sl:decode(M)}.
%% returns {error, ErrorCond} or {ok, Terms}
-sl0_getslot(_SearchedSlotname, []) -> ?ACL_ANY;
-sl0_getslot(SearchedSlotname,
- [{SlotName, SlotValue} | _MessageTail])
- when SlotName == SearchedSlotname ->
- SlotValue;
-sl0_getslot(SearchedSlotname, [_ | MessageTail]) ->
- sl0_getslot(SearchedSlotname, MessageTail).
+sl0_getslot(SearchedSlotname, Slots) when is_binary(SearchedSlotname) ->
+ sl0_getslot(binary_to_atom(SearchedSlotname, latin1), Slots);
+sl0_getslot(SearchedSlotname, Slots) when is_atom(SearchedSlotname) ->
+ proplists:get_value(SearchedSlotname, Slots, ?ACL_ANY).
sl0_getcontent(M) ->
- atom_to_list(sl0_getslot(content, M)).
+ sl0_getslot(content, M).
tolower(H) -> H + 32.
@@ -89,23 +82,18 @@ lowcase([H | T]) -> [H | lowcase(T)].
parse_message(M) ->
{ok, [SpeechAct | Slots]} = sl0_parse(M),
- LSpeechAct =
- list_to_atom(lowcase(atom_to_list(SpeechAct))),
+ io:format("Slots = ~p~n", [Slots]),
+ LSpeechAct = list_to_atom(lowcase(binary_to_list(SpeechAct))),
Message = #aclmessage{speechact = LSpeechAct,
- sender =
- translate_agent_identifier(sl0_getslot(sender,
- Slots)),
- receiver =
- translate_receiver_agent_identifier(sl0_getslot(receiver,
- Slots)),
+ sender = translate_agent_identifier(sl0_getslot(sender, Slots)),
+ receiver = translate_receiver_agent_identifier(sl0_getslot(receiver, Slots)),
'reply-to' = sl0_getslot('reply-to', Slots),
content = sl0_getslot(content, Slots),
language = sl0_getslot(language, Slots),
encoding = sl0_getslot(encoding, Slots),
ontology = sl0_getslot(ontology, Slots),
protocol = sl0_getslot(protocol, Slots),
- 'conversation-id' =
- sl0_getslot('conversation-id', Slots),
+ 'conversation-id' = sl0_getslot('conversation-id', Slots),
'reply-with' = sl0_getslot('reply-with', Slots),
'in-reply-to' = sl0_getslot('in-reply-to', Slots),
'reply-by' = sl0_getslot('reply-by', Slots)},
@@ -114,22 +102,23 @@ parse_message(M) ->
sl0_parsecontent(M) ->
sl0_parse(ensure_list(M#aclmessage.content)).
-translate_agent_identifier(ID = ['agent-identifier'
+translate_agent_identifier(ID = [<<'agent-identifier'>>
| _]) ->
sl_to_erlang_agent_identifier(ID);
translate_agent_identifier(ID) -> ID.
-translate_receiver_agent_identifier([set | Set]) ->
+translate_receiver_agent_identifier([<<"set">> | Set]) ->
[sl_to_erlang_agent_identifier(X) || X <- Set];
translate_receiver_agent_identifier(ID) ->
translate_agent_identifier(ID).
-sl_to_erlang_agent_identifier(['agent-identifier',
- {name, Name}, {addresses, Addresses}]) ->
- [Adr | _] = sl_sequence_to_erlang(Addresses),
- #'agent-identifier'{name = Name, addresses = Adr}.
+sl_to_erlang_agent_identifier([<<"agent-identifier">> | Slots]) ->
+ Name = sl0_getslot(name, Slots),
+ Addresses = sl0_getslot(addresses, Slots),
+ Adr = sl_sequence_to_erlang(Addresses),
+ normalize(#'agent-identifier'{name = Name, addresses = Adr}).
-sl_sequence_to_erlang([sequence | X]) -> X.
+sl_sequence_to_erlang([<<"sequence">> | X]) -> X.
to_list(X) when is_atom(X) -> atom_to_list(X);
to_list(X) -> X.
@@ -281,3 +270,9 @@ hexdigit($C) -> 12;
hexdigit($D) -> 13;
hexdigit($E) -> 14;
hexdigit($F) -> 15.
+
+
+normalize(#'agent-identifier'{name = N,
+ addresses = As} = AI) ->
+ AI#'agent-identifier'{name = iolist_to_binary(N),
+ addresses = [iolist_to_binary(X)||X<-As]}.
View
@@ -36,13 +36,13 @@ make_xml_envelope(To, From, Length, AclRepr) ->
[{name, [],
[binary_to_list(iolist_to_binary(To#'agent-identifier'.name))]},
{addresses, [],
- [{url, [], [X]}
+ [{url, [], [binary_to_list(X)]}
|| X <- To#'agent-identifier'.addresses]}]},
XmlFrom = {'agent-identifier', [],
[{name, [],
[binary_to_list(iolist_to_binary(From#'agent-identifier'.name))]},
{addresses, [],
- [{url, [], [X]}
+ [{url, [], [binary_to_list(X)]}
|| X <- From#'agent-identifier'.addresses]}]},
Envelope = {envelope, [],
[{params, [{index, 1}],
@@ -74,13 +74,12 @@ get_xml_element(_, _) -> {error, nil}.
get_xml_text(XML) ->
[Content | _] = XML#xmlElement.content,
Text = Content#xmlText.value,
- Text.
+ iolist_to_binary(Text).
parse_xml_envelope(XmlEnvelope) ->
{Envelope, _} = xmerl_scan:string(XmlEnvelope),
{ok, XTo} = get_xml_element(Envelope, to),
- {ok, XToAgentID} = get_xml_element(XTo,
- 'agent-identifier'),
+ {ok, XToAgentID} = get_xml_element(XTo, 'agent-identifier'),
{ok, XToAgentName} = get_xml_element(XToAgentID, name),
{ok, XToAgentAddress} = get_xml_element(XToAgentID,
addresses),
@@ -104,10 +103,10 @@ parse_xml_envelope(XmlEnvelope) ->
%io:format ("From ~s[~s]~n", [FromAgentName, FromURL]),
%io:format ("To ~s[~s]~n", [ToAgentName, ToURL]),
%io:format ("Repr ~s~n", [ACLRepresentation]),
- {#'agent-identifier'{name = list_to_atom(ToAgentName),
- addresses = ToURL},
- #'agent-identifier'{name = list_to_atom(FromAgentName),
- addresses = FromURL},
+ {#'agent-identifier'{name = ToAgentName,
+ addresses = [ToURL]},
+ #'agent-identifier'{name = FromAgentName,
+ addresses = [FromURL]},
ACLRepresentation}.
test() ->
View
@@ -76,17 +76,16 @@ current_platform() ->
(X) -> X =/= $@
end,
atom_to_list(node())),
- CurrentPlatform ++ "." ++ Hostname.
+ iolist_to_binary([CurrentPlatform, ".", Hostname]).
%%====================================================================
%% Func: split_agent_identifier/1
%% Returns: {string(), string()}
%%====================================================================
split_agent_identifier(AgentID) ->
- case lists:splitwith(fun (X) -> X =/= $@ end, AgentID)
- of
- {LocalID, []} -> {LocalID, current_platform()};
- {LocalID, [$@ | RealHAP]} -> {LocalID, RealHAP}
+ case binary:split(AgentID, <<"@">>) of
+ [LocalID] -> {LocalID, current_platform()};
+ [LocalID, RealHAP] -> {LocalID, RealHAP}
end.
%%====================================================================
View
@@ -22,9 +22,8 @@
%%
-module(http_server).
--export([decode/1, media_type_decode/1, terms_to_atom/1,
- terms_to_integer/1, terms_to_string/1, terms_to_tuple/1,
- tokenize/1, trim/1]).
+-export([terms_to_atom/1, terms_to_integer/1, terms_to_string/1,
+ terms_to_tuple/1, tokenize/1, trim/1]).
-author('csanto@diit.unict.it').
@@ -34,26 +33,24 @@ tokenize([]) -> [{'$end', 1}];
tokenize([H | T]) ->
[{list_to_atom([H]), 1} | tokenize(T)].
-terms_to_string([]) -> [];
-terms_to_string([H | T]) ->
+terms_to_string(L) ->
+ list_to_binary(terms_to_string0(L)).
+
+terms_to_string0([]) -> [];
+terms_to_string0([H | T]) ->
[L | _] = atom_to_list(element(1, H)),
- [L | terms_to_string(T)].
+ [L | terms_to_string0(T)].
-terms_to_atom(X) -> list_to_atom(terms_to_string(X)).
+terms_to_atom(X) -> list_to_atom(terms_to_string0(X)).
-terms_to_tuple(X) -> list_to_tuple(terms_to_string(X)).
+terms_to_tuple(X) -> list_to_tuple(terms_to_string0(X)).
terms_to_integer(X) ->
- Z = terms_to_string(X), list_to_integer(Z).
+ Z = terms_to_string0(X), list_to_integer(Z).
ltrim([]) -> [];
ltrim([$\s | T]) -> ltrim(T);
ltrim([H | T]) -> [H | T].
trim(S) ->
lists:reverse(ltrim(lists:reverse(ltrim(S)))).
-
-decode(M) -> T = tokenize(M), http_parser:parse(T).
-
-media_type_decode(M) ->
- T = tokenize(M), media_type:parse(T).
View
@@ -119,53 +119,41 @@ http_mtp_decode(Req, XmlContent, SLContent) ->
%%
decode_and_forward_acl(Req, _, _, Message,
"fipa.acl.rep.string.std") ->
- DecodedMessage = sl:decode(binary_to_list(Message)),
- %%io:format ("MSG = ~p~n", [DecodedMessage]),
- ACLMessage = list_to_tuple([aclmessage
- | tuple_to_list(DecodedMessage)]),
+ ACLMessage = acl:parse_message(binary_to_list(Message)),
%%io:format ("Message = ~w~n", [ACLMessage]),
%%io:format ("R = ~w~n", [ACLMessage#aclmessage.receiver]),
%%decode content
- ParsedMessage = case
- ontology_service:get_codec(ACLMessage#aclmessage.ontology)
- of
+ ParsedMessage = case ontology_service:get_codec(ACLMessage#aclmessage.ontology) of
{ok, Codec} ->
- {ok, SL} = sl:decode(ACLMessage#aclmessage.content,
- ascii_sl, erlang_sl),
+ {ok, SL} = sl:decode(ACLMessage#aclmessage.content, ascii_sl, erlang_sl),
%%io:format ("Content = ~p~n", [SL]),
ACLMessage#aclmessage{content = Codec:decode(SL)};
_ -> ACLMessage
end,
%%determine receiver list
- Receivers = case
- is_list(ParsedMessage#aclmessage.receiver)
- of
+ Receivers = case is_list(ParsedMessage#aclmessage.receiver) of
true -> ParsedMessage#aclmessage.receiver;
false -> [ParsedMessage#aclmessage.receiver]
end,
CurrentPlatform = exat:current_platform(),
%%io:format ("Receivers = ~w~n", [Receivers]),
- LocalReceivers = [V1
- || V1 <- Receivers,
- decode_and_forward_acl_1(V1, CurrentPlatform)],
- MessagesToSend = [ParsedMessage#aclmessage{receiver = X}
- || X <- LocalReceivers],
+ LocalReceivers = [V1 || V1 <- Receivers,
+ is_local(V1, CurrentPlatform)],
+ MessagesToSend = [ ParsedMessage#aclmessage{receiver = X} || X <- LocalReceivers],
%%io:format ("Parsed Message = ~w~n", [MessagesToSend]),
lists:foreach(fun (X) ->
- Receiver =
- (X#aclmessage.receiver)#'agent-identifier'.name,
+ Receiver = (X#aclmessage.receiver)#'agent-identifier'.name,
{ID, _} = exat:split_agent_identifier(Receiver),
%%io:format ("Recv = ~w~n", [Receiver]),
- gen_server:call(list_to_atom(ID), [acl_erl_native, X])
+ gen_server:call(binary_to_atom(ID, latin1), [acl_erl_native, X])
end,
MessagesToSend),
?OK_RESPONSE;
decode_and_forward_acl(Req, _, _, _, _) ->
?BAD_RESPONSE.
-decode_and_forward_acl_1(X, CurrentPlatform) ->
- {_ID, HAP} =
- exat:split_agent_identifier(X#'agent-identifier'.name),
+is_local(X, CurrentPlatform) ->
+ {_ID, HAP} = exat:split_agent_identifier(X#'agent-identifier'.name),
HAP == CurrentPlatform.
%%====================================================================
@@ -192,7 +180,8 @@ http_mtp_encode_and_send(To, From, Message) ->
[$\r, $\n, $\r, $\n], ACL, [$\r, $\n],
"--251D738450A171593A1583EB--",
[$\r, $\n, $\r, $\n]]),
- [ReceiverAddr | _] = To#'agent-identifier'.addresses,
+ [ReceiverAddr0 | _] = To#'agent-identifier'.addresses,
+ ReceiverAddr = binary_to_list(ReceiverAddr0),
{Host, Port} = case http_uri:parse(ReceiverAddr) of
{http, _, H, P, _, _} -> {H, P};
{ok, {http, _, H, P, _, _}} -> {H, P}
@@ -217,8 +206,7 @@ http_mtp_encode_and_send(To, From, Message) ->
%% Headers,
%% "multipart/mixed ; boundary=\"251D738450A171593A1583EB\"",
%% HTTPBody}, [], [{sync, true}]),
- HTTPID = gen_server:call(mtp_sender,
- {http_post, Request}),
+ HTTPID = gen_server:call(mtp_sender, {http_post, Request}),
{ok, RequestID} = HTTPID,
Result = RequestID,
%%io:format ("Result ~p, ~p~n", [self (), Result]),
View
@@ -45,15 +45,12 @@ decode(Message) -> decode(Message, ascii_sl, ontology).
%% Func: decode/3
%%====================================================================
decode(AsciiMessage, ascii_sl, erlang_sl) ->
- %%io:format ("ASCII ~s~n", [AsciiMessage]),
T = tokenize(AsciiMessage),
sl_parser:parse(T);
decode(SLMessage, erlang_sl, ontology) ->
fipa_ontology_sl_codec:decode(SLMessage);
decode(AsciiMessage, ascii_sl, ontology) ->
- {ok, ErlangSL} = decode(AsciiMessage, ascii_sl,
- erlang_sl),
- %%io:format ("~w~n", [ErlangSL]),
+ {ok, ErlangSL} = decode(AsciiMessage, ascii_sl, erlang_sl),
decode(ErlangSL, erlang_sl, ontology).
%%====================================================================
View
@@ -2086,7 +2086,7 @@ yeccpars2_23_(__Stack0) ->
yeccpars2_110_(__Stack0) ->
[__4,__3,__2,__1 | __Stack] = __Stack0,
[begin
- { list_to_atom ( __2 ) , __4 }
+ { binary_to_atom ( __2 , latin1 ) , __4 }
end | __Stack].
-compile({inline,yeccpars2_115_/1}).
View
@@ -61,7 +61,7 @@ elements -> elem wsp elements : ['$1'] ++ '$3'.
elem -> term : '$1'.
elem -> list : '$1'.
elem -> slot : '$1'.
-slot -> ':' term wsp slotterm : { list_to_atom ('$2'), '$4'}.
+slot -> ':' term wsp slotterm : { binary_to_atom ('$2', latin1), '$4'}.
slotterm -> '"' quotedstring '"' : '$2'.
slotterm -> term : '$1'.
slotterm -> list : '$1'.

0 comments on commit cd40fbf

Please sign in to comment.