From 9dc51d747b04aa002cd2e2ec340b251b64c41fbb Mon Sep 17 00:00:00 2001 From: Lukas Vacek Date: Wed, 11 Feb 2015 21:09:10 +0100 Subject: [PATCH] MockLocationThread now accepts connections and reads incoming data. --- .../github/luv/mockgeofix/MainActivity.java | 2 + .../luv/mockgeofix/MockLocationService.java | 79 ++++++++++++------- 2 files changed, 54 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/github/luv/mockgeofix/MainActivity.java b/app/src/main/java/github/luv/mockgeofix/MainActivity.java index a26b6d0..9f107c9 100644 --- a/app/src/main/java/github/luv/mockgeofix/MainActivity.java +++ b/app/src/main/java/github/luv/mockgeofix/MainActivity.java @@ -54,6 +54,8 @@ public void onReceive(Context context, Intent intent) { action.equals(MockLocationService.STOPPED)) { MainActivity.this.updateStatus(); MainActivity.this.updateStartStopButton(); + // in case an interface goes down or up + MainActivity.this.updateListensOn(); } if (action.equals(MockLocationService.ERROR) && mService != null) { Toast.makeText(getApplicationContext(), mService.getLastErr(), diff --git a/app/src/main/java/github/luv/mockgeofix/MockLocationService.java b/app/src/main/java/github/luv/mockgeofix/MockLocationService.java index 83e53a0..549e2fe 100644 --- a/app/src/main/java/github/luv/mockgeofix/MockLocationService.java +++ b/app/src/main/java/github/luv/mockgeofix/MockLocationService.java @@ -9,13 +9,20 @@ import android.util.Log; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.net.BindException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.NetworkInterface; -import java.net.ServerSocket; + import java.net.SocketAddress; import java.net.SocketException; + +import java.nio.ByteBuffer; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; import java.util.Enumeration; import java.util.Vector; @@ -178,34 +185,43 @@ public void kill() { @Override public void run() { super.run(); - Vector ssockets = new Vector<>(); try { while (!mStop) { + Selector selector = Selector.open(); mBindAddresses = mService.getBindAddresses(); for (SocketAddress address : mBindAddresses) { - ServerSocket ss = new ServerSocket(); - ss.setReuseAddress(true); - ss.bind(address); - ssockets.add(ss); + ServerSocketChannel ssc = ServerSocketChannel.open(); + ssc.configureBlocking(false); + ssc.socket().setReuseAddress(true); + ssc.socket().bind(address); + ssc.register(selector, SelectionKey.OP_ACCEPT); } mService.threadHasStartedSuccessfully(); try { while (!mStop && !mRebind) { Log.d(TAG, "running"); - try { - Thread.sleep(1000, 0); - } catch (InterruptedException ex) { + selector.select(); + for (SelectionKey key : selector.selectedKeys()) { + if (key.isAcceptable() && key.channel() instanceof ServerSocketChannel) { + // accept connection + SocketChannel client = ((ServerSocketChannel)key.channel()).accept(); + if (client != null) { + client.configureBlocking(false); + client.socket().setTcpNoDelay(true); + client.register(selector, SelectionKey.OP_READ); + } + } + if (key.isReadable() && key.channel() instanceof SocketChannel) { + onIncomingData((SocketChannel) key.channel()); + } } + selector.selectedKeys().clear(); } } finally { - for (ServerSocket ss : ssockets) { - try { - if (!ss.isClosed()) { ss.close(); } - } catch (IOException e) { - Log.e(TAG, e.toString()); - } + for (SelectionKey key : selector.keys()) { + try { key.channel().close(); } catch (IOException ignored) {} } - ssockets.clear(); + selector.close(); } } } catch (SocketException e) { @@ -215,21 +231,30 @@ public void run() { mService.errorHasOccurred(mContext.getString(R.string.err_address_already_in_use)); } else if (e.getClass() == BindException.class && e.getMessage().contains("EACCES") ) { mService.errorHasOccurred(mContext.getString(R.string.err_socket_permission_denied)); - } else { - mService.errorHasOccurred(e.toString()); } } catch (IOException e) { Log.e(TAG, e.toString() ); - mService.errorHasOccurred(e.toString()); } finally { - for (ServerSocket ss : ssockets) { - try { - if (!ss.isClosed()) { ss.close(); } - } catch (IOException e) { - Log.e(TAG, e.toString()); - } - } mService.threadHasStopped(); } } -} + + void onIncomingData(SocketChannel client) { + ByteBuffer buffer = ByteBuffer.allocate(1024); + int bytesRead; + try { + bytesRead = client.read(buffer); + } catch (IOException ex) { + try { client.close(); } catch (IOException ignored) {} + return; + } + if (bytesRead == -1) { + try { client.close(); } catch (IOException ignored) {} + return; + } + try { + String s = new String(buffer.array(), "UTF-8"); + Log.i(TAG, s); + } catch(UnsupportedEncodingException ignored) {} + } +} \ No newline at end of file