/*! This method runs all the time and listens to data from the SK6 and posts the data through
  	events to the event heap
  */
  public void run() {
    String data;
    String[] tokens;
    Integer value;
    Event e;

    System.out.println("Bluetooth thread, waiting for data");
    while (running == true) {
      try {
        if (inBufReader.ready()) {
          data = inBufReader.readLine();
          tokens = data.split(",");
          e = new Event("Shake");
          e.addField("ProxyID", proxyID);
          e.setPostValue("TimeToLive", new Integer(50)); // set time to live to 50 msec
          e.setPostValue("Sensor", tokens[0]);
          for (int i = 1; i < tokens.length; i++) {
            if (tokens[i].startsWith("+")) { // eliminate Number format exception
              value = new Integer(tokens[i].substring(1));
            } else {
              value = new Integer(tokens[i]);
            }
            e.setPostValue("Data" + i, value);
          }
          eventHeap.putEvent(e);
        }
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
    System.out.println("out of run");
  }
  public Sample() {
    try {
      EventHeap eh = new EventHeap("localhost");
      Event e = new Event("MyEventType");
      e.addField("AGE", new Integer(27));
      e.addField("NAME", "Tico Ballagas");
      Event templates[] = new Event[1];
      templates[0] = e;

      // register for a specific event template
      // this will only be matched when EventType = MyEventType and Age = 27 and Name = Tico
      // Ballagas
      eh.registerForEvents(templates, this);

      // send an event - commented out for template demonstration
      // uncomment to see both templates match
      // eh.putEvent(e);

      Event template = new Event("MyEventType");
      template.addField("AGE", Integer.class, FieldValueTypes.FORMAL, FieldValueTypes.FORMAL);
      template.addField("NAME", String.class, FieldValueTypes.FORMAL, FieldValueTypes.FORMAL);

      // register for a generic event template
      // this template will be matched for any event that has EventType = MyEventType
      // and contains the Age and Name fields with correct type
      // the values of Age and Name can be any value
      // (run the c tutorial app as a test, waitForEvent should print and registerForEvents should
      // not)
      Event received = eh.waitForEvent(template);
      System.out.println("waitForEvent: " + received.getEventType());

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  // Deal with absolute or relative cursor update
  public Point updatePoint(Event iEvent) throws EventHeapException {
    String srcMachineName = iEvent.getPostValueString("srcMachineName");
    Point res = new Point(0, 0);
    if (nameToPointTable.containsKey(srcMachineName)) {
      res = (Point) nameToPointTable.get(srcMachineName);
    }

    // Later distinguish between relative and aboslute creation
    Integer pos_x = (Integer) iEvent.getPostValue("X");
    Integer pos_y = (Integer) iEvent.getPostValue("Y");
    if (pos_x.intValue() <= LEAVE_MIN_X
        || pos_x.intValue() >= LEAVE_MAX_X
        || pos_y.intValue() <= LEAVE_MIN_Y
        || pos_y.intValue() >= LEAVE_MAX_Y) {
      res = null;
    } else {
      res = new Point(pos_x.intValue(), pos_y.intValue());
    }

    return res;
  }
    public void run() {
      try {
        Event tEvent = new Event("PointRightEvent");
        tEvent.addField("Screen", mMachineName);
        // This target
        tEvent.addField("PointRightEventType", "MouseEvent");
        // SrcMachine
        tEvent.addField("srcMachineName", String.class);
        // To distinguish unmarked
        tEvent.addField("level", Integer.class);
        // This type of event use Screen to determine target
        // tEvent.addField("Target", String.class);
        while (isRunning) {
          try {
            Event iEvent = mEventHeap.waitForEvent(tEvent);

            Integer level = (Integer) iEvent.getPostValue("level");
            if (level.intValue() < 1) continue;

            String Target = (String) iEvent.getPostValueString("Screen");
            if (!Target.equals(mMachineName)) continue;

            // Check if source is in list exists
            String srcMachineName = iEvent.getPostValueString("srcMachineName");
            // System.pi
            // If yes updatet the point that is associated by
            // else create a new name->point mapping
            boolean leftButtonPressed =
                ((Integer) iEvent.getPostValue("LeftButton")).equals(new Integer(1));
            boolean middeButtonPressed =
                ((Integer) iEvent.getPostValue("MiddleButton")).equals(new Integer(1));
            boolean rightButtonPressed =
                ((Integer) iEvent.getPostValue("RightButton")).equals(new Integer(1));

            // process postion events
            Point newPoint = updatePoint(iEvent);
            if (newPoint != null) nameToPointTable.put(srcMachineName, newPoint);
            else nameToPointTable.remove(srcMachineName);

            // process click events
            if (leftButtonPressed || middeButtonPressed || rightButtonPressed) {
              srcThatIsDragging = srcMachineName;
              handleButtonClickDown(leftButtonPressed, leftIsDown, InputEvent.BUTTON1_MASK);
              handleButtonClickDown(middeButtonPressed, middleIsDown, InputEvent.BUTTON2_MASK);
              handleButtonClickDown(rightButtonPressed, rightIsDown, InputEvent.BUTTON3_MASK);
            } else {
              if (srcThatIsDragging.equals(srcMachineName)) {
                handleButtonClickUp(leftIsDown, InputEvent.BUTTON1_MASK);
                handleButtonClickUp(middleIsDown, InputEvent.BUTTON2_MASK);
                handleButtonClickUp(rightIsDown, InputEvent.BUTTON3_MASK);
              } // end-if
            }
          } catch (EventHeapException eexpWait) {
          }
        } // end-while
      } catch (EventHeapException eexp) {
      }
    }