@Override public void receiveData(final RawData raw) { LOG.log(Level.INFO, "Received request: {0}", new String(raw.getBytes())); connector.send(new RawData("ACK".getBytes(), raw.getAddress(), raw.getPort())); } }
@After public void cleanup() { for (Connector connector : cleanup) { connector.stop(); } }
@Override public synchronized void destroy() { LOGGER.log(Level.INFO, "Destroying endpoint at address {0}", getAddress()); if (started) { stop(); } connector.destroy(); coapstack.destroy(); for (EndpointObserver obs : observers) { obs.destroyed(this); } }
new DaemonThreadFactory("CoapEndpoint-" + connector.getAddress() + '#'))); //$NON-NLS-1$ addObserver(new EndpointObserver() { @Override connector.start(); for (EndpointObserver obs : observers) { obs.started(this);
@Override public InetSocketAddress getAddress() { return connector.getAddress(); }
/** * Instantiates a new endpoint with the specified connector and * configuration. * * @param connector the connector * @param config the config */ public CoapEndpoint(Connector connector, NetworkConfig config) { this.config = config; this.connector = connector; this.serializer = new Serializer(); this.matcher = new Matcher(config); this.coapstack = new CoapStack(config, new OutboxImpl()); this.connector.setRawDataReceiver(new InboxImpl()); }
connector.start(); for (EndpointObserver obs:observers) obs.started(this);
@Override public InetSocketAddress getAddress() { return connector.getAddress(); }
/** * Creates a new endpoint for a connector, configuration and message exchange store. * * @param connector the connector * @param config the config * @param exchangeStore the store to use for keeping track of message exchanges. */ public CoapEndpoint(final Connector connector, final NetworkConfig config, final MessageExchangeStore exchangeStore) { this.config = config; this.connector = connector; this.connector.setRawDataReceiver(new InboxImpl()); MessageExchangeStore localExchangeStore = (null != exchangeStore) ? exchangeStore : new InMemoryMessageExchangeStore(config); // To make TCP support backwards compatible using less clean "instanceof" shortcut in 1.1 branch. // In 2.0 branch, the connector API has been expected to export a new isSchemeSupported(String scheme) // method, which is used instead. if (connector instanceof TcpConnector) { this.matcher = new TcpMatcher(config, localExchangeStore); this.coapstack = new CoapTcpStack(config, new OutboxImpl()); this.serializer = new TcpDataSerializer(); this.parser = new TcpDataParser(); } else { this.matcher = new UdpMatcher(config, localExchangeStore); this.coapstack = new CoapUdpStack(config, new OutboxImpl()); this.serializer = new UdpDataSerializer(); this.parser = new UdpDataParser(); } }
private void reject(Message message) { EmptyMessage rst = EmptyMessage.newRST(message); // sending directly through connector, not stack, thus set token rst.setToken(new byte[0]); for (MessageInterceptor interceptor:interceptors) interceptor.sendEmptyMessage(rst); // MessageInterceptor might have canceled if (!rst.isCanceled()) connector.send(serializer.serialize(rst)); }
@After public void cleanup() { for (Connector connector : cleanup) { connector.stop(); } }
@Override public synchronized void destroy() { LOGGER.log(Level.INFO, "Destroying endpoint at address " + getAddress()); if (started) stop(); connector.destroy(); coapstack.destroy(); for (EndpointObserver obs:observers) obs.destroyed(this); }
connector.send(serializer.serialize(request));
@Override public synchronized void stop() { if (!started) { LOGGER.log(Level.INFO, "Endpoint at " + getAddress() + " is already stopped"); } else { LOGGER.log(Level.INFO, "Stopping endpoint at address " + getAddress()); started = false; connector.stop(); matcher.stop(); for (EndpointObserver obs:observers) obs.stopped(this); matcher.clear(); } }
@Override public void sendResponse(Exchange exchange, Response response) { if (response.getDestination() == null) throw new NullPointerException("Response has no destination address"); if (response.getDestinationPort() == 0) throw new NullPointerException("Response has no destination port"); matcher.sendResponse(exchange, response); /* * Logging here causes significant performance loss. * If necessary, add an interceptor that logs the messages, * e.g., the MessageTracer. */ for (MessageInterceptor interceptor:interceptors) interceptor.sendResponse(response); // MessageInterceptor might have canceled if (response.isCanceled()) { if (null != exchange) { exchange.setComplete(); } } else { connector.send(serializer.serialize(response)); } }
@Override public synchronized void stop() { if (!started) { LOGGER.log(Level.INFO, "Endpoint at {0} is already stopped", getAddress()); } else { LOGGER.log(Level.INFO, "Stopping endpoint at address {0}", getAddress()); started = false; connector.stop(); matcher.stop(); for (EndpointObserver obs : observers) { obs.stopped(this); } matcher.clear(); } }
@Override public void sendEmptyMessage(Exchange exchange, EmptyMessage message) { if (message.getDestination() == null) throw new NullPointerException("Message has no destination address"); if (message.getDestinationPort() == 0) throw new NullPointerException("Message has no destination port"); matcher.sendEmptyMessage(exchange, message); /* * Logging here causes significant performance loss. * If necessary, add an interceptor that logs the messages, * e.g., the MessageTracer. */ for (MessageInterceptor interceptor:interceptors) interceptor.sendEmptyMessage(message); // MessageInterceptor might have canceled if (message.isCanceled()) { if (null != exchange) { exchange.setComplete(); } } else { connector.send(serializer.serialize(message)); } } }
@Override public void sendResponse(Exchange exchange, Response response) { assertMessageHasDestinationAddress(response); matcher.sendResponse(exchange, response); /* * Logging here causes significant performance loss. * If necessary, add an interceptor that logs the messages, * e.g., the MessageTracer. */ for (MessageInterceptor interceptor:interceptors) { interceptor.sendResponse(response); } // MessageInterceptor might have canceled if (response.isCanceled()) { if (null != exchange) { exchange.setComplete(); } } else { connector.send(serializer.serializeResponse(response)); } }
@Override public void sendEmptyMessage(Exchange exchange, EmptyMessage message) { assertMessageHasDestinationAddress(message); matcher.sendEmptyMessage(exchange, message); /* * Logging here causes significant performance loss. * If necessary, add an interceptor that logs the messages, * e.g., the MessageTracer. */ for (MessageInterceptor interceptor:interceptors) { interceptor.sendEmptyMessage(message); } // MessageInterceptor might have canceled if (message.isCanceled()) { if (null != exchange) { exchange.setComplete(); } } else { connector.send(serializer.serializeEmptyMessage(message)); } }
connector.send(message);