[ictk-cvs] ictk/samples/simpleICSClient ChannelListenerExample.java,NONE,1.1 SimpleICSClient.java,1.
chess library in java, PGN, FEN, ICS
Brought to you by:
jvarsoke
|
From: <jva...@us...> - 2003-08-30 04:46:02
|
Update of /cvsroot/ictk/ictk/samples/simpleICSClient In directory sc8-pr-cvs1:/tmp/cvs-serv9493/samples/simpleICSClient Modified Files: SimpleICSClient.java Added Files: ChannelListenerExample.java Log Message: added ChannelListenerExample to SimpleICSClient to demonstrate adding a channel listener and how to make it exclusive. --- NEW FILE: ChannelListenerExample.java --- /* * ICTK - Internet Chess ToolKit * More information is available at http://ictk.sourceforge.net * Copyright (C) 2002 J. Varsoke <jva...@gh...> * All rights reserved. * * $Id: ChannelListenerExample.java,v 1.1 2003/08/30 04:45:57 jvarsoke Exp $ * * This file is part of ICTK. * * ICTK is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * ICTK is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with ICTK; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import ictk.boardgame.chess.net.ics.*; import ictk.boardgame.chess.net.ics.event.*; import ictk.boardgame.chess.net.ics.ui.cli.ANSIConsole; import java.util.*; /** this class demonstrates how to create a listener for particular * channels. Mostly it's used to demonstrate how a class subscribes * to a channel via the ICSEventRouter. This class will label the * messages sent to channel 1 as [help]. */ public class ChannelListenerExample implements ICSEventListener { public boolean showTimestamp = true; public static Calendar cal = new GregorianCalendar(); //colors public final static char ESC = '\u001B'; public final static String BLACK = ESC + "[0;30", YELLOW = ESC + "[0;33m", CYAN = ESC + "[0;36m", BOLD_BLACK = ESC + "[1;30m", BOLD_RED = ESC + "[1;31m", BOLD_YELLOW = ESC + "[1;33m", BOLD_CYAN = ESC + "[1;36m", PLAIN = ESC + "[0;m"; public void icsEventDispatched (ICSEvent evt) { ICSChannelEvent chEvt = (ICSChannelEvent) evt; String prefix = null; if (showTimestamp) { System.out.print(BOLD_BLACK + getTimestampAsString(evt.getTimestamp()) + PLAIN); } System.out.print("<" + evt.getEventType() + ">"); switch (chEvt.getChannel()) { case 1: System.out.print(BOLD_BLACK); System.out.print("[help]"); if (chEvt.getAccountType().is(ICSAccountType.ADMIN)) System.out.print(BOLD_YELLOW); else if (chEvt.getAccountType().is(ICSAccountType.SERVICE_REP)) System.out.print(BOLD_RED); else System.out.print(PLAIN); System.out.print(chEvt.getPlayer()); System.out.print(BOLD_BLACK); System.out.print(": "); System.out.print(CYAN); System.out.print(chEvt.getMessage()); System.out.println(PLAIN); break; default: System.out.println("subscribed to unhandled channel [" + chEvt.getChannel() + "]"); } System.out.flush(); } public String getTimestampAsString (Date date) { StringBuffer sb = new StringBuffer(5); int tmp = 0; cal.setTime(date); tmp = cal.get(Calendar.HOUR_OF_DAY); if (tmp < 10) sb.append("0"); sb.append(tmp).append(":"); tmp = cal.get(Calendar.MINUTE); if (tmp < 10) sb.append("0"); sb.append(tmp); return sb.toString(); } } Index: SimpleICSClient.java =================================================================== RCS file: /cvsroot/ictk/ictk/samples/simpleICSClient/SimpleICSClient.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** SimpleICSClient.java 29 Aug 2003 08:42:21 -0000 1.3 --- SimpleICSClient.java 30 Aug 2003 04:45:57 -0000 1.4 *************** *** 35,39 **** --- 35,42 ---- import ictk.boardgame.chess.net.ics.ui.cli.ANSIConsole; + import ictk.boardgame.chess.net.ics.ICSProtocolHandler; + import ictk.boardgame.chess.net.ics.ICSEventRouter; import ictk.boardgame.chess.net.ics.fics.FICSProtocolHandler; + import ictk.boardgame.chess.net.ics.event.ICSEvent; public class SimpleICSClient { *************** *** 41,69 **** String passwd; ! FICSProtocolHandler fics; ANSIConsole ansiConsole; CommandInput commandLine; ! /** Creates new form SimpleICSClient */ ! public SimpleICSClient(String handle, String passwd) { ! this.handle = handle; ! this.passwd = passwd; ! fics = new FICSProtocolHandler(); ! ansiConsole = new ANSIConsole(); ! fics.getEventRouter().setDefaultListener(ansiConsole); ! commandLine = new CommandInput("Simple ICS Client", fics); ! } public void connect() { // Add your handling code here: ! if (!fics.isConnected()) { try { System.out.println("[Client] attempting to connect"); ! fics.setHandle(handle); ! fics.setPassword(passwd); ! fics.setLagCompensation(true); ! fics.connect(); } catch (java.net.UnknownHostException e) { --- 44,91 ---- String passwd; ! ICSProtocolHandler ics; ANSIConsole ansiConsole; CommandInput commandLine; ! /** creates a new SimpleICSClient registereing the ICS protocol handler ! * and the default listener for all events. ! */ ! public SimpleICSClient(String handle, String passwd) { ! this.handle = handle; ! this.passwd = passwd; ! ics = new FICSProtocolHandler(); ! ansiConsole = new ANSIConsole(); ! ICSEventRouter router = ics.getEventRouter(); ! router.setDefaultListener(ansiConsole); ! ! //channel 1 (help) goes to a different listener ! router.addChannelListener(ICSEvent.CHANNEL_EVENT, 1, ! new ChannelListenerExample()); ! ! //need all other channel events to still go to ansiConsole ! ics.getEventRouter().addEventListener(ICSEvent.CHANNEL_EVENT, ansiConsole); ! ! //so the main ChannelEvent listener doesn't also get the event ! router.setChannelExclusive(ICSEvent.CHANNEL_EVENT, 1, true); ! //so the defaultListener doesn't get the event ! router.setExclusive(ICSEvent.CHANNEL_EVENT, true); ! ! commandLine = new CommandInput("Simple ICS Client", ics); ! } + /** if not already connected this will establish a connection to the + * server. + */ public void connect() { // Add your handling code here: ! if (!ics.isConnected()) { try { System.out.println("[Client] attempting to connect"); ! ics.setHandle(handle); ! ics.setPassword(passwd); ! ics.setLagCompensation(true); ! ics.connect(); } catch (java.net.UnknownHostException e) { |