Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first

  • Loading branch information...
commit aad40525488288e9ff66dd637742cf3f30ce4438 0 parents
@hoterran authored
35 README.md
@@ -0,0 +1,35 @@
+
+one example, show how to swap erlang code online include inner state record
+
+#step
+
+>os:cmd("cp swap_test1.erl swap_test.erl").
+
+>c(swap_test).
+
+>swap_test:start_link(0).
+
+>swap_test:test_call().
+
+>swap_test:test_call().
+
+>os:cmd("cp swap_test2.erl swap_test.erl").
+
+>compile:file(swap_test).
+
+>sys:suspend(swap_test).
+
+>code:purge(swap_test).
+
+>code:load_file(swap_test).
+
+>sys:change_code(swap_test, swap_test, "0", [3]).
+
+>sys:resume(swap_test).
+
+>swap_test:test_call().
+
+>swap_test:test_call().
+
+
+
BIN  swap_test.beam
Binary file not shown
49 swap_test.erl
@@ -0,0 +1,49 @@
+-module(swap_test).
+-vesion("1").
+-behaviour(gen_server).
+
+-export([start_link/2, test_call/0]).
+
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
+
+-define(SERVER, swap_test).
+
+-record(state, {counter, step}).
+-record(old_state, {counter}).
+
+start_link(Start, Step) ->
+ gen_server:start_link({local, ?SERVER}, ?MODULE, [Start, Step], []).
+
+test_call() ->
+ gen_server:call(?SERVER, counter).
+
+%%
+%% CALLBACK
+%%
+
+init([Start, Step]) ->
+ io:format("Args is ~p~n", [Start]),
+ {ok, #state{counter=Start, step=Step}}.
+
+handle_call(Msg, {From,_}, State) ->
+ io:format("call ~p ~p~n", [Msg, From]),
+ #state{counter = Counter, step = Step} = State,
+ {reply, Counter, State#state{counter = Counter + Step}}.
+
+handle_cast(Msg, State) ->
+ io:format("cast ~p~n", [Msg]),
+ {noreply, State}.
+
+handle_info(Info, State) ->
+ io:format("info ~p~n", [Info]),
+ {stop, normal, State}.
+
+terminate(Reason, _State) ->
+ io:format("stop ~p~n", [Reason]),
+ ok.
+
+code_change("0", State, Extra) ->
+ {state, Counter} = State,
+ Step = lists:last(Extra),
+ {ok, #state{counter = Counter, step=Step}}.
+
46 swap_test1.erl
@@ -0,0 +1,46 @@
+-module(swap_test).
+-vesion("0").
+-behaviour(gen_server).
+
+-export([start_link/1, test_call/0]).
+
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
+
+-define(SERVER, swap_test).
+
+-record(state, {counter}).
+
+start_link(Start) ->
+ gen_server:start_link({local, ?SERVER}, ?MODULE, [Start], []).
+
+test_call() ->
+ gen_server:call(?SERVER, counter).
+
+%%
+%% CALLBACK
+%%
+
+init([Start]) ->
+ io:format("Args is ~p~n", [Start]),
+ {ok, #state{counter=Start}}.
+
+handle_call(Msg, {From,_}, State) ->
+ io:format("call ~p ~p~n", [Msg, From]),
+ Counter = State#state.counter,
+ {reply, Counter, State#state{counter = Counter + 1}}.
+
+handle_cast(Msg, State) ->
+ io:format("cast ~p~n", [Msg]),
+ {noreply, State}.
+
+handle_info(Info, State) ->
+ io:format("info ~p~n", [Info]),
+ {stop, normal, State}.
+
+terminate(Reason, _State) ->
+ io:format("stop ~p~n", [Reason]),
+ ok.
+
+code_change(_OldVsn, _State, _Extra) ->
+ {ok, _State}.
+
49 swap_test2.erl
@@ -0,0 +1,49 @@
+-module(swap_test).
+-vesion("1").
+-behaviour(gen_server).
+
+-export([start_link/2, test_call/0]).
+
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
+
+-define(SERVER, swap_test).
+
+-record(state, {counter, step}).
+-record(old_state, {counter}).
+
+start_link(Start, Step) ->
+ gen_server:start_link({local, ?SERVER}, ?MODULE, [Start, Step], []).
+
+test_call() ->
+ gen_server:call(?SERVER, counter).
+
+%%
+%% CALLBACK
+%%
+
+init([Start, Step]) ->
+ io:format("Args is ~p~n", [Start]),
+ {ok, #state{counter=Start, step=Step}}.
+
+handle_call(Msg, {From,_}, State) ->
+ io:format("call ~p ~p~n", [Msg, From]),
+ #state{counter = Counter, step = Step} = State,
+ {reply, Counter, State#state{counter = Counter + Step}}.
+
+handle_cast(Msg, State) ->
+ io:format("cast ~p~n", [Msg]),
+ {noreply, State}.
+
+handle_info(Info, State) ->
+ io:format("info ~p~n", [Info]),
+ {stop, normal, State}.
+
+terminate(Reason, _State) ->
+ io:format("stop ~p~n", [Reason]),
+ ok.
+
+code_change("0", State, Extra) ->
+ {state, Counter} = State,
+ Step = lists:last(Extra),
+ {ok, #state{counter = Counter, step=Step}}.
+
Please sign in to comment.
Something went wrong with that request. Please try again.