Erlang无法从它开始的位置接收消息

时间:2015-03-12 11:31:56

标签: erlang send distributed distributed-system receiver

这里有两个文件。

temp_converter() ->
receive
    {convertToCelsius, {From, TempF}} ->
        io:format("Temp-Server2 > Converter received message from ~w~n", [From]),
        ConvertedTempC = (TempF-32)*5/9,
        From ! finished,
        From ! {converted, ConvertedTempC},
        temp_converter();
    {From, {convertToCelsius, TempF}} ->
        io:format("Temp-Server > Converter received message from ~w~n", [From]),
        ConvertedTempC = (TempF-32)*5/9,
        From ! {converted, ConvertedTempC},
        temp_converter()
end.

另一个是:

sensor(Temp, Temp_Node, Dis_Node) ->
receive
    finished ->
        io:foramt("finished");
    % Receive the clock_tick from clock controller
    clock_tick ->
        io:format("Sensor_f received tick~n", []),
        {temp_converter, Temp_Node} ! {convertToCelsius, {self(), Temp}};
    % Receive the temperature which has been converted
    {converted, ConvertedTemp} ->
        io:format("Sensor_f received converted temperature~n", []),
        {display_module, Dis_Node} ! {inCelsius, ConvertedTemp};
    {'EXIT', From, Reason} ->
        io:foramt("Temperature Server down!~nGot ~p~n", [{'EXIT', From, Reason}])
end.

基本上,传感器将向temp_converter发送消息,该消息在“clock_tick - >”中实现。当temp_converter收到消息时,它会输出一些内容并发回消息。这是问题所在。 它确实输出了一些内容,但传感器无法从temp_converter接收消息。我的代码有什么问题吗?我也尝试发回“完成”的消息,但它仍然无法正常工作!!

如何发回信息?如何更改“From!finished”和“From!{converted,ConvertedTempC}”是否正确?

1 个答案:

答案 0 :(得分:0)

在转换中:

{convertToCelsius, {From, TempF}} ->
    io:format("Temp-Server2 > Converter received message from ~w~n", [From]),
    ConvertedTempC = (TempF-32)*5/9,
    From ! finished, %% Here you send to sensor the message 'finished'
    From ! {converted, ConvertedTempC}, %% the conversion will be received                                    %% after the first message (guaranteed by Erlang VM)
    temp_converter();

在传感器中:

finished ->
    io:foramt("finished"); %% when you receive the first message, you print it
                           %% and leave the function. The next message will be lost
                           %% because you do not loop in the sensor function.

你只需要在接收集团之后回忆sensor(Temp, Temp_Node, Dis_Node),它应该没问题。

注意消息finished在您的情况下是无用的,并且对转换函数的简单调用应该可以完成这项工作。