You can subscribe to this list here.
2002 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
(4) |
Dec
(1) |
---|
From: <rw...@us...> - 2002-12-17 18:39:08
|
Update of /cvsroot/wett-p2p/org/wettp2p/messagehandler In directory sc8-pr-cvs1:/tmp/cvs-serv14250/org/wettp2p/messagehandler Modified Files: Message.java Log Message: Message.DefaultImpl now implements Serializable Index: Message.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/messagehandler/Message.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Message.java 12 Nov 2002 02:29:38 -0000 1.3 --- Message.java 17 Dec 2002 18:39:00 -0000 1.4 *************** *** 80,87 **** * implementation of the Message interface. * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 **/ ! public static class DefaultImpl implements Message { /** the type of the message, stored as a class object */ --- 80,87 ---- * implementation of the Message interface. * ! * @author Paulo Mouat, Robert Wei ! * @version 1.4, 2002/12/16 **/ ! public static class DefaultImpl implements Message, java.io.Serializable { /** the type of the message, stored as a class object */ |
From: <rb...@us...> - 2002-11-12 02:29:41
|
Update of /cvsroot/wett-p2p/org/wettp2p/messagehandler In directory usw-pr-cvs1:/tmp/cvs-serv24619 Modified Files: Message.java MessageConsumer.java MessageFilter.java MessageFilterPriority.java MessageHandler.java MessageProducer.java MessageQueue.java MessageType.java Log Message: I added the GPL copyright/copyleft notice. Index: Message.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/messagehandler/Message.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Message.java 8 Nov 2002 04:13:28 -0000 1.2 --- Message.java 12 Nov 2002 02:29:38 -0000 1.3 *************** *** 11,14 **** --- 11,30 ---- * implementation of this interface. * + * Copyright (C) 2002, Paulo Mouat + * + * This program 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. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * * @author Paulo Mouat * @version 1.0, 2002/10/25 Index: MessageConsumer.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/messagehandler/MessageConsumer.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** MessageConsumer.java 8 Nov 2002 04:13:28 -0000 1.2 --- MessageConsumer.java 12 Nov 2002 02:29:38 -0000 1.3 *************** *** 9,12 **** --- 9,28 ---- * implementation of this interface. * + * Copyright (C) 2002, Paulo Mouat + * + * This program 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. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * * @author Paulo Mouat * @version 1.0, 2002/10/25 Index: MessageFilter.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/messagehandler/MessageFilter.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** MessageFilter.java 8 Nov 2002 04:13:28 -0000 1.2 --- MessageFilter.java 12 Nov 2002 02:29:38 -0000 1.3 *************** *** 6,9 **** --- 6,25 ---- * implementation of this interface. * + * Copyright (C) 2002, Paulo Mouat + * + * This program 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. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * * @author Paulo Mouat * @version 1.0, 2002/10/25 Index: MessageFilterPriority.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/messagehandler/MessageFilterPriority.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** MessageFilterPriority.java 8 Nov 2002 04:13:28 -0000 1.2 --- MessageFilterPriority.java 12 Nov 2002 02:29:38 -0000 1.3 *************** *** 5,8 **** --- 5,24 ---- * message filter priorities. * + * Copyright (C) 2002, Paulo Mouat + * + * This program 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. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * * @author Paulo Mouat * @version 1.0, 2002/10/25 Index: MessageHandler.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/messagehandler/MessageHandler.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** MessageHandler.java 8 Nov 2002 04:13:28 -0000 1.2 --- MessageHandler.java 12 Nov 2002 02:29:38 -0000 1.3 *************** *** 9,12 **** --- 9,28 ---- * Manages and controls message traffic. * + * Copyright (C) 2002, Paulo Mouat + * + * This program 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. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * * @author Paulo Mouat * @version 1.0, 2002/10/25 Index: MessageProducer.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/messagehandler/MessageProducer.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** MessageProducer.java 8 Nov 2002 04:13:28 -0000 1.2 --- MessageProducer.java 12 Nov 2002 02:29:38 -0000 1.3 *************** *** 6,9 **** --- 6,25 ---- * implementation of this interface. * + * Copyright (C) 2002, Paulo Mouat + * + * This program 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. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * * @author Paulo Mouat * @version 1.0, 2002/10/25 Index: MessageQueue.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/messagehandler/MessageQueue.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** MessageQueue.java 8 Nov 2002 04:13:28 -0000 1.2 --- MessageQueue.java 12 Nov 2002 02:29:38 -0000 1.3 *************** *** 11,14 **** --- 11,30 ---- * depends on structural integrity is synchronized. * + * Copyright (C) 2002, Paulo Mouat + * + * This program 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. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * * @author Paulo Mouat * @version 1.0, 2002/10/25 Index: MessageType.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/messagehandler/MessageType.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** MessageType.java 2 Nov 2002 05:41:21 -0000 1.1 --- MessageType.java 12 Nov 2002 02:29:38 -0000 1.2 *************** *** 5,8 **** --- 5,24 ---- * message types. * + * Copyright (C) 2002, Paulo Mouat + * + * This program 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. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * * @author Paulo Mouat * @version 1.0, 2002/10/25 |
Update of /cvsroot/wett-p2p/org/wettp2p/messagehandler In directory usw-pr-cvs1:/tmp/cvs-serv1243 Modified Files: Message.java MessageConsumer.java MessageFilter.java MessageFilterPriority.java MessageHandler.java MessageProducer.java MessageQueue.java Log Message: Submitting updated message handler code for rben who is submitting it for Paulo. Index: Message.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/messagehandler/Message.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Message.java 2 Nov 2002 05:41:21 -0000 1.1 --- Message.java 8 Nov 2002 04:13:28 -0000 1.2 *************** *** 1,182 **** ! package org.wettp2p.messagehandler; ! ! /** ! * Message interface. A message is defined as ! * having a type and its contents. The type is ! * indicated by an instance of <code>MessageType</code> ! * and contents is an arbitrary Java class stored ! * as a reference to <code>Object</code>. ! * ! * A static inner class is provided by way of default ! * implementation of this interface. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public interface Message ! { ! /** ! * Returns the MessageType object corresponding ! * to the message type. ! * ! * @return the MessageType object. ! **/ ! MessageType getType(); ! ! /** ! * Sets the massage type. ! * ! * @param type the MessageType object. ! **/ ! void setType( MessageType type ); ! ! /** ! * Returns the name of the producer that originated ! * this message. ! * ! * @return the contents Object. ! **/ ! String getProducerName(); ! ! /** ! * Sets the message producer name. ! * ! * @param name the producer name. ! **/ ! void setProducerName( String name ); ! ! /** ! * Returns the contents of the message. ! * ! * @return the contents Object. ! **/ ! Object getContents(); ! ! /** ! * Sets the contents of the message. ! * ! * @param contents the contents Object. ! **/ ! void setContents( Object contents ); ! ! /** ! * A static inner class providing a default ! * implementation of the Message interface. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public static class DefaultImpl implements Message ! { ! /** the type of the message */ ! private MessageType messageType = MessageType.GLOBAL; ! ! /** the producer name of the message */ ! private String producerName; ! ! /** the contents of the message */ ! private Object contents; ! ! /** ! * Creates an instance of Message.DefaultImpl. ! **/ ! public DefaultImpl() ! { ! } ! ! /** ! * Creates an instance of Message.DefaultImpl. ! * ! * @param messageType the type of the message ! * @param contents the contents of the message ! **/ ! public DefaultImpl( MessageType messageType, Object contents ) ! { ! this.messageType = messageType; ! this.contents = contents; ! } ! ! /** ! * Creates an instance of Message.DefaultImpl. ! * ! * @param messageType the type of the message ! * @param producerName the name of the producer of the message ! * @param contents the contents of the message ! **/ ! public DefaultImpl( MessageType messageType, String producerName, Object contents ) ! { ! this.messageType = messageType; ! this.producerName = producerName; ! this.contents = contents; ! } ! ! /** ! * Sets the massage type. ! * ! * @param type the MessageType object. ! **/ ! public void setType( MessageType messageType ) ! { ! this.messageType = messageType; ! } ! ! /** ! * Returns the MessageType object corresponding ! * to the message type. ! * ! * @return the MessageType object. ! **/ ! public MessageType getType() ! { ! return messageType; ! } ! ! /** ! * Sets the message producer name. ! * ! * @param name the producer name. ! **/ ! public void setProducerName( String producerName ) ! { ! this.producerName = producerName; ! } ! ! /** ! * Returns the name of the producer that originated ! * this message. ! * ! * @return the producer name. ! **/ ! public String getProducerName() ! { ! return producerName; ! } ! ! /** ! * Sets the contents of the message. ! * ! * @param contents the contents Object. ! **/ ! public void setContents( Object contents ) ! { ! this.contents = contents; ! } ! ! /** ! * Returns the contents of the message. ! * ! * @return the contents Object. ! **/ ! public Object getContents() ! { ! return contents; ! } ! ! public String toString() ! { ! return "message: type " + ( messageType != null ? messageType.toString() : "null" ) + ! ", producer " + ( producerName != null ? producerName : "null" ) + ! ", contents " + ( contents != null ? contents : "null" ); ! } ! } } --- 1,182 ---- ! package org.wettp2p.messagehandler; ! ! /** ! * Message interface. A message is defined as ! * having a type and its contents. The type is ! * indicated by an instance of <code>MessageType</code> ! * and contents is an arbitrary Java class stored ! * as a reference to <code>Object</code>. ! * ! * A static inner class is provided by way of default ! * implementation of this interface. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public interface Message ! { ! /** ! * Returns the MessageType object corresponding ! * to the message type. ! * ! * @return the MessageType object. ! **/ ! Class getType(); ! ! /** ! * Sets the massage type. ! * ! * @param type the message type class object. ! **/ ! void setType( Class type ); ! ! /** ! * Returns the name of the producer that originated ! * this message. ! * ! * @return the contents Object. ! **/ ! String getProducerName(); ! ! /** ! * Sets the message producer name. ! * ! * @param name the producer name. ! **/ ! void setProducerName( String name ); ! ! /** ! * Returns the contents of the message. ! * ! * @return the contents Object. ! **/ ! Object getContents(); ! ! /** ! * Sets the contents of the message. ! * ! * @param contents the contents Object. ! **/ ! void setContents( Object contents ); ! ! /** ! * A static inner class providing a default ! * implementation of the Message interface. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public static class DefaultImpl implements Message ! { ! /** the type of the message, stored as a class object */ ! private Class messageType = org.wettp2p.messagehandler.messagetypes.Global.class; ! ! /** the producer name of the message */ ! private String producerName; ! ! /** the contents of the message */ ! private Object contents; ! ! /** ! * Creates an instance of Message.DefaultImpl. ! **/ ! public DefaultImpl() ! { ! } ! ! /** ! * Creates an instance of Message.DefaultImpl. ! * ! * @param messageType the class object of the message type ! * @param contents the contents of the message ! **/ ! public DefaultImpl( Class messageType, Object contents ) ! { ! this.messageType = messageType; ! this.contents = contents; ! } ! ! /** ! * Creates an instance of Message.DefaultImpl. ! * ! * @param messageType the class object of the message type ! * @param producerName the name of the producer of the message ! * @param contents the contents of the message ! **/ ! public DefaultImpl( Class messageType, String producerName, Object contents ) ! { ! this.messageType = messageType; ! this.producerName = producerName; ! this.contents = contents; ! } ! ! /** ! * Sets the massage type. ! * ! * @param messageType the class object of the message type ! **/ ! public void setType( Class messageType ) ! { ! this.messageType = messageType; ! } ! ! /** ! * Returns the class object corresponding ! * to the message type. ! * ! * @return the class object of the message type. ! **/ ! public Class getType() ! { ! return messageType; ! } ! ! /** ! * Sets the message producer name. ! * ! * @param name the producer name. ! **/ ! public void setProducerName( String producerName ) ! { ! this.producerName = producerName; ! } ! ! /** ! * Returns the name of the producer that originated ! * this message. ! * ! * @return the producer name. ! **/ ! public String getProducerName() ! { ! return producerName; ! } ! ! /** ! * Sets the contents of the message. ! * ! * @param contents the contents Object. ! **/ ! public void setContents( Object contents ) ! { ! this.contents = contents; ! } ! ! /** ! * Returns the contents of the message. ! * ! * @return the contents Object. ! **/ ! public Object getContents() ! { ! return contents; ! } ! ! public String toString() ! { ! return "message: type " + ( messageType != null ? messageType.toString() : "null" ) + ! ", producer " + ( producerName != null ? producerName : "null" ) + ! ", contents " + ( contents != null ? contents : "null" ); ! } ! } } Index: MessageConsumer.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/messagehandler/MessageConsumer.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** MessageConsumer.java 2 Nov 2002 05:41:21 -0000 1.1 --- MessageConsumer.java 8 Nov 2002 04:13:28 -0000 1.2 *************** *** 1,114 **** ! package org.wettp2p.messagehandler; ! ! import java.util.HashSet; ! ! /** ! * MessageConsumer interface. ! * A static inner class is provided by way of default ! * implementation of this interface. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public interface MessageConsumer ! { ! /** ! * Receives the passed <code>Message</code>. ! * @param message the <code>Message</code> to be received. ! **/ ! void receive( Message message ); ! ! /** ! * Adds to the list of <code>MessageType</code>s types ! * this instance is interested in. ! * @param type the <code>MessageType</code>. ! **/ ! void subscribe( MessageType type ); ! ! /** ! * Adds a producer name to the list of producers whose ! * messages an instance of this class is interested in. ! * @param producerName the name of the producer. ! **/ ! void subscribe( String producerName ); ! ! /** ! * A static inner class providing a default ! * implementation of the MessageConsumer interface. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public static class DefaultImpl implements MessageConsumer ! { ! /** the message types subscribed by the consumer */ ! private HashSet subscribedTypes = new HashSet(); ! ! /** the producers subscribed by the consumer */ ! private HashSet subscribedProducers = new HashSet(); ! ! /** the consumer name, defaulted to the fully qualified class name */ ! private String name = this.getClass().getName(); ! ! /** ! * Default constructor. An instance created via this ! * constructor will have its name default to the fully ! * qualified class name. ! **/ ! public DefaultImpl() ! { ! } ! ! /** ! * Creates an instance and gives it the passed name. ! * @param name the name given to the instance. ! **/ ! public DefaultImpl( String name ) ! { ! this.name = name; ! } ! ! /** ! * Receives the passed <code>Message</code>. ! * @param message the <code>Message</code> to be received. ! **/ ! public void receive( Message message ) ! { ! if( subscribedTypes.contains( message.getType() ) || subscribedProducers.contains( message.getProducerName() ) ) ! doIt( message ); ! } ! ! /** ! * Called by the receive( Message message ) template method, ! * firing whatever tasks are useful according to the message. ! * @param message the <code>Message</code>. ! **/ ! protected void doIt( Message message ) ! { ! // does nothing, should be overridden in subclasses ! System.out.println( name + ": doIt(), message is " + message ); ! } ! ! /** ! * Adds to the list of <code>MessageType</code>s types ! * this instance is interested in. ! * @param type the <code>MessageType</code>. ! **/ ! public void subscribe( MessageType type ) ! { ! subscribedTypes.add( type ); ! MessageHandler.getInstance().subscribe( this ); ! } ! ! /** ! * Adds a producer name to the list of producers whose ! * messages an instance of this class is interested in. ! * @param producerName the name of the producer. ! **/ ! public void subscribe( String producerName ) ! { ! subscribedProducers.add( producerName ); ! MessageHandler.getInstance().subscribe( this ); ! } ! } } --- 1,144 ---- ! package org.wettp2p.messagehandler; ! ! import java.util.HashSet; ! import java.util.Iterator; ! ! /** ! * MessageConsumer interface. ! * A static inner class is provided by way of default ! * implementation of this interface. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public interface MessageConsumer ! { ! /** ! * Receives the passed <code>Message</code>. ! * @param message the <code>Message</code> to be received. ! **/ ! void receive( Message message ); ! ! /** ! * Adds a message type to the list of types ! * this instance is interested in. ! * @param type the class object of the message type. ! **/ ! void subscribe( Class type ); ! ! /** ! * Adds a producer name to the list of producers whose ! * messages an instance of this class is interested in. ! * @param producerName the name of the producer. ! **/ ! void subscribe( String producerName ); ! ! /** ! * A static inner class providing a default ! * implementation of the MessageConsumer interface. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public static class DefaultImpl implements MessageConsumer ! { ! /** the message types subscribed by the consumer */ ! private HashSet subscribedTypes = new HashSet(); ! ! /** the producers subscribed by the consumer */ ! private HashSet subscribedProducers = new HashSet(); ! ! /** the consumer name, defaulted to the fully qualified class name */ ! private String name = this.getClass().getName(); ! ! /** ! * Default constructor. An instance created via this ! * constructor will have its name default to the fully ! * qualified class name. ! **/ ! public DefaultImpl() ! { ! } ! ! /** ! * Creates an instance and gives it the passed name. ! * @param name the name given to the instance. ! **/ ! public DefaultImpl( String name ) ! { ! this.name = name; ! } ! ! /** ! * Receives the passed <code>Message</code>. ! * @param message the <code>Message</code> to be received. ! **/ ! public void receive( Message message ) ! { ! boolean type = subscribes( message.getType() ); ! boolean producer = subscribedProducers.contains( message.getProducerName() ); ! //System.out.println( name + " type match: " + type + " producer match: " + producer ); ! if( type || producer ) ! doIt( message ); ! } ! ! /** ! * Called by the <code>receive( Message message )</code> ! * template method, firing whatever tasks are useful ! * according to the message. ! * @param message the <code>Message</code>. ! **/ ! protected void doIt( Message message ) ! { ! // does nothing, should be overridden in subclasses ! System.out.println( name + ": doIt(), message is " + message ); ! } ! ! /** ! * Adds a message type to the list of types ! * this instance is interested in. ! * @param type the class object of the message type ! * being subscribed. ! **/ ! public void subscribe( Class type ) ! { ! subscribedTypes.add( type ); ! MessageHandler.getInstance().subscribe( this ); ! } ! ! /** ! * Adds a producer name to the list of producers whose ! * messages an instance of this class is interested in. ! * @param producerName the name of the producer. ! **/ ! public void subscribe( String producerName ) ! { ! subscribedProducers.add( producerName ); ! MessageHandler.getInstance().subscribe( this ); ! } ! ! /** ! * Utility method that tests whether this consumer ! * subscribes a particular class of message types or ! * any of its subclasses. ! * @param type the class object of the message type being ! * tested for subscription. ! * @return true if the consumer subscribes the specified ! * class or any of its subclasses, false otherwise. ! **/ ! private boolean subscribes( Class type ) ! { ! ! for( Iterator i = subscribedTypes.iterator(); i.hasNext(); ) ! { ! Object o = i.next(); ! //System.out.println( name + " testing subscribes with type " + type + " and subscribed type " + o ); ! if( ((Class) o).isAssignableFrom( type ) ) ! //if( type.isAssignableFrom( o.getClass() ) ) ! return true; ! } ! ! return false; ! } ! } } Index: MessageFilter.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/messagehandler/MessageFilter.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** MessageFilter.java 2 Nov 2002 05:34:58 -0000 1.1 --- MessageFilter.java 8 Nov 2002 04:13:28 -0000 1.2 *************** *** 1,117 **** ! package org.wettp2p.messagehandler; ! ! /** ! * MessageFilter interface. ! * A static inner class is provided by way of default ! * implementation of this interface. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public interface MessageFilter extends MessageConsumer ! { ! /** ! * Sets the priority value of this filter. ! * @param priority the priority value for the filter. ! **/ ! void setPriority( MessageFilterPriority priority ); ! ! /** ! * Gets the priority value of this filter. ! * @return the priority value for the filter. ! **/ ! MessageFilterPriority getPriority(); ! ! /** ! * Performs filtering on the passed <code>Message</code>. ! * @param message the <code>Message</code> to be filtered. ! **/ ! void filter( Message message ); ! ! /** ! * A static inner class providing a default ! * implementation of the MessageFilter interface. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public static class DefaultImpl extends MessageConsumer.DefaultImpl implements MessageFilter, Comparable ! { ! /** the priority value of the filter, defaults to normal */ ! private MessageFilterPriority priority = MessageFilterPriority.NORMAL; ! ! /** the filter name, defaulted to the fully qualified class name */ ! private String name = this.getClass().getName(); ! ! /** ! * Default constructor. An instance created via this ! * constructor will have its name default to the fully ! * qualified class name. ! **/ ! public DefaultImpl() ! { ! } ! ! /** ! * Creates an instance and gives it the passed name. ! * @param name the name given to the instance. ! **/ ! public DefaultImpl( String name ) ! { ! this.name = name; ! } ! ! /** ! * Called by the receive( Message message ) template method in ! * <code>MessageConsumer.DefaultImpl</code>, firing the ! * <code>filter( message )</code> method. ! * @param message the <code>Message</code>. ! **/ ! protected void doIt( Message message ) ! { ! filter( message ); ! } ! ! /** ! * Performs filtering on the passed <code>Message</code>. ! * @param message the <code>Message</code> to be filtered. ! **/ ! public void filter( Message message ) ! { ! // does nothing, should be overridden in subclasses ! System.out.println( name + " " + getPriority().value() + ": filter()" ); ! } ! ! /** ! * Sets the priority value of this filter. ! * @param priority the priority value for the filter. ! **/ ! public void setPriority( MessageFilterPriority priority ) ! { ! this.priority = priority; ! } ! ! /** ! * Gets the priority value of this filter. ! * @return the priority value for the filter. ! **/ ! public MessageFilterPriority getPriority() ! { ! return priority; ! } ! ! // ! // the Comparable interface ! // ! ! /** ! * Method specified by the Comparable interface. ! **/ ! public int compareTo( Object o ) ! { ! // will throw ClassCastException if o is not a MessageFilter ! ! return ( (MessageFilter) o ).getPriority().value() - priority.value(); ! } ! } } --- 1,102 ---- ! package org.wettp2p.messagehandler; ! ! /** ! * MessageFilter interface. ! * A static inner class is provided by way of default ! * implementation of this interface. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public interface MessageFilter extends MessageConsumer ! { ! /** ! * Sets the priority value of this filter. ! * @param priority the priority value for the filter. ! **/ ! void setPriority( MessageFilterPriority priority ); ! ! /** ! * Gets the priority value of this filter. ! * @return the priority value for the filter. ! **/ ! MessageFilterPriority getPriority(); ! ! /** ! * A static inner class providing a default ! * implementation of the MessageFilter interface. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public static class DefaultImpl extends MessageConsumer.DefaultImpl implements MessageFilter, Comparable ! { ! /** the priority value of the filter, defaults to normal */ ! private MessageFilterPriority priority = MessageFilterPriority.NORMAL; ! ! /** the filter name, defaulted to the fully qualified class name */ ! private String name = this.getClass().getName(); ! ! /** ! * Default constructor. An instance created via this ! * constructor will have its name default to the fully ! * qualified class name. ! **/ ! public DefaultImpl() ! { ! } ! ! /** ! * Creates an instance and gives it the passed name. ! * @param name the name given to the instance. ! **/ ! public DefaultImpl( String name ) ! { ! this.name = name; ! } ! ! /** ! * Called by the receive( Message message ) template method in ! * <code>MessageConsumer.DefaultImpl</code>, firing the ! * <code>filter( message )</code> method. ! * @param message the <code>Message</code>. ! **/ ! protected void doIt( Message message ) ! { ! // does nothing, should be overridden in subclasses ! System.out.println( name + " " + getPriority().value() + ": filter applied to " + message ); ! } ! ! /** ! * Sets the priority value of this filter. ! * @param priority the priority value for the filter. ! **/ ! public void setPriority( MessageFilterPriority priority ) ! { ! this.priority = priority; ! } ! ! /** ! * Gets the priority value of this filter. ! * @return the priority value for the filter. ! **/ ! public MessageFilterPriority getPriority() ! { ! return priority; ! } ! ! // ! // the Comparable interface ! // ! ! /** ! * Method specified by the Comparable interface. ! **/ ! public int compareTo( Object o ) ! { ! // will throw ClassCastException if o is not a MessageFilter ! ! return ( (MessageFilter) o ).getPriority().value() - priority.value(); ! } ! } } Index: MessageFilterPriority.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/messagehandler/MessageFilterPriority.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** MessageFilterPriority.java 2 Nov 2002 05:41:21 -0000 1.1 --- MessageFilterPriority.java 8 Nov 2002 04:13:28 -0000 1.2 *************** *** 1,36 **** ! package org.wettp2p.messagehandler; ! ! /** ! * Type-safe enum containing the possible ! * message filter priorities. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public class MessageFilterPriority ! { ! private final int value; ! ! private MessageFilterPriority( int value ) ! { ! this.value = value; ! } ! ! public String toString() ! { ! return String.valueOf( value ); ! } ! ! public int value() ! { ! return value; ! } ! ! public static final MessageFilterPriority LOWEST = new MessageFilterPriority( -2 ); ! public static final MessageFilterPriority LOW = new MessageFilterPriority( -1 ); ! public static final MessageFilterPriority NORMAL = new MessageFilterPriority( 0 ); ! public static final MessageFilterPriority HIGH = new MessageFilterPriority( 1 ); ! public static final MessageFilterPriority HIGHEST = new MessageFilterPriority( 2 ); ! ! // add other priority values here... } --- 1,36 ---- ! package org.wettp2p.messagehandler; ! ! /** ! * Type-safe enum containing the possible ! * message filter priorities. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public class MessageFilterPriority ! { ! private final int value; ! ! private MessageFilterPriority( int value ) ! { ! this.value = value; ! } ! ! public String toString() ! { ! return String.valueOf( value ); ! } ! ! public int value() ! { ! return value; ! } ! ! public static final MessageFilterPriority LOWEST = new MessageFilterPriority( -2 ); ! public static final MessageFilterPriority LOW = new MessageFilterPriority( -1 ); ! public static final MessageFilterPriority NORMAL = new MessageFilterPriority( 0 ); ! public static final MessageFilterPriority HIGH = new MessageFilterPriority( 1 ); ! public static final MessageFilterPriority HIGHEST = new MessageFilterPriority( 2 ); ! ! // add other priority values here... } Index: MessageHandler.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/messagehandler/MessageHandler.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** MessageHandler.java 2 Nov 2002 05:41:21 -0000 1.1 --- MessageHandler.java 8 Nov 2002 04:13:28 -0000 1.2 *************** *** 1,133 **** ! package org.wettp2p.messagehandler; ! ! import java.util.HashSet; ! import java.util.Iterator; ! import java.util.Vector; ! import java.util.Collections; ! ! /** ! * Manages and controls message traffic. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public class MessageHandler ! { ! // ! // queue management ! // ! ! /** the message queue */ ! private MessageQueue queue = new MessageQueue(); ! ! /** ! * Adds a message to the queue for sending. ! * @param message the <code>Message</code> to be sent ! **/ ! public void send( Message message ) ! { ! queue.add( message ); ! } ! ! /** ! * Processes the whole queue once. ! **/ ! public void processQueue() ! { ! for( Message message; ( message = queue.get() ) != null; ) ! { ! // process filtering... ! for( int i = 0; i < filters.size(); ++i ) ! ( (MessageFilter) filters.get( i ) ).filter( message ); ! ! // process consumers... ! Iterator iterator = consumers.iterator(); ! while( iterator.hasNext() ) ! ( (MessageConsumer) iterator.next() ).receive( message ); ! } ! } ! ! // ! // consumer and filter management ! // ! ! /** the list of consumers registered with this message handler */ ! private HashSet consumers = new HashSet(); ! ! /** the list of filters registered with this message handler */ ! private Vector filters = new Vector(); ! ! /** ! * Subscribes interest from a given consumer in messages ! * sent from the message handler. ! * @param consumer the <code>MessageConsumer</code> to be registered. ! **/ ! public void subscribe( MessageConsumer consumer ) ! { ! if( consumer instanceof MessageFilter ) ! { ! if( !filters.contains( consumer ) ) ! { ! filters.add( consumer ); ! Collections.sort( filters ); ! } ! } ! else ! consumers.add( consumer ); ! } ! ! // ! // utility methods ! // ! ! /** ! * Clears all consumers registered with the message handler. ! **/ ! public synchronized void resetConsumers() ! { ! filters.clear(); ! consumers.clear(); ! } ! ! /** ! * Clears all pending messages on the queue. ! **/ ! public void resetQueue() ! { ! queue.clear(); ! } ! ! /** ! * Returns the message handler to a state as if immediately ! * instantiated. ! **/ ! public void reset() ! { ! resetQueue(); ! resetConsumers(); ! } ! ! // ! // singleton interface ! // ! ! /** the one and only instance of this class */ ! private static MessageHandler instance = new MessageHandler(); ! ! /** ! * Static method that returns the single instance of this class. ! * @return the single instance of this class. ! **/ ! public static MessageHandler getInstance() ! { ! return instance; ! } ! ! /** ! * Singleton class, so the default constructor ! * is made private. ! **/ ! private MessageHandler() ! { ! } } --- 1,133 ---- ! package org.wettp2p.messagehandler; ! ! import java.util.HashSet; ! import java.util.Iterator; ! import java.util.Vector; ! import java.util.Collections; ! ! /** ! * Manages and controls message traffic. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public class MessageHandler ! { ! // ! // queue management ! // ! ! /** the message queue */ ! private MessageQueue queue = new MessageQueue(); ! ! /** ! * Adds a message to the queue for sending. ! * @param message the <code>Message</code> to be sent ! **/ ! public void send( Message message ) ! { ! queue.add( message ); ! } ! ! /** ! * Processes the whole queue once. ! **/ ! public void processQueue() ! { ! for( Message message; ( message = queue.get() ) != null; ) ! { ! // process filtering... ! for( int i = 0; i < filters.size(); ++i ) ! ( (MessageFilter) filters.get( i ) ).receive( message ); ! ! // process consumers... ! Iterator iterator = consumers.iterator(); ! while( iterator.hasNext() ) ! ( (MessageConsumer) iterator.next() ).receive( message ); ! } ! } ! ! // ! // consumer and filter management ! // ! ! /** the list of consumers registered with this message handler */ ! private HashSet consumers = new HashSet(); ! ! /** the list of filters registered with this message handler */ ! private Vector filters = new Vector(); ! ! /** ! * Subscribes interest from a given consumer in messages ! * sent from the message handler. ! * @param consumer the <code>MessageConsumer</code> to be registered. ! **/ ! public void subscribe( MessageConsumer consumer ) ! { ! if( consumer instanceof MessageFilter ) ! { ! if( !filters.contains( consumer ) ) ! { ! filters.add( consumer ); ! Collections.sort( filters ); ! } ! } ! else ! consumers.add( consumer ); ! } ! ! // ! // utility methods ! // ! ! /** ! * Clears all consumers registered with the message handler. ! **/ ! public synchronized void resetConsumers() ! { ! filters.clear(); ! consumers.clear(); ! } ! ! /** ! * Clears all pending messages on the queue. ! **/ ! public void resetQueue() ! { ! queue.clear(); ! } ! ! /** ! * Returns the message handler to a state as if immediately ! * instantiated. ! **/ ! public void reset() ! { ! resetQueue(); ! resetConsumers(); ! } ! ! // ! // singleton interface ! // ! ! /** the one and only instance of this class */ ! private static MessageHandler instance = new MessageHandler(); ! ! /** ! * Static method that returns the single instance of this class. ! * @return the single instance of this class. ! **/ ! public static MessageHandler getInstance() ! { ! return instance; ! } ! ! /** ! * Singleton class, so the default constructor ! * is made private. ! **/ ! private MessageHandler() ! { ! } } Index: MessageProducer.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/messagehandler/MessageProducer.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** MessageProducer.java 2 Nov 2002 05:41:21 -0000 1.1 --- MessageProducer.java 8 Nov 2002 04:13:28 -0000 1.2 *************** *** 1,60 **** ! package org.wettp2p.messagehandler; ! ! /** ! * MessageProducer interface. ! * A static inner class is provided by way of default ! * implementation of this interface. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public interface MessageProducer ! { ! /** ! * Sends the passed <code>Message</code>. ! * @param message the <code>Message</code> to be sent. ! **/ ! void send( Message message ); ! ! /** ! * A static inner class providing a default ! * implementation of the MessageProducer interface. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public static class DefaultImpl implements MessageProducer ! { ! /** the producer name, defaulted to the fully qualified class name */ ! private String name = this.getClass().getName(); ! ! /** ! * Default constructor. An instance created via this ! * constructor will have its name default to the fully ! * qualified class name. ! **/ ! public DefaultImpl() ! { ! } ! ! /** ! * Creates an instance and gives it the passed name. ! * @param name the name given to the instance. ! **/ ! public DefaultImpl( String name ) ! { ! this.name = name; ! } ! ! /** ! * Sends the passed <code>Message</code> to the ! * <code>MessageHandler</code>. ! * @param message the <code>Message</code> to be sent. ! **/ ! public void send( Message message ) ! { ! message.setProducerName( name ); ! MessageHandler.getInstance().send( message ); ! } ! } } --- 1,60 ---- ! package org.wettp2p.messagehandler; ! ! /** ! * MessageProducer interface. ! * A static inner class is provided by way of default ! * implementation of this interface. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public interface MessageProducer ! { ! /** ! * Sends the passed <code>Message</code>. ! * @param message the <code>Message</code> to be sent. ! **/ ! void send( Message message ); ! ! /** ! * A static inner class providing a default ! * implementation of the MessageProducer interface. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public static class DefaultImpl implements MessageProducer ! { ! /** the producer name, defaulted to the fully qualified class name */ ! private String name = this.getClass().getName(); ! ! /** ! * Default constructor. An instance created via this ! * constructor will have its name default to the fully ! * qualified class name. ! **/ ! public DefaultImpl() ! { ! } ! ! /** ! * Creates an instance and gives it the passed name. ! * @param name the name given to the instance. ! **/ ! public DefaultImpl( String name ) ! { ! this.name = name; ! } ! ! /** ! * Sends the passed <code>Message</code> to the ! * <code>MessageHandler</code>. ! * @param message the <code>Message</code> to be sent. ! **/ ! public void send( Message message ) ! { ! message.setProducerName( name ); ! MessageHandler.getInstance().send( message ); ! } ! } } Index: MessageQueue.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/messagehandler/MessageQueue.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** MessageQueue.java 2 Nov 2002 05:41:21 -0000 1.1 --- MessageQueue.java 8 Nov 2002 04:13:28 -0000 1.2 *************** *** 1,88 **** ! package org.wettp2p.messagehandler; ! ! import java.util.LinkedList; ! ! /** ! * A (FIFO) queue for <code>Message</code>s. Elements are ! * added to the end of the queue and retrieved from the ! * start. A method is provided to check the element at ! * the start without removing it. ! * All access that results in structural change or that ! * depends on structural integrity is synchronized. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public class MessageQueue ! { ! /** the list maintaining a queue of messages */ ! LinkedList queue = new LinkedList(); ! ! /** ! * Adds a <code>Message</code> to the end of the ! * queue. This method is synchronized. ! * @param message the Message to be added to the queue. ! **/ ! public synchronized void add( Message message ) ! { ! if( message == null ) // disallow nulls ! return; ! ! queue.add( message ); ! } ! ! /** ! * Retrieves (and removes) the <code>Message</code> ! * at the start of the queue. This method is ! * synchronized. ! * @return the <code>Message</code> at the start of ! * the queue or <code>null</code> if the queue is empty. ! **/ ! public synchronized Message get() ! { ! Message first = null; ! ! try ! { ! first = (Message) queue.removeFirst(); ! } ! catch( java.util.NoSuchElementException e ) ! { ! System.out.println( this + ": get(), queue is empty!" ); ! // ignore exception silently ! } ! ! return first; ! } ! ! /** ! * Retrieves (without removing) the <code>Message</code> ! * at the start of the queue. This method is synchronized. ! * @return the <code>Message</code> at the start of ! * the queue or <code>null</code> if the queue is empty. ! **/ ! public synchronized Message peek() ! { ! Message first = null; ! ! try ! { ! first = (Message) queue.getFirst(); ! } ! catch( java.util.NoSuchElementException e ) ! { ! System.out.println( this + ": peek(), queue is empty!" ); ! // ignore exception silently ! } ! ! return first; ! } ! ! /** ! * Clears all messages currently in the queue. ! **/ ! public synchronized void clear() ! { ! queue.clear(); ! } } --- 1,88 ---- ! package org.wettp2p.messagehandler; ! ! import java.util.LinkedList; ! ! /** ! * A (FIFO) queue for <code>Message</code>s. Elements are ! * added to the end of the queue and retrieved from the ! * start. A method is provided to check the element at ! * the start without removing it. ! * All access that results in structural change or that ! * depends on structural integrity is synchronized. ! * ! * @author Paulo Mouat ! * @version 1.0, 2002/10/25 ! **/ ! public class MessageQueue ! { ! /** the list maintaining a queue of messages */ ! LinkedList queue = new LinkedList(); ! ! /** ! * Adds a <code>Message</code> to the end of the ! * queue. This method is synchronized. ! * @param message the Message to be added to the queue. ! **/ ! public synchronized void add( Message message ) ! { ! if( message == null ) // disallow nulls ! return; ! ! queue.add( message ); ! } ! ! /** ! * Retrieves (and removes) the <code>Message</code> ! * at the start of the queue. This method is ! * synchronized. ! * @return the <code>Message</code> at the start of ! * the queue or <code>null</code> if the queue is empty. ! **/ ! public synchronized Message get() ! { ! Message first = null; ! ! try ! { ! first = (Message) queue.removeFirst(); ! } ! catch( java.util.NoSuchElementException e ) ! { ! System.out.println( this + ": get(), queue is empty!" ); ! // ignore exception silently ! } ! ! return first; ! } ! ! /** ! * Retrieves (without removing) the <code>Message</code> ! * at the start of the queue. This method is synchronized. ! * @return the <code>Message</code> at the start of ! * the queue or <code>null</code> if the queue is empty. ! **/ ! public synchronized Message peek() ! { ! Message first = null; ! ! try ! { ! first = (Message) queue.getFirst(); ! } ! catch( java.util.NoSuchElementException e ) ! { ! System.out.println( this + ": peek(), queue is empty!" ); ! // ignore exception silently ! } ! ! return first; ! } ! ! /** ! * Clears all messages currently in the queue. ! **/ ! public synchronized void clear() ! { ! queue.clear(); ! } } |
Update of /cvsroot/wett-p2p/org/wettp2p/messagehandler In directory usw-pr-cvs1:/tmp/cvs-serv6767 Added Files: Message.java MessageConsumer.java MessageFilterPriority.java MessageHandler.java MessageProducer.java MessageQueue.java MessageType.java Log Message: This is the first cut at the MessageHandler by Paulo Mouat. His comments -- Included is a test directory with a small program that exercises most of the classes and I hope exemplies much of their usage. These should provide a good start for revision of design and functionality, as what they provide is really basic functionality and are more of a proof-of-concept rather than finalized code. --- NEW FILE: Message.java --- package org.wettp2p.messagehandler; /** * Message interface. A message is defined as * having a type and its contents. The type is * indicated by an instance of <code>MessageType</code> * and contents is an arbitrary Java class stored * as a reference to <code>Object</code>. * * A static inner class is provided by way of default * implementation of this interface. * * @author Paulo Mouat * @version 1.0, 2002/10/25 **/ public interface Message { /** * Returns the MessageType object corresponding * to the message type. * * @return the MessageType object. **/ MessageType getType(); /** * Sets the massage type. * * @param type the MessageType object. **/ void setType( MessageType type ); /** * Returns the name of the producer that originated * this message. * * @return the contents Object. **/ String getProducerName(); /** * Sets the message producer name. * * @param name the producer name. **/ void setProducerName( String name ); /** * Returns the contents of the message. * * @return the contents Object. **/ Object getContents(); /** * Sets the contents of the message. * * @param contents the contents Object. **/ void setContents( Object contents ); /** * A static inner class providing a default * implementation of the Message interface. * * @author Paulo Mouat * @version 1.0, 2002/10/25 **/ public static class DefaultImpl implements Message { /** the type of the message */ private MessageType messageType = MessageType.GLOBAL; /** the producer name of the message */ private String producerName; /** the contents of the message */ private Object contents; /** * Creates an instance of Message.DefaultImpl. **/ public DefaultImpl() { } /** * Creates an instance of Message.DefaultImpl. * * @param messageType the type of the message * @param contents the contents of the message **/ public DefaultImpl( MessageType messageType, Object contents ) { this.messageType = messageType; this.contents = contents; } /** * Creates an instance of Message.DefaultImpl. * * @param messageType the type of the message * @param producerName the name of the producer of the message * @param contents the contents of the message **/ public DefaultImpl( MessageType messageType, String producerName, Object contents ) { this.messageType = messageType; this.producerName = producerName; this.contents = contents; } /** * Sets the massage type. * * @param type the MessageType object. **/ public void setType( MessageType messageType ) { this.messageType = messageType; } /** * Returns the MessageType object corresponding * to the message type. * * @return the MessageType object. **/ public MessageType getType() { return messageType; } /** * Sets the message producer name. * * @param name the producer name. **/ public void setProducerName( String producerName ) { this.producerName = producerName; } /** * Returns the name of the producer that originated * this message. * * @return the producer name. **/ public String getProducerName() { return producerName; } /** * Sets the contents of the message. * * @param contents the contents Object. **/ public void setContents( Object contents ) { this.contents = contents; } /** * Returns the contents of the message. * * @return the contents Object. **/ public Object getContents() { return contents; } public String toString() { return "message: type " + ( messageType != null ? messageType.toString() : "null" ) + ", producer " + ( producerName != null ? producerName : "null" ) + ", contents " + ( contents != null ? contents : "null" ); } } } --- NEW FILE: MessageConsumer.java --- package org.wettp2p.messagehandler; import java.util.HashSet; /** * MessageConsumer interface. * A static inner class is provided by way of default * implementation of this interface. * * @author Paulo Mouat * @version 1.0, 2002/10/25 **/ public interface MessageConsumer { /** * Receives the passed <code>Message</code>. * @param message the <code>Message</code> to be received. **/ void receive( Message message ); /** * Adds to the list of <code>MessageType</code>s types * this instance is interested in. * @param type the <code>MessageType</code>. **/ void subscribe( MessageType type ); /** * Adds a producer name to the list of producers whose * messages an instance of this class is interested in. * @param producerName the name of the producer. **/ void subscribe( String producerName ); /** * A static inner class providing a default * implementation of the MessageConsumer interface. * * @author Paulo Mouat * @version 1.0, 2002/10/25 **/ public static class DefaultImpl implements MessageConsumer { /** the message types subscribed by the consumer */ private HashSet subscribedTypes = new HashSet(); /** the producers subscribed by the consumer */ private HashSet subscribedProducers = new HashSet(); /** the consumer name, defaulted to the fully qualified class name */ private String name = this.getClass().getName(); /** * Default constructor. An instance created via this * constructor will have its name default to the fully * qualified class name. **/ public DefaultImpl() { } /** * Creates an instance and gives it the passed name. * @param name the name given to the instance. **/ public DefaultImpl( String name ) { this.name = name; } /** * Receives the passed <code>Message</code>. * @param message the <code>Message</code> to be received. **/ public void receive( Message message ) { if( subscribedTypes.contains( message.getType() ) || subscribedProducers.contains( message.getProducerName() ) ) doIt( message ); } /** * Called by the receive( Message message ) template method, * firing whatever tasks are useful according to the message. * @param message the <code>Message</code>. **/ protected void doIt( Message message ) { // does nothing, should be overridden in subclasses System.out.println( name + ": doIt(), message is " + message ); } /** * Adds to the list of <code>MessageType</code>s types * this instance is interested in. * @param type the <code>MessageType</code>. **/ public void subscribe( MessageType type ) { subscribedTypes.add( type ); MessageHandler.getInstance().subscribe( this ); } /** * Adds a producer name to the list of producers whose * messages an instance of this class is interested in. * @param producerName the name of the producer. **/ public void subscribe( String producerName ) { subscribedProducers.add( producerName ); MessageHandler.getInstance().subscribe( this ); } } } --- NEW FILE: MessageFilterPriority.java --- package org.wettp2p.messagehandler; /** * Type-safe enum containing the possible * message filter priorities. * * @author Paulo Mouat * @version 1.0, 2002/10/25 **/ public class MessageFilterPriority { private final int value; private MessageFilterPriority( int value ) { this.value = value; } public String toString() { return String.valueOf( value ); } public int value() { return value; } public static final MessageFilterPriority LOWEST = new MessageFilterPriority( -2 ); public static final MessageFilterPriority LOW = new MessageFilterPriority( -1 ); public static final MessageFilterPriority NORMAL = new MessageFilterPriority( 0 ); public static final MessageFilterPriority HIGH = new MessageFilterPriority( 1 ); public static final MessageFilterPriority HIGHEST = new MessageFilterPriority( 2 ); // add other priority values here... } --- NEW FILE: MessageHandler.java --- package org.wettp2p.messagehandler; import java.util.HashSet; import java.util.Iterator; import java.util.Vector; import java.util.Collections; /** * Manages and controls message traffic. * * @author Paulo Mouat * @version 1.0, 2002/10/25 **/ public class MessageHandler { // // queue management // /** the message queue */ private MessageQueue queue = new MessageQueue(); /** * Adds a message to the queue for sending. * @param message the <code>Message</code> to be sent **/ public void send( Message message ) { queue.add( message ); } /** * Processes the whole queue once. **/ public void processQueue() { for( Message message; ( message = queue.get() ) != null; ) { // process filtering... for( int i = 0; i < filters.size(); ++i ) ( (MessageFilter) filters.get( i ) ).filter( message ); // process consumers... Iterator iterator = consumers.iterator(); while( iterator.hasNext() ) ( (MessageConsumer) iterator.next() ).receive( message ); } } // // consumer and filter management // /** the list of consumers registered with this message handler */ private HashSet consumers = new HashSet(); /** the list of filters registered with this message handler */ private Vector filters = new Vector(); /** * Subscribes interest from a given consumer in messages * sent from the message handler. * @param consumer the <code>MessageConsumer</code> to be registered. **/ public void subscribe( MessageConsumer consumer ) { if( consumer instanceof MessageFilter ) { if( !filters.contains( consumer ) ) { filters.add( consumer ); Collections.sort( filters ); } } else consumers.add( consumer ); } // // utility methods // /** * Clears all consumers registered with the message handler. **/ public synchronized void resetConsumers() { filters.clear(); consumers.clear(); } /** * Clears all pending messages on the queue. **/ public void resetQueue() { queue.clear(); } /** * Returns the message handler to a state as if immediately * instantiated. **/ public void reset() { resetQueue(); resetConsumers(); } // // singleton interface // /** the one and only instance of this class */ private static MessageHandler instance = new MessageHandler(); /** * Static method that returns the single instance of this class. * @return the single instance of this class. **/ public static MessageHandler getInstance() { return instance; } /** * Singleton class, so the default constructor * is made private. **/ private MessageHandler() { } } --- NEW FILE: MessageProducer.java --- package org.wettp2p.messagehandler; /** * MessageProducer interface. * A static inner class is provided by way of default * implementation of this interface. * * @author Paulo Mouat * @version 1.0, 2002/10/25 **/ public interface MessageProducer { /** * Sends the passed <code>Message</code>. * @param message the <code>Message</code> to be sent. **/ void send( Message message ); /** * A static inner class providing a default * implementation of the MessageProducer interface. * * @author Paulo Mouat * @version 1.0, 2002/10/25 **/ public static class DefaultImpl implements MessageProducer { /** the producer name, defaulted to the fully qualified class name */ private String name = this.getClass().getName(); /** * Default constructor. An instance created via this * constructor will have its name default to the fully * qualified class name. **/ public DefaultImpl() { } /** * Creates an instance and gives it the passed name. * @param name the name given to the instance. **/ public DefaultImpl( String name ) { this.name = name; } /** * Sends the passed <code>Message</code> to the * <code>MessageHandler</code>. * @param message the <code>Message</code> to be sent. **/ public void send( Message message ) { message.setProducerName( name ); MessageHandler.getInstance().send( message ); } } } --- NEW FILE: MessageQueue.java --- package org.wettp2p.messagehandler; import java.util.LinkedList; /** * A (FIFO) queue for <code>Message</code>s. Elements are * added to the end of the queue and retrieved from the * start. A method is provided to check the element at * the start without removing it. * All access that results in structural change or that * depends on structural integrity is synchronized. * * @author Paulo Mouat * @version 1.0, 2002/10/25 **/ public class MessageQueue { /** the list maintaining a queue of messages */ LinkedList queue = new LinkedList(); /** * Adds a <code>Message</code> to the end of the * queue. This method is synchronized. * @param message the Message to be added to the queue. **/ public synchronized void add( Message message ) { if( message == null ) // disallow nulls return; queue.add( message ); } /** * Retrieves (and removes) the <code>Message</code> * at the start of the queue. This method is * synchronized. * @return the <code>Message</code> at the start of * the queue or <code>null</code> if the queue is empty. **/ public synchronized Message get() { Message first = null; try { first = (Message) queue.removeFirst(); } catch( java.util.NoSuchElementException e ) { System.out.println( this + ": get(), queue is empty!" ); // ignore exception silently } return first; } /** * Retrieves (without removing) the <code>Message</code> * at the start of the queue. This method is synchronized. * @return the <code>Message</code> at the start of * the queue or <code>null</code> if the queue is empty. **/ public synchronized Message peek() { Message first = null; try { first = (Message) queue.getFirst(); } catch( java.util.NoSuchElementException e ) { System.out.println( this + ": peek(), queue is empty!" ); // ignore exception silently } return first; } /** * Clears all messages currently in the queue. **/ public synchronized void clear() { queue.clear(); } } --- NEW FILE: MessageType.java --- package org.wettp2p.messagehandler; /** * Type-safe enum containing the possible * message types. * * @author Paulo Mouat * @version 1.0, 2002/10/25 **/ public class MessageType { private final String value; private MessageType( String value ) { this.value = value; } public String toString() { return value; } public static final MessageType GLOBAL = new MessageType( "Global" ); public static final MessageType GROUP_PLAYER_CHAT = new MessageType( "GroupPlayerChat" ); public static final MessageType GROUP_CHARACTER_CHAT = new MessageType( "GroupCharacterChat" ); public static final MessageType PRIVATE_PLAYER_CHAT = new MessageType( "PrivatePlayerChat" ); public static final MessageType PRIVATE_CHARACTER_CHAT = new MessageType( "PrivateCharacterChat" ); // add other message types here... } |
From: <rb...@us...> - 2002-11-02 05:35:01
|
Update of /cvsroot/wett-p2p/org/wettp2p/messagehandler In directory usw-pr-cvs1:/tmp/cvs-serv5295a Added Files: MessageFilter.java Log Message: This is a first draft of the messagehandler code submitted by Raulo Mouat. His comments -- Included is a test directory with a small program that exercises most of the classes and I hope exemplies much of their usage. These should provide a good start for revision of design and functionality, as what they provide is really basic functionality and are more of a proof-of-concept rather than finalized code. --- NEW FILE: MessageFilter.java --- package org.wettp2p.messagehandler; /** * MessageFilter interface. * A static inner class is provided by way of default * implementation of this interface. * * @author Paulo Mouat * @version 1.0, 2002/10/25 **/ public interface MessageFilter extends MessageConsumer { /** * Sets the priority value of this filter. * @param priority the priority value for the filter. **/ void setPriority( MessageFilterPriority priority ); /** * Gets the priority value of this filter. * @return the priority value for the filter. **/ MessageFilterPriority getPriority(); /** * Performs filtering on the passed <code>Message</code>. * @param message the <code>Message</code> to be filtered. **/ void filter( Message message ); /** * A static inner class providing a default * implementation of the MessageFilter interface. * * @author Paulo Mouat * @version 1.0, 2002/10/25 **/ public static class DefaultImpl extends MessageConsumer.DefaultImpl implements MessageFilter, Comparable { /** the priority value of the filter, defaults to normal */ private MessageFilterPriority priority = MessageFilterPriority.NORMAL; /** the filter name, defaulted to the fully qualified class name */ private String name = this.getClass().getName(); /** * Default constructor. An instance created via this * constructor will have its name default to the fully * qualified class name. **/ public DefaultImpl() { } /** * Creates an instance and gives it the passed name. * @param name the name given to the instance. **/ public DefaultImpl( String name ) { this.name = name; } /** * Called by the receive( Message message ) template method in * <code>MessageConsumer.DefaultImpl</code>, firing the * <code>filter( message )</code> method. * @param message the <code>Message</code>. **/ protected void doIt( Message message ) { filter( message ); } /** * Performs filtering on the passed <code>Message</code>. * @param message the <code>Message</code> to be filtered. **/ public void filter( Message message ) { // does nothing, should be overridden in subclasses System.out.println( name + " " + getPriority().value() + ": filter()" ); } /** * Sets the priority value of this filter. * @param priority the priority value for the filter. **/ public void setPriority( MessageFilterPriority priority ) { this.priority = priority; } /** * Gets the priority value of this filter. * @return the priority value for the filter. **/ public MessageFilterPriority getPriority() { return priority; } // // the Comparable interface // /** * Method specified by the Comparable interface. **/ public int compareTo( Object o ) { // will throw ClassCastException if o is not a MessageFilter return ( (MessageFilter) o ).getPriority().value() - priority.value(); } } } |