view doc/dbg_interactive.py.sample @ 629:7ae66129fd73 1.0.3-rc1

Prepare for upcoming 1.0.3
author Sebastien Decugis <sdecugis@nict.go.jp>
date Tue, 14 Dec 2010 11:54:04 +0900
parents 330be61dbf43
children 134e4fb9eef5
line wrap: on
line source

# Example file for the dbg_interactive.fdx extension.
#
# This extension provides an interactive python interpreter console that allows
# interacting with freeDiameter framework.
#
# The adaptation layer between Python and C is provided by SWIG (http://swig.org). 
# You may refer to SWIG documentation for more information on how the wrapper is generated and used.
# The name of the module wrapping freeDiameter framework is: _fDpy
#
# Similar to all freeDiameter extensions, an optional filename can be specified in the
# main freeDiameter.conf configuration file for the dbg_interactive.fdx extension.
# If such file is provided, it will be passed to the python interpreter as a python script
# to execute. Otherwise, the interpreter will be interactive.
#
# SWIG deals with structures as follow:
# Given the structure:
# struct foo { int a; }
# The following functions are available to python (their C equivalent processing is given in [ ]):
# s = new_foo()	   [ s = calloc(1, sizeof(struct foo)) ]
# foo_a_set(s, 2)  [ s->a = 2 ]
# foo_a_get(s)     [ returns s->a value ]
# delete_foo(s)    [ free(s)  ]
#
# In addition, thanks to the proxy (aka shadow) class, we can also do the more user-friendly:
# s = foo()
# s.a = 2
# s.a
# del s
#

# The remaining of this file gives some examples of how to use the python interpreter.
# Note that the support is not yet totally usable. You'll probably have to extend some classes
# or write some typemaps in the source code of the extension to do what you want.


############# Compilation-time constants (from freeDiameter-host.h) ############

# Display current version
print "%s %d.%d.%d" % (FD_PROJECT_NAME, FD_PROJECT_VERSION_MAJOR, FD_PROJECT_VERSION_MINOR, FD_PROJECT_VERSION_REV)


############# Debug ############

# Change the global debug level of the framework (cvar contains all global variables)
cvar.fd_g_debug_lvl = FULL


# Turn on debug for a specific function (if framework compiled with DEBUG support)
cvar.fd_debug_one_function = "gc_th_fct"


# Print messages to freeDiameter's debug facility
# Note: the python version does not support printf-like argument list. The formating should be done in python.
#       See SWIG documentation about varargs functions for more information.
fd_log_debug("3 + 4 = %d\n" % (7))


# Display some framework state information
r = fd_event_send(cvar.fd_g_config.cnf_main_ev, FDEV_DUMP_PEERS, 0, None)
r = fd_event_send(cvar.fd_g_config.cnf_main_ev, FDEV_DUMP_SERV, 0, None)
r = fd_event_send(cvar.fd_g_config.cnf_main_ev, FDEV_DUMP_EXT, 0, None)


############# Global variables ############

# Display the local Diameter Identity:
print "Local Diameter Identity:", cvar.fd_g_config.cnf_diamid
# Display realm, without using the low-level functions (skip proxy classe definitions):
print "Realm:", _fDpy.fd_config_cnf_diamrlm_get(_fDpy.cvar.fd_g_config)



############# Lists ############
l1 = fd_list()   # The creator has an implicit fd_list_init call
l2 = fd_list()
fd_list_insert_after(l1, l2)
l1.dump()
del l2		 # The destructor has an implicit fd_list_unlink call
l1.dump()
del l1


############# Hash ############
hex(fd_hash("hello world"))	# A typemap is applied to accept binary data


############# Dictionary ############

# Create a dedicated dictionary for our tests
d = dictionary()
d.dump()

# New vendor
v = dict_vendor_data()
v.vendor_id = 123
v.vendor_name = "My test vendor"
r, my_vendor = fd_dict_new(d, DICT_VENDOR, v, None)
del v
d.dump()
d.vendors_list()

# New application
a = dict_application_data()
a.application_id = 99
a.application_name = "My test appl"
r, my_appl = fd_dict_new(d, DICT_APPLICATION, a, my_vendor)
del a

# New type (callbacks are not supported yet...)
t = dict_type_data()
t.type_base = AVP_TYPE_INTEGER32
t.type_name = "My integer AVP"
r, my_type_int = fd_dict_new(d, DICT_TYPE, t, my_appl)
t.type_base = AVP_TYPE_OCTETSTRING
t.type_name = "My binary buffer AVP"
r, my_type_os = fd_dict_new(d, DICT_TYPE, t, my_appl)
del t

# Constants
c = dict_enumval_data()
c.enum_name = "AVP_VALUE_TROIS"
c.enum_value.i32 = 3
fd_dict_new(d, DICT_ENUMVAL, c, my_type_int)

c.enum_name = "A_BUFFER_CONSTANT"
osval = avp_value_os("This is a very long AVP value that we prefer to represent as a constant")
c.enum_value.os = osval
c.enum_value.os.dump()
del d

c = dict_enumval_data()
c.enum_value.os = "coucou"
c.enum_value.os.dump()


gdict = cvar.fd_g_config.cnf_dict
r, obj = fd_dict_search ( gdict, DICT_APPLICATION, APPLICATION_BY_ID, 3, -1 )
obj.dump()
r, obj = fd_dict_search( gdict, DICT_AVP, AVP_BY_NAME, "Origin-Host", -1)
obj.dump()

t = new_dict_object_type_ptr()
fd_dict_gettype(obj, t)
dict_object_type_ptr_dump(t)
delete_dict_object_type_ptr(t)
objdata = new_dict_application_data()
fd_dict_getval(obj, objdata)
dict_application_data_application_name_get(objdata)
delete_dict_application_data(objdata)

vd = new_dict_vendor_data()
dict_vendor_data_vendor_id_set(vd, 123)
dict_vendor_data_vendor_name_set(vd, "my test vendor")
pobj = new_dict_object_pptr()
fd_dict_new ( gdict, DICT_VENDOR, vd, None, pobj)
delete_dict_vendor_data(vd)
obj = dict_object_pptr_value(pobj)
delete_dict_object_pptr(pobj)
fd_dict_dump_object(obj)


# Sessions
pmyhdl = new_session_handler_pptr()
fd_sess_handler_create_internal(pmyhdl, None)
### Have to work on this one, a cleanup handler is actually required.
### How to define the handler in python ?
myhdl = session_handler_pptr_value(pmyhdl)
delete_session_handler_pptr(pmyhdl)

psess = new_session_pptr()
fd_sess_new (psess, fd_config_cnf_diamid_get(cvar.fd_g_config), "dbg_interactive", 0)
sess = session_pptr_value(psess)
fd_sess_dump(0, sess)
fd_sess_destroy(psess)
delete_session_pptr(psess)


# Routing data
prtd = new_rt_data_pptr()
fd_rtd_init(prtd)
fd_rtd_candidate_add(rt_data_pptr_value(prtd), "p1.testbed.aaa", "testbed.aaa")
fd_rtd_candidate_add(rt_data_pptr_value(prtd), "p2.testbed.aaa", "testbed.aaa")
fd_rtd_candidate_add(rt_data_pptr_value(prtd), "p3.testbed.aaa", "testbed.aaa")
fd_rtd_candidate_del(rt_data_pptr_value(prtd), "p2.testbed.aaa", 0)
pcands = new_fd_list_pptr()
fd_rtd_candidate_extract(rt_data_pptr_value(prtd), pcands, 0)
li = fd_list_pptr_value(pcands)
li = fd_list_next_get(li)
c = fd_list_to_rtd_candidate(li)
rtd_candidate_diamid_get(c)
li = fd_list_next_get(li)
c = fd_list_to_rtd_candidate(li)
rtd_candidate_diamid_get(c)


# Messages
gdict = fd_config_cnf_dict_get(cvar.fd_g_config)
pobj = new_dict_object_pptr()
fd_dict_search ( gdict, DICT_COMMAND, CMD_BY_NAME, char_to_void("Capabilities-Exchange-Request"), pobj, -1 )
cerdict = dict_object_pptr_value(pobj)
fd_dict_search ( gdict, DICT_AVP, AVP_BY_NAME, char_to_void("Origin-Host"), pobj, -1 )
ohdict = dict_object_pptr_value(pobj)
delete_dict_object_pptr(pobj)

pmsg = new_msg_pptr()
fd_msg_new(cerdict, MSGFL_ALLOC_ETEID, pmsg)
msg = msg_pptr_value(pmsg);
pavp = new_avp_pptr()
fd_msg_avp_new(ohdict, 0, pavp)
avp = avp_pptr_value(pavp);
fd_msg_avp_add(msg, MSG_BRW_FIRST_CHILD, avp)
fd_msg_dump_walk(0, msg)

pahdr = new_avp_hdr_pptr()
fd_msg_avp_hdr(avp, pahdr)
ahdr = avp_hdr_pptr_value(pahdr)
delete_avp_hdr_pptr(pahdr)
avp_hdr_avp_code_get(ahdr)
os = new_avp_value_os()
avp_value_os_fromstr(os, fd_config_cnf_diamid_get(cvar.fd_g_config))
val = new_avp_value()
avp_value_os_set(val, os)
delete_avp_value_os(os)
fd_msg_avp_setvalue(avp, val)
delete_avp_value(val)

r,buf = fd_msg_bufferize_py(msg)
fd_msg_free(msg)
delete_avp_pptr(pavp)


# Create a new peer_info structure and add the peer to the framework.
mypeer = peer_info()
mypeer.pi_diamid = "nas.testbed.aaa"
mypeer.config.pic_flags.pro4 = 1   # 1 for TCP, for some reason PI_P4_TCP is not defined
fd_peer_add(mypeer, "python", None, None)
del mypeer

"Welcome to our mercurial repository"