All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sparshui.server.Group Maven / Gradle / Ivy

There is a newer version: 14.31.10
Show newest version
package com.sparshui.server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.sparshui.GestureType;
import com.sparshui.common.Event;
import com.sparshui.common.TouchState;
import com.sparshui.gestures.Gesture;

/**
 * Represents a group of touch points 
 * for the gesture server.
 * 
 * @author Tony Ross
 * 
 */
public class Group {

	private int _id;
	private List _gestureTypes;
	private List _gestures;
	private List _touchPoints;
	private ServerToClientProtocol _clientProtocol;

	/**
	 * Construct a new group with the given gesture IDs and
	 * the given connection to the client.  Groups are associate
	 * with one client only.
	 * @param id 
	 * 
	 * @param gestureTypes
	 * 		The list of gesture types (integer or String class name) 
	 *    that this group should process.
	 * @param clientProtocol
	 * 		Represents the connection to the client.
	 */
	public Group(int id, List gestureTypes,
			ServerToClientProtocol clientProtocol) {
		_id = id;
		_gestureTypes = gestureTypes;
		_gestures = new ArrayList();
		_touchPoints = new ArrayList();
		_clientProtocol = clientProtocol;
		for (int i = 0; i < _gestureTypes.size(); i++) {
		  Gesture gesture = GestureFactory.createGesture(_gestureTypes.get(i));
		  if (gesture != null)
  			_gestures.add(gesture);
		}
	}

	/**
	 * 
	 * @return
	 * 		The group ID
	 */
	public int getID() {
		return _id;
	}

	/**
   * Update the given touch point that belongs to this group.
   * 
   * @param changedPoint
   *          The changed touch point.
   */
  public synchronized void update(TouchPoint changedPoint) {
    List events = new ArrayList();
    
    int state = changedPoint.getState();

    if (state == TouchState.BIRTH)
      _touchPoints.add(changedPoint);

    /*
    System.out.print("Group _touchPoints ");
    for (int i = 0; i < _touchPoints.size(); i++) {
      System.out.print(" / " + i + ": " + (TouchPoint) _touchPoints.get(i));
    }
    System.out.println();
    */
    
    //List clonedPoints = null;
    /*
     * // until this is implemented somewhere, why go to the trouble? -- BH
     * 
     * clonedPoints = new Vector(); for (int i = 0; i < nPoints; i++) {
     * TouchPoint touchPoint = (TouchPoint) _touchPoints.get(i); synchronized
     * (touchPoint) { TouchPoint clonedPoint = (TouchPoint) touchPoint.clone();
     * clonedPoints.add(clonedPoint); } }
     */
    for (int i = 0; i < _gestures.size(); i++) {
      Gesture gesture = _gestures.get(i);
      // System.out.println(_gestures.size());
      // System.out.println("Gesture allowed: " + gesture.getName());
      events.addAll(gesture.processChange(
          //clonedPoints == null ? 
          _touchPoints
          //: clonedPoints
          , changedPoint));
      // System.out.println("Got some events - size: " + events.size());
    }

    // moved to after processing. 
    if (state == TouchState.DEATH)
      _touchPoints.remove(changedPoint);

    try {
      _clientProtocol.processEvents(_id, events);
    } catch (IOException e) {
      /*
       * Do nothing here. We're ignoring the error because the client will get
       * killed on the next touch point birth and we do not have a reference to
       * the client or the server from group to avoid circular references.
       */
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy