2019年1月20日星期日

go笔记1-递归算法

最近开始学习go,找个地方做点笔记。
1.递归算法实现99乘法表
package main

import (
   "fmt")

func main ()  {
   fmt.Println(table99(9))
   }


func middlep(a int) (res string)  {
   fmt.Println("")
   return}


func table99(i int ) (res string) {
   if i>=1 {
      table99(i-1)
      defer middlep(i)
      for j:=1;j<=i;j++ {
         fmt.Printf("%d*%d=%d\t",i,j,i*j)
      }
   }
   return}
2.递归实现阶乘
package main

import "fmt"
func main()  {
   fmt.Println(fib1(4))
}

func fib1(n int) (res int)  {
   if n == 0 {
      res = 1   } else {
      res = n * fib1(n-1)
   }
   return}
3.递归实现斐波那契数列
package main

import "fmt"
func main ()  {
   for i:= 1; i<=10 ;i++  {
      fmt.Println(i, fib(i))
   }
}


func fib(n int) (res int)  {
   if n <=1{
      res = n
   } else {
      res = fib(n-1) + fib(n-2)
   }
   return}

2012年9月28日星期五

关于freeswitch运营的一点想法

freeswitch默认sip帐号认证是读xml文件,一个帐号一个文件,当帐号到达10w级别,效率那是大大滴差,即使有mod_xml_curl模块,个人觉得从运营角度考虑还是不靠谱。应该有专门的sip认证服务器,freeswitch作为媒体服务器,同时通过event_socket实现计费等定制功能。长假期间,打算测试下opensips+freeswitch的组合。

2012年9月27日星期四

freeswitch之nibble模块安装与测试

环境centos 5.7 64bit
1.先安装unixODBC相关包,fs在编译的时候发现有odbc相关的lib,就会自动编译进去。
yum install unixODBC-devel mysql-connector-odbc64
2.安装fs
安装之间先安装相关依赖软件
yum install autoconf automake gcc-c++ git-core libjpeg-devel libtool make ncurses-devel pkgconfig
yum install unixODBC-devel openssl-devel libogg-devel libvorbis-devel curl-devel libtiff-devel libjpeg-devel python-devel expat-devel zlib zlib-devel bzip2 which
获取fs的最新源代码
cd /usr/local/src/
git clone git://git.freeswitch.org/freeswitch.git
cd freeswitch
./bootstrap.sh
./configure
Edit modules.conf
applications/mod_nibblebill前面的#去掉,其他模块根据需要选择安装。
make && make install
安装声音文件
make sounds-install
make moh-install

ps:安装过程中有问题自行google解决。

3.测试UNIXodbc是否安装正确
Uncomment the MySQL sample driver confirguration in /etc/odbcinst.ini
此文件包含4种驱动配置,mysql,mysql64,postgresql,postgresql64
我用的是mysql 64:

[MySQL64]
Description = ODBC for MySQL (64 bit)
Driver = /usr/lib/libmyodbc5.so
Setup = /usr/lib/libodbcmyS64.so
Driver64 = /usr/lib64/libmyodbc5.so
Setup64 = /usr/lib64/libodbcmyS64.so
FileUsage = 1
四个文件都存在即可。

vi /etc/odbc.ini加入如下配置
[freeswitch]
Driver   = MySQL64
SERVER   = localhost
PORT     = 3306
DATABASE = freeswitch
OPTION  = 67108864
Socket   = /var/lib/mysql/mysql.sock

安装mysql相关,需要提醒的是yum上的mysqlclient15好像有bug,不能支持odbc。
可以安装这个代替http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/x86_64/mysqlclient15-5.0.92-3.ius.el5.x86_64.rpm


mysql中新建数据库freeswitch,赋予帐号权限。

[root@fs freeswitch]# isql freeswitch root 123456
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL> 
能看到如上显示,说明odbc配置成功!!!


4.fs中相关配置
vi /usr/local/freeswitch/conf/autoload_configs/modules.conf.xml
启用<load module="mod_nibblebill"/>

vi /usr/local/freeswitch/conf/autoload_configs/spidermonkey.conf.xml
启用<load module="mod_spidermonkey_odbc"/>

vi /usr/local/freeswitch/conf/autoload_configs/switch.conf.xml
<param name="core-db-dsn" value="freeswitch:root:123456" />

vi /usr/local/freeswitch/conf/dialplan/default.xml
<include>
  <context name="default">

<extension name="call_out">
    <condition field="destination_number" expression="^0(\d+)$">
<action application="set" data="nibble_rate=0.05"/>
        <action application="set" data="$${nibble_account}"/>
<!--<action application="socket" data="127.0.0.1:8088 async full"/>-->
        <action application="bridge" data="sofia/gateway/gw1/$1"/>
<!--<action application="bridge" data="sofia/gateway/gw2/$1"/>-->
    </condition>
    </extension>

gw1落地配置不再说明。

根据数据库信息自己修改:
vi /usr/local/freeswitch/conf/autoload_configs/nibblebill.conf.xml
<!-- Information for connecting to your database -->
    <param name="db_username" value="root"/>
    <param name="db_password" value="123456"/>
    <param name="db_dsn" value="freeswitch"/>

    <!-- The database table where your CASH column is located -->
    <param name="db_table" value="accounts"/>

    <!-- The column name where we store the value of the account -->
    <param name="db_column_cash" value="cash"/>

    <!-- The column name for the unique ID identifying the account -->
    <param name="db_column_account" value="id"/>

vi /usr/local/freeswitch/conf/directory/default/1001.xml
增加
  <variable name="nibble_rate" value="0.05"/>
  <variable name="nibble_account" value="1001"/>


建立个供计费的表
 CREATE TABLE accounts
 (
   id int NOT NULL PRIMARY KEY,
   name VARCHAR(255),
   cash double precision NOT NULL
 );

加入一条数据,id=1001 name=任意 cash=金额

运行freeswitch。

eyebeam用1001登录,拨打外线。


fs_cli中看到如下信息,并且被叫响铃,说明计费正常。

2012-09-27 03:44:33.496220 [DEBUG] mod_nibblebill.c:453 Attempting to bill at $0.05 per minute to account 1001
2012-09-27 03:44:33.496220 [DEBUG] mod_nibblebill.c:465 Not billing 1001 - call is not in answered state
2012-09-27 03:44:33.496220 [DEBUG] mod_nibblebill.c:383 Doing lookup query
[SELECT cash AS nibble_balance FROM accounts WHERE id='1001']
2012-09-27 03:44:33.496220 [DEBUG] mod_nibblebill.c:393 Retrieved current balance for account 1001 (balance = 99.985383)
2012-09-27 03:44:33.496220 [DEBUG] mod_nibblebill.c:469 Comparing 99.985383 to hangup balance of 0.000000
2012-09-27 03:44:33.496220 [DEBUG] mod_nibblebill.c:383 Doing lookup query
[SELECT cash AS nibble_balance FROM accounts WHERE id='1001']
2012-09-27 03:44:33.496220 [DEBUG] mod_nibblebill.c:393 Retrieved current balance for account 1001 (balance = 99.985383)

接着你可以测试各种情况,比如cash=0.000.1,看电话还能不能打,能打多久,等等,留着给你慢慢测试吧!!!

以上只是最基础的nibble的计费功能,后续再继续研究,不得不说,freeswitch博大精深啊。

freeswitch详细说明请参考http://wiki.freeswitch.org/wiki/Mod_nibblebill






2012年9月26日星期三

实验帖

安家于此。