nanohttpd: refactor: rename myPort->port, myThread->listenerThread, myServerSocket->serverSocket

This commit is contained in:
Haowei Wen 2020-08-26 20:44:18 +08:00
parent cda96f96e1
commit c4a769271a
No known key found for this signature in database
GPG key ID: 5BC167F73EA558E4

View file

@ -174,7 +174,7 @@ public abstract class NanoHTTPD {
@Override @Override
public void run() { public void run() {
try { try {
myServerSocket.bind(hostname != null ? new InetSocketAddress(hostname, myPort) : new InetSocketAddress(myPort)); serverSocket.bind(hostname != null ? new InetSocketAddress(hostname, port) : new InetSocketAddress(port));
hasBinded = true; hasBinded = true;
} catch (IOException e) { } catch (IOException e) {
this.bindException = e; this.bindException = e;
@ -183,7 +183,7 @@ public abstract class NanoHTTPD {
do { do {
try { try {
@SuppressWarnings("resource") @SuppressWarnings("resource")
final Socket finalAccept = NanoHTTPD.this.myServerSocket.accept(); final Socket finalAccept = NanoHTTPD.this.serverSocket.accept();
finalAccept.setSoTimeout(SOCKET_READ_TIMEOUT); finalAccept.setSoTimeout(SOCKET_READ_TIMEOUT);
@SuppressWarnings("resource") @SuppressWarnings("resource")
final InputStream inputStream = finalAccept.getInputStream(); final InputStream inputStream = finalAccept.getInputStream();
@ -191,7 +191,7 @@ public abstract class NanoHTTPD {
} catch (IOException e) { } catch (IOException e) {
log(DEBUG, "Communication with the client broken", e); log(DEBUG, "Communication with the client broken", e);
} }
} while (!NanoHTTPD.this.myServerSocket.isClosed()); } while (!NanoHTTPD.this.serverSocket.isClosed());
} }
} }
@ -214,14 +214,12 @@ public abstract class NanoHTTPD {
} }
private final String hostname; private final String hostname;
private final int port;
private final int myPort; private volatile ServerSocket serverSocket;
private Thread listenerThread;
private volatile ServerSocket myServerSocket; private final AsyncRunner asyncRunner = new AsyncRunner();
private Thread myThread;
private AsyncRunner asyncRunner = new AsyncRunner();
/** /**
* Constructs an HTTP server on given port. * Constructs an HTTP server on given port.
@ -243,15 +241,15 @@ public abstract class NanoHTTPD {
*/ */
public NanoHTTPD(String hostname, int port) { public NanoHTTPD(String hostname, int port) {
this.hostname = hostname; this.hostname = hostname;
this.myPort = port; this.port = port;
} }
public final int getListeningPort() { public final int getListeningPort() {
return this.myServerSocket == null ? -1 : this.myServerSocket.getLocalPort(); return this.serverSocket == null ? -1 : this.serverSocket.getLocalPort();
} }
public final boolean isAlive() { public final boolean isAlive() {
return wasStarted() && !this.myServerSocket.isClosed() && this.myThread.isAlive(); return wasStarted() && !this.serverSocket.isClosed() && this.listenerThread.isAlive();
} }
public String getHostname() { public String getHostname() {
@ -288,14 +286,14 @@ public abstract class NanoHTTPD {
* if the socket is in use. * if the socket is in use.
*/ */
public void start(boolean daemon) throws IOException { public void start(boolean daemon) throws IOException {
this.myServerSocket = new ServerSocket(); this.serverSocket = new ServerSocket();
this.myServerSocket.setReuseAddress(true); this.serverSocket.setReuseAddress(true);
ServerRunnable serverRunnable = new ServerRunnable(); ServerRunnable serverRunnable = new ServerRunnable();
this.myThread = new Thread(serverRunnable); this.listenerThread = new Thread(serverRunnable);
this.myThread.setDaemon(daemon); this.listenerThread.setDaemon(daemon);
this.myThread.setName("NanoHttpd Main Listener"); this.listenerThread.setName("NanoHttpd Main Listener");
this.myThread.start(); this.listenerThread.start();
while (!serverRunnable.hasBinded && serverRunnable.bindException == null) { while (!serverRunnable.hasBinded && serverRunnable.bindException == null) {
try { try {
Thread.sleep(10L); Thread.sleep(10L);
@ -315,10 +313,10 @@ public abstract class NanoHTTPD {
*/ */
public void stop() { public void stop() {
try { try {
safeClose(this.myServerSocket); safeClose(this.serverSocket);
this.asyncRunner.closeAll(); this.asyncRunner.closeAll();
if (this.myThread != null) { if (this.listenerThread != null) {
this.myThread.join(); this.listenerThread.join();
} }
} catch (Exception e) { } catch (Exception e) {
log(ERROR, "Could not stop all connections", e); log(ERROR, "Could not stop all connections", e);
@ -326,6 +324,6 @@ public abstract class NanoHTTPD {
} }
public final boolean wasStarted() { public final boolean wasStarted() {
return this.myServerSocket != null && this.myThread != null; return this.serverSocket != null && this.listenerThread != null;
} }
} }