BryanKMorrow

Looping through your hubs, robots and probes with Java - Code Sample

Blog Post created by BryanKMorrow Employee on Jun 17, 2015

I wanted to provide an example of looping through all the hubs, robots and probes in your UIM domain.

 

The first thing I like to do is check for the robot's hub, in case whatever custom probe you are running is not deployed to a hub. To do this I use the controller callback of gethub. You also want to make sure that you disconnect and close your NimRequest, which I call in the finally block. In this case I have also set a NimLog instance to the variable logger. I will grab the PDS values with the getString method.

 

ArrayList<String> hublist = new ArrayList<String>();
NimRequest getHubRequest = null;
String hubaddress = "";
  PDS getHubPds = null;
  try {
      getHubRequest = new NimRequest("controller", "gethub");
      getHubPds = getHubRequest.send();
  } catch (NimException e) {
      logger.log(NimLog.ERROR, "Probe failed to contact local robot to get hub information. Cause: " + e.getMessage());
      logger.logStackTrace(NimLog.INFO, e);
      // there is no point to keep on going.
  } finally {
      if (getHubRequest != null) {
            getHubRequest.disconnect();
            getHubRequest.close();
      }
  }
hubaddress = "/" + getHubPds.getString("hubdomain") + "/" + getHubPds.getString("hubname") + "/" + getHubPds.getString("hubrobotname") + "/hub";






 

Once I have the current hub, the next step is to get all the hubs with the hub callback of gethubs. I will store the list of hubs in an ArrayList so I'm not stuck looping through each hub->robot->probe at this time.

 

NimRequest getHubsRequest = null;
  PDS getHubsResponse = null;
  try {
      getHubsRequest = new NimRequest(hubaddress, "gethubs");
      getHubsResponse = getHubsRequest.send();
  } catch (NimException e) {
      logger.log(NimLog.ERROR, "Probe failed to contact local hub " + hubaddress + " to request list of hubs. Cause: " + e.getMessage());
      logger.logStackTrace(NimLog.INFO, e);    
      // there is no point to keep on going - if we do not have a list of all hubs
  } finally {
      if (getHubsRequest != null) {
            getHubsRequest.disconnect();
            getHubsRequest.close();
      }
  }
  if (getHubsResponse == null) { // not sure if this could ever happen
      logger.log(NimLog.ERROR, "Probe received an empty list of hubs from " + hubaddress + ". Giving up the hub scan.");
      // there is no point to keep on going - if we do not have a list of all the hubs
  }
  PDS[] pdss = getHubsResponse.getTablePDSs("hublist");
  for (int entry = 0; entry < pdss.length; entry++)  {
      String hubaddr = pdss[entry].getStringIfExists("addr"); // get hub address
      int status = pdss[entry].getIntIfExists("status"); // get the hub status
      logger.warn("Found hub " + hubaddr );
      if (status != 2) { // Skip inactive hubs
            hublist.add(hubaddr);
      }
  }



 

 

The next step is iteration through the hublist ArrayList and find all the robots + probes connected to each hub. Once I get the probe list I will iterate through that and print the probe name in this example.

 

for (String hub : hublist) {

  logger.warn("Contacting hub " + hub + " for robot list");
  try {
       NimRequest getRobotsRequest = null;
       PDS getRobotsResponse = null;
       try {
            getRobotsRequest = new NimRequest(hub, "getrobots");
            getRobotsResponse = getRobotsRequest.send();
       } catch (NimException e) {
            logger.log(NimLog.ERROR, "Failed to retrieve the list of robots from hub " + hub + ". We will skip discovering this hub for now. Cause: " + e.getMessage());
            logger.logStackTrace(NimLog.INFO, e);
            // if we encounter a callback failure, blacklist this hub
      } finally {
            if (getRobotsRequest != null) {
                 getRobotsRequest.disconnect();
                 getRobotsRequest.close();
            }
       }
      if (getRobotsResponse != null) { // ok, we received a list of robots from the hub
         pdss = getRobotsResponse.getTablePDSs("robotlist");
         for (int entry = 0; entry < pdss.length; entry++) { // robot iteration
            String robotaddress = pdss[entry].getStringIfExists("addr"); // get robot address
            String robotname = pdss[entry].getStringIfExists("name"); // get robot name
            logger.warn("Getting probe list from: " + robotaddress);
            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.getMessage());
                      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);
                      // THIS IS WHERE YOU COULD INTERACT WITH EACH PROBE
                      // I WILL JUST PRINT THE PROBE NAME
                      System.out.println(probeList.getString("name"));
                 } // end while
            } // end if
       } // end for
     } // end if
       
   } catch (NimException e) {
       logger.log(NimLog.ERROR, "Failed to retrieve robot list from " + hub);
   }
 }

 

 

 

As the Probe Framework becomes more popular and available hopefully this post and the others that follow will help you when building out custom probes in Java. If there are any questions or mistakes please let me know.

 

Bryan

Outcomes