📄 udpservice.java
字号:
/* * Copyright (C) butor.com. All rights reserved. * * This software is published under the terms of the GNU Library General * Public License (GNU LGPL), a copy of which has been included with this * distribution in the LICENSE.txt file. */package org.butor.net.udp;import java.util.Hashtable;import java.util.Vector;import org.butor.fwService.FwService;import org.butor.fwService.IFwServiceImpl;import org.butor.log.Log;/** * * * @author Aiman SAWAN */public abstract class UDPService { public final static String PROPERTY_LISTEN_PORT = "listen_port"; public final static String PROPERTY_BUFFER_LENGTH = "buffer_length"; public final static String PROPERTY_SINGLE_LISTENER_MODE = "single_listener_mode"; /** * Config key indicating wether or not to register * itself against the UDP service */ public static final String PROPERTY_ENABLE_UDP_MESSAGE_CFG = "enable_udp_message"; /** * By default, register itself against the UDP service */ public static final boolean PROPERTY_ENABLE_UDP_MESSAGE_CFG_DEF = true; /* * will register org.butor.web.listener until an implementation * will be registered. */ protected static Vector s_listeners; protected static FwService fwService = new FwService(UDPService.class); public static boolean isImplCompatible(IFwServiceImpl impl) { return (impl instanceof IUDPService); } public static String getServiceName() { return UDPService.class.getName(); } public static void registerImpl(IFwServiceImpl impl) { fwService.registerImpl(impl); IUDPService uImpl = getImpl("registerImpl()"); if (null != uImpl) { // register waiting liteners if any. if (s_listeners != null) { for (int i=0; i<s_listeners.size(); i++) { uImpl.registerUdpMessageListener((IUDPMessageListener) s_listeners.elementAt(i)); } } } } public static void unregisterImpl(IFwServiceImpl impl) { fwService.unregisterImpl(impl); } protected static IUDPService getImpl(String calledMethod) { return (IUDPService)fwService.getImpl(calledMethod); } /** * Tell if the service is started in single org.butor.web.listener mode. * * Send message to all registered listeners if not in single * org.butor.web.listener mode. If in single org.butor.web.listener mode then send message * to the specified org.butor.web.listener only. * * @return boolean. */ public static boolean isSingleListenerMode() { IUDPService impl = getImpl("isSingleListenerMode()"); if (null != impl) { return impl.isSingleListenerMode(); } return false; } /** * Register an IUDPListener * * @param org.butor.web.listener IUDPListener, org.butor.web.listener interested to * receive datagram messages. */ public synchronized static void registerUdpMessageListener(IUDPMessageListener listener) { IUDPService impl = getImpl("isSingleListenerMode()"); if (null != impl) { impl.registerUdpMessageListener(listener); return; } Log.logStr(UDPService.class, Log.LOG_TYPE_INFO, "registerUdpMessageListener()", "keeping org.butor.web.listener until an implementation register."); if (s_listeners == null) { s_listeners = new Vector(); } s_listeners.addElement(listener); } /** * unRegister an IUDPListener * * @param org.butor.web.listener IUDPListener */ public synchronized static void unregisterUdpMessageListener(IUDPMessageListener listener) { IUDPService impl = getImpl("unregisterUdpMessageListener()"); if (null != impl) { impl.unregisterUdpMessageListener(listener); } } /** * Get registered listeners. * * @return Hashtable */ public static Hashtable getListeners() { IUDPService impl = getImpl("getListeners()"); if (null != impl) { return impl.getListeners(); } return null; } /** * Get registered org.butor.web.listener. * * @param name String, org.butor.web.listener name. * * @return IAdminMessageHandler */ public static IUDPMessageListener getUdpMessageListener(String name) { IUDPService impl = getImpl("getListeners()"); if (null != impl) { return impl.getUdpMessageListener(name); } return null; } /** * Returns the port number on which datagrams are listened. * * @return port number */ public static int getListenPort() { IUDPService impl = getImpl("getListeners()"); if (null != impl) { return impl.getListenPort(); } return -1; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -