Server.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright © 2018 Zhenjie Yan.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.yanzhenjie.andserver;
  17. import java.net.InetAddress;
  18. import java.net.ServerSocket;
  19. import java.net.Socket;
  20. import java.util.concurrent.TimeUnit;
  21. import javax.net.ServerSocketFactory;
  22. import javax.net.ssl.SSLContext;
  23. /**
  24. * Created by Zhenjie Yan on 2018/9/10.
  25. */
  26. public interface Server {
  27. /**
  28. * Server running status.
  29. *
  30. * @return return true, not return false.
  31. */
  32. boolean isRunning();
  33. /**
  34. * Start the server.
  35. */
  36. void startup();
  37. /**
  38. * Quit the server.
  39. */
  40. void shutdown();
  41. /**
  42. * Get the local address of this server socket.
  43. *
  44. * @return {@link InetAddress}.
  45. *
  46. * @throws IllegalStateException if the server is not started, an IllegalStateException is thrown.
  47. * @see ServerSocket#getInetAddress()
  48. */
  49. InetAddress getInetAddress();
  50. /**
  51. * Returns the port number on which this socket is listening.
  52. *
  53. * @return the local port number to which this socket is bound or -1 if the socket is not bound yet.
  54. *
  55. * @throws IllegalStateException if the server is not started, an IllegalStateException is thrown.
  56. * @see Socket#getLocalPort()
  57. */
  58. int getPort();
  59. interface Builder<T extends Builder, S extends Server> {
  60. /**
  61. * Specified server need to monitor the ip address.
  62. */
  63. T inetAddress(InetAddress inetAddress);
  64. /**
  65. * Specify the port on which the server listens.
  66. */
  67. T port(int port);
  68. /**
  69. * Connection and response timeout.
  70. */
  71. T timeout(int timeout, TimeUnit timeUnit);
  72. /**
  73. * Assigns {@link ServerSocketFactory} instance.
  74. */
  75. T serverSocketFactory(ServerSocketFactory factory);
  76. /**
  77. * Assigns {@link SSLContext} instance.
  78. */
  79. T sslContext(SSLContext sslContext);
  80. /**
  81. * Assigns {@link SSLSocketInitializer} instance.
  82. */
  83. T sslSocketInitializer(SSLSocketInitializer initializer);
  84. /**
  85. * Set the server listener.
  86. */
  87. T listener(Server.ServerListener listener);
  88. /**
  89. * Create a server.
  90. */
  91. S build();
  92. }
  93. interface ProxyBuilder<T extends ProxyBuilder, S extends Server> {
  94. /**
  95. * Add host address to proxy.
  96. *
  97. * @param hostName such as: {@code www.example.com}, {@code api.example.com}, {@code 192.168.1.111}.
  98. * @param proxyHost such as: {@code http://127.0.0.1:8080}, {@code http://localhost:8181}
  99. */
  100. T addProxy(String hostName, String proxyHost);
  101. /**
  102. * Specified server need to monitor the ip address.
  103. */
  104. T inetAddress(InetAddress inetAddress);
  105. /**
  106. * Specify the port on which the server listens.
  107. */
  108. T port(int port);
  109. /**
  110. * Connection and response timeout.
  111. */
  112. T timeout(int timeout, TimeUnit timeUnit);
  113. /**
  114. * Assigns {@link ServerSocketFactory} instance.
  115. */
  116. T serverSocketFactory(ServerSocketFactory factory);
  117. /**
  118. * Assigns {@link SSLContext} instance.
  119. */
  120. T sslContext(SSLContext sslContext);
  121. /**
  122. * Assigns {@link SSLSocketInitializer} instance.
  123. */
  124. T sslSocketInitializer(SSLSocketInitializer initializer);
  125. /**
  126. * Set the server listener.
  127. */
  128. T listener(Server.ServerListener listener);
  129. /**
  130. * Create a server.
  131. */
  132. S build();
  133. }
  134. interface ServerListener {
  135. /**
  136. * When the server is started.
  137. */
  138. void onStarted();
  139. /**
  140. * When the server stops running.
  141. */
  142. void onStopped();
  143. /**
  144. * An error occurred while starting the server.
  145. */
  146. void onException(Exception e);
  147. }
  148. }