Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.nio.channels.CompletionHandler;
import java.security.InvalidParameterException;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
Expand Down Expand Up @@ -66,6 +67,12 @@ public final class AioQuickServer {
* <p>调用AioQuickClient的各setXX()方法,都是为了设置config的各配置项</p>
*/
private final IoServerConfig config = new IoServerConfig();

/**
* banner实例
* <p>可以通过资源文件里面的banner.txt来自定义banner</p>
*/
private final Banner banner;
private static long threadSeqNumber;
/**
* write 内存池
Expand All @@ -76,6 +83,16 @@ public final class AioQuickServer {
*/
private BufferPagePool readBufferPool = null;

/**
* 默认banner
*/
private final Consumer<IoServerConfig> defaultBanner = (config) -> {
System.out.println(IoServerConfig.BANNER + "\r\n :: smart-socket " + "::\t(" + IoServerConfig.VERSION + ") [port: " + config.getPort() + ", threadNum:" + config.getThreadNum() + "]");
System.out.println("Technical Support:");
System.out.println(" - Document: https://smartboot.tech]");
System.out.println(" - Gitee: https://gitee.com/smartboot/smart-socket");
System.out.println(" - Github: https://github.com/smartboot/smart-socket");
};

/**
* 设置服务端启动必要参数配置
Expand All @@ -89,6 +106,8 @@ public <T> AioQuickServer(int port, Protocol<T> protocol, MessageProcessor<T> me
config.setProtocol(protocol);
config.setProcessor(messageProcessor);
config.setThreadNum(Runtime.getRuntime().availableProcessors());

this.banner = new Banner(config);
}

/**
Expand Down Expand Up @@ -118,13 +137,7 @@ public void start() throws IOException {
* @throws IOException IO异常
*/
public void start(AsynchronousChannelGroup asynchronousChannelGroup) throws IOException {
if (config.isBannerEnabled()) {
System.out.println(IoServerConfig.BANNER + "\r\n :: smart-socket " + "::\t(" + IoServerConfig.VERSION + ") [port: " + config.getPort() + ", threadNum:" + config.getThreadNum() + "]");
System.out.println("Technical Support:");
System.out.println(" - Document: https://smartboot.tech]");
System.out.println(" - Gitee: https://gitee.com/smartboot/smart-socket");
System.out.println(" - Github: https://github.com/smartboot/smart-socket");
}
if (config.isBannerEnabled()) banner.printBanner("banner.txt", defaultBanner);
try {
if (writeBufferPool == null) {
this.writeBufferPool = BufferPagePool.DEFAULT_BUFFER_PAGE_POOL;
Expand Down
32 changes: 32 additions & 0 deletions aio-core/src/main/java/org/smartboot/socket/transport/Banner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.smartboot.socket.transport;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.function.Consumer;

/**
* @author Frish2021
* 用于输出banner
*/
final class Banner {
private final IoServerConfig config;
private final BannerFormat format;

Banner(IoServerConfig config) {
this.config = config;
this.format = new BannerFormat(config);
}

void printBanner(String fileName, Consumer<IoServerConfig> defaultPrint) throws IOException {
try(InputStream in = AioQuickServer.class.getClassLoader().getResourceAsStream(fileName)) {
if (in == null) {
defaultPrint.accept(config);
return;
}

List<String> lines = IOUtil.readLineFromStream(in);
lines.forEach(format::printf);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.smartboot.socket.transport;

import java.util.Map;
import java.util.WeakHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author Frish2021
* 用于格式化banner以及提供格式化banner时的占位符API
*/
final class BannerFormat {
private final static Pattern pattern = Pattern.compile("\\$\\{([^}]+)}");
private final Map<String, Format> map = new WeakHashMap<>();
private final IoServerConfig config;

BannerFormat(IoServerConfig config) {
this.config = config;

{
map.put("version", ignore -> IoServerConfig.VERSION);
map.put("port", IoServerConfig::getPort);
map.put("threads", IoServerConfig::getThreadNum);
map.put("backlog", IoServerConfig::getBacklog);
}
}

void printf(String line) {
Matcher matcher = pattern.matcher(line);
StringBuffer result = new StringBuffer();
replaceAll(matcher, (key) -> containsKeyReplace(key, (replacement) -> {
matcher.appendReplacement(result, replacement);
}));

matcher.appendTail(result);
System.out.println(result);
}

/**
* 替换展位符的同时判断是否是存在的占位符
*/
private void containsKeyReplace(String key, Replace replace) {
if (map.containsKey(key)) {
String replacement = toString(key);
replace.replace(replacement);
}
}

/**
* 替换所有的占位符
*/
private void replaceAll(Matcher matcher, Replace replace) {
while (matcher.find()) {
String key = matcher.group(1);
replace.replace(key);
}
}

/**
* 获取到展位符的值
*/
private String toString(String key) {
return map.get(key).format(config).toString();
}

/**
* 用于replaceAll和containsKeyReplace方法的封装
*/
@FunctionalInterface
interface Replace {
void replace(String key);
}

/**
* 占位符格式化接口
*/
@FunctionalInterface
interface Format {
Object format(IoServerConfig config);
}
}
20 changes: 20 additions & 0 deletions aio-core/src/main/java/org/smartboot/socket/transport/IOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@

package org.smartboot.socket.transport;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.NotYetConnectedException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
* @author 三刀
Expand Down Expand Up @@ -41,4 +47,18 @@ static void close(AsynchronousSocketChannel channel) {
}
}

/**
* @param in 文件输入流
*/
static List<String> readLineFromStream(InputStream in) throws IOException {
List<String> result = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
result.add(line);
}
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ final class IoServerConfig {

/**
* banner信息
* @deprecated 通过banner.txt可以自定义banner内容
*/
@Deprecated
public static final String BANNER = "\n" +
" _ _ _ \n" +
" ( )_ ( ) ( )_ \n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.DatagramChannel;
import java.util.function.Consumer;

/**
* UDP服务启动类
Expand All @@ -41,6 +42,18 @@ public class UdpBootstrap {
private Worker worker;
private boolean innerWorker = false;

/**
* banner实例
* <p>可以通过banner.txt来自定义</p>
*/
private final Banner banner;

/**
* 默认banner
*/
private final Consumer<IoServerConfig> defaultBanner = (config) -> {
System.out.println(IoServerConfig.BANNER + "\r\n :: smart-socket[udp] ::\t(" + IoServerConfig.VERSION + ")");
};

public <Request> UdpBootstrap(Protocol<Request> protocol, MessageProcessor<Request> messageProcessor, Worker worker) {
this(protocol, messageProcessor);
Expand All @@ -50,6 +63,8 @@ public <Request> UdpBootstrap(Protocol<Request> protocol, MessageProcessor<Reque
public <Request> UdpBootstrap(Protocol<Request> protocol, MessageProcessor<Request> messageProcessor) {
config.setProtocol(protocol);
config.setProcessor(messageProcessor);

this.banner = new Banner(config);
}

/**
Expand Down Expand Up @@ -78,9 +93,7 @@ public UdpChannel open(int port) throws IOException {
*/
public UdpChannel open(String host, int port) throws IOException {
// 增加广告说明
if (config.isBannerEnabled()) {
System.out.println(IoServerConfig.BANNER + "\r\n :: smart-socket[udp] ::\t(" + IoServerConfig.VERSION + ")");
}
if (config.isBannerEnabled()) banner.printBanner("banner.txt", defaultBanner);
//初始化内存池
if (writeBufferPool == null) {
this.writeBufferPool = BufferPagePool.DEFAULT_BUFFER_PAGE_POOL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
public class PushServer {
public static void main(String[] args) throws IOException {
AioQuickServer server = new AioQuickServer(8080, new StringProtocol(), new PushServerProcessorMessage());
server.setBannerEnabled(true);
server.start();
}
}
5 changes: 5 additions & 0 deletions example/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Version: ${version} | Port: ${port} | backlog: ${backlog}
Technical Support:
- Document: https://smartboot.tech]
- Gitee: https://gitee.com/smartboot/smart-socket
- Github: https://github.com/smartboot/smart-socket