Giter Club home page Giter Club logo

Comments (4)

seconemouse avatar seconemouse commented on May 13, 2024 1

Great, It works.

erl:

-module(my_bank).
-behavior(gen_server).
-export([start/0,stop/0,new_account/1,deposit/2,withdraw/2,
        init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

%%====================================================================
%% server interface function
%%====================================================================
start()->
    gen_server:start_link({local,?MODULE}, ?MODULE,[],[]).

stop()->
    gen_server:call(?MODULE,stop).

new_account(Who)->
    gen_server:call(?MODULE,{new,Who}).

deposit(Who,Amount)->
    gen_server:call(?MODULE,{add,Who,Amount}).

withdraw(Who,Amount)->
    gen_server:call(?MODULE,{remove,Who,Amount}).

%%====================================================================
%% gen_server callbacks
%%====================================================================
init([]) ->
    Init=ets:new(?MODULE,[]),
    io:format("init ~p ~p",[Init,?MODULE]),
    {ok, Init}.

handle_call({new,Who}, _From, State) ->
    Reply = case ets:lookup(State,Who) of
        []->ets:insert(State,{Who,0}),
            {welcome,Who};
        [_]->{exsits,Who,you_already_are_a_customer}
    end,
    {reply, Reply, State}; 
handle_call({add,Who,Amount},_From,State)->
    Reply = case ets:lookup(State,Who) of
        []->not_a_customer;
        [{Who,Balance}]->
            NewBalance=Balance+Amount,
            ets:insert(State,{Who,NewBalance}),
            {thanks,Who,you_balance_is,NewBalance}
    end,
    {reply, Reply, State}; 
handle_call({remove,Who,Amount},_From,State)->
    Reply = case ets:lookup(State,Who) of
        []->not_a_customer;
        [{Who,Balance}] when Amount =< Balance ->
            NewBalance=Balance-Amount,
            ets:insert(State,{Who,NewBalance}),
            {thanks,Who,your_balance_is,NewBalance};
        [{Who,Balance}]->
            {sorry,Who,you_only_have,Balance,in_the_bank}
    end,
    {reply, Reply, State}.

handle_cast(_Msg, State) ->
    {noreply, State}.

handle_info(_Info, State) ->
    {noreply, State}.

terminate(_Reason, _State) ->
    io:format("is terminate.",[]),
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

go

ret, _ := process.Call(etf.Tuple{"my_bank", "erlmain@locahost"}, etf.Tuple{etf.Atom("remove"), "tyt", 1}) 

from ergo.

halturin avatar halturin commented on May 13, 2024

Thanks for the question. I would recommend rewriting your erlang code to the OTP design patterns using gen_server behavior. The reason you don't receive the message because you didn't specify the correct pattern for that. Process.Call does the same as gen_server:call - a sending message has pattern {'$gen_call', From, Message}.

There are 3 way to send a message:

As of your erlang code... here is how it should look like (if you don't want to follow OTP design)

receive                                   
        {'$gen_call', _From, Message} ->        % in case of using Process.Call
            io:format("Got 'call' message: ~p", [Message]),
            % you have to reply on it with correct answer like this https://github.coml/halturin/ergo/blob/master/gen_server.go#L81
            % otherwise Process.Call fail with timeout
            loop();                           
        {'$gen_cast', Message} ->  % in case of using Process.Cast            
            % you don't have to reply here
            io:format("Got 'cast' message: ~p", [Message]), 
            loop();          
        OtherMessage -> % in case of using Process.Send             
            io:format("Got 'other' message: ~p", [OtherMessage]),
            loop()
    end.

from ergo.

seconemouse avatar seconemouse commented on May 13, 2024

Thanks a lot,I'll try it later

from ergo.

halturin avatar halturin commented on May 13, 2024

Let's leave it open for the others who get the same question.

from ergo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.