/hazelcast/src/main/java/com/hazelcast/nio/NodeIOService.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 251 lines · 185 code · 48 blank · 18 comment · 16 complexity · a1c4c056219bcafee7aec60e4da7ee09 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  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.hazelcast.nio;
  17. import com.hazelcast.cluster.AddOrRemoveConnection;
  18. import com.hazelcast.config.*;
  19. import com.hazelcast.impl.Node;
  20. import com.hazelcast.impl.OutOfMemoryErrorDispatcher;
  21. import com.hazelcast.impl.Processable;
  22. import com.hazelcast.impl.ThreadContext;
  23. import com.hazelcast.impl.ascii.TextCommandService;
  24. import com.hazelcast.impl.base.SystemLogService;
  25. import com.hazelcast.logging.ILogger;
  26. import java.util.Collection;
  27. import java.util.Collections;
  28. import java.util.HashSet;
  29. import java.util.Set;
  30. public class NodeIOService implements IOService {
  31. final Node node;
  32. public NodeIOService(Node node) {
  33. this.node = node;
  34. }
  35. public boolean isActive() {
  36. return node.isActive();
  37. }
  38. public ILogger getLogger(String name) {
  39. return node.getLogger(name);
  40. }
  41. public SystemLogService getSystemLogService() {
  42. return node.getSystemLogService();
  43. }
  44. public void onOutOfMemory(OutOfMemoryError oom) {
  45. // node.onOutOfMemory(oom);
  46. OutOfMemoryErrorDispatcher.onOutOfMemory(oom);
  47. }
  48. public void handleInterruptedException(Thread thread, RuntimeException e) {
  49. node.handleInterruptedException(thread, e);
  50. }
  51. public void onIOThreadStart() {
  52. ThreadContext.get().setCurrentFactory(node.factory);
  53. }
  54. public Address getThisAddress() {
  55. return node.getThisAddress();
  56. }
  57. public void onFatalError(Exception e) {
  58. getSystemLogService().logConnection(e.getClass().getName() + ": " + e.getMessage());
  59. node.shutdown(false, false);
  60. }
  61. public SocketInterceptorConfig getSocketInterceptorConfig() {
  62. return node.getConfig().getNetworkConfig().getSocketInterceptorConfig();
  63. }
  64. public SymmetricEncryptionConfig getSymmetricEncryptionConfig() {
  65. return node.getConfig().getNetworkConfig().getSymmetricEncryptionConfig();
  66. }
  67. public AsymmetricEncryptionConfig getAsymmetricEncryptionConfig() {
  68. return node.getConfig().getNetworkConfig().getAsymmetricEncryptionConfig();
  69. }
  70. public SSLConfig getSSLConfig() {
  71. return node.getConfig().getNetworkConfig().getSSLConfig();
  72. }
  73. public void handleClientPacket(Packet p) {
  74. node.clientHandlerService.handle(p);
  75. }
  76. public void handleMemberPacket(Packet p) {
  77. node.clusterService.enqueuePacket(p);
  78. }
  79. public TextCommandService getTextCommandService() {
  80. return node.getTextCommandService();
  81. }
  82. public boolean isMemcacheEnabled() {
  83. return node.groupProperties.MEMCACHE_ENABLED.getBoolean();
  84. }
  85. public boolean isRestEnabled() {
  86. return node.groupProperties.REST_ENABLED.getBoolean();
  87. }
  88. public void removeEndpoint(Address endPoint) {
  89. AddOrRemoveConnection addOrRemoveConnection = new AddOrRemoveConnection(endPoint, false);
  90. addOrRemoveConnection.setNode(node);
  91. node.clusterManager.enqueueAndReturn(addOrRemoveConnection);
  92. }
  93. public String getThreadPrefix() {
  94. return node.getThreadPoolNamePrefix("IO");
  95. }
  96. public ThreadGroup getThreadGroup() {
  97. return node.threadGroup;
  98. }
  99. public void onFailedConnection(Address address) {
  100. if (!node.joined()) {
  101. node.failedConnection(address);
  102. }
  103. }
  104. public void shouldConnectTo(Address address) {
  105. if (node.getThisAddress().equals(address)) {
  106. throw new RuntimeException("Connecting to self! " + address);
  107. }
  108. }
  109. public boolean isReuseSocketAddress() {
  110. return node.getConfig().getNetworkConfig().isReuseAddress();
  111. }
  112. public int getSocketPort() {
  113. return node.getConfig().getNetworkConfig().getPort();
  114. }
  115. public boolean isSocketBindAny() {
  116. return node.groupProperties.SOCKET_CLIENT_BIND_ANY.getBoolean();
  117. }
  118. public boolean isSocketPortAutoIncrement() {
  119. return node.getConfig().getNetworkConfig().isPortAutoIncrement();
  120. }
  121. public int getSocketReceiveBufferSize() {
  122. return this.node.getGroupProperties().SOCKET_RECEIVE_BUFFER_SIZE.getInteger();
  123. }
  124. public int getSocketSendBufferSize() {
  125. return this.node.getGroupProperties().SOCKET_SEND_BUFFER_SIZE.getInteger();
  126. }
  127. public int getSocketLingerSeconds() {
  128. return this.node.getGroupProperties().SOCKET_LINGER_SECONDS.getInteger();
  129. }
  130. public boolean getSocketKeepAlive() {
  131. return this.node.getGroupProperties().SOCKET_KEEP_ALIVE.getBoolean();
  132. }
  133. public boolean getSocketNoDelay() {
  134. return this.node.getGroupProperties().SOCKET_NO_DELAY.getBoolean();
  135. }
  136. public int getSelectorThreadCount() {
  137. return node.groupProperties.IO_THREAD_COUNT.getInteger();
  138. }
  139. public void disconnectExistingCalls(final Address deadEndpoint) {
  140. if (deadEndpoint != null) {
  141. node.clusterManager.enqueueAndReturn(new Processable() {
  142. public void process() {
  143. node.clusterManager.disconnectExistingCalls(deadEndpoint);
  144. }
  145. });
  146. }
  147. }
  148. public boolean isClient() {
  149. return false;
  150. }
  151. public long getConnectionMonitorInterval() {
  152. return node.groupProperties.CONNECTION_MONITOR_INTERVAL.getLong();
  153. }
  154. public int getConnectionMonitorMaxFaults() {
  155. return node.groupProperties.CONNECTION_MONITOR_MAX_FAULTS.getInteger();
  156. }
  157. public void onShutdown() {
  158. try {
  159. ThreadContext.get().setCurrentFactory(node.factory);
  160. node.clusterManager.sendProcessableToAll(new AddOrRemoveConnection(getThisAddress(), false), false);
  161. // wait a little
  162. Thread.sleep(100);
  163. } catch (Throwable ignored) {
  164. }
  165. }
  166. public void executeAsync(final Runnable runnable) {
  167. node.executorManager.executeNow(runnable);
  168. }
  169. public Collection<Integer> getOutboundPorts() {
  170. final NetworkConfig networkConfig = node.getConfig().getNetworkConfig();
  171. final Collection<String> portDefinitions = networkConfig.getOutboundPortDefinitions() == null
  172. ? Collections.<String>emptySet() : networkConfig.getOutboundPortDefinitions();
  173. final Set<Integer> ports = networkConfig.getOutboundPorts() == null
  174. ? new HashSet<Integer>() : new HashSet<Integer>(networkConfig.getOutboundPorts());
  175. if (portDefinitions.isEmpty() && ports.isEmpty()) {
  176. return Collections.emptySet(); // means any port
  177. }
  178. if (portDefinitions.contains("*") || portDefinitions.contains("0")) {
  179. return Collections.emptySet(); // means any port
  180. }
  181. // not checking port ranges...
  182. for (String portDef : portDefinitions) {
  183. String[] portDefs = portDef.split("[,; ]");
  184. for (String def : portDefs) {
  185. def = def.trim();
  186. final int dashPos = def.indexOf('-');
  187. if (dashPos > 0) {
  188. final int start = Integer.parseInt(def.substring(0, dashPos));
  189. final int end = Integer.parseInt(def.substring(dashPos + 1));
  190. for (int port = start; port <= end; port++) {
  191. ports.add(port);
  192. }
  193. } else {
  194. ports.add(Integer.parseInt(def));
  195. }
  196. }
  197. }
  198. if (ports.contains(0)) {
  199. return Collections.emptySet(); // means any port
  200. }
  201. return ports;
  202. }
  203. }