@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 {
private void setUri(String uri) throws DataSourceException { setUri(UriBasedVehicleInterfaceMixin.createUri(massageUri(uri))); }
mConnectionLock.writeLock().lock(); try { if(isConnected()) { Log.d(TAG, "Disconnecting from the socket " + mSocket); try { disconnected(); mConnectionLock.writeLock().unlock();
@Test public void testResourceChanged() throws DataSourceException { source = new NetworkVehicleInterface(getContext(), goodUri); assertTrue(source.setResource(missingPrefixUri)); }
@Test public void testValidateResource() { assertTrue(NetworkVehicleInterface.validateResource(goodUri)); assertFalse(NetworkVehicleInterface.validateResource(missingPortUri)); assertFalse(NetworkVehicleInterface.validateResource(incorrectSchemeUri)); assertTrue(NetworkVehicleInterface.validateResource(missingPrefixUri)); }
@Test public void testMissingPrefix() throws DataSourceException { source = new NetworkVehicleInterface(getContext(), missingPrefixUri); } }
public NetworkVehicleInterface(Context context, String uriString) throws DataSourceException { this(context, UriBasedVehicleInterfaceMixin.createUri( massageUri(uriString))); }
@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; }
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(); } }
@Test public void testResourceMatching() throws DataSourceException { source = new NetworkVehicleInterface(getContext(), goodUri); assertFalse(source.setResource(goodUri)); }
private void setNetworkStatus(boolean enabled) { Log.i(TAG, "Setting network data source to " + enabled); if(enabled) { String address = getPreferenceString(R.string.network_host_key); String port = getPreferenceString(R.string.network_port_key); String combinedAddress = address + ":" + port; if(address == null || port == null || !NetworkVehicleInterface.validateResource( combinedAddress)) { String error = "Network host URI (" + combinedAddress + ") not valid -- not starting network data source"; Log.w(TAG, error); SharedPreferences.Editor editor = getPreferences().edit(); editor.putBoolean(getString(R.string.uploading_checkbox_key), false); editor.commit(); } else { try { getVehicleManager().setVehicleInterface( NetworkVehicleInterface.class, combinedAddress); } catch(VehicleServiceException e) { Log.e(TAG, "Unable to add network interface", e); } } } } }
@Test public void testValidUri() throws DataSourceException { source = new NetworkVehicleInterface(getContext(), goodUri); }
/** * 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 testValidateInvalidPort() throws DataSourceException { assertFalse(NetworkVehicleInterface.validateResource("http://localhost:70000")); }
@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 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"); }