001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.mts.base;
028:
029: import java.net.URI;
030: import java.util.Iterator;
031: import java.util.Set;
032:
033: import org.cougaar.core.component.ServiceBroker;
034: import org.cougaar.core.component.ServiceProvider;
035: import org.cougaar.core.mts.MessageAddress;
036: import org.cougaar.core.mts.MulticastMessageAddress;
037: import org.cougaar.core.service.LoggingService;
038: import org.cougaar.core.service.wp.AddressEntry;
039: import org.cougaar.core.service.wp.Callback;
040: import org.cougaar.core.service.wp.Response;
041: import org.cougaar.core.service.wp.WhitePagesService;
042: import org.cougaar.core.wp.ListAllNodes;
043:
044: import org.cougaar.mts.std.AspectSupport;
045:
046: /**
047: * This {@link ServiceProvider} provides the {@link NameSupport}
048: * service. An inner class implements that service.
049: */
050: public final class NameSupportImpl implements ServiceProvider {
051: private NameSupport service;
052:
053: NameSupportImpl(String id, ServiceBroker sb) {
054: service = new ServiceImpl(id, sb);
055: AspectSupport aspectSupport = (AspectSupport) sb.getService(
056: this , AspectSupport.class, null);
057: service = (NameSupport) aspectSupport.attachAspects(service,
058: NameSupport.class);
059: }
060:
061: public Object getService(ServiceBroker sb, Object requestor,
062: Class serviceClass) {
063: if (serviceClass == NameSupport.class) {
064: return service;
065: } else {
066: return null;
067: }
068: }
069:
070: public void releaseService(ServiceBroker sb, Object requestor,
071: Class serviceClass, Object service) {
072: }
073:
074: private final static class ServiceImpl implements NameSupport {
075: private LoggingService loggingService;
076: private WhitePagesService wpService;
077: private MessageAddress myNodeAddress;
078: private String id;
079: private String hostname;
080:
081: private ServiceImpl(String id, ServiceBroker sb) {
082: this .id = id;
083: wpService = (WhitePagesService) sb.getService(this ,
084: WhitePagesService.class, null);
085: loggingService = (LoggingService) sb.getService(this ,
086: LoggingService.class, null);
087: myNodeAddress = MessageAddress.getMessageAddress(id);
088:
089: try {
090: hostname = java.net.InetAddress.getLocalHost()
091: .getHostAddress();
092: } catch (java.net.UnknownHostException ex) {
093: loggingService.error(null, ex);
094: }
095: }
096:
097: public MessageAddress getNodeMessageAddress() {
098: return myNodeAddress;
099: }
100:
101: private class VoidWPCallback implements Callback {
102: public void execute(Response response) {
103: if (response.isSuccess()) {
104: if (loggingService.isInfoEnabled()) {
105: loggingService.info("WP Response: " + response);
106: }
107: } else {
108: loggingService.error("WP Error: " + response);
109: }
110: }
111: }
112:
113: private final void _register(String agent, URI ref,
114: String protocol) {
115: AddressEntry entry = AddressEntry.getAddressEntry(agent,
116: protocol, ref);
117: try {
118: Callback cb = new VoidWPCallback();
119: wpService.rebind(entry, cb);
120: } catch (Exception ex) {
121: loggingService.error(null, ex);
122: }
123: }
124:
125: private final void _unregister(String agent, URI ref,
126: String protocol) {
127: AddressEntry entry = AddressEntry.getAddressEntry(agent,
128: protocol, ref);
129: Callback callback = new VoidWPCallback();
130: try {
131: wpService.unbind(entry, callback);
132: } catch (Exception ex) {
133: loggingService.error(null, ex);
134: }
135: }
136:
137: public void registerAgentInNameServer(URI reference,
138: MessageAddress addr, String protocol) {
139: _register(addr.getAddress(), reference, protocol);
140: }
141:
142: public void unregisterAgentInNameServer(URI reference,
143: MessageAddress addr, String protocol) {
144: _unregister(addr.getAddress(), reference, protocol);
145: }
146:
147: public void lookupAddressInNameServer(MessageAddress address,
148: String protocol, Callback callback) {
149: wpService.get(address.getAddress(), protocol, callback);
150: }
151:
152: public URI lookupAddressInNameServer(MessageAddress address,
153: String protocol) {
154: return lookupAddressInNameServer(address, protocol, 0);
155: }
156:
157: public URI lookupAddressInNameServer(MessageAddress address,
158: String protocol, long timeout) {
159: AddressEntry entry;
160: try {
161: entry = wpService.get(address.getAddress(), protocol,
162: timeout);
163: } catch (Exception ex) {
164: entry = null;
165: loggingService.error(null, ex);
166: }
167: return (entry == null ? null : entry.getURI());
168: }
169:
170: private static class EmptyIterator implements Iterator {
171: public boolean hasNext() {
172: return false;
173: }
174:
175: public Object next() {
176: return null;
177: }
178:
179: public void remove() {
180: throw new UnsupportedOperationException();
181: }
182: }
183:
184: public Iterator lookupMulticast(MulticastMessageAddress address) {
185: try {
186: Set result = ListAllNodes
187: .listAllNodes(wpService, 30000);
188: if (result == null)
189: return new EmptyIterator();
190: final Iterator iter = result.iterator();
191: return new Iterator() {
192: public boolean hasNext() {
193: return iter.hasNext();
194: }
195:
196: public Object next() {
197: String node = (String) iter.next();
198: return MessageAddress.getMessageAddress(node);
199: }
200:
201: public void remove() {
202: throw new UnsupportedOperationException();
203: }
204: };
205: } catch (Exception ex) {
206: if (loggingService.isWarnEnabled())
207: loggingService.warn("Multicast had WP timout");
208: return new EmptyIterator();
209: }
210: }
211:
212: }
213:
214: }
|