Search in sources :

Example 21 with ServiceURL

use of ch.ethz.iks.slp.ServiceURL in project ecf by eclipse.

the class ServiceURLTest method testServiceURL2.

public void testServiceURL2() throws Exception {
    String urlString = "service:test:myservice://localhost:80";
    ServiceURL url = new ServiceURL(urlString, 0);
    assertEquals("service:test:myservice", url.getServiceType().toString());
    assertEquals("localhost", url.getHost());
    assertEquals(80, url.getPort());
    assertEquals("", url.getURLPath());
    assertEquals("", url.getUserInfo());
    assertEquals(null, url.getProtocol());
    assertEquals(urlString, url.toString());
}
Also used : ServiceURL(ch.ethz.iks.slp.ServiceURL)

Example 22 with ServiceURL

use of ch.ethz.iks.slp.ServiceURL in project ecf by eclipse.

the class SLPDaemonImpl method handleMessage.

/**
 * all incoming messages are handled here.
 *
 * @param msg
 *            the message to be processed.
 * @return the reply if the handled message came in via TCP. Otherwise null
 *         will be returned.
 * @throws ServiceLocationException
 *             for various reasons like authentication failures etc.
 */
public ReplyMessage handleMessage(final SLPMessage msg) throws ServiceLocationException {
    if (msg == null) {
        return null;
    }
    String via = msg.tcp ? " (tcp)" : " (udp)";
    SLPCore.platform.logTraceMessage("RECEIVED (" + msg.address + ":" + msg.port + ") " + msg.toString() + via);
    ReplyMessage reply = null;
    switch(msg.funcID) {
        case SLPMessage.SRVRQST:
            ServiceRequest req = (ServiceRequest) msg;
            List results = new ArrayList();
            for (Iterator scopes = req.scopeList.iterator(); scopes.hasNext(); ) {
                String scope = (String) scopes.next();
                List services = (List) registeredServices.get(scope.toLowerCase());
                if (services == null) {
                    continue;
                }
                for (Iterator srvs = services.iterator(); srvs.hasNext(); ) {
                    Service service = (Service) srvs.next();
                    if (service.url.getServiceType().matches(req.serviceType)) {
                        if (req.predicate == null) {
                            results.add(service.url);
                            continue;
                        }
                        if (req.predicate.match(service.attributes)) {
                            results.add(service.url);
                        }
                    }
                }
            }
            /*
			 * if there is no result, don't send a reply. This causes the SA to
			 * get the same message at least two more times but the RFC strictly
			 * demands this for multicast requests
			 */
            if (results.size() == 0 && req.multicast) {
                return null;
            }
            reply = new ServiceReply(req, results);
            if (SLPCore.CONFIG.getSecurityEnabled()) {
                ((ServiceReply) reply).sign(req.spi);
            }
            return reply;
        case SLPMessage.ATTRRQST:
            AttributeRequest attreq = (AttributeRequest) msg;
            List attResult = new ArrayList();
            for (Iterator scopes = attreq.scopeList.iterator(); scopes.hasNext(); ) {
                String scope = (String) scopes.next();
                List services = (List) registeredServices.get(scope.toLowerCase());
                if (services == null) {
                    continue;
                }
                // the request can either be for a ServiceURL or a ServiceType
                Object reqService;
                boolean fullurl = false;
                if (attreq.url.indexOf("//") == -1) {
                    reqService = new ServiceType(attreq.url);
                } else {
                    fullurl = true;
                    reqService = new ServiceURL(attreq.url, 0);
                }
                // the tag list has to be empty
                if (attreq.spi.equals("") || (fullurl && attreq.tagList.isEmpty())) {
                    for (Iterator srvs = services.iterator(); srvs.hasNext(); ) {
                        Service service = (Service) srvs.next();
                        if (service.url.matches(reqService)) {
                            attResult.addAll(SLPUtils.findMatches(attreq.tagList, service.attributes));
                        }
                    }
                }
            }
            reply = new AttributeReply(attreq, attResult);
            if (SLPCore.CONFIG.getSecurityEnabled()) {
                ((AttributeReply) reply).sign(attreq.spi);
            }
            return reply;
        case SLPMessage.SRVTYPERQST:
            ServiceTypeRequest streq = (ServiceTypeRequest) msg;
            ArrayList result = new ArrayList();
            // iterate over scopes
            for (Iterator scopeIter = streq.scopeList.iterator(); scopeIter.hasNext(); ) {
                // iterate over the registered services
                String scope = (String) scopeIter.next();
                List services = ((List) registeredServices.get(scope.toLowerCase()));
                if (services == null) {
                    continue;
                }
                for (Iterator iter = services.iterator(); iter.hasNext(); ) {
                    Service service = (Service) iter.next();
                    ServiceType type = service.url.getServiceType();
                    if (streq.namingAuthority.equals("*") || streq.namingAuthority.equals("") || type.getNamingAuthority().equals(streq.namingAuthority)) {
                        if (!result.contains(type)) {
                            result.add(type);
                        }
                    }
                }
            }
            reply = new ServiceTypeReply(streq, result);
            return reply;
        case SLPMessage.SRVREG:
            registerService((ServiceRegistration) msg);
            reply = new ServiceAcknowledgement(msg, 0);
            return reply;
        case SLPMessage.SRVDEREG:
            deregisterService((ServiceDeregistration) msg);
            reply = new ServiceAcknowledgement(msg, 0);
            return reply;
        case SLPMessage.SRVACK:
            final ReplyMessage rep = (ReplyMessage) msg;
            if (rep.errorCode != 0) {
                SLPCore.platform.logWarning(msg.address + " replied with error code " + rep.errorCode + " (" + rep + ")");
            }
            return null;
        default:
            // exception during parsing
            throw new ServiceLocationException(ServiceLocationException.NOT_IMPLEMENTED, "The message type " + SLPMessage.getType(msg.funcID) + " is not implemented");
    }
}
Also used : ArrayList(java.util.ArrayList) ServiceType(ch.ethz.iks.slp.ServiceType) Iterator(java.util.Iterator) ServiceURL(ch.ethz.iks.slp.ServiceURL) ArrayList(java.util.ArrayList) List(java.util.List) ServiceLocationException(ch.ethz.iks.slp.ServiceLocationException)

Example 23 with ServiceURL

use of ch.ethz.iks.slp.ServiceURL in project ecf by eclipse.

the class SelfDiscoveryTest method setUp.

/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
public void setUp() throws InterruptedException {
    try {
        service = new ServiceURL("service:osgi://" + HOST_AND_PORT, 10800);
        int i = 0;
        properties = new Hashtable();
        properties.put("attr", Boolean.FALSE);
        properties.put("attr" + i++, "value");
        properties.put("attr" + i++, "foo,bar");
        properties.put("attr" + i++, "foo:bar");
        properties.put("attr" + i++, "foo bar");
        TestActivator.advertiser.register(service, properties);
    } catch (ServiceLocationException e) {
        Assert.fail(e.getMessage());
        e.printStackTrace();
    }
}
Also used : Hashtable(java.util.Hashtable) ServiceURL(ch.ethz.iks.slp.ServiceURL) ServiceLocationException(ch.ethz.iks.slp.ServiceLocationException)

Aggregations

ServiceURL (ch.ethz.iks.slp.ServiceURL)23 ServiceLocationException (ch.ethz.iks.slp.ServiceLocationException)4 ArrayList (java.util.ArrayList)4 IServiceInfo (org.eclipse.ecf.discovery.IServiceInfo)3 IServiceTypeID (org.eclipse.ecf.discovery.identity.IServiceTypeID)3 JSLPServiceInfo (org.eclipse.ecf.provider.jslp.container.JSLPServiceInfo)3 ServiceType (ch.ethz.iks.slp.ServiceType)2 URI (java.net.URI)2 Iterator (java.util.Iterator)2 List (java.util.List)2 IServiceID (org.eclipse.ecf.discovery.identity.IServiceID)2 ServicePropertiesAdapter (org.eclipse.ecf.internal.provider.jslp.ServicePropertiesAdapter)2 ServiceURLAdapter (org.eclipse.ecf.internal.provider.jslp.ServiceURLAdapter)2 Hashtable (java.util.Hashtable)1 Entry (java.util.Map.Entry)1