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