multicastclient.java

来自「这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统」· Java 代码 · 共 94 行

JAVA
94
字号
package net.sf.dz.pnp;import java.net.SocketException;import java.util.Iterator;import java.util.HashSet;import java.util.Set;import org.freehold.jukebox.logger.LogChannel;import org.freehold.jukebox.service.ActiveService;/** * An abstract multicast client. * * <p> * * Provides a protocol independent listener abstraction for plug-and-play * features. * * @author Copyright &copy; <a href="mailto:vt@freehold.crocodile.org">Vadim Tkachenko</a> 2004 * @version $Id: MulticastClient.java,v 1.3 2004/07/22 17:53:59 vtt Exp $ */abstract public class MulticastClient extends ActiveService {    public static final LogChannel CH_MC = new LogChannel("MulticastClient");        private Set listenerSet = new HashSet();    protected void execute() throws Throwable {            while ( isEnabled() ) {                    try {                            MulticastEvent message = receive();                                complain(LOG_DEBUG, CH_MC, "Received announce: " + message);                                // VT: FIXME: Provide facilities to broadcast to *our*                // listeners                                synchronized ( this ) {                    for ( Iterator i = listenerSet.iterator(); i.hasNext(); ) {                                            MulticastEventListener l = (MulticastEventListener)i.next();                                                try {                                                    complain(LOG_DEBUG, CH_MC, "Notifying " + l);                                                    l.multicastEventReceived(this, message);                                                    } catch ( Throwable t ) {                                                    complain(LOG_WARNING, CH_MC, "Failed to complete notification on '" + message + "':", t);                        }                    }                }                            } catch ( Throwable t ) {                            // If we've just been stopped, and no event was received                // since then, we'll get the "Socket closed" exception.                 // Ignore it. All others won't be ignored, just because I                // don't know what they could be.                                if ( !isEnabled() ) {                                    if ( t instanceof SocketException && "Socket closed".equals(t.getMessage()) ) {                                            // Just return                        return;                    }                }                            complain(LOG_WARNING, CH_MC, "Failed to receive", t);            }        }    }        public final synchronized void addListener(MulticastEventListener l) {            listenerSet.add(l);    }        public final synchronized void removeListener(MulticastEventListener l) {            listenerSet.remove(l);    }        abstract protected MulticastEvent receive() throws Throwable;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?