📄 get.cpp
字号:
Oid addoid( oid_ptr->get_printable());
CListBox *lb;
lb = ( CListBox*) GetDlgItem( IDC_PDU);
CString scalar;
scalar = tree->GetItemText( node);
scalar += " ";
scalar += addoid.get_printable();
lb->InsertString( lb->GetCount(),scalar);
}
void Get::OnRemove()
{
// TODO: Add your control notification handler code here
CListBox *lb;
int selection;
lb = ( CListBox*) GetDlgItem( IDC_PDU);
if((selection = lb->GetCurSel()) != LB_ERR)
lb->DeleteString( selection);
}
void Get::OnDblclkMibtree(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
OnAdd();
*pResult = 0;
}
//====================================================================
// SNMP++ callback function for async events
// this callback is used for all async requests
void my_getcallback( int reason,
Snmp* snmp,
Pdu &pdu,
SnmpTarget &target,
void * callback_data)
{
// resolve the callback data to a MFC Get Window Handle
Get * get_window = (Get *) callback_data;
CButton * continue_mode = (CButton*) get_window->GetDlgItem( IDC_CONTINUOS);
// based on the reason handle the reason
switch ( reason)
{
// an asynchronous response has arrived
case SNMP_CLASS_ASYNC_RESPONSE:
{
get_window->display_response( pdu, target, snmp);
// look for continuos mode
if (( continue_mode->GetCheck()) && (pdu.get_error_status()== 0))
get_window->get_another();
else
get_window->enable_controls( TRUE);
}
break;
// a timeout has occured
case SNMP_CLASS_TIMEOUT:
get_window->display_timeout( target);
get_window->enable_controls( TRUE);
break;
} // end switch
};
// get another one
void Get::get_another()
{
OnGet();
};
// display the response value
void Get::display_response( Pdu &pdu, SnmpTarget &target, Snmp *snmp)
{
GenAddress address; // SNMP++ generic Address
Vb vb;
int error_status = pdu.get_error_status();
char buffer[50];
// show the result status
m_output = "Status = ";
m_output += snmp->error_msg( error_status);
m_output += "\r\n";
// show the details if check box is selected
CButton * all_details = (CButton*) GetDlgItem( IDC_ALL_DETAILS);
if ( all_details->GetCheck())
{
target.get_address( address);
m_output += "Response from ";
m_output += address.get_printable();
m_output += "\r\n";
m_output += "Error Status = ";
m_output += snmp->error_msg( error_status );
m_output += "\r\n";
sprintf( buffer,"Error Index = %d\r\n", pdu.get_error_index());
m_output += buffer;
sprintf( buffer,"Request ID = %ld\r\n",pdu.get_request_id());
m_output += buffer;
sprintf( buffer,"Variable Binding Count = %d\r\n",pdu.get_vb_count());
m_output += buffer;
}
// show the Vbs
for ( int x=0;x<pdu.get_vb_count();x++)
{
pdu.get_vb( vb,x);
sprintf( buffer,"Vb #%d\r\n",x+1);
m_output += buffer;
m_output += " Oid = ";
m_output += vb.get_printable_oid();
m_output += "\r\n";
m_output += " Value = ";
m_output += vb.get_printable_value();
m_output += "\r\n";
}
// update the output display
UpdateData( FALSE);
};
// display a timeout message
void Get::display_timeout( SnmpTarget &target)
{
m_output = "";
UpdateData( FALSE);
m_output = "Request Timed Out";
UpdateData( FALSE);
};
// invoke a SNMP++ Get
void Get::OnGet()
{
//------------------------------------------------------
// Here is the real SNMP++ code !
//
// Algorithm:
// - build a Pdu using the Oids from the Pdu list box
// - create a CTarget
// - send an Asynchronous Get Request
// - response will come to specified callback
CListBox *lb;
int count;
char buffer[80],*ptr;
Pdu pdu; // SNMP++ Pdu object
// get the ListBox id
lb = ( CListBox*) GetDlgItem( IDC_PDU);
// make sure we have something to get
if ( (count=lb->GetCount()) <1)
{
AfxMessageBox("There are No PDU Variables to Get");
return;
}
// build up a SNMP++ Pdu with all the Vbs requested
for ( int z =0;z<count;z++)
{
// get the text from the listbox
lb->GetText(z,buffer);
// get rid of the text part
ptr = buffer;
while (*ptr != ' ') ptr++;
ptr++;
// make a SNMP++ Oid with the dotted notation
Oid oid( ptr);
// make a SNMP++ Vb with the oid
Vb vb( oid);
// add the vb to the Pdu
pdu += vb;
}
// make a SnmpTarget using the Target_Factory
CComboBox *cb = ( CComboBox *) GetDlgItem( IDC_TARGETS);
char key[80];
if ( cb->GetCurSel() == CB_ERR)
{
AfxMessageBox("No Target Selected!");
return;
}
cb->GetLBText( cb->GetCurSel(), key);
// get a target from the target factory
SnmpTarget *target = target_factory( key);
if ( target == NULL)
{
AfxMessageBox("Unable To find Target");
return;
}
// Invoke a SNMP++ Get for the Pdu requested
// from the target created
int status = snmp->get( pdu,
*target,
(snmp_callback) &my_getcallback,
(void *) this);
if ( status != SNMP_CLASS_SUCCESS)
AfxMessageBox( snmp->error_msg( status));
// clear the output display
m_output = "SNMP++ Get In Progress...";
UpdateData( FALSE);
// free up the target when complete
delete target;
// disable all controls while get is pending
enable_controls( FALSE);
};
// Manual Oid Specification
void Get::OnAddoid()
{
// TODO: Add your control notification handler code here
UpdateData( TRUE);
Oid oid( m_oid);
if ( oid.valid())
{
CListBox *lb;
lb = ( CListBox*) GetDlgItem( IDC_PDU);
CString item;
item = "Custom ";
item += oid.get_printable();
lb->InsertString( lb->GetCount(),item);
}
else
AfxMessageBox("Invalid Custom MIB Object Identifier");
}
// enable the controls
void Get::enable_controls( int state)
{
CWnd *control;
control = GetDlgItem( IDC_TARGETS);
control->EnableWindow( state ? TRUE : FALSE);
control = GetDlgItem( IDC_MIBTREE);
control->EnableWindow( state ? TRUE : FALSE);
control = GetDlgItem( IDC_ADD);
control->EnableWindow( state ? TRUE : FALSE);
control = GetDlgItem( IDC_REMOVE);
control->EnableWindow( state ? TRUE : FALSE);
control = GetDlgItem( IDC_PDU);
control->EnableWindow( state ? TRUE : FALSE);
control = GetDlgItem( IDC_OID);
control->EnableWindow( state ? TRUE : FALSE);
control = GetDlgItem( IDC_ADDOID);
control->EnableWindow( state ? TRUE : FALSE);
control = GetDlgItem( IDC_ALL_DETAILS);
control->EnableWindow( state ? TRUE : FALSE);
control = GetDlgItem( IDC_CONTINUOS);
control->EnableWindow( state ? TRUE : FALSE);
};
void Get::OnStop()
{
// TODO: Add your control notification handler code here
CButton * continue_mode = (CButton*) GetDlgItem( IDC_CONTINUOS);
continue_mode->SetCheck(0);
}
void Get::OnCancel()
{
// TODO: Add extra cleanup here
DestroyWindow();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -