Use override/default for RTPortableServer
[ACE_TAO.git] / ACE / ASNMP / agent / agent_impl.cpp
blobad60553f715f3f9f9bb35d8aa28cc6b00a392217
1 // implement a prototype SNMP Agent using ASNMP and ACE
3 #include <ace/Reactor.h>
4 #include <ace/SOCK_Dgram.h>
5 #include <ace/INET_Addr.h>
6 #include <ace/Signal.h>
8 #include "agent_impl.h"
10 agent_impl::agent_impl(unsigned short port, const char *rd, const char *wr) :
11 sagent(port)
13 ACE_TRACE("agent_impl::agent_impl");
14 tgt_.set_read_community(rd);
15 tgt_.set_write_community(wr);
16 agent_clock_.start();
19 agent_impl::~agent_impl()
21 ACE_TRACE("agent_impl::~agent_impl");
24 // callback : have received a Pdu from the target host with given read comm str
25 // this is really simplistic, but gives the general idea
26 int agent_impl::handle_get( Pdu &pdu, UdpTarget &target)
28 ACE_TRACE("agent_impl::handle_get");
29 OctetStr mgr_rd_str, agent_rd_str;
30 target.get_read_community(mgr_rd_str); // requster's read community string
31 tgt_.get_read_community(agent_rd_str); // this agent's read community string
33 // 1. verify we have a valid read string else drop pdu (no response to caller)
34 if (mgr_rd_str != agent_rd_str) {
35 ACE_DEBUG((LM_DEBUG, "agent_impl::handle_get: invalid read community recvd\n"));
36 return 0;
39 // 2. iterate over each varbind in the pdu, filling providing responses
40 int fdone = 0;
41 for (int i = 0; (i < pdu.get_vb_count()) && !fdone; i++) {
42 Vb vb;
43 pdu.get_vb(vb, i);
44 if (get_response(vb)) { // set a value for the oid if we can else
45 set_error_status(&pdu, SNMP_ERROR_NO_SUCH_NAME); // these ought to be member
46 set_error_index(&pdu, i); // functions but are not yet...
47 fdone++; // trigger flag to exit loop early
49 else // failed, return noSuch error
50 pdu.set_vb(vb, i);
53 // 3. lastly, return the pkt to the caller
54 return respond(pdu, target);
57 // this routine makes up the brains of the agent
58 // it knows only the MIB II system group set of variables for a get operation
59 int agent_impl::get_response(Vb& vb)
61 // these objects represent the MIB II system group per RFC 1213
62 static Oid sysDescr("1.3.6.1.2.1.1.1.0"),
63 sysObjectID("1.3.6.1.2.1.1.2.0"), sysUpTime("1.3.6.1.2.1.1.3.0"),
64 sysContact("1.3.6.1.2.1.1.4.0"), sysName("1.3.6.1.2.1.1.5.0"),
65 sysLocation("1.3.6.1.2.1.1.6.0"), sysServices("1.3.6.1.2.1.1.7.0");
67 Oid oid;
68 vb.get_oid(oid);
69 if (oid == sysDescr) {
70 OctetStr desc("ASNMP Prototype Agent 1.0");
71 vb.set_value(desc);
73 else if (oid == sysObjectID) { // the IANA gives assigns Enterprise Numbers
74 // see ftp://ftp.isi.edu/in-notes/iana/assignments/enterprise-numbers
75 // for the official list of enterprise numbers. Then under this tree
76 // assign a unique subtree to identify this agent
77 Oid id("1.3.6.1.4.1.2533.9.1");
78 vb.set_value(id);
80 else if (oid == sysUpTime) {
81 ACE_Time_Value tv;
82 agent_clock_.elapsed_time (tv);
83 TimeTicks tt(tv.msec());
84 vb.set_value(tt);
86 else if (oid == sysContact) {
87 OctetStr contact("mrm@acm.org");
88 vb.set_value(contact);
90 else if (oid == sysName) {
91 OctetStr fqdn("foo.org"); // extract this from the gethostbyname() TODO
92 vb.set_value(fqdn);
94 else if (oid == sysLocation) {
95 OctetStr loc("");
96 vb.set_value(loc);
98 else if (oid == sysServices) {
99 SnmpInt32 svcs(72);
100 vb.set_value(svcs);
102 else
103 return 1; // noSuchName
105 return 0;
108 int agent_impl::handle_get_next( Pdu &, UdpTarget &)
110 ACE_TRACE("agent_impl::handle_get_next -NI");
111 return 0;
114 int agent_impl::handle_set( Pdu &, UdpTarget &)
116 ACE_TRACE("agent_impl::handle_set -NI");
117 return 0;
120 // stuff used by process_requests
122 // called when SIGINT
123 static sig_atomic_t finished = 0;
125 extern "C" void
126 sig_handler (int)
128 ACE_TRACE("::sig_handler");
129 finished = 1;
132 int agent_impl::process_requests()
134 ACE_TRACE("agent_impl::process_requests");
135 ACE_Reactor reactor;
137 ACE_Sig_Action sa ((ACE_SignalHandler) sig_handler, SIGINT);
138 ACE_UNUSED_ARG (sa);
140 // Read data from other side.
141 if (reactor.register_handler (this, ACE_Event_Handler::READ_MASK) == -1)
142 ACE_ERROR_RETURN ((LM_ERROR, "ACE_Reactor::register_handler"), -1);
144 // TODO: register signal handler to shut down gracefully here
146 while (!finished)
148 reactor.handle_events ();
149 ACE_DEBUG ((LM_DEBUG, "return from handle events\n"));
152 ACE_DEBUG ((LM_DEBUG, "return from handle events - normal shut down\n"));
153 return 0;