📄 connectorfactory.java
字号:
package net.sf.dz.daemon.tcp.client;import java.net.InetAddress;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Set;import java.util.StringTokenizer;import java.util.TreeMap;import java.util.TreeSet;import org.freehold.jukebox.logger.LogAware;import org.freehold.jukebox.logger.LogChannel;import org.freehold.jukebox.logger.Logger;import net.sf.dz.pnp.MulticastClient;import net.sf.dz.pnp.MulticastEvent;import net.sf.dz.pnp.MulticastEventListener;/** * TCP connector factory. * * <p> * * This object listens to the broadcast announcements and allows its clients * to automatically instantiate TCP connectors connected to the services * they are interested in. * * <p> * * In other words, this object is a Plug-and-Play facilitator on the client * side. * * @author Copyright © <a href="mailto:vt@freehold.crocodile.org">Vadim Tkachenko</a> 2001-2004 * @version $Id: ConnectorFactory.java,v 1.2 2004/06/28 20:35:47 vtt Exp $ */public class ConnectorFactory extends LogAware implements MulticastEventListener { public static final LogChannel CH_CF = new LogChannel("ConnectorFactory"); /** * Set of multicast clients that feed us with data. */ private Set multicastClientSet = new HashSet(); /** * Map of data sources we have. * * <p> * * The key is the service signature, the value is a map where the key is * the source address and the value is the service handler. */ private Map spMap = new TreeMap(); public ConnectorFactory(Logger l) { setLogger(l); // VT: FIXME: This class should be made configurable and provide // different service providers ServiceProvider sp = new ServiceProvider(); sp.setLogger(getLogger()); spMap.put("DZ DAC Sensors", sp); spMap.put("DZ DAC Switches", sp); } /** * Add a multicast client to retrieve capabilities from. * * It is a responsibility of the caller to ensure that the multicast * client is up and running. * * @param client Multicast client preconfigured to recive a certain kind * of announce. */ public synchronized void addMulticastClient(MulticastClient client) { // VT: FIXME: Isn't this kind of redundant? multicastClientSet.add(client); client.addListener(this); complain(LOG_INFO, CH_CF, "Multicast client added: " + client); } /** * Start listening to events generated by a (service signature, device * address) pair. * * @param listener An entity that wishes to listen to events generated * by a given (service signature, device address) pair. * * @param serviceSignature Service signature. Currently supported are * "DZ DAC Sensors", "DZ DAC Switches", "DZ Core". * * @param deviceAddress A service-specific address of a device at the * server side access to which is required by the caller. * * @return An instance of a device connector corresponding with the * required device. This instance is to be considered reliable - it will * handle transient connection loss or network configuration changes in * a graceful manner. */ public synchronized DeviceConnector getConnector(String serviceSignature, String deviceAddress) { complain(LOG_DEBUG, CH_CF, "Requested " + serviceSignature + "[" + deviceAddress + "]"); return getServiceProvider(serviceSignature).getConnector(deviceAddress); } /** * Get a service provider for a service. * * @param serviceSignature A service signature to get the provider for. * * @return A service provider. <strong>Important:</strong> we can't * return null here, so if there is no known service provider, a * transparent dummy one should be returned instead. */ private ServiceProvider getServiceProvider(String serviceSignature) { ServiceProvider sp = (ServiceProvider)spMap.get(serviceSignature); if ( sp == null ) { // VT: FIXME: Actually, have to create one throw new IllegalStateException("Don't have a service provider for '" + serviceSignature + "'"); } return sp; } /** * Handle a multicast event received by PnP subsystem. */ public void multicastEventReceived(MulticastClient source, MulticastEvent e) { //complain(LOG_DEBUG, CH_CF, e.getServiceSignature() + " at " + e.getSourceAddress() + " said: " + e.getMessage()); getServiceProvider(e.getServiceSignature()).multicastEventReceived(e); } protected class DataSourceEvent extends MulticastEvent { private final Set addressSet = new TreeSet(); public DataSourceEvent(MulticastEvent source) { super(source); for ( StringTokenizer st = new StringTokenizer(getMessage(), " "); st.hasMoreTokens(); ) { addressSet.add(st.nextToken()); } } public Set getAddressSet() { return addressSet; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -