📄 sample_client1.cxx
字号:
*/ ACE_DEBUG((LM_DEBUG, "(%P|%t) Client: Sending test acct message\n")); AAAAvpContainerManager cm; AAAAvpContainerEntryManager em; AAAAvpContainer *c_sessId = cm.acquire("Session-Id"); AAAAvpContainer *c_orhost = cm.acquire("Origin-Host"); AAAAvpContainer *c_orrealm = cm.acquire("Origin-Realm"); AAAAvpContainer *c_drealm = cm.acquire("Destination-Realm"); AAAAvpContainer *c_rectype = cm.acquire("Accounting-Record-Type"); AAAAvpContainer *c_recnum = cm.acquire("Accounting-Record-Number"); AAAAvpContainer *c_acctid = cm.acquire("Acct-Application-Id"); AAAAvpContainer *c_user = cm.acquire("User-Name"); AAAAvpContainer *c_subid = cm.acquire("Accounting-Sub-Session-Id"); AAAAvpContainer *c_acctsid = cm.acquire("Accounting-Session-Id"); AAAAvpContainer *c_acctmsid = cm.acquire("Acct-Multi-Session-Id"); AAAAvpContainer *c_intvl = cm.acquire("Accounting-Interim-Interval"); AAAAvpContainer *c_realtime = cm.acquire("Accounting-Realtime-Required"); AAAAvpContainer *c_origin = cm.acquire("Origin-State-Id"); AAAAvpContainerEntry *e; e = em.acquire(AAA_AVP_UTF8_STRING_TYPE); c_sessId->add(e); e = em.acquire(AAA_AVP_DIAMID_TYPE); c_orhost->add(e); e = em.acquire(AAA_AVP_DIAMID_TYPE); c_orrealm->add(e); e = em.acquire(AAA_AVP_DIAMID_TYPE); diameter_identity_t &drealm = e->dataRef(Type2Type<diameter_identity_t>()); c_drealm->add(e); e = em.acquire(AAA_AVP_UINTEGER32_TYPE); diameter_unsigned32_t &rectyp = e->dataRef(Type2Type<diameter_unsigned32_t>()); c_rectype->add(e); e = em.acquire(AAA_AVP_UINTEGER32_TYPE); diameter_unsigned32_t &recnum = e->dataRef(Type2Type<diameter_unsigned32_t>()); c_recnum->add(e); e = em.acquire(AAA_AVP_INTEGER32_TYPE); diameter_integer32_t &acctid = e->dataRef(Type2Type<diameter_integer32_t>()); c_acctid->add(e); e = em.acquire(AAA_AVP_UTF8_STRING_TYPE); diameter_utf8string_t &user = e->dataRef(Type2Type<diameter_utf8string_t>()); c_user->add(e); e = em.acquire(AAA_AVP_UINTEGER64_TYPE); diameter_unsigned64_t &subid = e->dataRef(Type2Type<diameter_unsigned64_t>()); c_subid->add(e); e = em.acquire(AAA_AVP_STRING_TYPE); diameter_octetstring_t &acctsid = e->dataRef(Type2Type<diameter_octetstring_t>()); c_acctsid->add(e); e = em.acquire(AAA_AVP_UTF8_STRING_TYPE); diameter_utf8string_t &acctmsid = e->dataRef(Type2Type<diameter_utf8string_t>()); c_acctmsid->add(e); e = em.acquire(AAA_AVP_UINTEGER32_TYPE); diameter_unsigned32_t &intvl = e->dataRef(Type2Type<diameter_unsigned32_t>()); c_intvl->add(e); e = em.acquire(AAA_AVP_UINTEGER32_TYPE); diameter_unsigned32_t &realtime = e->dataRef(Type2Type<diameter_unsigned32_t>()); c_realtime->add(e); e = em.acquire(AAA_AVP_UINTEGER32_TYPE); diameter_unsigned32_t &origin = e->dataRef(Type2Type<diameter_unsigned32_t>()); c_origin->add(e); AAAMessage acctMsg; hdr_flag flag = {1,0,0,0,0}; AAADiameterHeader h(1, 0, flag, AAASampleAccountingRequestMsg::ACR, 20000, 0, 0); acctMsg.hdr = h; acctMsg.acl.add(c_sessId); acctMsg.acl.add(c_orhost); acctMsg.acl.add(c_orrealm); acctMsg.acl.add(c_drealm); acctMsg.acl.add(c_rectype); acctMsg.acl.add(c_recnum); acctMsg.acl.add(c_acctid); acctMsg.acl.add(c_user); acctMsg.acl.add(c_subid); acctMsg.acl.add(c_acctsid); acctMsg.acl.add(c_acctmsid); acctMsg.acl.add(c_intvl); acctMsg.acl.add(c_realtime); acctMsg.acl.add(c_origin); drealm = _destRealm; rectyp = type; // event record only recnum = ++ rec_counter; acctid = 0; // sample only user.assign("user@sample"); subid = 0; // sample only acctsid.assign("samplesessid"); acctmsid.assign("samplemsid"); intvl = 30; // 30 sec interval for sampling realtime = 2; // grant and store origin = 0; // sample only /* * The use of AAAMessageControl is shown here. * It is a utility class used in sending messages * via the local session object as well as setting * the result code. */ AAAMessageControl msgControl(this); if (msgControl.Send(acctMsg) != AAA_ERR_SUCCESS) { ACE_ERROR((LM_ERROR, "(%P|%t) Client: Failed sending message\n")); return (AAA_ERR_FAILURE); } else { ACE_DEBUG((LM_DEBUG, "(%P|%t) Client: Sent Acct Message\n")); } return (AAA_ERR_SUCCESS);}/*! * Event handler for interim record events. This * method is invoked when interim record events * has been scheduled via SetInterimRecordInterval. * * In our example we use it to generate accounting * request. The request are EVENT types only. For * duration type messages (START,INTERIN,STOP), you * need to extend this class to provide such * functionality. * * Note that you can use payload parameter to pass * in user data. */AAAReturnCode AAASampleAccountingClient::HandleInterimRecordEvent(RECTYPE type, AAASessionPayload payload){ ACE_DEBUG((LM_DEBUG, "(%P|%t) Client: Interim record event ... collecting data\n")); /* The interim handler is our queue * to collect accounting data and * send a request. In our sample * we simply send a request to and * mimic collection */ return SendAcctMessage(type);}AAASampleAccountingRequestMsg::AAASampleAccountingRequestMsg(AAASampleAccountingClient &sess , AAACommandCode code) : AAASessionMessageHandler((AAAApplicationCore&)sess.GetApplicationCore(), code), session(sess){ /* * To facilitate simplicity, the registration of * the message handler to the session is done * in the handlers constructor */ session.RegisterMessageHandler(this);}AAASampleAccountingRequestMsg::~AAASampleAccountingRequestMsg(){ /* * Likewise, the registration removal is done * in the destructor */ session.RemoveMessageHandler(this);}AAAReturnCode AAASampleAccountingRequestMsg::HandleMessage(AAAMessage &msg){ /* This is the default handler for accounting messages. * In real implementations, applications MUST examine * the contents of the answer message and act accordingly. * Applications may terminate in interim record generator * as a result. */ if (msg.hdr.code == AAASampleAccountingRequestMsg::ACR && ! msg.hdr.flags.r) { /* * It is important to note that for test purposes, you have to * make sure that accounting interval will occurr while this * peer still has an active connection to the server. This * sample code DOES NOT check for this. Real application MUST * do so. */ if (session.numRecords() == AAASampleAccountingRequestMsg::rec_threshold) { ACE_DEBUG((LM_DEBUG, "(%P|%t) Client: Accounting session ready for termination\n")); } else { ACE_DEBUG((LM_DEBUG, "(%P|%t) Client: Scheduling an accounting interim\n")); /* * We set a 1 sec interval in scheduling an interim * handler event. */ session.SetInterimRecordInterval(AAAAccountingClientSession::RECTYPE_EVENT, 0, // collect records immediately NULL); } } return (AAA_ERR_SUCCESS);}int main(int argc, char *argv[]){ /* * The main test program is straightforward. It * processes all arguments passed to it then * creates the appropriate number of sessions. * It then waits till all sessions have terminated * completely before exiting. * * The number of sessions as well as the authorizing * host and realm is passed in as arguments. Also, * the configuration file is an argument. */ if (argc != 5) { ACE_DEBUG((LM_DEBUG, "(%P|%t) Client: Usage: aaa_sample_client [host] [realm] [num session] [config file]\n")); return (1); } int howManySessions = atoi(argv[3]); if (howManySessions <= 0) { ACE_ERROR((LM_ERROR, "(%P|%t) Client: Invalid number of sessions\n")); return (1); } ACE_DEBUG((LM_DEBUG, "(%P|%t) Client: Application starting\n")); AAA_Task myTask; try { myTask.Start(10); } catch (...) { ACE_ERROR((LM_ERROR, "(%P|%t) Client: Cannot start task\n")); return (1); } AAAApplicationCore myCore; if (myCore.Open(argv[4], myTask) != AAA_ERR_SUCCESS) { ACE_ERROR((LM_ERROR, "(%P|%t) Client: Application core open error\n")); return (1); } std::string destHost(argv[1]); std::string destRealm(argv[2]); holderList _holderList; // Primitive wait. Waiting for the client to // establish connection with the peers while (myCore.GetNumActivePeerConnections() == 0); for (int i = 0; i < howManySessions; i++) { /* * This creates the session and sets * application id to zero for this sample. * * Note that we are repsonsible for creating * instances of the client session. Hence, * we are able to make references of our * allocation and delete it afterwards. * * For accounting sessions, creation of * a new instance of the holder will * create a session id for the accounting * session. */ AAASampleSessionHolder *holder = new AAASampleSessionHolder(myCore, 10000); if (holder) { ACE_DEBUG((LM_DEBUG, "(%P|%t) Client: Starting client # %d\n", i)); holder->clientSession().destHost(destHost); holder->clientSession().destRealm(destRealm); holder->acctSession().destRealm(destRealm); /* * A call to start session will create the session * id and add this session to the local database. * On success, a session may be established with * a server by sending an authorization request. */ holder->clientSession().Start(); /* * Send the auth request */ holder->clientSession().SendTestAuth(); /* * Start accounting */ holder->acctSession().SetInterimRecordInterval(AAAAccountingClientSession::RECTYPE_EVENT, 0, // collect records immediately NULL); /* * Applications need to call Update on the session * to notify the library wether the application * specific authorization request was successful or * not. This processing is application specific and * can only be done at the application level. But since * the result is required by the session state machine, * an update call is necessary. */ holder->clientSession().Update(AAASession::EVENT_AUTH_REQUEST); /* store our holders and cleanup later */ _holderList.push_back(holder); } } /* * This is to make sure we wait for accounting * session to complete */ ACE_OS::sleep(AAASampleAccountingRequestMsg::rec_threshold * 2); /* * this will block until all session are done. * i.e. thier HandleDisconnect() is called. */ AAASampleClient::semaphore().acquire(); /* * cleanup our allocations. we delete all * our client allocations here. */ while (! _holderList.empty()) { AAASampleSessionHolder *holder = _holderList.front(); _holderList.pop_front(); delete holder; } ACE_DEBUG((LM_DEBUG, "(%P|%t) Client: Exiting\n")); return (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -