DX Unified Infrastructure Management

  • 1.  Java probe callback and parsing

    Posted Apr 01, 2015 10:36 AM

    I'm looking for an example of a Java probe callback to another probe, like the controller, and how to parse the resulting PDS data.  Any help would be appreciated.

     

    Thanks

     

    Scott




  • 2.  Re: Java probe callback and parsing

    Posted Apr 02, 2015 01:33 AM

    I will provide a couple of examples and do my best to explain them.

     

    First, here is a simple callback that does not require any arguments.

     

    
    NimRequest getHubRequest = new NimRequest("controller", "gethub");
    
    

    PDS getHubPds = getHubRequest.send();

    
    

     

    This will return a list of key/value pairs that you can retrieve with the following.  We want to just return the hubname in this example.

     

    getHubPds.getStringIfExists("hubname");
    

     

    There is also a utility to display the PDS output to help you debug.

     

    System.out.println(NimUtility.displayPDS(getHubPds);
    

     

    Second, a callback that accepts an argument.

     

    PDS args1 = new PDS();
      args1.putString("directory", probeList.getString("workdir"));
      args1.putString("file", probeList.getString("config"));
      args1.putInt("buffer_size", 1000000);
      NimRequest probeConfig = new NimRequest("Domain/Hub/Robot/controller", "text_file_get", args1);
      PDS configPDS = probeConfig.send();
    
      System.out.println(configPDS.getString("file_content"));
    

     

    As you can see here we are creating a PDS (Portable Data Stream) to store the key/value pair that needs to be passed into the callback. These are the same key/value pairs you will see in the Probe Utility. We then print out the file_content key that is returned by the method.

     

    Third, looping through a PDS structure (like a robot's probe list).

     

     NimRequest getProbesRequest = null;
     PDS getProbesResponse = null;
      try {
           getProbesRequest = new NimRequest(robotaddress+"/controller", "probe_list");
           logger.warn(robotaddress+"/controller");
           getProbesResponse = getProbesRequest.send();
      } catch (NimException e) {
           logger.log(NimLog.ERROR, "Failed to retrieve the list of probes from robot " + robotname + ". We will skip discovering this robot for now. Cause: " + e.getLocalizedMessage());
           logger.logStackTrace(NimLog.INFO, e);
      } finally {
           if (getProbesRequest != null) {
           getProbesRequest.disconnect();
           getProbesRequest.close();
      }
    }
    if (getProbesResponse != null) { // ok, we received a list of probes from the robot
           Enumeration<String> p = getProbesResponse.keys();
           while (p.hasMoreElements()) {
                String probeKey = p.nextElement();
                PDS probeList = getProbesResponse.getPDS(probeKey);
                System.out.println(NimUtility.displayPDS(probeList);
           }
    }
    

        

    This one has a few more moving parts, but the part we'll focus on is lines 16-23. First we check to see if we got a response, if we did the next step is to create an Enumeration in order to loop through each probe in the list. Then we will create the while loop, grab the probe name and print it out.

     

     

    If you have any more questions please feel free to post here or contact me at bryan.morrow@ca.com and I'll do my best to help.

     

    Bryan