Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
F
freeswitch
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
张华
freeswitch
Commits
e83af319
提交
e83af319
authored
3月 26, 2011
作者:
Josh Perry
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Updated message creation
上级
2b1793ed
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
44 行增加
和
18 行删除
+44
-18
mod_event_zmq.cpp
src/mod/event_handlers/mod_event_zmq/mod_event_zmq.cpp
+44
-18
没有找到文件。
src/mod/event_handlers/mod_event_zmq/mod_event_zmq.cpp
浏览文件 @
e83af319
#include <switch.h>
#include <zmq.hpp>
#include <string>
#include <exception>
#include <stdexcept>
#include <memory>
...
...
@@ -15,13 +14,6 @@ extern "C" {
SWITCH_MODULE_DEFINITION
(
mod_event_zmq
,
load
,
shutdown
,
runtime
);
};
class
ZmqStringMessage
:
public
zmq
::
message_t
{
public
:
ZmqStringMessage
(
const
std
::
string
&
msg
)
{
}
};
// Handles publishing events out to clients
class
ZmqEventPublisher
{
public
:
...
...
@@ -29,19 +21,28 @@ public:
context
(
1
),
event_publisher
(
context
,
ZMQ_PUB
)
{
event_publisher
.
bind
(
"tcp://*.5556"
);
event_publisher
.
bind
(
"tcp://*:5556"
);
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_DEBUG
,
"Listening for clients
\n
"
);
}
void
PublishEvent
(
const
switch_event_t
*
event
)
{
// Serialize the event into a JSON string
char
*
pjson
;
switch_event_serialize_json
(
const_cast
<
switch_event_t
*>
(
event
),
&
pjson
);
std
::
auto_ptr
<
char
>
json
(
pjson
);
ZmqStringMessage
msg
(
json
.
get
());
// Use the JSON string as the message body
zmq
::
message_t
msg
(
pjson
,
strlen
(
pjson
),
free_message_data
,
NULL
);
// Send the message
event_publisher
.
send
(
msg
);
}
private
:
static
void
free_message_data
(
void
*
data
,
void
*
hint
)
{
free
(
data
);
}
zmq
::
context_t
context
;
zmq
::
socket_t
event_publisher
;
};
...
...
@@ -49,25 +50,39 @@ private:
// Handles global inititalization and teardown of the module
class
ZmqModule
{
public
:
ZmqModule
(
switch_loadable_module_interface_t
**
module_interface
,
switch_memory_pool_t
*
pool
)
{
ZmqModule
(
switch_loadable_module_interface_t
**
module_interface
,
switch_memory_pool_t
*
pool
)
:
_running
(
false
)
{
// Subscribe to all switch events of any subclass
// Store a pointer to ourself in the user data
if
(
switch_event_bind_removable
(
modname
,
SWITCH_EVENT_ALL
,
SWITCH_EVENT_SUBCLASS_ANY
,
event_handler
,
(
void
*
)
this
,
&
_node
)
!=
SWITCH_STATUS_SUCCESS
)
{
throw
std
::
runtime_error
(
"Couldn't bind to switch events."
);
}
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_DEBUG
,
"Subscribed to events
\n
"
);
// Create our module interface registration
*
module_interface
=
switch_loadable_module_create_module_interface
(
pool
,
modname
);
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_DEBUG
,
"Module loaded
\n
"
);
}
void
Listen
()
{
if
(
_running
)
return
;
_publisher
.
reset
(
new
ZmqEventPublisher
());
_running
=
true
;
while
(
_running
)
{
switch_yield
(
100000
);
}
}
~
ZmqModule
()
{
// Unsubscribe from the switch events
_running
=
false
;
switch_event_unbind
(
&
_node
);
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_DEBUG
,
"Module shut down
\n
"
);
}
private
:
...
...
@@ -79,11 +94,14 @@ private:
module
->
_publisher
->
PublishEvent
(
event
);
}
catch
(
std
::
exception
ex
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_WARNING
,
"Error publishing event via 0MQ: %s
\n
"
,
ex
.
what
());
}
catch
(...)
{
// Exceptions must not propogate to C caller
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_WARNING
,
"Unknown error publishing event via 0MQ
\n
"
);
}
}
switch_event_node_t
*
_node
;
std
::
auto_ptr
<
ZmqEventPublisher
>
_publisher
;
bool
_running
;
};
//*****************************//
...
...
@@ -99,8 +117,8 @@ SWITCH_MODULE_LOAD_FUNCTION(load) {
try
{
module
.
reset
(
new
ZmqModule
(
module_interface
,
pool
));
return
SWITCH_STATUS_SUCCESS
;
}
catch
(
const
std
::
exception
&
ex
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Error loading 0MQ module
: %s
\n
"
,
ex
.
what
()
);
}
catch
(
...)
{
// Exceptions must not propogate to C caller
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Error loading 0MQ module
\n
"
);
return
SWITCH_STATUS_GENERR
;
}
...
...
@@ -110,15 +128,23 @@ SWITCH_MODULE_RUNTIME_FUNCTION(runtime) {
try
{
// Begin listening for clients
module
->
Listen
();
}
catch
(...)
{
}
}
catch
(
std
::
exception
&
ex
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Error listening for clients: %s
\n
"
,
ex
.
what
());
}
catch
(...)
{
// Exceptions must not propogate to C caller
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Unknown error listening for clients
\n
"
);
}
// Tell the switch to stop calling this runtime loop
return
SWITCH_STATUS_
FALSE
;
return
SWITCH_STATUS_
TERM
;
}
SWITCH_MODULE_SHUTDOWN_FUNCTION
(
shutdown
)
{
// Free the module object
module
.
reset
();
try
{
// Free the module object
module
.
reset
();
}
catch
(...)
{
// Exceptions must not propogate to C caller
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Error shutting down module
\n
"
);
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论