#
# $Id$
#
# jsonrpc module testing script

debug = 2
memlog=1   # debug level is higher or equal memlog
mem_summary=8
log_stderror=yes
fork=no
children=1
listen = 127.0.0.1:5060

check_via=no    # (cmd. line: -v)
dns=yes          # (cmd. line: -r)
rev_dns=no      # (cmd. line: -R)
sip_warning=yes

# ------------------ module loading ----------------------------------

loadpath "modules:modules_k"

loadmodule "pv"
loadmodule "tm"
loadmodule "json"
loadmodule "xlog"
loadmodule "jsonrpc-c"

#modparam("jsonrpc", "min_srv_ttl", 1);
modparam("jsonrpc", "result_pv", "$var(foobar)");
modparam("jsonrpc", "server", "conn=pingpong;addr=foobar;port=7080;priority=10;weight=0;hwm=1");
#modparam("jsonrpc", "server", "conn=pingpong;addr=localhost;port=7081;priority=10;weight=0;hwm=10");
#modparam("jsonrpc", "server", "conn=pingpong;addr=localhost;port=7082;priority=10;weight=0;hwm=10");
#modparam("jsonrpc", "server", "conn=pingpong;addr=localhost;port=7083;priority=10;weight=0;hwm=10");
#modparam("jsonrpc", "server", "conn=pingpong;addr=localhost;port=7084;priority=10;weight=0;hwm=1");
#modparam("jsonrpc", "server", "conn=fail;addr=localhost;port=9999;priority=10;weight=0");
#modparam("jsonrpc", "server", "conn=test0;srv=_test0._tcp.sandbox.internal");
#modparam("jsonrpc", "server", "conn=test0;srv=_test1._tcp.sandbox.internal");
#modparam("jsonrpc", "server", "conn=tests;srv=_test2._tcp.sandbox.internal");


# ------------------- unit tests ------------------------------------

route {

    jsonrpc_request("test0", "echo", '[{"foo":"bar"}]', "route=RESPONSE;retry=10;timeout=30");

    xlog("done\n");
}

route[RESPONSE] {
    if(json_get($var(jsrpc_result), "internal_error", "$var(internal)")) {
        route(INTERNAL);
    } else if(json_get($var(jsrpc_result), "error", "$var(error)")) {
        route(ERROR);
    } else if(json_get($var(jsrpc_result), "result", "$var(result)")) {
        route(RESULT);
    }
    t_reply("200", "OK");
}

route[RESULT] {
    xlog("result is $var(result)\n");
    xlog("success\n");
}

route[ERROR] {
    xlog("There was an error\n");
    if(json_get($var(error), "code", "$var(c)")) {
        xlog("code is $var(c)\n");
    }

    if(json_get($var(error), "message", "$var(r)")) {
        xlog("error is $var(r)\n");
    }

    if(json_get($var(error), "data", "$var(d)")) {
        xlog("data is $var(d)\n");
    }
}

route[INTERNAL] {
    xlog("There was an internal error\n");

    json_get($var(internal), "code", "$var(c)");
    xlog("code is $var(c)\n");

    json_get($var(internal), "message", "$var(r)");
    xlog("error is $var(r)\n");

    if(json_get($var(internal), "data", "$var(d)")) {
        xlog("request is $var(d)\n");
    }
}