Changeset 638:9448cba86673 in freeDiameter for doc/dbg_interactive.py.sample
- Timestamp:
- Dec 17, 2010, 6:41:19 PM (13 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/dbg_interactive.py.sample
r637 r638 104 104 105 105 ############# Hash ############ 106 hex(fd_hash("hello world")) # A typemap is applied to accept binary data 106 107 hex(fd_hash("hello world")) # It accepts binary data 107 108 108 109 … … 122 123 d.vendors_list() 123 124 125 # Compact invocation also possible: 126 v2 = dict_vendor_data(124, "My test vendor 2") 127 del v2 128 124 129 # New application 125 130 a = dict_application_data() … … 128 133 my_appl = d.new_obj(DICT_APPLICATION, a, my_vendor) 129 134 del a 135 136 a2 = dict_application_data(99, "My test appl 2") 137 del a2 130 138 131 139 # New type (callbacks are not supported yet...) … … 139 147 del t 140 148 149 t2 = dict_type_data(AVP_TYPE_UNSIGNED32, "u32 type") 150 del t2 151 141 152 # Constants 142 153 c = dict_enumval_data() … … 150 161 d.new_obj(DICT_ENUMVAL, c, my_type_os) 151 162 del c 163 164 c2 = dict_enumval_data("enum 23", 23) # The constructor only accepts unsigned32, for other values, set them afterwards 165 c3 = dict_enumval_data("enum other") 166 c3.os = "other value" 167 del c2 168 del c3 152 169 153 170 # AVP … … 166 183 my_avp_os = d.new_obj(DICT_AVP, a, my_type_os) 167 184 del a 185 186 a2 = dict_avp_data(235, "no vendor, not mandatory", AVP_TYPE_OCTETSTRING) 187 a3 = dict_avp_data(236, "vendor 12, not mandatory", AVP_TYPE_OCTETSTRING, 12) 188 a4 = dict_avp_data(237, "vendor 12, mandatory", AVP_TYPE_OCTETSTRING, 12, 1) 189 a5 = dict_avp_data(238, "no vendor, mandatory", AVP_TYPE_OCTETSTRING, 0, 1) 190 del a2 191 del a3 192 del a4 193 del a5 194 168 195 169 196 # Command … … 178 205 my_ans = d.new_obj(DICT_COMMAND, c, my_appl) 179 206 del c 207 208 c2 = dict_cmd_data(346, "Second-Request", 1) # Default created with PROXIABLE flag. 209 c3 = dict_cmd_data(346, "Second-Answer", 0) 210 del c2 211 del c3 180 212 181 213 # Rule … … 195 227 del d 196 228 229 r2 = dict_rule_data(my_avp_int, RULE_REQUIRED) # min & max are optional parameters, default to -1 230 r3 = dict_rule_data(my_avp_int, RULE_REQUIRED, 2, 3) # min is 2, max is 3 231 r4 = dict_rule_data(my_avp_int, RULE_FIXED_HEAD) # The r4.rule_order = 1 by default, change afterwards if needed. 232 del r2 233 del r3 234 del r4 197 235 198 236 ####### Now play with the "real" dictionary … … 272 310 ## AVP 273 311 274 # Create empty (as for messages, pass None or a dictionary object as 1st param, and flags as optional 2nd param)312 # Create empty 275 313 blank_avp = avp() 276 314 del blank_avp 277 315 316 # Create from dictionary definitions 278 317 oh = avp(cvar.fd_g_config.cnf_dict.search ( DICT_AVP, AVP_BY_NAME, "Origin-Host")) # Octet String 279 318 vi = avp(cvar.fd_g_config.cnf_dict.search ( DICT_AVP, AVP_BY_NAME, "Vendor-Id")) # U32 … … 289 328 vsai.add_child(vi) # call as add_child(vi, 1) to add the new AVP at the beginning, default is at the end 290 329 330 # It is possible to initialize the AVP with a blank value as follow: 331 blank_with_value = avp(None, AVPFL_SET_BLANK_VALUE) 332 # it enables this without doing the setval call: 333 blank_with_value.header().avp_value.u32 = 12 334 291 335 292 336 ## Messages 293 337 294 # Create empt y338 # Create empt (as for avps, pass None or a dictionary object as 1st param, and flags as optional 2nd param)y 295 339 a_msg = msg() 296 340 a_msg.dump() … … 358 402 hex(oh_hdr.avp_flags) 359 403 oh_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. 404 oh_hdr.avp_value.os.as_str() 405 361 406 362 407 # Get or set the routing data … … 390 435 391 436 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: 438 mydwr.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) 442 def send_callback(msg, obj): 443 print "Received answer:" 444 msg.dump() 445 print "Associated data:" 446 obj 447 return None 448 449 mydwr.send(send_callback, some_object) 450 451 452 # Set a result code in an answer message. 453 dwa = mydwr.create_answer() 454 dwa.rescode_set() # This adds the DIAMETER_SUCCESS result code 455 dwa.rescode_set("DIAMETER_LIMITED_SUCCESS" ) # This adds a different result code 456 dwa.rescode_set("DIAMETER_LIMITED_SUCCESS", "Something went not so well" ) # This adds a different result code + specified Error-Message 457 dwa.rescode_set("DIAMETER_INVALID_AVP", None, faulty_avp ) # This adds a Failed-AVP 458 dwa.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 461 mydwr.add_origin() # adds Origin-Host & Origin-Realm 462 mydwr.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: 469 def 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 495 gdict = cvar.fd_g_config.cnf_dict 496 d_si = gdict.search ( DICT_AVP, AVP_BY_NAME, "Session-Id" ) 497 d_oh = gdict.search ( DICT_AVP, AVP_BY_NAME, "Origin-Host" ) 498 d_or = gdict.search ( DICT_AVP, AVP_BY_NAME, "Origin-Realm" ) 499 d_dh = gdict.search ( DICT_AVP, AVP_BY_NAME, "Destination-Host" ) 500 d_dr = gdict.search ( DICT_AVP, AVP_BY_NAME, "Destination-Realm" ) 501 d_rc = gdict.search ( DICT_AVP, AVP_BY_NAME, "Result-Code" ) 502 d_vnd = gdict.new_obj(DICT_VENDOR, dict_vendor_data(999999, "app_test_py vendor") ) 503 d_app = gdict.new_obj(DICT_APPLICATION, dict_application_data(0xffffff, "app_test_py appli"), d_vnd) 504 d_req = gdict.new_obj(DICT_COMMAND, dict_cmd_data(0xfffffe, "Test_py-Request", 1), d_app) 505 d_ans = gdict.new_obj(DICT_COMMAND, dict_cmd_data(0xfffffe, "Test_py-Answer", 0), d_app) 506 d_avp = gdict.new_obj(DICT_AVP, dict_avp_data(0xffffff, "app_test_py avp", AVP_TYPE_INTEGER32, 999999 )) 507 gdict.new_obj(DICT_RULE, dict_rule_data(d_si, RULE_FIXED_HEAD, 1, 1), d_req) 508 gdict.new_obj(DICT_RULE, dict_rule_data(d_si, RULE_FIXED_HEAD, 1, 1), d_ans) 509 gdict.new_obj(DICT_RULE, dict_rule_data(d_avp, RULE_REQUIRED, 1, 1), d_req) 510 gdict.new_obj(DICT_RULE, dict_rule_data(d_avp, RULE_REQUIRED, 1, 1), d_ans) 511 gdict.new_obj(DICT_RULE, dict_rule_data(d_oh, RULE_REQUIRED, 1, 1), d_req) 512 gdict.new_obj(DICT_RULE, dict_rule_data(d_oh, RULE_REQUIRED, 1, 1), d_ans) 513 gdict.new_obj(DICT_RULE, dict_rule_data(d_or, RULE_REQUIRED, 1, 1), d_req) 514 gdict.new_obj(DICT_RULE, dict_rule_data(d_or, RULE_REQUIRED, 1, 1), d_ans) 515 gdict.new_obj(DICT_RULE, dict_rule_data(d_dr, RULE_REQUIRED, 1, 1), d_req) 516 gdict.new_obj(DICT_RULE, dict_rule_data(d_dh, RULE_OPTIONAL, 0, 1), d_req) 517 gdict.new_obj(DICT_RULE, dict_rule_data(d_rc, RULE_REQUIRED, 1, 1), d_ans) 518 519 # Now, create the Test_app server callback: 520 def 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: 532 hdl = 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. 535 fd_disp_app_support ( d_app, d_vnd, 1, 0 ) 536 537 538 ### For the fun, the client part of the test_app: 539 540 def 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 553 import random 554 555 def 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 576 send_query() 577 578 579 ############# FIFO queues ############ 580 581 myqueue = fifo() 582 583 # enqueue any object 584 myqueue.post(3) 585 myqueue.post("blah") 586 myqueue.post( [ 3, 2 ] ) 587 588 # Simple get (blocks when the queue is empty) 589 myqueue.get() 590 591 # Try get: returns the next object, or None if the queue is empty 592 myqueue.tryget() 593 594 # timed get: like get, but returns None after x seconds 595 myqueue.timedget(3) 596 597 # Show the number of items in the queue 598 myqueue.length() 599 600 del 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) 608 peers = cvar.fd_g_peers.enum_as("struct peer_hdr *") 609 for p in peers: 610 print "Peer:", p.info.pi_diamid 611 612 613 # Create a new peer 614 np = peer_info() 615 np.pi_diamid = "nas.localdomain" 616 np.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. 622 np.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: 626 def 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: 637 np.add(add_cb) 638 639
Note: See TracChangeset
for help on using the changeset viewer.