PageRenderTime 66ms CodeModel.GetById 20ms app.highlight 41ms RepoModel.GetById 1ms app.codeStats 0ms

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