guidance
Dustin Sallings
dustin@REDACTED
Tue Apr 27 10:48:13 CEST 2004
I've got thermometers that sit on my LAN and multicast readings and I
wanted to pick up the readings and make them available.
I have a little C program that uses the packet protocol to retransmit
those messages into erlang. Currently, I've got the following code:
---------------------
start() ->
spawn(?MODULE, init, []).
init() ->
register(mrecv, self()),
process_flag(trap_exit, true),
Port = open_port({spawn, "./mrecv"}, [{packet, 1}]),
loop(Port, dict:new()).
loop(Port, Dict) ->
receive
{Port, {data, Data}} ->
Vals = string:tokens(Data, "\t"),
Key = lists:nth(2, Vals),
Val = list_to_float(lists:nth(3, Vals)),
loop(Port, dict:update(Key, fun(_) -> Val end, Val, Dict));
{lookup, From, SN} ->
From ! dict:fetch(SN, Dict),
loop(Port, Dict);
{getdict, From} ->
From ! Dict,
loop(Port, Dict);
Unhandled ->
io:format("Unhandled message: ~p\n", [Unhandled]),
loop(Port, Dict)
end.
getdict() ->
mrecv ! {getdict, self()},
receive
Rv -> Rv
end.
getval(SN) ->
mrecv ! {lookup, self(), SN},
receive
Rv -> Rv
end.
---------------------
I've got two problems with this which make me think it might not be
the best way to do things.
1) It rather feels like OO. Is this normal?
2) It doesn't seem to work across a distributed connection. I can
spawn it on a remote system, but I can't get readings from that remote
system. I tried registering mrecv as the remote process ID, but I get
the following error:
(dustinti@REDACTED)1> Pid = spawn(rubik@REDACTED, mrecv, init, []).
<4147.40.0>
(dustinti@REDACTED)2> register(mrecv, Pid).
true
(dustinti@REDACTED)3> mrecv:getdict().
=ERROR REPORT==== 27-Apr-2004::01:43:17 ===
Error in process <0.30.0> on node 'dustinti@REDACTED' with exit value:
{badarg,[{mrecv,getdict,0},{erl_eval,do_apply,5},{shell,eval_loop,2}]}
** exited: {badarg,[{mrecv,getdict,0},
{erl_eval,do_apply,5},
{shell,eval_loop,2}]} **
vs. the other system:
(rubik@REDACTED)1> mrecv:getdict().
{dict,4,
16,
16,
8,
80,
48,
{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
{{[],
[],
[],
[],
[["10E8C214000000E4"|19.0500]],
[],
[],
[],
[["10258D2A000000EA"|18.2400]],
[],
[["10C8892A00000096"|22.8100]],
[],
[["1013A51E00000035"|23.9200]],
[],
[],
[]}}}
--
SPY My girlfriend asked me which one I like better.
pub 1024/3CAE01D5 1994/11/03 Dustin Sallings <dustin@REDACTED>
| Key fingerprint = 87 02 57 08 02 D0 DA D6 C8 0F 3E 65 51 98 D8 BE
L_______________________ I hope the answer won't upset her. ____________
More information about the erlang-questions
mailing list