Navigation


Changeset 638:9448cba86673 in freeDiameter for doc


Ignore:
Timestamp:
Dec 17, 2010, 6:41:19 PM (13 years ago)
Author:
Sebastien Decugis <sdecugis@nict.go.jp>
Branch:
default
Phase:
public
Message:

Improved usability of dbg_interactive

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/dbg_interactive.py.sample

    r637 r638  
    104104
    105105############# Hash ############
    106 hex(fd_hash("hello world"))     # A typemap is applied to accept binary data
     106
     107hex(fd_hash("hello world"))     # It accepts binary data
    107108
    108109
     
    122123d.vendors_list()
    123124
     125# Compact invocation also possible:
     126v2 = dict_vendor_data(124, "My test vendor 2")
     127del v2
     128
    124129# New application
    125130a = dict_application_data()
     
    128133my_appl = d.new_obj(DICT_APPLICATION, a, my_vendor)
    129134del a
     135
     136a2 = dict_application_data(99, "My test appl 2")
     137del a2
    130138
    131139# New type (callbacks are not supported yet...)
     
    139147del t
    140148
     149t2 = dict_type_data(AVP_TYPE_UNSIGNED32, "u32 type")
     150del t2
     151
    141152# Constants
    142153c = dict_enumval_data()
     
    150161d.new_obj(DICT_ENUMVAL, c, my_type_os)
    151162del c
     163
     164c2 = dict_enumval_data("enum 23", 23)  # The constructor only accepts unsigned32, for other values, set them afterwards
     165c3 = dict_enumval_data("enum other")
     166c3.os = "other value"
     167del c2
     168del c3
    152169
    153170# AVP
     
    166183my_avp_os = d.new_obj(DICT_AVP, a, my_type_os)
    167184del a
     185
     186a2 = dict_avp_data(235, "no vendor, not mandatory", AVP_TYPE_OCTETSTRING)
     187a3 = dict_avp_data(236, "vendor 12, not mandatory", AVP_TYPE_OCTETSTRING, 12)
     188a4 = dict_avp_data(237, "vendor 12, mandatory", AVP_TYPE_OCTETSTRING, 12, 1)
     189a5 = dict_avp_data(238, "no vendor, mandatory", AVP_TYPE_OCTETSTRING, 0, 1)
     190del a2
     191del a3
     192del a4
     193del a5
     194
    168195
    169196# Command
     
    178205my_ans = d.new_obj(DICT_COMMAND, c, my_appl)
    179206del c
     207
     208c2 = dict_cmd_data(346, "Second-Request", 1) # Default created with PROXIABLE flag.
     209c3 = dict_cmd_data(346, "Second-Answer",  0)
     210del c2
     211del c3
    180212
    181213# Rule
     
    195227del d
    196228
     229r2 = dict_rule_data(my_avp_int, RULE_REQUIRED) # min & max are optional parameters, default to -1
     230r3 = dict_rule_data(my_avp_int, RULE_REQUIRED, 2, 3) # min is 2, max is 3
     231r4 = dict_rule_data(my_avp_int, RULE_FIXED_HEAD) # The r4.rule_order = 1 by default, change afterwards if needed.
     232del r2
     233del r3
     234del r4
    197235
    198236####### Now play with the "real" dictionary
     
    272310## AVP
    273311
    274 # Create empty (as for messages, pass None or a dictionary object as 1st param, and flags as optional 2nd param)
     312# Create empty
    275313blank_avp = avp()
    276314del blank_avp
    277315
     316# Create from dictionary definitions
    278317oh = avp(cvar.fd_g_config.cnf_dict.search ( DICT_AVP, AVP_BY_NAME, "Origin-Host"))                      # Octet String
    279318vi = avp(cvar.fd_g_config.cnf_dict.search ( DICT_AVP, AVP_BY_NAME, "Vendor-Id"))                        # U32
     
    289328vsai.add_child(vi) # call as add_child(vi, 1) to add the new AVP at the beginning, default is at the end
    290329
     330# It is possible to initialize the AVP with a blank value as follow:
     331blank_with_value = avp(None, AVPFL_SET_BLANK_VALUE)
     332# it enables this without doing the setval call:
     333blank_with_value.header().avp_value.u32 = 12
     334
    291335
    292336## Messages
    293337
    294 # Create empty
     338# Create empt (as for avps, pass None or a dictionary object as 1st param, and flags as optional 2nd param)y
    295339a_msg = msg()
    296340a_msg.dump()
     
    358402hex(oh_hdr.avp_flags)
    359403oh_hdr.avp_vendor
    360 oh_hdr.avp_value.os.dump()  # The initial avp value must be set with setval(), but then this accessor is allowed.
     404oh_hdr.avp_value.os.as_str()
     405
    361406
    362407# Get or set the routing data
     
    390435
    391436
    392 
    393 
    394 
    395 
    396 
    397 
    398 
    399 
    400 ######################### old stuff (need update) ######################
    401 
    402 
    403 # Create a new peer_info structure and add the peer to the framework.
    404 mypeer = peer_info()
    405 mypeer.pi_diamid = "nas.testbed.aaa"
    406 mypeer.config.pic_flags.pro4 = 1   # 1 for TCP, for some reason PI_P4_TCP is not defined
    407 fd_peer_add(mypeer, "python", None, None)
    408 del mypeer
    409 
     437# Send a message:
     438mydwr.send()
     439
     440# Optionaly, a callback can be registered when a message is sent, with an optional object.
     441# This callback takes the answer message as parameter and should return None or a message. (cf. fd_msg_send)
     442def send_callback(msg, obj):
     443    print "Received answer:"
     444    msg.dump()
     445    print "Associated data:"
     446    obj
     447    return None
     448
     449mydwr.send(send_callback, some_object)
     450
     451
     452# Set a result code in an answer message.
     453dwa = mydwr.create_answer()
     454dwa.rescode_set()   # This adds the DIAMETER_SUCCESS result code
     455dwa.rescode_set("DIAMETER_LIMITED_SUCCESS" )   # This adds a different result code
     456dwa.rescode_set("DIAMETER_LIMITED_SUCCESS", "Something went not so well" )   # This adds a different result code + specified Error-Message
     457dwa.rescode_set("DIAMETER_INVALID_AVP", None, faulty_avp )   # This adds a Failed-AVP
     458dwa.rescode_set("DIAMETER_SUCCESS", None, None, 1 )   # This adds origin information (see fd_msg_rescode_set's type_id for more info)
     459
     460# Set the origin to local host
     461mydwr.add_origin()  # adds Origin-Host & Origin-Realm
     462mydwr.add_origin(1) # adds Origin-State-Id in addition.
     463
     464
     465############# DISPATCH (aka. server application) ############
     466
     467# As for sessions, only one dispatch handler can be registered in this extension at the moment.
     468# The callback for the handler has the following syntax:
     469def dispatch_cb_model(inmsg, inavp, insession):
     470   print "Callback trigged on message: "
     471   inmsg.dump()
     472   # inavp is None or the AVP that trigged the callback, depending on how it was registered.
     473   if inavp:
     474     print "From the following AVP:"
     475     inavp.dump()
     476   else:
     477     print "No AVP"
     478   # Session is provided only if a Session-Id is in the message
     479   if insession:
     480     print "The session is: ", insession.getsid()
     481   else:
     482     print "No session"
     483   # Now, for the return value.
     484   # This callback must return 3 elements:
     485   # - an integer which is interpreted as an error code (errno.h)
     486   # - a message or None, depending on the next item
     487   # - an enum disp_action value, with the same meaning as in C (see libfreeDiameter.h)
     488   del inmsg
     489   return [ 0, None, DISP_ACT_CONT ]
     490
     491
     492### Example use: rebuild the server-side of test_app.fdx in python
     493
     494# The following block defines the dictionary objects from the test_app.fdx application that we use on the remote peer
     495gdict = cvar.fd_g_config.cnf_dict
     496d_si = gdict.search ( DICT_AVP, AVP_BY_NAME, "Session-Id" )
     497d_oh  = gdict.search ( DICT_AVP, AVP_BY_NAME, "Origin-Host" )
     498d_or  = gdict.search ( DICT_AVP, AVP_BY_NAME, "Origin-Realm" )
     499d_dh  = gdict.search ( DICT_AVP, AVP_BY_NAME, "Destination-Host" )
     500d_dr  = gdict.search ( DICT_AVP, AVP_BY_NAME, "Destination-Realm" )
     501d_rc  = gdict.search ( DICT_AVP, AVP_BY_NAME, "Result-Code" )
     502d_vnd = gdict.new_obj(DICT_VENDOR,      dict_vendor_data(999999,        "app_test_py vendor") )
     503d_app = gdict.new_obj(DICT_APPLICATION, dict_application_data(0xffffff, "app_test_py appli"), d_vnd)
     504d_req = gdict.new_obj(DICT_COMMAND,     dict_cmd_data(0xfffffe, "Test_py-Request", 1), d_app)
     505d_ans = gdict.new_obj(DICT_COMMAND,     dict_cmd_data(0xfffffe, "Test_py-Answer",  0), d_app)
     506d_avp = gdict.new_obj(DICT_AVP,         dict_avp_data(0xffffff, "app_test_py avp", AVP_TYPE_INTEGER32, 999999 ))
     507gdict.new_obj(DICT_RULE, dict_rule_data(d_si, RULE_FIXED_HEAD, 1, 1), d_req)
     508gdict.new_obj(DICT_RULE, dict_rule_data(d_si, RULE_FIXED_HEAD, 1, 1), d_ans)
     509gdict.new_obj(DICT_RULE, dict_rule_data(d_avp, RULE_REQUIRED, 1, 1), d_req)
     510gdict.new_obj(DICT_RULE, dict_rule_data(d_avp, RULE_REQUIRED, 1, 1), d_ans)
     511gdict.new_obj(DICT_RULE, dict_rule_data(d_oh, RULE_REQUIRED, 1, 1), d_req)
     512gdict.new_obj(DICT_RULE, dict_rule_data(d_oh, RULE_REQUIRED, 1, 1), d_ans)
     513gdict.new_obj(DICT_RULE, dict_rule_data(d_or, RULE_REQUIRED, 1, 1), d_req)
     514gdict.new_obj(DICT_RULE, dict_rule_data(d_or, RULE_REQUIRED, 1, 1), d_ans)
     515gdict.new_obj(DICT_RULE, dict_rule_data(d_dr, RULE_REQUIRED, 1, 1), d_req)
     516gdict.new_obj(DICT_RULE, dict_rule_data(d_dh, RULE_OPTIONAL, 0, 1), d_req)
     517gdict.new_obj(DICT_RULE, dict_rule_data(d_rc, RULE_REQUIRED, 1, 1), d_ans)
     518
     519# Now, create the Test_app server callback:
     520def test_app_cb(inmsg, inavp, insession):
     521   tval = inmsg.search(d_avp).header().avp_value.u32
     522   print "Py ECHO Test message from '%s' with test value %x, replying..." % (inmsg.search(d_oh).header().avp_value.os.as_str(), tval)
     523   answ = inmsg.create_answer()
     524   answ.rescode_set()
     525   answ.add_origin()
     526   ta = avp(d_avp, AVPFL_SET_BLANK_VALUE)
     527   ta.header().avp_value.u32 = tval
     528   answ.add_child(ta)
     529   return [ 0, answ, DISP_ACT_SEND ]
     530
     531# Register the callback for dispatch thread:
     532hdl = disp_hdl(test_app_cb, DISP_HOW_CC, disp_when(d_app, d_req))  # disp_when() takes 0 to 4 arguments as follow: (app=NULL, cmd=NULL, avp=NULL, val=NULL)
     533
     534# Don't forget to register the application in the daemon for CER/CEA capabilities.
     535fd_disp_app_support ( d_app, d_vnd, 1, 0 )
     536
     537
     538###  For the fun, the client part of the test_app:
     539
     540def receive_answer(ans, testval):
     541   try:
     542     tval = ans.search(d_avp).header().avp_value.u32
     543   except:
     544     print "Error in receive_answer: no Test-AVP included"
     545     tval = 0
     546   try:
     547     print "Py RECV %x (expected: %x) Status: %d From: '%s'" % (tval, testval, ans.search(d_rc).header().avp_value.u32, ans.search(d_oh).header().avp_value.os.as_str())
     548   except:
     549     print "Error in receive_answer: Result-Code or Origin-Host are missing"
     550   del ans
     551   return None
     552
     553import random
     554
     555def send_query(destrealm="localdomain"):
     556   qry = msg(d_req)
     557   sess = session()
     558   tv = random.randint(1, 1<<32)
     559   # Session-Id
     560   a = avp(d_si, AVPFL_SET_BLANK_VALUE)
     561   a.header().avp_value.os = sess.getsid()
     562   qry.add_child(a)
     563   # Destination-Realm
     564   a = avp(d_dr, AVPFL_SET_BLANK_VALUE)
     565   a.header().avp_value.os = destrealm
     566   qry.add_child(a)
     567   # Origin-Host, Origin-Realm
     568   qry.add_origin()
     569   # Test-AVP
     570   a = avp(d_avp, AVPFL_SET_BLANK_VALUE)
     571   a.header().avp_value.u32 = tv
     572   qry.add_child(a)
     573   print "Py SEND %x to '%s'" % (tv, destrealm)
     574   qry.send(receive_answer, tv)
     575
     576send_query()
     577
     578
     579############# FIFO queues ############
     580
     581myqueue = fifo()
     582
     583# enqueue any object
     584myqueue.post(3)
     585myqueue.post("blah")
     586myqueue.post( [ 3, 2 ] )
     587
     588# Simple get (blocks when the queue is empty)
     589myqueue.get()
     590
     591# Try get: returns the next object, or None if the queue is empty
     592myqueue.tryget()
     593
     594# timed get: like get, but returns None after x seconds
     595myqueue.timedget(3)
     596
     597# Show the number of items in the queue
     598myqueue.length()
     599
     600del myqueue
     601
     602
     603
     604############# PEERS ############
     605
     606# Get the list of peers defined in the system
     607# (well, we are supposed actually to readlock fd_g_peers_rw before doing this, but it should be fine most of the time)
     608peers = cvar.fd_g_peers.enum_as("struct peer_hdr *")
     609for p in peers:
     610   print "Peer:", p.info.pi_diamid
     611
     612
     613# Create a new peer
     614np = peer_info()
     615np.pi_diamid = "nas.localdomain"
     616np.config.pic_flags.pro4 = 1   # 1 for TCP, for some reason PI_P4_TCP is not defined
     617
     618
     619
     620
     621# Add this peer into the framework.
     622np.add()
     623
     624# It is possible to specify a callback for when the connection completes or fails to this peer.
     625# The prototype is as follow:
     626def add_cb(peer):
     627    if peer:
     628        if peer.runtime.pir_state == STATE_OPEN:
     629           print "Connection to peer '%s' completed" % (peer.pi_diamid)
     630           # can find more information in peer.runtime.*
     631        else:
     632           print "Connection to peer '%s' failed (state:%d)" % (peer.pi_diamid, peer.runtime.pir_state)
     633    else:
     634        print "The peer has been destroyed before it completed the connection."
     635
     636# Then add the peer simply like this:
     637np.add(add_cb)
     638
     639
Note: See TracChangeset for help on using the changeset viewer.