Search in sources :

Example 6 with ServiceLocationException

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

the class SLPDaemonImpl method registerService.

/**
 * register a service with the SLP framework. For the scopes, where DAs are
 * known, the service will be registered with all DAs.
 *
 * @param reg
 *            the ServiceRegistration.
 */
private void registerService(final ServiceRegistration reg) {
    Service service = new Service(reg);
    for (Iterator scopeIter = reg.scopeList.iterator(); scopeIter.hasNext(); ) {
        String scope = (String) scopeIter.next();
        scope = scope.toLowerCase();
        synchronized (registeredServices) {
            SLPUtils.addValue(registeredServices, scope, service);
        }
        if (reg.url.getLifetime() > ServiceURL.LIFETIME_PERMANENT) {
            synchronized (serviceDisposalQueue) {
                long next = System.currentTimeMillis() + (reg.url.getLifetime() * 1000);
                ArrayList keys = new ArrayList(serviceDisposalQueue.keySet());
                for (Iterator iter = keys.iterator(); iter.hasNext(); ) {
                    Object key = iter.next();
                    if (serviceDisposalQueue.get(key).equals(reg.url)) {
                        serviceDisposalQueue.remove(key);
                    }
                }
                serviceDisposalQueue.put(new Long(next), reg.url);
                serviceDisposalQueue.notifyAll();
            }
        }
        SLPCore.platform.logTraceReg("REGISTERED " + reg.url);
        // register the service with all known DAs in the scopes
        List daList = (List) SLPCore.dAs.get(scope);
        // try to find one
        if ((daList == null || daList.isEmpty()) && !SLPCore.noDiscovery) {
            try {
                SLPCore.daLookup(Arrays.asList(new String[] { (String) scope }));
                // wait a short time for incoming replies
                synchronized (SLPCore.dAs) {
                    try {
                        SLPCore.dAs.wait(SLPCore.CONFIG.getWaitTime());
                    } catch (InterruptedException e) {
                    }
                }
                daList = (List) SLPCore.dAs.get(scope);
            } catch (ServiceLocationException sle) {
                SLPCore.platform.logError(sle.getMessage(), sle.fillInStackTrace());
            }
        }
        if (daList != null && !daList.isEmpty()) {
            final String[] dAs = (String[]) daList.toArray(new String[daList.size()]);
            final ServiceRegistration announcement = new ServiceRegistration(reg.url, reg.serviceType, reg.scopeList, reg.attList, reg.locale);
            announcement.authBlocks = reg.authBlocks;
            for (int i = 0; i < dAs.length; i++) {
                try {
                    announceService(dAs[i], announcement);
                    SLPCore.platform.logTraceReg("ANNOUNCED " + announcement.url + " to " + dAs[i]);
                } catch (ServiceLocationException e) {
                    // remove DA from list
                    SLPUtils.removeValueFromAll(SLPCore.dAs, dAs[i]);
                    SLPCore.dASPIs.remove(dAs[i]);
                    SLPCore.platform.logError(e.getMessage(), e.fillInStackTrace());
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ServiceLocationException(ch.ethz.iks.slp.ServiceLocationException)

Example 7 with ServiceLocationException

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

the class SLPMessage method attributeStringToList.

/**
 * @param input
 * @return
 * @throws ServiceLocationException
 *
 * @author Markus Alexander Kuppe
 * @since 1.1
 */
protected List attributeStringToList(String input) throws ServiceLocationException {
    if ("".equals(input)) {
        return new ArrayList();
    }
    Parser parser = new Parser();
    try {
        Rule parse = parser.parse("attr-list", input);
        AttributeListVisitor visitor = new AttributeListVisitor();
        parse.visit(visitor);
        return visitor.getAttributes();
    } catch (IllegalArgumentException e) {
        throw new ServiceLocationException(ServiceLocationException.PARSE_ERROR, e.getMessage());
    } catch (ParserException e) {
        throw new ServiceLocationException(ServiceLocationException.PARSE_ERROR, e.getMessage());
    }
}
Also used : AttributeListVisitor(ch.ethz.iks.slp.impl.attr.AttributeListVisitor) ParserException(ch.ethz.iks.slp.impl.attr.gen.ParserException) ArrayList(java.util.ArrayList) Rule(ch.ethz.iks.slp.impl.attr.gen.Rule) Parser(ch.ethz.iks.slp.impl.attr.gen.Parser) ServiceLocationException(ch.ethz.iks.slp.ServiceLocationException)

Example 8 with ServiceLocationException

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

the class AttributeParserTest method testByteAttributeType.

/**
 * Test method for {@link ch.ethz.iks.slp.impl.SLPMessage#attributeStringToList(java.lang.String)}.
 */
public void testByteAttributeType() {
    SLPTestMessage stm = new SLPTestMessage();
    String input = BYTE;
    List attributeStringToList = new ArrayList();
    try {
        attributeStringToList = stm.attributeStringToList(input);
    } catch (ServiceLocationException e) {
        fail("Input " + input + " must be valid");
    }
    assertEquals(1, attributeStringToList.size());
    assertTrue(attributeStringToList.contains(BYTE));
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ServiceLocationException(ch.ethz.iks.slp.ServiceLocationException)

Example 9 with ServiceLocationException

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

the class AttributeParserTest method testAllAttributeType.

/**
 * Test method for {@link ch.ethz.iks.slp.impl.SLPMessage#attributeStringToList(java.lang.String)}.
 */
public void testAllAttributeType() {
    SLPTestMessage stm = new SLPTestMessage();
    String input = STRING + "," + BOOLEAN + "," + BYTE + "," + VENDOR_WITH_EN + "," + POS_INTEGER + "," + NEG_INTEGER;
    List attributeStringToList = new ArrayList();
    try {
        attributeStringToList = stm.attributeStringToList(input);
    } catch (ServiceLocationException e) {
        fail("Input " + input + " must be valid");
    }
    assertEquals(6, attributeStringToList.size());
    assertTrue(attributeStringToList.contains(NEG_INTEGER));
    assertTrue(attributeStringToList.contains(POS_INTEGER));
    assertTrue(attributeStringToList.contains(VENDOR_WITH_EN));
    assertTrue(attributeStringToList.contains(STRING));
    assertTrue(attributeStringToList.contains(BOOLEAN));
    assertTrue(attributeStringToList.contains(BYTE));
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ServiceLocationException(ch.ethz.iks.slp.ServiceLocationException)

Example 10 with ServiceLocationException

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

the class AttributeReply method getAuthData.

/**
 * get the authentication data.
 *
 * @param spiStr
 *            the SPI.
 * @param timestamp
 *            the timestamp.
 * @return the auth data.
 * @throws ServiceLocationException
 *             in case of IO errors.
 */
private byte[] getAuthData(final String spiStr, final int timestamp) throws ServiceLocationException {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        dos.writeUTF(spiStr);
        dos.writeUTF(listToString(attributes, ","));
        dos.writeInt(timestamp);
        return bos.toByteArray();
    } catch (IOException ioe) {
        throw new ServiceLocationException(ServiceLocationException.INTERNAL_SYSTEM_ERROR, ioe.getMessage());
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ServiceLocationException(ch.ethz.iks.slp.ServiceLocationException)

Aggregations

ServiceLocationException (ch.ethz.iks.slp.ServiceLocationException)29 IOException (java.io.IOException)15 ArrayList (java.util.ArrayList)14 List (java.util.List)13 InterruptedIOException (java.io.InterruptedIOException)6 Iterator (java.util.Iterator)6 DataOutputStream (java.io.DataOutputStream)5 ProtocolException (java.net.ProtocolException)5 SocketException (java.net.SocketException)5 ServiceURL (ch.ethz.iks.slp.ServiceURL)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 DatagramPacket (java.net.DatagramPacket)4 UnknownHostException (java.net.UnknownHostException)4 ServiceType (ch.ethz.iks.slp.ServiceType)3 DataInputStream (java.io.DataInputStream)3 DatagramSocket (java.net.DatagramSocket)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 BindException (java.net.BindException)2 InetAddress (java.net.InetAddress)2 MulticastSocket (java.net.MulticastSocket)2