PageRenderTime 18ms CodeModel.GetById 6ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 98 lines | 68 code | 15 blank | 15 comment | 15 complexity | 6c41ce09532d66d08b4e51972861af76 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.impl.base.SystemLogService;
20import com.hazelcast.logging.ILogger;
21
22import java.io.IOException;
23import java.nio.channels.SelectionKey;
24import java.nio.channels.Selector;
25import java.util.logging.Level;
26
27abstract class AbstractSelectionHandler implements SelectionHandler {
28
29    protected final ILogger logger;
30
31    protected final SocketChannelWrapper socketChannel;
32
33    protected final Connection connection;
34
35    protected final InOutSelector inOutSelector;
36
37    protected final ConnectionManager connectionManager;
38
39    protected final SystemLogService systemLogService;
40
41    protected SelectionKey sk = null;
42
43    public AbstractSelectionHandler(final Connection connection, final InOutSelector inOutSelector) {
44        super();
45        this.connection = connection;
46        this.inOutSelector = inOutSelector;
47        this.socketChannel = connection.getSocketChannelWrapper();
48        this.connectionManager = connection.getConnectionManager();
49        this.logger = connectionManager.ioService.getLogger(this.getClass().getName());
50        this.systemLogService = connectionManager.ioService.getSystemLogService();
51    }
52
53    protected void shutdown() {
54    }
55
56    final void handleSocketException(Throwable e) {
57        if (e instanceof OutOfMemoryError) {
58            connectionManager.ioService.onOutOfMemory((OutOfMemoryError) e);
59        }
60        if (sk != null) {
61            sk.cancel();
62        }
63        connection.close(e);
64        if (connection.getType().isClient() && !connection.getType().isBinary()) {
65            return;
66        }
67        StringBuilder sb = new StringBuilder();
68        sb.append(Thread.currentThread().getName());
69        sb.append(" Closing socket to endpoint ");
70        sb.append(connection.getEndPoint());
71        sb.append(", Cause:").append(e);
72        if (e instanceof IOException) {
73            logger.log(Level.WARNING, sb.toString());
74        } else {
75            logger.log(Level.WARNING, sb.toString(), e);
76        }
77    }
78
79    final void registerOp(final Selector selector, final int operation) {
80        try {
81            if (!connection.live())
82                return;
83            if (sk == null) {
84                sk = socketChannel.keyFor(selector);
85            }
86            if (sk == null) {
87                sk = socketChannel.register(selector, operation, connection);
88            } else {
89                sk.interestOps(sk.interestOps() | operation);
90                if (sk.attachment() != connection) {
91                    sk.attach(connection);
92                }
93            }
94        } catch (Throwable e) {
95            handleSocketException(e);
96        }
97    }
98}