| 1 |
package org.apache.commons.jcs3.utils.discovery; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
import java.io.IOException; |
| 23 |
import java.util.ArrayList; |
| 24 |
|
| 25 |
import org.apache.commons.jcs3.log.Log; |
| 26 |
import org.apache.commons.jcs3.log.LogManager; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
@Deprecated |
| 33 |
public class UDPDiscoverySenderThread |
| 34 |
implements Runnable |
| 35 |
{ |
| 36 |
|
| 37 |
private static final Log log = LogManager.getLog( UDPDiscoverySenderThread.class ); |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private final UDPDiscoveryAttributes attributes; |
| 43 |
|
| 44 |
|
| 45 |
private ArrayList<String> cacheNames = new ArrayList<>(); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
protected void setCacheNames( final ArrayList<String> cacheNames ) |
| 51 |
{ |
| 52 |
log.info( "Resetting cacheNames = [{0}]", cacheNames ); |
| 53 |
this.cacheNames = cacheNames; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
protected ArrayList<String> getCacheNames() |
| 60 |
{ |
| 61 |
return cacheNames; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public UDPDiscoverySenderThread( final UDPDiscoveryAttributes attributes, final ArrayList<String> cacheNames ) |
| 72 |
{ |
| 73 |
this.attributes = attributes; |
| 74 |
|
| 75 |
this.cacheNames = cacheNames; |
| 76 |
|
| 77 |
log.debug( "Creating sender thread for discoveryAddress = [{0}] and " |
| 78 |
+ "discoveryPort = [{1}] myHostName = [{2}] and port = [{3}]", |
| 79 |
attributes::getUdpDiscoveryAddr, |
| 80 |
attributes::getUdpDiscoveryPort, |
| 81 |
attributes::getServiceAddress, |
| 82 |
attributes::getServicePort); |
| 83 |
|
| 84 |
try (UDPDiscoverySender sender = new UDPDiscoverySender( |
| 85 |
attributes.getUdpDiscoveryAddr(), |
| 86 |
attributes.getUdpDiscoveryPort(), |
| 87 |
attributes.getUdpTTL())) |
| 88 |
{ |
| 89 |
|
| 90 |
sender.requestBroadcast(); |
| 91 |
|
| 92 |
log.debug( "Sent a request broadcast to the group" ); |
| 93 |
} |
| 94 |
catch ( final IOException e ) |
| 95 |
{ |
| 96 |
log.error( "Problem sending a Request Broadcast", e ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
@Override |
| 104 |
public void run() |
| 105 |
{ |
| 106 |
|
| 107 |
|
| 108 |
try (UDPDiscoverySender sender = new UDPDiscoverySender( |
| 109 |
attributes.getUdpDiscoveryAddr(), |
| 110 |
attributes.getUdpDiscoveryPort(), |
| 111 |
attributes.getUdpTTL())) |
| 112 |
{ |
| 113 |
sender.passiveBroadcast( attributes.getServiceAddress(), attributes.getServicePort(), cacheNames ); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
log.debug( "Called sender to issue a passive broadcast" ); |
| 119 |
} |
| 120 |
catch ( final IOException e ) |
| 121 |
{ |
| 122 |
log.error( "Problem calling the UDP Discovery Sender [{0}:{1}]", |
| 123 |
attributes.getUdpDiscoveryAddr(), |
| 124 |
attributes.getUdpDiscoveryPort(), e ); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
protected void shutdown() |
| 132 |
{ |
| 133 |
|
| 134 |
|
| 135 |
try (UDPDiscoverySender sender = new UDPDiscoverySender( |
| 136 |
attributes.getUdpDiscoveryAddr(), |
| 137 |
attributes.getUdpDiscoveryPort(), |
| 138 |
attributes.getUdpTTL())) |
| 139 |
{ |
| 140 |
sender.removeBroadcast( attributes.getServiceAddress(), attributes.getServicePort(), cacheNames ); |
| 141 |
|
| 142 |
log.debug( "Called sender to issue a remove broadcast in shutdown." ); |
| 143 |
} |
| 144 |
catch ( final IOException e ) |
| 145 |
{ |
| 146 |
log.error( "Problem calling the UDP Discovery Sender", e ); |
| 147 |
} |
| 148 |
} |
| 149 |
} |