星号中的普通文件 I/O 操作

发布于 2024-10-03 14:10:17 字数 226 浏览 11 评论 0原文

我正在使用星号来实现语音服务器。我有以下两个问题:

  1. 我希望能够读/写文件以赋予一些附加功能。有没有办法在星号中做到这一点?我注意到用于写入输出的正常函数(例如 cout 等)被抑制。

  2. 有没有办法调试用星号编写的应用程序?我的意思是像星号的 gdb 这样的东西?

非常欢迎对上述问题的任何帮助。
谢谢,
斯里拉姆。

I am using asterisk to implement a voice server. I have the following two questions:

  1. I want to be able to read/write files to impart some additional functionality. Is there a way to do so in asterisk? I noticed that normal functions such as cout etc. for writing output is suppressed.

  2. Is there a way to debug applications written in asterisk? I mean something like a gdb for asterisk?

Any help on the above questions is most welcome.
Thanks,
Sriram.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〃温暖了心ぐ 2024-10-10 14:10:17

我注意到您在对 Michael 的回答的评论中说您正在使用 C / C++。

你绝对可以使用 C 或 C++(或任何语言)在 Asterisk 中执行文件 I/O。 Asterisk 支持四种主要类型的应用程序:

拨号方案:默认的 Asterisk 脚本语言。 extensions.conf 中的任何代码都是拨号计划。您可以使用这种语言来控制入站呼叫,并执行一些操作。对于文件 I/O,拨号方案提供了一个 System 命令以及一个 AGI 命令,它们都允许您运行其他软件(例如 bash 脚本、C 二进制文件) ,真的什么都可以)。然后,在 bash 脚本、C 程序等中,您可以执行正常的文件 I/O,就像您自己在命令行上运行程序一样。 EX:

[some_context]
exten => s,1,System(/bin/echo "hello, world" >> /tmp/test.txt)
exten => s,n,Return()

[launch_an_agi]
exten => s,1,AGI(/var/lib/asterisk/agi-bin/my_c_program,optional_cmd_line_arguments,to,pass)
exten => s,n,Hangup()

AGI:Asterisk AGI(从拨号计划代码启动)允许您运行外部程序(C 程序或其他程序),并向您的 C 程序传递一些有关当前正在处理的呼叫的 Asterisk 变量,您的程序可以使用这些变量。您可以在这里使用普通的编码工具来执行您想要的任何文件 I/O,例如:

/* some_c_agi_program */
include <stdio.h>
int main(void) {
    FILE *f = fopen("/tmp/test.txt", "w");
    (void)fprintf(f, "MY FILE IO WORKS! THANKS STACKOVERFLOW!\n");
    fclose(f);
    return 0;
}

AMI:AMI 是 Asterisk 提供的网络 API。您可以使用它来编写在另一台服务器上运行的程序,这些程序可以创建、控制和显示 Asterisk 服务器上的呼叫状态。这个想法是通过 TCP 套接字连接到程序中的星号服务器,然后发送命令并读回响应。我不打算展示一个例子,因为这将是一个巨大的 C 程序。但你明白了。

现在,进行调试!

根据您正在编写的应用程序的类型,有多种调试方法。如果您熟悉单元测试的概念,则可以编写单元测试,并确保针对我将要描述的任何方法运行它们。对于通用调试(如果您正在编写 AGI 或 AMI 程序),可以使用普通的 C 调试器 (GDB)。

I noticed that in your comment on Michael's answer, you said you were using C / C++.

You can definitely do file I/O in Asterisk using C or C++ (or any language really). Asterisk supports four primary types of applications:

Dial Plan: The default Asterisk scripting language. Any code that's in extensions.conf is dial plan. You can use this language to control inbound calls, and do stuff. For file I/O, dial plan provides a System command, as well as an AGI command which both allow you to run other software (like a bash script, a C binary, anything really). Then in your bash script, C program, etc., you can just do normal file I/O as if you were running the program on the command line yourself. EX:

[some_context]
exten => s,1,System(/bin/echo "hello, world" >> /tmp/test.txt)
exten => s,n,Return()

[launch_an_agi]
exten => s,1,AGI(/var/lib/asterisk/agi-bin/my_c_program,optional_cmd_line_arguments,to,pass)
exten => s,n,Hangup()

AGI: The Asterisk AGI (launched from dial plan code) lets you run external programs (a C program or whatever), and passes your C program some Asterisk variables about the call which is currently being processed, that your program can use. You can use your normal coding tools here to do any file I/O you want, eg:

/* some_c_agi_program */
include <stdio.h>
int main(void) {
    FILE *f = fopen("/tmp/test.txt", "w");
    (void)fprintf(f, "MY FILE IO WORKS! THANKS STACKOVERFLOW!\n");
    fclose(f);
    return 0;
}

AMI: The AMI is a network API that Asterisk provides. You can use this to write programs that run on another server, that can create, control, and show you the status of calls on an Asterisk server. The idea is that you connect over a TCP socket to the asterisk server in your program, then send commands and read back the responses. I'm not going to show an example, because it would be a huge program in C. But you get the idea.

Now, onto debugging!

Depending on the type of application you're writing, there are various ways to debug. If you're familiar with the concept of unit testing, you can write unit tests, and make sure that they are ran for any of the methods I'm about to describe. For general purpose debugging (if you're writing an AGI or AMI program), your normal C debugger (GDB) can be used.

回忆那么伤 2024-10-10 14:10:17

也许我错过了一些东西,但它是否需要比 Asterisk 自己内置的 FILE 功能?

示例:

读取文件的全部内容。

Set(foo=${FILE(/tmp/test.txt)})

读取文件的第3行。

Set(foo=${FILE(/tmp/test.txt,3,1,l)})

将文件的最后一行替换为“cat”

Set(FILE(/tmp/foo.txt,-1,,l)=cat)

将“dog”添加到带有换行符的文件中 拨号

Set(FILE(/tmp/foo.txt,,,al)=dog)

方案示例:

exten => 800,n,Set(FILE(/tmp/cid.txt,,,al)=${CALLERID(num)})

Perhaps I'm missing something, but does it need to be any more complex than Asterisk's own built in FILE function?

Examples:

reads the entire content of the file.

Set(foo=${FILE(/tmp/test.txt)})

reads the 3rd line of the file.

Set(foo=${FILE(/tmp/test.txt,3,1,l)})

Replace the last line of the file with "cat"

Set(FILE(/tmp/foo.txt,-1,,l)=cat)

Append "dog" to the file with a newline

Set(FILE(/tmp/foo.txt,,,al)=dog)

Dialplan example:

exten => 800,n,Set(FILE(/tmp/cid.txt,,,al)=${CALLERID(num)})
但可醉心 2024-10-10 14:10:17
  1. 你使用AGI吗?当然,使用 AGI,您可以读取/写入文件。

  2. 您可以使用-vvvvvvv命令行选项使星号变得详细。更多 v 使星号更加详细。对我来说,调试拨号计划就足够了。如果我想调试 AGI,那么我会使用日志记录到某个日志文件,或者对于更简单的任务,我使用假 agi_lib(对于真正的 AGI 不可用的单元测试很有用)。

  1. Do you use AGI? Of course with AGI you can read/write files.

  2. You can use -vvvvvvv command line option to make asterisk verbose. More v make asterisk more verbose. For me it was enough to debug dialplans. If I wanted to debug AGI, then I used logging to some log file, or for simplier tasks I used fake agi_lib (good for unit tests where true AGI is not available).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文