Search in sources :

Example 6 with ServiceType

use of ch.ethz.iks.slp.ServiceType 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 7 with ServiceType

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

the class SelfDiscoveryTest method testFilterWithWildcard.

/**
 * Test method for
 * {@link ch.ethz.iks.slp.Locator}.
 */
public void testFilterWithWildcard() throws Exception {
    int count = 0;
    for (ServiceLocationEnumeration services = TestActivator.locator.findServices(new ServiceType("service:osgi"), null, "(attr=*)"); services.hasMoreElements(); ) {
        assertEquals(services.next().toString(), "service:osgi://" + HOST_AND_PORT);
        count++;
    }
    assertEquals(1, count);
}
Also used : ServiceType(ch.ethz.iks.slp.ServiceType) ServiceLocationEnumeration(ch.ethz.iks.slp.ServiceLocationEnumeration)

Aggregations

ServiceType (ch.ethz.iks.slp.ServiceType)7 ServiceLocationEnumeration (ch.ethz.iks.slp.ServiceLocationEnumeration)3 ServiceLocationException (ch.ethz.iks.slp.ServiceLocationException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ServiceURL (ch.ethz.iks.slp.ServiceURL)2 IOException (java.io.IOException)2 InterruptedIOException (java.io.InterruptedIOException)2 SocketException (java.net.SocketException)2 UnknownHostException (java.net.UnknownHostException)2 BindException (java.net.BindException)1 DatagramPacket (java.net.DatagramPacket)1 DatagramSocket (java.net.DatagramSocket)1 InetAddress (java.net.InetAddress)1 ProtocolException (java.net.ProtocolException)1 URI (java.net.URI)1 Iterator (java.util.Iterator)1