2012-03-11 38 views
2

我想通過調用我寫的函數來測試一個系統,這個函數是在一個腳本中並且在Erlang shell(在windows上)編譯的。從腳本連接到節點?

的函數連接到其他節點(其正在運行)。

我得到一個問題,指出「無法啓動二郎:適用,[......」,我想這是因爲我還沒有開始外殼充當分佈式節點。

可能有人請指教一下我需要做的,能夠運行從shell腳本,該消息的其他節點?

+0

這裏是呼籲其他節點功能的一個簡單的例子有些類似的問題:http://stackoverflow.com/questions/4199823/simple-distributed-erlang。如果您需要更多幫助,請提供您正在嘗試運行的代碼。 – mbesso 2012-03-12 07:09:11

回答

1

即使世界兩件事情可能出錯,

一,你根本不成立爲一個節點。在Windows中,你可以通過「-node節點名」到werl以類似的方式,以Unix的解決這個問題,比如:

$ erl -name zephmatic 
([email protected])1> node(). 
'[email protected]' 

如果你已經設置了節點名稱(只是要確定運行!節點()和Erlang:get_cookie()在ERL),我猜這是在你遇到麻煩二郎山實際節點消息語義 - 這是一個可以理解的寬泛的主題和整個Erlang的語言幾乎完全着重於解決正確的消息傳遞原語的難題。規範的OTP參考是Distributed Erlang,但幽默和(在我看來)對該主題的出色介紹是Learn You Some Erlang For Great Good

如果它對你的直接需求有幫助,下面是一些示例代碼,用於處理我測試過的gen_server行爲,可以隨意調用它,連接它,並獲得更高質量的分佈式編程(僅限無限時間)!

最好的運氣,當你有一個十億機器的力量在您的處置不接管世界太快!

-module(communicate). 
-author('Zephyr Pellerin'). 

-behavior(gen_server). 

-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). 

-export([start/0, grab/2, lookup/1, return/1]). 

%%% The reason I declare all of these is to make calling 
%%% namespaced functions easier, i.e, simply calling start(). 
%%% rather than gen_server:start_link() 
start() -> 
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). 
grab(Which, Element) -> 
    gen_server:call(?MODULE, {grab, Which, Element}). 
lookup(Element) -> 
    gen_server:call(?MODULE, {lookup, Element}). 
return(Element) -> 
    gen_server:call(?MODULE, {return, Element}). 

% The init method is a "callback" (Whatever that designation may imply) 
% that occurs when another client connects 
init([]) -> 
    Resource = dict:new(), 
    {ok, Resource}. 

%%% The Generic server specifies that handling calls happens 
%%% through handle_call, which would have thought! 
%%% If you're not very experienced with Erlang, this below might seem 
%%% strange to you -- it's just different ways to handle 
%%% input given differing "types" (Not a real thing in Erl). 

% This function simply looks to see if an element from 
% a "Resource" is in use, if not it tells the other node 
% that it is locked, or returns it if not. 
handle_call({grab, Which, Element}, _From, Resource) -> 
    Response = case dict:is_key(Element, Resource) of 
     true -> 
      NewResource = Resource, 
      {locked, Element}; 
     false -> 
      NewResource = dict:append(Element, Which, Resource), 
      ok 
    end, 
    {reply, Response, NewResource}; 

% This function checks for the existence of an element 
handle_call({lookup, Element}, _From, Resource) -> 
    Response = case dict:is_key(Element, Resource) of 
     true -> 
      {which, lists:nth(1, dict:fetch(Element, Resource))}; 
     false -> 
      {not_locked, Element} 
    end, 
    {reply, Response, Resource}; 

% This uses scary language to annihilate an element! 
handle_call({annihilate, Element}, _From, Resource) -> 
    NewResource = dict:erase(Element, Resource), 
    {reply, ok, NewResource}; 

% These are simply functions that must be defined according to gen_server 
% its not so special. 
handle_call(_Message, _From, Resource) -> 
    {reply, error, Resource}. 
handle_cast(_Message, Resource) -> 
    {noreply, Resource}. 
handle_info(_Message, Resource) -> 
    {noreply, Resource}. 

% You need to have a method that knows when to fold em' if someone requests termination 

terminate(_Reason, _Resource) -> 
    ok. 

% While not *strictly* nessasary, this is a cool method that allows you to inject code 
code_change(_OldVersion, Resource, _Extra) -> 
    {ok, Resource}. 
3

要從escript啓動Erlang分佈,你要麼需要通過-name Name參數作爲一個熱點評論%%! -name Name在escript,或者您需要手動啓動發行。我爲Erlang Factory 2011寫了一個例子(在這裏談論 - http://www.erlang-factory.com/conference/SFBay2011/speakers/GeoffCant)。

我也有從我在談話中使用的github repoexample code to start erlang distribution in an escript的鏈接。

它歸結爲:

net_kernel:start([Name, longnames]), 
erlang:set_cookie(Name, list_to_atom(Cookie)). 
+0

我可能誤解了你的問題 - 我以爲你在談論escript,而不是erlang shell。然而,這個代碼可以工作。 – archaelus 2012-03-13 21:05:02