[ictk-cvs] ictk/src/ictk/boardgame/chess/net/ics ICSEventRouter.java,1.4,1.5
chess library in java, PGN, FEN, ICS
Brought to you by:
jvarsoke
|
From: <jva...@us...> - 2003-08-29 08:42:26
|
Update of /cvsroot/ictk/ictk/src/ictk/boardgame/chess/net/ics
In directory sc8-pr-cvs1:/tmp/cvs-serv27976/src/ictk/boardgame/chess/net/ics
Modified Files:
ICSEventRouter.java
Log Message:
Seperated out the GUI from SimpleICSClient so it is more apparent how things
are set up. Added CommandInput. Client GUI is now AWT -- better response to
typing. Also improving the Router
Index: ICSEventRouter.java
===================================================================
RCS file: /cvsroot/ictk/ictk/src/ictk/boardgame/chess/net/ics/ICSEventRouter.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** ICSEventRouter.java 20 Aug 2003 15:42:50 -0000 1.4
--- ICSEventRouter.java 29 Aug 2003 08:42:21 -0000 1.5
***************
*** 33,45 ****
*/
public class ICSEventRouter {
! /** the default router receives all events that aren't sent elsewhere*/
! ICSEventListener defaultRoute;
/** subscribers to ICSEvents */
protected ICSEventListener[][] subscribers;
/** subscribers to individual channels.
! ** the key is the channel number,
** the value is a ICSEventListener[] */
protected HashMap chSubscribers,
/** subscribers to individual boards.
** the key is the board number.
--- 33,48 ----
*/
public class ICSEventRouter {
! /** the default listener receives all events or those not sent to a
! ** exclusive listener (depending on set options). */
! ICSEventListener defaultListener;
/** subscribers to ICSEvents */
protected ICSEventListener[][] subscribers;
/** subscribers to individual channels.
! ** the key is an int[2] with [0] set to the ChannelType (shout, TCh etc)
! ** and the [1] set the the channel number,
** the value is a ICSEventListener[] */
protected HashMap chSubscribers,
+
/** subscribers to individual boards.
** the key is the board number.
***************
*** 49,74 ****
boardSubscribers;
public ICSEventRouter () {
subscribers = new ICSEventListener[ICSEvent.NUM_EVENTS][];
chSubscribers = new HashMap();
boardSubscribers = new HashMap();
}
! /* setDefaltRoute ********************************************************/
/** This listener receives all message that are not exclusively
* processed by some other listener. Uncategorized output is also
! * sent to the default route.
*/
! public void setDefaultRoute (ICSEventListener eh) {
! defaultRoute = eh;
}
! /* getDefaltRoute ********************************************************/
/** This listener receives all message that are not exclusively
* processed by some other listener. Uncategorized output is also
! * sent to the default route.
*/
! public ICSEventListener getDefaultRoute () {
! return defaultRoute;
}
--- 52,93 ----
boardSubscribers;
+ /** a list of which events are exclusively listed to by the
+ ** subscriber (event if null) instead of also being sent
+ ** to the defaultListener */
+ protected boolean[] exclusive;
+
+ /** determines if the channel(shout, tournament channel, etc)
+ ** events will be sent only to the listener subscribing
+ ** specifically to the channel number. If this is false
+ ** the CHANNEL_EVENT or SHOUT_EVENT listener will also
+ ** receive the event. */
+ protected boolean exclusiveChannel,
+
+ exclusiveBoard;
+
+
public ICSEventRouter () {
subscribers = new ICSEventListener[ICSEvent.NUM_EVENTS][];
chSubscribers = new HashMap();
boardSubscribers = new HashMap();
+ exclusive = new boolean[ICSEvent.NUM_EVENTS];
}
! /* setDefaltListener *****************************************************/
/** This listener receives all message that are not exclusively
* processed by some other listener. Uncategorized output is also
! * sent to the default listener.
*/
! public void setDefaultListener (ICSEventListener eh) {
! defaultListener = eh;
}
! /* getDefaltListener *****************************************************/
/** This listener receives all message that are not exclusively
* processed by some other listener. Uncategorized output is also
! * sent to the default listener.
*/
! public ICSEventListener getDefaultListener () {
! return defaultListener;
}
***************
*** 78,86 ****
*/
public void dispatch (ICSEvent evt) {
! if (subscribers[evt.getEventType()] != null) {
}
! else {
! defaultRoute.icsEventDispatched(evt);
}
}
--- 97,146 ----
*/
public void dispatch (ICSEvent evt) {
! int type = evt.getEventType(),
! i = 0;
! ICSEventListener[] listeners = null;
! boolean done = false,
! done2 = false;
!
! switch (type) {
! //channel events
! case ICSEvent.CHANNEL_EVENT:
! case ICSEvent.TOURNAMENT_CHANNEL_EVENT:
! case ICSEvent.SHOUT_EVENT:
! int[] key = new int[2];
! key[0] = type;
! key[1] = ((ICSChannelEvent) evt).getChannel();
!
! listeners = (ICSEventListener[]) chSubscribers.get(key);
! done = exclusiveChannel;
!
! break;
!
! //board events
! case ICSEvent.BOARD_UPDATE_EVENT:
! case ICSEvent.KIBITZ_EVENT:
! case ICSEvent.WHISPER_EVENT:
! case ICSEvent.BOARD_SAY_EVENT:
!
! default:
! break;
}
!
! //send to the specific listerners from the switch
! if (listeners != null)
! for (i = 0; i < listeners.length; i++)
! listeners[i].icsEventDispatched(evt);
!
! //send to the event subscribers if the switch listener wasn't exclusive
! if (!done
! && (listeners = subscribers[type]) != null) {
! for (i=0; i < listeners.length; i++)
! listeners[i].icsEventDispatched(evt);
! done2 = true;
}
+
+ //send to the default route if this event isn't exclusive
+ if (!exclusive[type] && defaultListener != null)
+ defaultListener.icsEventDispatched(evt);
}
***************
*** 95,103 ****
}
/* addBoardListener ******************************************************/
/** adding this type of listener will subscribe the listener to
* the following types of events for this board number:<br>
! * type 1: board updates and moves, resignations<br>
! * type 2: tackbacks, draw offers, adjourn and pause requests<br>
* type 3: kibitzes, whispers, and board says<br>
* NOTE: all types must be registered independently
--- 155,179 ----
}
+ /* setExclusive **********************************************************/
+ /** should the event be routed only to listeners subscribed to this
+ * event, or also to the default listener.
+ *
+ * @param t if true then the default listener will not receive the
+ * event even if there are no listeners for this event.
+ */
+ public void setExclusive (int eventType, boolean t) {
+ exclusive[eventType] = t;
+ }
+
+ /* isExclusive ***********************************************************/
+ public boolean isExclusive (int eventType) {
+ return exclusive[eventType];
+ }
+
/* addBoardListener ******************************************************/
/** adding this type of listener will subscribe the listener to
* the following types of events for this board number:<br>
! * type 1: board updates, moves forward and back, resignations<br>
! * type 2: tackback requests, draw offers, adjourn and pause requests<br>
* type 3: kibitzes, whispers, and board says<br>
* NOTE: all types must be registered independently
***************
*** 111,118 ****
/** adds a listener to a particular channel. This is useful if you want
* to log particular channels, or send them to different display
! * areas.
*/
public void addChannelListener (ICSEventListener eh,
int channelNumber) {
}
}
--- 187,263 ----
/** adds a listener to a particular channel. This is useful if you want
* to log particular channels, or send them to different display
! * areas. If a listener wish to listen to all channel events then
! * it would be better to subscribe via addEventListener().
! *
! * @param channelType is the EventType for this sort of channel. For
! * example: ICSEvent.CHANNEL_EVENT is for normal
! * channel tells, ICSEvent.SHOUT_EVENT is for
! * shouts.
! * @param channelNumber is number of the channel, or in the case of
! * shouts is the type of shout.
*/
public void addChannelListener (ICSEventListener eh,
+ int channelType,
int channelNumber) {
+ int[] key = new int[2];
+ ICSEventListener[] list;
+
+ key[0] = channelType;
+ key[1] = channelNumber;
+
+ list = (ICSEventListener[]) chSubscribers.get(key);
+ list = _addListener(list, eh);
+
+ chSubscribers.put(key, list);
}
+
+ public void removeChannelListener (ICSEventListener eh,
+ int channelType,
+ int channelNumber) {
+ int[] key = new int[2];
+ ICSEventListener[] list;
+
+ key[0] = channelType;
+ key[1] = channelNumber;
+
+ list = (ICSEventListener[]) chSubscribers.get(key);
+ list = _removeListener(list, eh);
+
+ if (list == null)
+ chSubscribers.remove(key);
+ else
+ chSubscribers.put(key, list);
+ }
+
+ /* _addListener **********************************************************/
+ protected ICSEventListener[] _addListener (ICSEventListener[] list,
+ ICSEventListener evt) {
+ ICSEventListener[] tmp = null;
+
+ if (list == null) {
+ tmp = new ICSEventListener[1];
+ tmp[0] = evt;
+ }
+ else {
+ tmp = new ICSEventListener[list.length+1];
+ System.arraycopy(list, 0, tmp, 0, list.length);
+ tmp[list.length] = evt;
+ }
+ return tmp;
+ }
+
+ /* _removeListener *******************************************************/
+ protected ICSEventListener[] _removeListener (ICSEventListener[] list,
+ ICSEventListener evt) {
+ ICSEventListener[] tmp = null;
+ if (list != null && list.length > 1) {
+ tmp = new ICSEventListener[list.length - 1];
+ int count = 0;
+ for (int i=0; i < list.length; i++)
+ if (list[i] != evt)
+ tmp[count++] = list[i];
+ }
+ return tmp;
+ }
+
}
|