/** * Construct an instance of NetworkVehicleInterface with a receiver * callback and custom device URI. * * If the device cannot be found at initialization, the object will block * waiting for a signal to check again. * * * @param context * The Activity or Service context, used to get access to the * Android NetworkManager. * @param callback * An object implementing the SourceCallback that should receive * data as it is received and parsed. * @param uri * The network host's address. * @throws DataSourceException * If no connection could be established */ public NetworkVehicleInterface(SourceCallback callback, Context context, URI uri) throws DataSourceException { super(callback, context); setUri(uri); start(); }
private void setUri(String uri) throws DataSourceException { setUri(UriBasedVehicleInterfaceMixin.createUri(massageUri(uri))); }
public NetworkVehicleInterface(Context context, String uriString) throws DataSourceException { this(context, UriBasedVehicleInterfaceMixin.createUri( massageUri(uriString))); }
@Override protected void connect() throws NetworkSourceException { if(!isRunning()) { return; mSocket.connect(new InetSocketAddress(mUri.getHost(), mUri.getPort()), SOCKET_TIMEOUT); if(!isConnected()) { Log.d(TAG, "Could not connect to server"); disconnected(); } else { connected(); connectStreams(); disconnect(); throw new NetworkSourceException(message, e); } catch(AssertionError e) { String message = "Error opening streams"; Log.e(TAG, message, e); disconnect(); throw new NetworkSourceException(message, e); } finally { mConnectionLock.writeLock().unlock();
@Test public void testResourceChanged() throws DataSourceException { source = new NetworkVehicleInterface(getContext(), goodUri); assertTrue(source.setResource(missingPrefixUri)); }
@Test public void testMissingPrefix() throws DataSourceException { source = new NetworkVehicleInterface(getContext(), missingPrefixUri); } }
@Test public void testValidateResource() { assertTrue(NetworkVehicleInterface.validateResource(goodUri)); assertFalse(NetworkVehicleInterface.validateResource(missingPortUri)); assertFalse(NetworkVehicleInterface.validateResource(incorrectSchemeUri)); assertTrue(NetworkVehicleInterface.validateResource(missingPrefixUri)); }
private void connectStreams() throws NetworkSourceException { mConnectionLock.writeLock().lock(); try { try { mInStream = mSocket.getInputStream(); mOutStream = mSocket.getOutputStream(); Log.i(TAG, "Socket created, streams assigned"); } catch(IOException e) { String message = "Error opening Network socket streams"; Log.e(TAG, message, e); disconnected(); throw new NetworkSourceException(message); } } finally { mConnectionLock.writeLock().unlock(); } }
@Override protected int read(byte[] bytes) throws IOException { mConnectionLock.readLock().lock(); int bytesRead = -1; try { if(isConnected() && mInStream != null) { bytesRead = mInStream.read(bytes, 0, bytes.length); } } finally { mConnectionLock.readLock().unlock(); } return bytesRead; }
@After public void tearDown() throws Exception { if(source != null) { source.stop(); } }
@Test public void testResourceMatching() throws DataSourceException { source = new NetworkVehicleInterface(getContext(), goodUri); assertFalse(source.setResource(goodUri)); }
@Test public void testValidUri() throws DataSourceException { source = new NetworkVehicleInterface(getContext(), goodUri); }
@Override public boolean setResource(String otherResource) throws DataSourceException { if(!UriBasedVehicleInterfaceMixin.sameResource(mUri, massageUri(otherResource))) { setUri(otherResource); try { if(mSocket != null) { mSocket.close(); } } catch(IOException e) { } return true; } return false; }
@Test public void testValidateInvalidPort() throws DataSourceException { assertFalse(NetworkVehicleInterface.validateResource("http://localhost:70000")); }
/** * Return true if the address and port are valid. * * @return true if the address and port are valid. */ public static boolean validateResource(String uriString) { try { URI uri = UriBasedVehicleInterfaceMixin.createUri(massageUri(uriString)); return UriBasedVehicleInterfaceMixin.validateResource(uri) && uri.getPort() < 65536; } catch(DataSourceException e) { return false; } }
/** * Writes given data to the socket. * * @param bytes data to write to the socket. * @return true if the data was written successfully. */ protected synchronized boolean write(byte[] bytes) { mConnectionLock.readLock().lock(); boolean success = true; try { if(isConnected()) { Log.v(TAG, "Writing " + bytes.length + " to socket"); mOutStream.write(bytes); } else { Log.w(TAG, "No connection established, could not send anything."); success = false; } } catch(IOException e) { Log.w(TAG, "Unable to write CAN message to Network. Error: " + e.toString()); success = false; } finally { mConnectionLock.readLock().unlock(); } return success; }
@Test public void testResourceMatchingMassaged() throws DataSourceException { source = new NetworkVehicleInterface(getContext(), missingPrefixUri); assertFalse(source.setResource(missingPrefixUri)); }
@Test public void testMalformedUri() throws DataSourceException { try { source = new NetworkVehicleInterface(getContext(), missingPortUri); } catch(DataSourceResourceException e) { return; } Assert.fail("Expected a DataSourceResourceException"); }
@Test public void testInvalidPort() throws DataSourceException { try { source = new NetworkVehicleInterface(getContext(), "http://localhost:70000"); } catch(DataSourceResourceException e) { return; } Assert.fail("Expected a DataSourceResourceException"); }
@Test public void testUriWithBadScheme() throws DataSourceException { try { source = new NetworkVehicleInterface(getContext(), incorrectSchemeUri); } catch(DataSourceResourceException e) { return; } Assert.fail("Expected a DataSourceResourceException"); }