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) ->
         {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)

getdict() ->
     mrecv ! {getdict, self()},
         Rv -> Rv

getval(SN) ->
     mrecv ! {lookup, self(), SN},
         Rv -> Rv


	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, []).
(dustinti@REDACTED)2> register(mrecv, Pid).
(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: 

** exited: {badarg,[{mrecv,getdict,0},
                     {shell,eval_loop,2}]} **

vs. the other system:

(rubik@REDACTED)1> mrecv:getdict().

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