1 package com.sparshui.server;
2 
3 import java.io.IOException;
4 
5 import javajs.util.Lst;
6 
7 import com.sparshui.GestureType;
8 import com.sparshui.common.Event;
9 import com.sparshui.common.TouchState;
10 import com.sparshui.gestures.Gesture;
11 
12 /**
13  * Represents a group of touch points
14  * for the gesture server.
15  *
16  * @author Tony Ross
17  *
18  */
19 public class Group {
20 
21 	private int _id;
22 	private Lst<GestureType> _gestureTypes;
23 	private Lst<Gesture> _gestures;
24 	private Lst<TouchPoint> _touchPoints;
25 	private ServerToClientProtocol _clientProtocol;
26 
27 	/**
28 	 * Construct a new group with the given gesture IDs and
29 	 * the given connection to the client.  Groups are associate
30 	 * with one client only.
31 	 * @param id
32 	 *
33 	 * @param gestureTypes
34 	 * 		The list of gesture types (integer or String class name)
35 	 *    that this group should process.
36 	 * @param clientProtocol
37 	 * 		Represents the connection to the client.
38 	 */
Group(int id, Lst<GestureType> gestureTypes, ServerToClientProtocol clientProtocol)39 	public Group(int id, Lst<GestureType> gestureTypes,
40 			ServerToClientProtocol clientProtocol) {
41 		_id = id;
42 		_gestureTypes = gestureTypes;
43 		_gestures = new Lst<Gesture>();
44 		_touchPoints = new Lst<TouchPoint>();
45 		_clientProtocol = clientProtocol;
46 		for (int i = 0; i < _gestureTypes.size(); i++) {
47 		  Gesture gesture = GestureFactory.createGesture(_gestureTypes.get(i));
48 		  if (gesture != null)
49   			_gestures.addLast(gesture);
50 		}
51 	}
52 
53 	/**
54 	 *
55 	 * @return
56 	 * 		The group ID
57 	 */
getID()58 	public int getID() {
59 		return _id;
60 	}
61 
62 	/**
63    * Update the given touch point that belongs to this group.
64    *
65    * @param changedPoint
66    *          The changed touch point.
67    */
update(TouchPoint changedPoint)68   public synchronized void update(TouchPoint changedPoint) {
69     Lst<Event> events = new Lst<Event>();
70 
71     int state = changedPoint.getState();
72 
73     if (state == TouchState.BIRTH)
74       _touchPoints.addLast(changedPoint);
75 
76     /*
77     System.out.print("Group _touchPoints ");
78     for (int i = 0; i < _touchPoints.size(); i++) {
79       System.out.print(" / " + i + ": " + (TouchPoint) _touchPoints.get(i));
80     }
81     System.out.println();
82     */
83 
84     //List<TouchPoint> clonedPoints = null;
85     /*
86      * // until this is implemented somewhere, why go to the trouble? -- BH
87      *
88      * clonedPoints = new Vector(); for (int i = 0; i < nPoints; i++) {
89      * TouchPoint touchPoint = (TouchPoint) _touchPoints.get(i); synchronized
90      * (touchPoint) { TouchPoint clonedPoint = (TouchPoint) touchPoint.clone();
91      * clonedPoints.add(clonedPoint); } }
92      */
93     for (int i = 0; i < _gestures.size(); i++) {
94       Gesture gesture = _gestures.get(i);
95       //System.out.println(_gestures.size());
96       //System.out.println("Gesture allowed: " + gesture.getName());
97       events.addAll(gesture.processChange(
98           //clonedPoints == null ?
99           _touchPoints
100           //: clonedPoints
101           , changedPoint));
102       //System.out.println("Got some events - size: " + events.size());
103     }
104 
105     // moved to after processing.
106     if (state == TouchState.DEATH)
107       _touchPoints.removeObj(changedPoint);
108 
109     try {
110       _clientProtocol.processEvents(_id, events);
111     } catch (IOException e) {
112       /*
113        * Do nothing here. We're ignoring the error because the client will get
114        * killed on the next touch point birth and we do not have a reference to
115        * the client or the server from group to avoid circular references.
116        */
117     }
118   }
119 
120 }
121