2012-04-11 2 views
1

Может кто-то помочь мне с этим простым распространенным экзаменом erlang. Как я могу запустить эту программу erlang, чтобы увидеть, как она работает? Я смотрел 3 оболочки с erl -sname pc1, erl -sname pc2 и erl -sname server и так же ping с сервера pc1 и pc2, чтобы установить соединение между ними. теперь что еще мне нужно сделать, чтобы проверить эту программу?как запустить этот пример erlang

-module(pubsub2). 
-export([startDispatcher/0, startClient/0, 
    subscribe/2, publish/3]). 

startClient() -> 
    Pid = spawn(fun clientLoop/0), 
    register(client, Pid). 

clientLoop() -> 
    receive {Topic, Message} -> 
     io:fwrite("Received message ~w for topic ~w~n", 
       [Message, Topic]), 
     clientLoop() 
    end. 

subscribe(Host, Topic) -> 
    {dispatcher, Host} ! {subscribe, node(), Topic}. 

publish(Host, Topic, Message) -> 
    {dispatcher, Host} ! {publish, Topic, Message}. 

startDispatcher() -> 
    Pid = spawn(fun dispatcherLoop/0), 
    register(dispatcher, Pid). 

dispatcherLoop() -> 
    io:fwrite("Dispatcher started\n"), 
    dispatcherLoop([]). 
dispatcherLoop(Interests) -> 
    receive 
    {subscribe, Client, Topic} -> 
     dispatcherLoop(addInterest(Interests, Client, Topic)); 
    {publish, Topic, Message} -> 
     Destinations = computeDestinations(Topic, Interests), 
     send(Topic, Message, Destinations), 
     dispatcherLoop(Interests) 
    end. 

computeDestinations(_, []) -> []; 
computeDestinations(Topic, [{SelectedTopic, Clients}|T]) -> 
    if SelectedTopic == Topic -> Clients; 
     SelectedTopic =/= Topic -> computeDestinations(Topic, T) 
    end. 

send(_, _, []) -> ok; 
send(Topic, Message, [Client|T]) -> 
    {client, Client} ! {Topic, Message}, 
    send(Topic, Message, T). 

addInterest(Interests, Client, Topic) -> 
    addInterest(Interests, Client, Topic, []). 
addInterest([], Client, Topic, Result) -> 
    Result ++ [{Topic, [Client]}]; 
addInterest([{SelectedTopic, Clients}|T], Client, Topic, Result) -> 
    if SelectedTopic == Topic -> 
     NewClients = Clients ++ [Client], 
     Result ++ [{Topic, NewClients}] ++ T; 
     SelectedTopic =/= Topic -> 
     addInterest(T, Client, Topic, Result ++ [{SelectedTopic, Clients}]) 
    end. 

ответ

1

Я предлагаю вам следующее: http://www.erlang.org/doc/getting_started/conc_prog.html

Во всяком случае, учитывая, что вы все узлы одни и те же печенье, начать 3 различных оболочек

erl -sname n1 
([email protected])1> pubsub2:startClient(). 

erl -sname n2 
([email protected])1> pubsub2:startDispatcher(). 

erl -sname n3 
([email protected])1> pubsub2:startClient(). 

в n1 сделать:

([email protected])1> pubsub2:startClient(). 
([email protected])2> pubsub2:subscribe('[email protected]', football). 

in n3 do:

([email protected])1> pubsub2:startClient(). 
([email protected])1> pubsub2:publish('[email protected]', football, news1). 

в n1 вы должны получить:

Received message news1 for topic football 

Конечно, вы можете расширить его, как вы хотите.

Смежные вопросы