1 package javaclients.graphical;
  2 
  3 import java.awt.Point;
  4 import java.awt.Rectangle;
  5 
  6 import org.xmlBlaster.util.Global;
  7 import org.xmlBlaster.client.qos.ConnectQos;
  8 import org.xmlBlaster.client.I_XmlBlasterAccess;
  9 import org.xmlBlaster.util.MsgUnit;
 10 
 11 
 12 /**
 13  * This client connects to xmlBlaster and subscribes to a message.
 14  * <p />
 15  * We then publish the message and receive it asynchronous in the update() method.
 16  * <p />
 17  * Invoke: java Simulator
 18  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html" target="others">xmlBlaster interface</a>
 19  */
 20 public class Simulator {
 21 
 22    private Point newPoint(Point oldPoint, Rectangle bound) {
 23       int x, y;
 24       if (oldPoint != null) {
 25          x = (int)Math.round(10.0 * Math.random()) + oldPoint.x - 5;
 26          y = (int)Math.round(10.0 * Math.random()) + oldPoint.y - 5;
 27       }
 28       else {
 29          x = (int)Math.round(bound.width * Math.random()) + bound.x;
 30          y = (int)Math.round(bound.height * Math.random()) + bound.y;
 31       }
 32       
 33       if (x > (bound.x+bound.width)) x = bound.x + bound.width;
 34       if (x < bound.x) x = bound.x;
 35 
 36       if (y > (bound.y+bound.height)) y = bound.y + bound.height;
 37       if (y < bound.y) y = bound.y;
 38       return new Point(x, y);
 39    }
 40    
 41 
 42    public Simulator(final Global glob) {
 43       String oidPrefix = glob.getProperty().get("oidPrefix", "ambulance");
 44       int nmax = glob.getProperty().get("nmax", 5);
 45       int sweeps = glob.getProperty().get("sweeps", 500);
 46       Point[] points = new Point[nmax];
 47       Rectangle bound = new Rectangle(0, 0, 1000, 500);
 48       for (int i=0; i < nmax; i++) points[i] = newPoint(null, bound);
 49 
 50       I_XmlBlasterAccess con = null;
 51       try {
 52          con = glob.getXmlBlasterAccess();
 53 
 54          ConnectQos qos = new ConnectQos(glob);
 55          con.connect(qos, null);  // Login to xmlBlaster, register for updates
 56 
 57          for (int j=0; j < sweeps; j++) {
 58             for (int i=0; i < nmax; i++) {
 59                points[i] = newPoint(points[i], bound);
 60                String content = new String(points[i].x + ";" + points[i].y);
 61                con.publish(new MsgUnit(glob, "<key oid='" + oidPrefix + "." + (i+1) + "'><" + oidPrefix + "/></key>", content.getBytes(),
 62                                            "<qos/>"));
 63             }
 64             Thread.sleep(200L);
 65          }
 66 
 67          for (int i=0; i < nmax; i++) {
 68             con.erase("<key oid='" + oidPrefix + "." + (i+1) + "'/>", null);
 69          }
 70       }
 71       catch (Exception e) {
 72          System.err.println(e.getMessage());
 73       }
 74       finally {
 75          try {
 76             if (con != null) con.disconnect(null);
 77          }
 78          catch (Exception ex) {         
 79             System.err.println(ex.getMessage());
 80          }
 81       }
 82    }
 83 
 84    /**
 85     * Try
 86     * <pre>
 87     *   java Simulator -help
 88     * </pre>
 89     * for usage help
 90     */
 91    public static void main(String args[]) {
 92       Global glob = new Global();
 93 
 94       if (glob.init(args) != 0) { // Get help with -help
 95          System.out.println(glob.usage());
 96          System.out.println("Example: java Simulator -session.name Jack");
 97          System.exit(1);
 98       }
 99 
100       new Simulator(glob);
101    }
102 }


syntax highlighted by Code2HTML, v. 0.9.1