Wiki source code of Integrate the generated code into the main program
Last modified by Frank Fock on 2024/05/25 11:55
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | This page describes how-to integrate AgenPro generated code for SNMP4J-Agent into an agent's main program. | ||
| 2 | |||
| 3 | Not Applicable for Maven-Task-Pluign Usage | ||
| 4 | |||
| 5 | {{warning}} | ||
| 6 | Although this description uses AgenPro Maven-Plugin files, it steps described herein are not useful when actually also using the AgenPro-Maven-Plugin. The plugin facilitates already many of the below steps. Please read the [[AgenPro Manual >>url:https://agentpp.com/agenpro4/AgenProManual.pdf]]for details.Type your warning message here. | ||
| 7 | {{/warning}} | ||
| 8 | |||
| 9 | == Prerequisites == | ||
| 10 | |||
| 11 | 1. The code has been generated by AgenPro into a folder that can be used to further develop the agent software. Otherwise, modifications to the generated code cannot be (easily) integrated into regenerated code, for example if there is a MIB specification update. | ||
| 12 | For this tutorial that **gen-folder** is C:\Users\myuser\Documents\agenpro4\generated\snmp4j\src\org\snmp4j\agent\tutorial. You can replace the name **gen-folder **in the following by this folder. | ||
| 13 | 1. //Optionally//: Install the latest SNMP4J.jar, SNMP4J-Agent.jar, and optionally SNMP4J-AgentX.jar in a **lib** directory. For this tutorial we use C:\Users\myuser\Documents\agenpro4\lib for that. You can dowload latest release JARs from the AGENT++ Maven repository at: [[https:~~/~~/snmp.app/dist/release>>https://snmp.app/dist/release]]. | ||
| 14 | 1. Install Java JDK (the JRE is not sufficient, because you need the Java compiler). | ||
| 15 | 1. //Optionally~:// Install Maven 3.x. Note: If you do not install Maven, you need to perform step 2 and manually compile the sources (not explained here). | ||
| 16 | 1. If you have already a main program for your SNMP4J-Agent ready, then continue with step ?? of the Step-by-step Guide below. | ||
| 17 | |||
| 18 | == Step-by-step Guide == | ||
| 19 | |||
| 20 | 1. ((( | ||
| 21 | //Optionally//: **Only if you are using your own main agent program implementation**, you need first to fix the import section as described in the box below and then add the following function registerMIBs() to that class: | ||
| 22 | |||
| 23 | **Import Statements for Integration** | ||
| 24 | |||
| 25 | |((( | ||
| 26 | {{code}} | ||
| 27 | // Alternatively you can also import the complete package you specified in AgenPro project wizard step 1: | ||
| 28 | // import <package>.*; | ||
| 29 | import org.snmp4j.agent.tutorial.Modules; | ||
| 30 | import org.snmp4j.agent.tutorial.Snmp4jAgentTutorialMib; | ||
| 31 | {{/code}} | ||
| 32 | |||
| 33 | |||
| 34 | ))) | ||
| 35 | |||
| 36 | **Code to Register Generated MIBs** | ||
| 37 | |||
| 38 | {{code}} | ||
| 39 | /** | ||
| 40 | * Register your own MIB modules in the specified context of the agent. | ||
| 41 | * The {@link MOFactory} provided to the <code>Modules</code> constructor | ||
| 42 | * is returned by {@link #getFactory()}. | ||
| 43 | */ | ||
| 44 | protected void registerMIBs() | ||
| 45 | { | ||
| 46 | if (modules == null) { | ||
| 47 | modules = new Modules(getFactory()); | ||
| 48 | } | ||
| 49 | try { | ||
| 50 | modules.registerMOs(server, null); | ||
| 51 | } | ||
| 52 | catch (DuplicateRegistrationException drex) { | ||
| 53 | logger.error("Duplicate registration: "+drex.getMessage()+"."+ | ||
| 54 | " MIB object registration may be incomplete!", drex); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | {{/code}} | ||
| 58 | |||
| 59 | Then call the above function in your agent initialization code before the agent is actually started with agent.run(): | ||
| 60 | |||
| 61 | **Call Module Registration** | ||
| 62 | |||
| 63 | {{code}} | ||
| 64 | public void run() { | ||
| 65 | // initialize agent before registering our own modules | ||
| 66 | agent.initialize(); | ||
| 67 | // this requires sysUpTime to be available. | ||
| 68 | registerMIBs(); // <---- here the MIB module registration is called | ||
| 69 | // add proxy forwarder | ||
| 70 | agent.setupProxyForwarder(); | ||
| 71 | // now continue agent setup and launch it. | ||
| 72 | agent.run(); | ||
| 73 | } | ||
| 74 | {{/code}} | ||
| 75 | ))) | ||
| 76 | 1. ((( | ||
| 77 | Compile the sources with Maven: | ||
| 78 | |||
| 79 | **Agent Compilation** | ||
| 80 | |||
| 81 | {{code language="none"}} | ||
| 82 | cd C: | ||
| 83 | cd \Users\myuser\Documents\agenpro4\generated\snmp4j\ | ||
| 84 | mvn clean install | ||
| 85 | {{/code}} | ||
| 86 | ))) | ||
| 87 | 1. ((( | ||
| 88 | Run the agent: | ||
| 89 | |||
| 90 | {{code language="bash"}} | ||
| 91 | java -jar target\tutorial-1.0.0-SNAPSHOT-jar-with-dependencies.jar udp:0.0.0.0/161 | ||
| 92 | {{/code}} | ||
| 93 | |||
| 94 | or alternatively without Maven support: | ||
| 95 | |||
| 96 | **Run the Agent** | ||
| 97 | |||
| 98 | {{code language="none"}} | ||
| 99 | cd ..\..\..\.. | ||
| 100 | java -cp .;..\lib\* org.snmp4j.agent.tutorial.Agent udp:0.0.0.0/161 | ||
| 101 | {{/code}} | ||
| 102 | ))) | ||
| 103 | |||
| 104 | |||
| 105 |