@Before public void setupServer() { try { System.out.println("\nStart " + getClass().getSimpleName()); EndpointManager.clear(); CoAPEndpoint endpoint = new CoAPEndpoint(); server = new CoapServer(); server.addEndpoint(endpoint); server.add( new CoapResource(ACC_RESOURCE) { public void handlePOST(CoapExchange exchange) { exchange.accept(); System.out.println("gotit"); exchange.respond(SERVER_RESPONSE); } }); server.add( new CoapResource(NO_ACC_RESOURCE) { public void handlePOST(CoapExchange exchange) { exchange.respond(SERVER_RESPONSE); } }); server.start(); serverPort = endpoint.getAddress().getPort(); } catch (Throwable t) { t.printStackTrace(); } }
public void destroy() { // Destroy server coapServer.destroy(); // Destroy registries if (clientRegistry instanceof Destroyable) { ((Destroyable) clientRegistry).destroy(); } if (securityRegistry instanceof Destroyable) { ((Destroyable) securityRegistry).destroy(); } if (observationRegistry instanceof Destroyable) { ((Destroyable) observationRegistry).destroy(); } LOG.info("LW-M2M server destroyed"); }
@Override public void stop() { // Stop server coapServer.stop(); // Start registries if (clientRegistry instanceof Stoppable) { ((Stoppable) clientRegistry).stop(); } if (securityRegistry instanceof Stoppable) { ((Stoppable) securityRegistry).stop(); } if (observationRegistry instanceof Stoppable) { ((Stoppable) observationRegistry).stop(); } LOG.info("LW-M2M server stopped"); }
public static void main(String[] args) throws Exception { System.out.println("Californium (Cf) Observe Benchmark Client"); System.out.println("(c) 2015, Institute for Pervasive Computing, ETH Zurich"); System.out.println(); System.out.println("This machine has " + CORES + " cores"); System.out.println("Operating system: " + OS); String address = null; int port = DEFAULT_PORT; int udp_sender = DEFAULT_SENDER_COUNT; int udp_receiver = DEFAULT_RECEIVER_COUNT; int protocol_threads = DEFAULT_PROTOCOL_STAGE_THREAD_COUNT; boolean verbose = false; boolean use_executor = false; // Parse input if (args.length > 0) { int index = 0; while (index < args.length) { String arg = args[index]; if ("-usage".equals(arg) || "-help".equals(arg) || "-h".equals(arg) || "-?".equals(arg)) { printUsage(); } else if ("-t".equals(arg)) { protocol_threads = Integer.parseInt(args[index + 1]); } else if ("-s".equals(arg)) { udp_sender = Integer.parseInt(args[index + 1]); } else if ("-r".equals(arg)) { udp_receiver = Integer.parseInt(args[index + 1]); } else if ("-p".equals(arg)) { port = Integer.parseInt(args[index + 1]); } else if ("-a".equals(arg)) { address = args[index + 1]; } else if ("-v".equals(arg)) { verbose = true; } else if ("-use-executor".equals(arg)) { use_executor = true; } else { System.err.println("Unknwon arg " + arg); printUsage(); } index += 2; } } // Parse address InetAddress addr = address != null ? InetAddress.getByName(address) : null; InetSocketAddress sockAddr = new InetSocketAddress((InetAddress) addr, port); setBenchmarkConfiguration(udp_sender, udp_receiver, verbose); // Create server CoapServer server = new CoapServer(); if (use_executor) { System.out.println("Using a scheduled thread pool with " + protocol_threads + " workers"); server.setExecutor(Executors.newScheduledThreadPool(protocol_threads)); } System.out.println("Number of receiver/sender threads: " + udp_receiver + "/" + udp_sender); server.add(new AnnounceResource("announce")); server.addEndpoint(new CoapEndpoint(sockAddr)); server.start(); System.out.println("Observe benchmark announcement server listening on " + sockAddr); }
/** * Initialize a server which will bind to the specified address and port. * * @param localAddress the address to bind the CoAP server. * @param localAddressSecure the address to bind the CoAP server for DTLS connection. * @param privateKey for RPK authentication mode * @param publicKey for RPK authentication mode */ public LeshanServer( InetSocketAddress localAddress, InetSocketAddress localAddressSecure, final ClientRegistry clientRegistry, final SecurityRegistry securityRegistry, final ObservationRegistry observationRegistry, final LwM2mModelProvider modelProvider) { Validate.notNull(localAddress, "IP address cannot be null"); Validate.notNull(localAddressSecure, "Secure IP address cannot be null"); Validate.notNull(clientRegistry, "clientRegistry cannot be null"); Validate.notNull(securityRegistry, "securityRegistry cannot be null"); Validate.notNull(observationRegistry, "observationRegistry cannot be null"); Validate.notNull(modelProvider, "modelProvider cannot be null"); // Init registries this.clientRegistry = clientRegistry; this.securityRegistry = securityRegistry; this.observationRegistry = observationRegistry; this.modelProvider = modelProvider; // Cancel observations on client unregistering this.clientRegistry.addListener( new ClientRegistryListener() { @Override public void updated(final Client clientUpdated) {} @Override public void unregistered(final Client client) { LeshanServer.this.observationRegistry.cancelObservations(client); } @Override public void registered(final Client client) {} }); // default endpoint coapServer = new CoapServer(); final Endpoint endpoint = new CoAPEndpoint(localAddress); coapServer.addEndpoint(endpoint); // secure endpoint DTLSConnector connector = new DTLSConnector(localAddressSecure); connector .getConfig() .setPskStore(new LwM2mPskStore(this.securityRegistry, this.clientRegistry)); PrivateKey privateKey = this.securityRegistry.getServerPrivateKey(); PublicKey publicKey = this.securityRegistry.getServerPublicKey(); if (privateKey != null && publicKey != null) { connector.getConfig().setPrivateKey(privateKey, publicKey); // TODO this should be automatically done by scandium connector.getConfig().setPreferredCipherSuite(CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8); } else { // TODO this should be automatically done by scandium connector.getConfig().setPreferredCipherSuite(CipherSuite.TLS_PSK_WITH_AES_128_CCM_8); } final Endpoint secureEndpoint = new SecureEndpoint(connector); coapServer.addEndpoint(secureEndpoint); // define /rd resource final RegisterResource rdResource = new RegisterResource(new RegistrationHandler(this.clientRegistry, this.securityRegistry)); coapServer.add(rdResource); // create sender final Set<Endpoint> endpoints = new HashSet<>(); endpoints.add(endpoint); endpoints.add(secureEndpoint); requestSender = new CaliforniumLwM2mRequestSender( endpoints, this.clientRegistry, this.observationRegistry, modelProvider); }
@After public void after() { server.destroy(); System.out.println("End " + getClass().getSimpleName()); }