Search in sources :

Example 1 with UdpEndpointContextMatcher

use of org.eclipse.californium.elements.UdpEndpointContextMatcher in project californium by eclipse.

the class EndpointContextMatcherFactory method create.

/**
 * Create endpoint context matcher related to connector according the
 * configuration.
 *
 * If connector supports "coaps:", RESPONSE_MATCHING is used to determine,
 * if {@link StrictDtlsEndpointContextMatcher},
 * {@link RelaxedDtlsEndpointContextMatcher}, or
 * {@link PrincipalEndpointContextMatcher} is used.
 *
 * If connector supports "coap:", RESPONSE_MATCHING is used to determine, if
 * {@link UdpEndpointContextMatcher} is used with disabled
 * ({@link MatcherMode#RELAXED}) or enabled address check (otherwise).
 *
 * For other protocol flavors the corresponding matcher is used.
 *
 * @param connector connector to create related endpoint context matcher.
 * @param config configuration.
 * @return endpoint context matcher
 * @since 3.0 (changed parameter to Configuration)
 */
public static EndpointContextMatcher create(Connector connector, Configuration config) {
    String protocol = null;
    if (null != connector) {
        protocol = connector.getProtocol();
        if (CoAP.PROTOCOL_TCP.equalsIgnoreCase(protocol)) {
            return new TcpEndpointContextMatcher();
        } else if (CoAP.PROTOCOL_TLS.equalsIgnoreCase(protocol)) {
            return new TlsEndpointContextMatcher();
        }
    }
    MatcherMode mode = config.get(CoapConfig.RESPONSE_MATCHING);
    switch(mode) {
        case RELAXED:
            if (CoAP.PROTOCOL_UDP.equalsIgnoreCase(protocol)) {
                return new UdpEndpointContextMatcher(false);
            } else {
                return new RelaxedDtlsEndpointContextMatcher();
            }
        case PRINCIPAL:
            if (CoAP.PROTOCOL_UDP.equalsIgnoreCase(protocol)) {
                return new UdpEndpointContextMatcher(true);
            } else {
                return new PrincipalEndpointContextMatcher();
            }
        case PRINCIPAL_IDENTITY:
            if (CoAP.PROTOCOL_UDP.equalsIgnoreCase(protocol)) {
                return new UdpEndpointContextMatcher(true);
            } else {
                return new PrincipalEndpointContextMatcher(true);
            }
        case STRICT:
        default:
            if (CoAP.PROTOCOL_UDP.equalsIgnoreCase(protocol)) {
                return new UdpEndpointContextMatcher(true);
            } else {
                return new StrictDtlsEndpointContextMatcher();
            }
    }
}
Also used : PrincipalEndpointContextMatcher(org.eclipse.californium.elements.PrincipalEndpointContextMatcher) TlsEndpointContextMatcher(org.eclipse.californium.elements.TlsEndpointContextMatcher) MatcherMode(org.eclipse.californium.core.config.CoapConfig.MatcherMode) TcpEndpointContextMatcher(org.eclipse.californium.elements.TcpEndpointContextMatcher) RelaxedDtlsEndpointContextMatcher(org.eclipse.californium.elements.RelaxedDtlsEndpointContextMatcher) StrictDtlsEndpointContextMatcher(org.eclipse.californium.elements.StrictDtlsEndpointContextMatcher) UdpEndpointContextMatcher(org.eclipse.californium.elements.UdpEndpointContextMatcher)

Example 2 with UdpEndpointContextMatcher

use of org.eclipse.californium.elements.UdpEndpointContextMatcher in project californium by eclipse.

the class ClientSynchronousTest method testSynchronousPingWithPrincipalIdentity.

@Test
public void testSynchronousPingWithPrincipalIdentity() throws Exception {
    final AtomicBoolean sent = new AtomicBoolean();
    CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
    builder.setEndpointContextMatcher(new UdpEndpointContextMatcher(true) {

        final AtomicBoolean first = new AtomicBoolean();

        @Override
        public Object getEndpointIdentity(EndpointContext context) {
            if (first.compareAndSet(false, true)) {
                // emulates first access, when no principal is available
                throw new IllegalArgumentException("first access during test");
            }
            return "TEST";
        }
    });
    builder.setInetSocketAddress(TestTools.LOCALHOST_EPHEMERAL);
    CoapEndpoint clientEndpoint = builder.build();
    cleanup.add(clientEndpoint);
    clientEndpoint.addInterceptor(new MessageInterceptorAdapter() {

        @Override
        public void sendRequest(Request request) {
            sent.set(true);
        }
    });
    String uri = TestTools.getUri(serverEndpoint, TARGET);
    CoapClient client = new CoapClient(uri).useExecutor();
    cleanup.add(client);
    client.setEndpoint(clientEndpoint);
    // Check that we get the right content when calling get()
    boolean ping = client.ping();
    assertTrue(ping);
    assertTrue("Ping not sent using provided endpoint", sent.get());
    client.shutdown();
}
Also used : EndpointContext(org.eclipse.californium.elements.EndpointContext) Request(org.eclipse.californium.core.coap.Request) MessageInterceptorAdapter(org.eclipse.californium.core.network.interceptors.MessageInterceptorAdapter) CoapClient(org.eclipse.californium.core.CoapClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UdpEndpointContextMatcher(org.eclipse.californium.elements.UdpEndpointContextMatcher) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) Test(org.junit.Test)

Example 3 with UdpEndpointContextMatcher

use of org.eclipse.californium.elements.UdpEndpointContextMatcher in project californium by eclipse.

the class CoapStackTest method parameters.

@Parameterized.Parameters
public static List<Object[]> parameters() {
    Outbox udpOutbox = mock(Outbox.class);
    Outbox tcpOutbox = mock(Outbox.class);
    SystemConfig.register();
    TcpConfig.register();
    CoapConfig.register();
    Configuration config = Configuration.createStandardWithoutFile();
    List<Object[]> parameters = new ArrayList<>();
    parameters.add(new Object[] { new CoapTcpStack("tcp-test ", config, new TcpEndpointContextMatcher(), tcpOutbox), tcpOutbox });
    parameters.add(new Object[] { new CoapUdpStack("udp-test ", config, new UdpEndpointContextMatcher(true), udpOutbox), udpOutbox });
    return parameters;
}
Also used : Configuration(org.eclipse.californium.elements.config.Configuration) Outbox(org.eclipse.californium.core.network.Outbox) ArrayList(java.util.ArrayList) TcpEndpointContextMatcher(org.eclipse.californium.elements.TcpEndpointContextMatcher) UdpEndpointContextMatcher(org.eclipse.californium.elements.UdpEndpointContextMatcher)

Aggregations

UdpEndpointContextMatcher (org.eclipse.californium.elements.UdpEndpointContextMatcher)3 TcpEndpointContextMatcher (org.eclipse.californium.elements.TcpEndpointContextMatcher)2 ArrayList (java.util.ArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 CoapClient (org.eclipse.californium.core.CoapClient)1 Request (org.eclipse.californium.core.coap.Request)1 MatcherMode (org.eclipse.californium.core.config.CoapConfig.MatcherMode)1 CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)1 Outbox (org.eclipse.californium.core.network.Outbox)1 MessageInterceptorAdapter (org.eclipse.californium.core.network.interceptors.MessageInterceptorAdapter)1 EndpointContext (org.eclipse.californium.elements.EndpointContext)1 PrincipalEndpointContextMatcher (org.eclipse.californium.elements.PrincipalEndpointContextMatcher)1 RelaxedDtlsEndpointContextMatcher (org.eclipse.californium.elements.RelaxedDtlsEndpointContextMatcher)1 StrictDtlsEndpointContextMatcher (org.eclipse.californium.elements.StrictDtlsEndpointContextMatcher)1 TlsEndpointContextMatcher (org.eclipse.californium.elements.TlsEndpointContextMatcher)1 Configuration (org.eclipse.californium.elements.config.Configuration)1 Test (org.junit.Test)1