123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- /*
- * Copyright © 2018 Zhenjie Yan.
- *
- * 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 com.yanzhenjie.andserver;
- import java.net.InetAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.concurrent.TimeUnit;
- import javax.net.ServerSocketFactory;
- import javax.net.ssl.SSLContext;
- /**
- * Created by Zhenjie Yan on 2018/9/10.
- */
- public interface Server {
- /**
- * Server running status.
- *
- * @return return true, not return false.
- */
- boolean isRunning();
- /**
- * Start the server.
- */
- void startup();
- /**
- * Quit the server.
- */
- void shutdown();
- /**
- * Get the local address of this server socket.
- *
- * @return {@link InetAddress}.
- *
- * @throws IllegalStateException if the server is not started, an IllegalStateException is thrown.
- * @see ServerSocket#getInetAddress()
- */
- InetAddress getInetAddress();
- /**
- * Returns the port number on which this socket is listening.
- *
- * @return the local port number to which this socket is bound or -1 if the socket is not bound yet.
- *
- * @throws IllegalStateException if the server is not started, an IllegalStateException is thrown.
- * @see Socket#getLocalPort()
- */
- int getPort();
- interface Builder<T extends Builder, S extends Server> {
- /**
- * Specified server need to monitor the ip address.
- */
- T inetAddress(InetAddress inetAddress);
- /**
- * Specify the port on which the server listens.
- */
- T port(int port);
- /**
- * Connection and response timeout.
- */
- T timeout(int timeout, TimeUnit timeUnit);
- /**
- * Assigns {@link ServerSocketFactory} instance.
- */
- T serverSocketFactory(ServerSocketFactory factory);
- /**
- * Assigns {@link SSLContext} instance.
- */
- T sslContext(SSLContext sslContext);
- /**
- * Assigns {@link SSLSocketInitializer} instance.
- */
- T sslSocketInitializer(SSLSocketInitializer initializer);
- /**
- * Set the server listener.
- */
- T listener(Server.ServerListener listener);
- /**
- * Create a server.
- */
- S build();
- }
- interface ProxyBuilder<T extends ProxyBuilder, S extends Server> {
- /**
- * Add host address to proxy.
- *
- * @param hostName such as: {@code www.example.com}, {@code api.example.com}, {@code 192.168.1.111}.
- * @param proxyHost such as: {@code http://127.0.0.1:8080}, {@code http://localhost:8181}
- */
- T addProxy(String hostName, String proxyHost);
- /**
- * Specified server need to monitor the ip address.
- */
- T inetAddress(InetAddress inetAddress);
- /**
- * Specify the port on which the server listens.
- */
- T port(int port);
- /**
- * Connection and response timeout.
- */
- T timeout(int timeout, TimeUnit timeUnit);
- /**
- * Assigns {@link ServerSocketFactory} instance.
- */
- T serverSocketFactory(ServerSocketFactory factory);
- /**
- * Assigns {@link SSLContext} instance.
- */
- T sslContext(SSLContext sslContext);
- /**
- * Assigns {@link SSLSocketInitializer} instance.
- */
- T sslSocketInitializer(SSLSocketInitializer initializer);
- /**
- * Set the server listener.
- */
- T listener(Server.ServerListener listener);
- /**
- * Create a server.
- */
- S build();
- }
- interface ServerListener {
- /**
- * When the server is started.
- */
- void onStarted();
- /**
- * When the server stops running.
- */
- void onStopped();
- /**
- * An error occurred while starting the server.
- */
- void onException(Exception e);
- }
- }
|