1 package org.apache.jcs.auxiliary.lateral.socket.tcp.discovery;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.jcs.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes;
28 import org.apache.jcs.engine.behavior.ICompositeCacheManager;
29
30 /***
31 * This manages UDPDiscovery Services. We should end up with one service per
32 * Lateral Cache Manager Instance. One service works for multiple regions. We
33 * don't want a connection for each region.
34 *
35 * @author Aaron Smuts
36 *
37 */
38 public class UDPDiscoveryManager
39 {
40 private final static Log log = LogFactory.getLog( UDPDiscoveryManager.class );
41
42 private static UDPDiscoveryManager INSTANCE = new UDPDiscoveryManager();
43
44 private Map services = new HashMap();
45
46 private UDPDiscoveryManager()
47 {
48
49 }
50
51 /***
52 * Singelton
53 *
54 * @return UDPDiscoveryManager
55 */
56 public static UDPDiscoveryManager getInstance()
57 {
58 return INSTANCE;
59 }
60
61 /***
62 * Returns the UDP Discovery service associated with this instance.
63 *
64 * @param lca
65 * ITCPLateralCacheAttributes
66 * @param cacheMgr
67 * @return
68 */
69 public synchronized UDPDiscoveryService getService( ITCPLateralCacheAttributes lca, ICompositeCacheManager cacheMgr )
70 {
71 UDPDiscoveryService service = getService( lca.getUdpDiscoveryAddr(), lca.getUdpDiscoveryPort(), lca.getTcpListenerPort(), cacheMgr );
72
73
74 service.setTcpLateralCacheAttributes( lca );
75 return service;
76 }
77
78 /***
79 * Creates a service for the address and port if one doesn't exist already.
80 * <p>
81 * TODO we may need to key this using the listener port too
82 *
83 * @param discoveryAddress
84 * @param discoveryPort
85 * @param servicePort
86 * @param cacheMgr
87 * @return
88 */
89 public synchronized UDPDiscoveryService getService( String discoveryAddress, int discoveryPort, int servicePort,
90 ICompositeCacheManager cacheMgr )
91 {
92 String key = discoveryAddress + ":" + discoveryPort;
93
94 UDPDiscoveryService service = (UDPDiscoveryService) services.get( key );
95 if ( service == null )
96 {
97 if ( log.isInfoEnabled() )
98 {
99 log.info( "Creating service for address:port [" + key + "]" );
100 }
101
102 service = new UDPDiscoveryService( discoveryAddress, discoveryPort, servicePort, cacheMgr );
103 services.put( key, service );
104 }
105
106 if ( log.isDebugEnabled() )
107 {
108 log.debug( "Returning service [" + service + "] for key [" + key + "]" );
109 }
110
111 return service;
112
113 }
114
115 }