ProxyLinkFactory implements LinkFactory<ProxyLinkConfig> { @Override public ConnectionBasedLink newLink(ProxyLinkConfig config) throws UnknownHostException, IOException { final ProxyConnectionToRemote remote = config.getRemote(); remote.send(CONNECT_CMD.getCommand()); remote.send(checkNotNull(config.getPort(), "port must not be null")); remote.send(String.valueOf(config.getSpeed())); String response = remote.read(); checkState(OK.equals(response), "Did not receive %s from remote, got %s", OK, response); Socket socket = remote.getSocket(); Protocol proto = ArdulinkProtocol2.instance(); return new ConnectionBasedLink(new StreamConnection( socket.getInputStream(), socket.getOutputStream(), proto), proto) { @Override public void close() throws IOException { super.close(); remote.close(); } }; }  @Override String getName(); @Override ConnectionBasedLink newLink(ProxyLinkConfig config); @Override ProxyLinkConfig newLinkConfig(); static final String OK; }
@Test public void canConnect() throws Exception { LinkManager connectionManager = LinkManager.getInstance(); Configurer configurer = connectionManager.getConfigurer(URIs .newURI("ardulink: + proxyServerDouble.getLocalPort())); ConfigAttribute port = configurer.getAttribute("port"); Object[] values = new String[] { "myPortNr0" }; assertThat(port.getChoiceValues(), is(values)); port.setValue(values[0]); configurer.newLink().close(); assertThat(proxyServerDouble.getReceived(), is(Arrays.asList( "ardulink:networkproxyserver:get_port_list", "ardulink:networkproxyserver:get_port_list", "ardulink:networkproxyserver:connect", "myPortNr0", "115200"))); }
@Test public void canSwitchAnalogPort() throws Exception { LinkManager connectionManager = LinkManager.getInstance(); Configurer configurer = connectionManager.getConfigurer(URIs .newURI("ardulink: + proxyServerDouble.getLocalPort())); Object[] values = new String[] { "myPortNr0" }; configurer.getAttribute("port").setValue(values[0]); Link newLink = configurer.newLink(); newLink.switchAnalogPin(Pin.analogPin(1), 123); assertThat(proxyServerDouble.getReceived(), is(Arrays.asList( "ardulink:networkproxyserver:get_port_list", "ardulink:networkproxyserver:connect", "myPortNr0", "115200", "alp: newLink.close(); }