- 浏览: 576817 次
- 性别:
- 来自: 广州杭州
文章分类
最新评论
-
bohc:
谢谢,搞了两天了,现在才算是找到问题所在,解决了。
文件在使用FileChannel.map后不能被删除(Windows上) -
zhang0000jun:
在jdk1.8中执行正好和楼主的结果相反,请指教
从Java视角理解CPU缓存(CPU Cache) -
在世界的中心呼喚愛:
forenroll 写道请问楼主的那个分析工具cachemis ...
从Java视角理解CPU缓存(CPU Cache) -
xgj1988:
我这里打出的结果是: 0 L1-dcache-load-mis ...
从Java视角理解CPU缓存(CPU Cache) -
thebye85:
请教下大神,为什么频繁的park会导致大量context sw ...
从Java视角理解CPU上下文切换(Context Switch)
2 Gen_Server Behaviour
This chapter should be read in conjunction with gen_server(3)
, where all interface functions and callback functions are described in detail.
2.1 Client-Server Principles
The client-server model is characterized by a central server and an arbitrary number of clients. The client-server model is generally used for resource management operations, where several different clients want to share a common resource. The server is responsible for managing this resource.
Client-Server Model 客户-服务器模型
2.2 Example
An example of a simple server written in plain Erlang was given in Overview. The server can be re-implemented using gen_server
, resulting in this callback module:
java 代码
- -module(ch3).
- -behaviour(gen_server).
- -export([start_link/0]).
- -export([alloc/0, free/1]).
- -export([init/1, handle_call/3, handle_cast/2]).
- start_link() ->
- gen_server:start_link({local, ch3}, ch3, [], []).
- alloc() ->
- gen_server:call(ch3, alloc).
- free(Ch) ->
- gen_server:cast(ch3, {free, Ch}).
- init(_Args) ->
- {ok, channels()}.
- handle_call(alloc, _From, Chs) ->
- {Ch, Chs2} = alloc(Chs),
- {reply, Ch, Chs2}.
- handle_cast({free, Ch}, Chs) ->
- Chs2 = free(Ch, Chs),
- {noreply, Chs2}.
The code is explained in the next sections.
2.3 Starting a Gen_Server
In the example in the previous section, the gen_server is started by calling ch3:start_link()
:
java 代码
- start_link() ->
- gen_server:start_link({local, ch3}, ch3, [], []) => {ok, Pid}
start_link
calls the function gen_server:start_link/4
. This function spawns and links to a new process, a gen_server.
- The first argument
{local, ch3}
specifies the name. In this case, the gen_server will be locally registered asch3
.
If the name is omitted, the gen_server is not registered. Instead its pid must be used. The name could also be given as{global, Name}
, in which case the gen_server is registered usingglobal:register_name/2
.
- The second argument,
ch3
, is the name of the callback module, that is the module where the callback functions are located.
In this case, the interface functions (start_link
,alloc
andfree
) are located in the same module as the callback functions (init
,handle_call
andhandle_cast
). This is normally good programming practice, to have the code corresponding to one process contained in one module.
- The third argument, [], is a term which is passed as-is to the callback function
init
. Here,init
does not need any indata and ignores the argument.
- The fourth argument, [], is a list of options. See
gen_server(3)
for available options.
If name registration succeeds, the new gen_server process calls the callback function ch3:init([])
. init
is expected to return {ok, State}
, where State
is the internal state of the gen_server. In this case, the state is the available channels.
java 代码
- init(_Args) ->
- {ok, channels()}.
Note that gen_server:start_link
is synchronous. It does not return until the gen_server has been initialized and is ready to receive requests.
gen_server:start_link
must be used if the gen_server is part of a supervision tree, i.e. is started by a supervisor. There is another function gen_server:start
to start a stand-alone gen_server, i.e. a gen_server which is not part of a supervision tree.
2.4 Synchronous Requests - Call
The synchronous request alloc()
is implemented using gen_server:call/2
:
java 代码
- alloc() ->
- gen_server:call(ch3, alloc).
ch3
is the name of the gen_server and must agree with the name used to start it. alloc
is the actual request.
The request is made into a message and sent to the gen_server. When the request is received, the gen_server calls handle_call(Request, From, State)
which is expected to return a tuple {reply, Reply, State1}
. Reply
is the reply which should be sent back to the client, and State1
is a new value for the state of the gen_server.
java 代码
- handle_call(alloc, _From, Chs) ->
- {Ch, Chs2} = alloc(Chs),
- {reply, Ch, Chs2}.
In this case, the reply is the allocated channel Ch
and the new state is the set of remaining available channels Chs2
.
Thus, the call ch3:alloc()
returns the allocated channel Ch
and the gen_server then waits for new requests, now with an updated list of available channels.
2.5 Asynchronous Requests - Cast
The asynchronous request free(Ch)
is implemented using gen_server:cast/2
:
java 代码
- free(Ch) ->
- gen_server:cast(ch3, {free, Ch}).
ch3
is the name of the gen_server. {free, Ch}
is the actual request.
The request is made into a message and sent to the gen_server. cast
, and thus free
, then returns ok
.
When the request is received, the gen_server calls handle_cast(Request, State)
which is expected to return a tuple {noreply, State1}
. State1
is a new value for the state of the gen_server.
java 代码
- handle_cast({free, Ch}, Chs) ->
- Chs2 = free(Ch, Chs),
- {noreply, Chs2}.
In this case, the new state is the updated list of available channels Chs2
. The gen_server is now ready for new requests.
2.6 Stopping
2.6.1 In a Supervision Tree
If the gen_server is part of a supervision tree, no stop function is needed. The gen_server will automatically be terminated by its supervisor. Exactly how this is done is defined by a shutdown strategy set in the supervisor.
If it is necessary to clean up before termination, the shutdown strategy must be a timeout value and the gen_server must be set to trap exit signals in the init
function. When ordered to shutdown, the gen_server will then call the callback function terminate(shutdown, State)
:
java 代码
- init(Args) ->
- ...,
- process_flag(trap_exit, true),
- ...,
- {ok, State}.
- ...
- terminate(shutdown, State) ->
- ..code for cleaning up here..
- ok.
2.6.2 Stand-Alone Gen_Servers
If the gen_server is not part of a supervision tree, a stop function may be useful, for example:
java 代码
- ...
- export([stop/0]).
- ...
- stop() ->
- gen_server:cast(ch3, stop).
- ...
- handle_cast(stop, State) ->
- {stop, normal, State};
- handle_cast({free, Ch}, State) ->
- ....
- ...
- terminate(normal, State) ->
- ok.
The callback function handling the stop
request returns a tuple {stop, normal, State1}
, where normal
specifies that it is a normal termination and State1
is a new value for the state of the gen_server. This will cause the gen_server to call terminate(normal,State1)
and then terminate gracefully.
2.7 Handling Other Messages
If the gen_server should be able to receive other messages than requests, the callback function handle_info(Info, State)
must be implemented to handle them. Examples of other messages are exit messages, if the gen_server is linked to other processes (than the supervisor) and trapping exit signals.
java 代码
- handle_info({'EXIT', Pid, Reason}, State) ->
- ..code to handle exits here..
- {noreply, State1}.
发表评论
-
ubuntu安装otp R11B 的一些记录
2007-11-16 12:30 2828新的ubuntu系统会缺少一些工具 和lib. 用apt-ge ... -
emulator调试日志: driver篇
2007-10-08 16:35 2318--------- driver篇 ------------- ... -
修正Programming Erlang中linked driver实例的小问题
2007-10-08 14:50 2485也许很多人碰上过, 用example1_lid:sta ... -
emulator调试日志: port篇
2007-10-06 16:14 2408------------------ port 篇 ----- ... -
supervisor一小技巧
2007-09-04 13:20 1833simple_one_for_one可以让supervisor ... -
gen_server
2007-08-29 21:52 1944State用来存数据, 任何erlang term都行 ge ... -
application
2007-08-29 02:01 1760用pman 可以看出application controlle ... -
epmd源码学习
2007-07-26 10:14 2046注: 此处节点是指分布式中分布在各终端的点, 而结点是指存在数 ... -
Tracing和dbg
2007-07-15 21:49 2573代码不必用特殊的标记(比如debug_info)来编译,也可以 ... -
ets,dets与大数据存储
2007-07-15 12:49 4970ets与dets都是用来存大数据的机制 ets是Erl ... -
用telnet来与ejabberd交互
2007-07-11 15:41 3242看了一篇文章,觉得用telnet来调试ejabberd也是一种 ... -
ejabberd管理页面和客户端
2007-07-11 00:23 9784转战到97机器。在ejabber.config加上这么一行. ... -
ejabberd在linux平台的安装与配置
2007-07-05 21:17 11971这些天捣鼓了下ejabberd,准备研究它的代码,做为榜样~ ... -
mnesia相关笔记
2007-06-29 12:17 2361当前版本OTP 5.5的mensia建表的表名可以和记录名不一 ... -
OTP设计原则:应用
2007-06-27 00:32 19647 Applications This chapter sh ... -
erlang网络编程的几个性能调优和注意点
2007-06-26 09:56 17882前些天给echo_server写了 ... -
erlc
2007-06-24 15:08 3854erlc 命令 erlc 概要 编译器 描述 Th ... -
echo_server
2007-06-23 14:45 2470代码 -module(echo_server ... -
OTP设计原则:Supervisor行为
2007-06-22 12:15 27585 Supervisor Behaviour This s ... -
OTP设计原则:Gen_Event 行为
2007-06-22 11:59 20414 Gen_Event 行为 这一章应该与gen_event ...
相关推荐
Erlang OTP(Open Telephony Platform)是Erlang编程语言的一个核心部分,它提供了...通过深入理解这些文档和概念,开发者能够构建出符合OTP原则的、高度可靠的Erlang系统,有效地处理并发、故障恢复和系统扩展性问题。
1. **服务器端代码**:包含gen_server行为的实现,用于处理连接、消息传递和状态管理。 2. **客户端代码**:可能使用gen_tcp来建立与服务器的连接,并提供用户界面,让用户输入和显示聊天内容。 3. **通信协议**:...
通过遵循这些步骤和最佳实践,我们可以构建一个符合OTP原则的非阻塞TCP服务器,它具有高可用性、容错能力和良好的可扩展性。对于不熟悉OTP的开发者,建议阅读相关的教程和文档,以深入理解其设计理念和用法。
它包含了一组设计原则和库模块,如 supervision trees(监督树)、gen_server行为、gen_event行为等,为开发者提供了一种结构化的方式来处理并发和错误恢复。 1. **Supervision Trees**:OTP的核心概念之一是监督树...
### OTP设计原则详解 #### 概览 OTP(Open Telecom Platform)是Erlang/OTP框架的核心组成部分之一,它提供了一套成熟的、可扩展的、容错的应用程序设计模式。OTP设计原则指导开发者如何构建稳定可靠的分布式系统...
文档详细讲解了Gen_Server行为模式,其中包括如何启动和停止Gen_Server,处理同步(Call)和异步(Cast)调用,以及在监督树中的集成。Gen_Fsm行为介绍了有限状态机的基本原理以及如何使用Gen_Fsm来管理状态变化和...
7. **行为模块**:如gen_server、gen_event、gen_fsm等,是OTP设计模式的具体实现,简化了编写服务器、事件处理器和有限状态机的代码。 关于压缩包内的"otp_src_17.3",这是Erlang OTP 17.3版本的源代码目录。为了...
gen_server行为是Erlang OTP设计模式之一,用于创建有状态的服务进程,而Elixir的GenServer是Erlang gen_server的封装,提供了更友好的Elixir语法和API。 批处理服务器的主要功能包括: 1. **任务调度**:gen-...
例如Gen_Server、Gen_Fsm、Gen_Event和Supervisor等。每个行为模式都有其通用部分和特定部分。通用部分由Erlang/OTP库提供,开发者只需要专注于实现特定部分,即回调模块,以及根据需要导出特定的回调函数。 3. ...
10. **系统架构**:OTP鼓励使用微服务和模块化的设计,手册将指导如何组织和构建符合OTP原则的系统架构。 这份离线手册的R14B03版本可能相对较旧,但依然具有很高的参考价值,尤其是对于初学者和在旧系统上工作的...
5. **Supervisor和gen_server**:OTP的监控和行为模式,帮助构建有弹性的系统,能够自动检测和恢复错误。 6. **Distributed OTP**:支持Erlang节点间的通信和分布式应用,确保即使在网络故障或节点失败后,系统也能...
例如,Gen_Server和Gen_Fsm都是Erlang OTP提供的行为模式,它们定义了处理消息的基本结构,并要求开发者实现与具体逻辑相关的回调函数。 应用(Application)在Erlang/OTP中是一个封装了代码、模块、资源文件和配置...
- **Gen_Server 行为**:文档中未给出具体细节,但通常涉及服务器进程的设计和实现。 - **Gen_Fsm 行为** - **有限状态机**:介绍了如何使用Gen_Fsm实现状态机逻辑。 - **示例**:提供了Gen_Fsm的具体用法示例。 ...
- **行为(Behaviours)**:如gen_server、gen_event和gen_fsm等,定义了标准的服务器、事件管理和有限状态机的行为模式,为编写可靠服务提供模板。 - **应用(Applications)**:Erlang程序组织成应用,每个应用有...
#### 六、OTP设计原则 **2.1 概述** - **OTP (Open Telecom Platform)**: Erlang的一个高级框架,提供了构建可扩展、容错系统的一套设计模式和工具集。 - **组件**: - 监督树 (Supervision Trees): 管理进程的...
### OTP设计原则 #### 2.1 概述 - **2.1.1 监督树**:介绍了Erlang OTP框架中的监督树概念。 - **2.1.2 Behaviour**:行为模块是用于编写特定类型进程的模板。 - **2.1.3 应用**:解释如何将模块和行为组合成应用...
2. **模块(`.erl`)**:实际实现功能的代码,可以是普通的函数模块,也可以是行为模块(如gen_server,gen_event等)。 3. **启动脚本(`.boot`)**:指定启动应用时的初始状态,包括启动哪些进程及其顺序。 4. *...
1. **gen_server行为模块**:Timekeeper可能实现了一个gen_server行为,这是一个标准的Erlang OTP服务器模板,用于处理客户端的请求并保持内部状态。gen_server提供了一种结构化的方式来处理异步请求,更新状态,并...