X-Git-Url: http://plrg.eecs.uci.edu/git/?p=jpf-core.git;a=blobdiff_plain;f=src%2Fexamples%2FRobotManager.java;fp=src%2Fexamples%2FRobotManager.java;h=2bf025b2a4013eab1becaaa31d4f9601608c6150;hp=0000000000000000000000000000000000000000;hb=a9bc9081ebda74eb6ee5451d2b719405db3a955c;hpb=eb7cfaa7d9ce99087e9678a61f6840d3cd48f2b2 diff --git a/src/examples/RobotManager.java b/src/examples/RobotManager.java new file mode 100644 index 0000000..2bf025b --- /dev/null +++ b/src/examples/RobotManager.java @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2014, United States Government, as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All rights reserved. + * + * The Java Pathfinder core (jpf-core) platform is licensed under the + * Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Random; + +/** + * a simplified version of the AWT RobotManager example + */ +public class RobotManager { + + static class Robot { + String id; + + Robot (String id){ + this.id = id; + } + + String getId(){ + return id; + } + + String processSequence(String sequence){ + System.out.printf("robot %s processing sequence: %s\n", id, sequence); + return "Ok"; + } + } + + class StatusAcquisitionThread extends Thread { + boolean done; + + public StatusAcquisitionThread () { + setDaemon(true); + } + + @Override + public void run () { + int n = robotList.size(); + Random random = new Random(0); + + while (!done) { + int idx = random.nextInt(n); + Robot robot = robotList.get(idx); + setRobotOnline(robot, !isRobotOnline(robot.id)); + + try { + Thread.sleep(3000); + } catch (InterruptedException ix) { + } + } + } + + public void terminate () { + done = true; + } + } + + List robotList = new ArrayList(); + HashMap onlineRobots = new HashMap(); + StatusAcquisitionThread acquisitionThread; + + public RobotManager() { + robotList.add( new Robot("RATS-1")); + robotList.add( new Robot("RATS-2")); + robotList.add( new Robot("RCAT-1")); + robotList.add( new Robot("POGO-1")); + + for (Robot r : robotList) { + setRobotOnline(r, true); + } + } + + public void setRobotOnline (Robot robot, boolean isOnline) { + if (isOnline) { + onlineRobots.put(robot.getId(), robot); + } else { + onlineRobots.remove(robot.getId()); + } + } + + public boolean isRobotOnline (String robotName) { + return onlineRobots.containsKey(robotName); + } + + public Robot getOnlineRobot (String robotName) { + return onlineRobots.get(robotName); + } + + public String sendSequence(Robot robot, String sequence) { + return robot.processSequence(sequence); + } + + void startStatusAcquisitionThread (){ + acquisitionThread = new StatusAcquisitionThread(); + acquisitionThread.start(); + } + + void stopStatusAcquisitionThread(){ + acquisitionThread.terminate(); + } + + void processInput (){ + String robotName = "POGO-1"; + String sequence = "left; go"; + + if (isRobotOnline(robotName)){ + Robot robot = getOnlineRobot( robotName); + String result = robot.processSequence(sequence); + System.out.printf("sent sequence \"%s\" to robot %s => %s\n", sequence, robotName, result); + } else { + System.out.print("robot not online: "); + System.out.println(robotName); + } + } + + public static void main (String[] args){ + RobotManager robotManager = new RobotManager(); + robotManager.startStatusAcquisitionThread(); + robotManager.processInput(); + //robotManager.stopStatusAcquisitionThread(); + } +}