Skip to content

Commit

Permalink
Server threads now extend PayloadThread
Browse files Browse the repository at this point in the history
  • Loading branch information
apangin committed Jun 14, 2018
1 parent 5583b69 commit a7c136c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/one/nio/server/AcceptorThread.java
Expand Up @@ -48,7 +48,7 @@ final class AcceptorThread extends Thread {
if (config.ssl != null) {
SslContext sslContext = SslContext.create();
sslContext.configure(config.ssl);
serverSocket = serverSocket.ssl(sslContext);
serverSocket = serverSocket.sslWrap(sslContext);
}
this.serverSocket = serverSocket;

Expand Down
45 changes: 45 additions & 0 deletions src/one/nio/server/PayloadThread.java
@@ -0,0 +1,45 @@
/*
* Copyright 2018 Odnoklassniki Ltd, Mail.Ru Group
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package one.nio.server;

public class PayloadThread extends Thread {
protected Object payload;

public PayloadThread(Runnable target) {
super(target);
}

public PayloadThread(String name) {
super(name);
}

public PayloadThread(Runnable target, String name) {
super(target, name);
}

public final Object payload() {
return payload;
}

public final void setPayload(Object payload) {
this.payload = payload;
}

public static PayloadThread current() {
return (PayloadThread) Thread.currentThread();
}
}
2 changes: 1 addition & 1 deletion src/one/nio/server/SelectorThread.java
Expand Up @@ -23,7 +23,7 @@
import java.io.IOException;
import java.util.Iterator;

public final class SelectorThread extends Thread {
public final class SelectorThread extends PayloadThread {
private static final int BUFFER_SIZE = 64000;

public final Selector selector;
Expand Down
30 changes: 17 additions & 13 deletions src/one/nio/server/ServerConfig.java
Expand Up @@ -34,13 +34,10 @@ public class ServerConfig {
public int keepAlive;
public int threadPriority = Thread.NORM_PRIORITY;

@Deprecated // Do not use for new servers! Use ConfigParser instead
public static ServerConfig from(String conn) {
return from(new ConnectionString(conn));
public ServerConfig() {
}

@Deprecated // Do not use for new servers! Use ConfigParser instead
public static ServerConfig from(ConnectionString conn) {
private ServerConfig(ConnectionString conn) {
AcceptorConfig ac = new AcceptorConfig();
ac.address = conn.getHost();
ac.port = conn.getPort();
Expand All @@ -52,14 +49,21 @@ public static ServerConfig from(ConnectionString conn) {
ac.ssl = SslConfig.from(System.getProperties());
}

ServerConfig sc = new ServerConfig();
sc.acceptors = new AcceptorConfig[]{ac};
sc.selectors = conn.getIntParam("selectors", 0);
sc.minWorkers = conn.getIntParam("minWorkers", 0);
sc.maxWorkers = conn.getIntParam("maxWorkers", 0);
sc.queueTime = conn.getIntParam("queueTime", 0) / 1000;
sc.threadPriority = conn.getIntParam("threadPriority", Thread.NORM_PRIORITY);
this.acceptors = new AcceptorConfig[]{ac};
this.selectors = conn.getIntParam("selectors", 0);
this.minWorkers = conn.getIntParam("minWorkers", 0);
this.maxWorkers = conn.getIntParam("maxWorkers", 0);
this.queueTime = conn.getIntParam("queueTime", 0) / 1000;
this.threadPriority = conn.getIntParam("threadPriority", Thread.NORM_PRIORITY);
}

return sc;
@Deprecated // Do not use for new servers! Use ConfigParser instead
public static ServerConfig from(String conn) {
return new ServerConfig(new ConnectionString(conn));
}

@Deprecated // Do not use for new servers! Use ConfigParser instead
public static ServerConfig from(ConnectionString conn) {
return new ServerConfig(conn);
}
}
2 changes: 1 addition & 1 deletion src/one/nio/server/WorkerPool.java
Expand Up @@ -54,7 +54,7 @@ void gracefulShutdown(long timeout) {

@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r, "NIO Worker #" + index.incrementAndGet());
Thread thread = new PayloadThread(r, "NIO Worker #" + index.incrementAndGet());
thread.setUncaughtExceptionHandler(this);
thread.setPriority(threadPriority);
return thread;
Expand Down

0 comments on commit a7c136c

Please sign in to comment.