Ignore:
Timestamp:
Apr 2, 2007 11:23:09 AM (17 years ago)
Author:
bennylp
Message:

More work on stateful proxy sample

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/branches/pjproject-0.5-stable/pjsip-apps/src/samples/proxy.h

    r1121 r1123  
    5454    puts("Options:\n" 
    5555         "\n" 
    56          " -p, --port N    Set local listener port to N\n" 
    57          " -R, --rr        Perform record routing\n" 
    58          " -h, --help      Show this help screen\n" 
     56         " --port N       Set local listener port to N\n" 
     57         " --rr           Perform record routing\n" 
     58         " --log-level N  Set log level to N (default: 4)\n" 
     59         " --help         Show this help screen\n" 
    5960         ); 
    6061} 
     
    6465{ 
    6566    struct pj_getopt_option long_opt[] = { 
    66         { "port",1, 0, 'p'}, 
    67         { "rr",  1, 0, 'R'}, 
    68         { "help",1, 0, 'h'}, 
     67        { "port",       1, 0, 'p'}, 
     68        { "rr",         0, 0, 'R'}, 
     69        { "log-level",  1, 0, 'L'}, 
     70        { "help",       0, 0, 'h'}, 
    6971    }; 
    7072    int c; 
     
    7274 
    7375    pj_optind = 0; 
    74     while((c=pj_getopt_long(argc, argv, "pRh", long_opt, &opt_ind))!=-1) { 
     76    while((c=pj_getopt_long(argc, argv, "", long_opt, &opt_ind))!=-1) { 
    7577        switch (c) { 
    7678        case 'p': 
     
    8486            break; 
    8587 
     88        case 'L': 
     89            pj_log_set_level(atoi(pj_optarg)); 
     90            break; 
     91 
    8692        case 'h': 
    8793            usage(); 
     
    8995 
    9096        default: 
    91             puts("Unknown option ignored"); 
    92             break; 
     97            puts("Unknown option. Run with --help for help."); 
     98            return -1; 
    9399        } 
    94100    } 
     
    96102    return PJ_SUCCESS; 
    97103} 
     104 
     105 
     106/***************************************************************************** 
     107 * This is a very simple PJSIP module, whose sole purpose is to display 
     108 * incoming and outgoing messages to log. This module will have priority 
     109 * higher than transport layer, which means: 
     110 * 
     111 *  - incoming messages will come to this module first before reaching 
     112 *    transaction layer. 
     113 * 
     114 *  - outgoing messages will come to this module last, after the message 
     115 *    has been 'printed' to contiguous buffer by transport layer and 
     116 *    appropriate transport instance has been decided for this message. 
     117 * 
     118 */ 
     119 
     120/* Notification on incoming messages */ 
     121static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata) 
     122{ 
     123    PJ_LOG(5,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n" 
     124                         "%.*s\n" 
     125                         "--end msg--", 
     126                         rdata->msg_info.len, 
     127                         pjsip_rx_data_get_info(rdata), 
     128                         rdata->tp_info.transport->type_name, 
     129                         rdata->pkt_info.src_name, 
     130                         rdata->pkt_info.src_port, 
     131                         (int)rdata->msg_info.len, 
     132                         rdata->msg_info.msg_buf)); 
     133     
     134    /* Always return false, otherwise messages will not get processed! */ 
     135    return PJ_FALSE; 
     136} 
     137 
     138/* Notification on outgoing messages */ 
     139static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata) 
     140{ 
     141     
     142    /* Important note: 
     143     *  tp_info field is only valid after outgoing messages has passed 
     144     *  transport layer. So don't try to access tp_info when the module 
     145     *  has lower priority than transport layer. 
     146     */ 
     147 
     148    PJ_LOG(5,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n" 
     149                         "%.*s\n" 
     150                         "--end msg--", 
     151                         (tdata->buf.cur - tdata->buf.start), 
     152                         pjsip_tx_data_get_info(tdata), 
     153                         tdata->tp_info.transport->type_name, 
     154                         tdata->tp_info.dst_name, 
     155                         tdata->tp_info.dst_port, 
     156                         (int)(tdata->buf.cur - tdata->buf.start), 
     157                         tdata->buf.start)); 
     158 
     159    /* Always return success, otherwise message will not get sent! */ 
     160    return PJ_SUCCESS; 
     161} 
     162 
     163/* The module instance. */ 
     164static pjsip_module mod_msg_logger =  
     165{ 
     166    NULL, NULL,                         /* prev, next.          */ 
     167    { "mod-msg-logger", 14 },           /* Name.                */ 
     168    -1,                                 /* Id                   */ 
     169    PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority            */ 
     170    NULL,                               /* load()               */ 
     171    NULL,                               /* start()              */ 
     172    NULL,                               /* stop()               */ 
     173    NULL,                               /* unload()             */ 
     174    &logging_on_rx_msg,                 /* on_rx_request()      */ 
     175    &logging_on_rx_msg,                 /* on_rx_response()     */ 
     176    &logging_on_tx_msg,                 /* on_tx_request.       */ 
     177    &logging_on_tx_msg,                 /* on_tx_response()     */ 
     178    NULL,                               /* on_tsx_state()       */ 
     179 
     180}; 
    98181 
    99182 
     
    142225    global.pool = pj_pool_create(&global.cp.factory, "proxyapp",  
    143226                                 4000, 4000, NULL); 
     227 
     228    /* Register the logger module */ 
     229    pjsip_endpt_register_module(global.endpt, &mod_msg_logger); 
    144230 
    145231    return PJ_SUCCESS; 
     
    413499 
    414500    /* We're not interested to receive request destined to us, so 
    415      * respond with 404/Not Found. 
    416      */ 
    417     pjsip_endpt_respond_stateless(global.endpt, rdata, 
    418                                   PJSIP_SC_NOT_FOUND, NULL, 
    419                                   NULL, NULL); 
     501     * respond with 404/Not Found (only if request is not ACK!). 
     502     */ 
     503    if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) { 
     504        pjsip_endpt_respond_stateless(global.endpt, rdata, 
     505                                      PJSIP_SC_NOT_FOUND, NULL, 
     506                                      NULL, NULL); 
     507    } 
    420508 
    421509    /* Delete the request since we're not forwarding it */ 
Note: See TracChangeset for help on using the changeset viewer.