1 22 package org.xsocket.stream; 23 24 import java.util.concurrent.Executor ; 25 import java.util.logging.Level ; 26 import java.util.logging.Logger ; 27 28 import org.xsocket.ILifeCycle; 29 import org.xsocket.Synchronized; 30 import org.xsocket.Synchronized.Mode; 31 import org.xsocket.stream.io.spi.IIoHandlerContext; 32 33 34 35 39 final class IoHandlerContext implements IIoHandlerContext { 40 41 private static final Logger LOG = Logger.getLogger(IoHandlerContext.class.getName()); 42 43 private static final SingleThreadedWorkerPool SINGLE_THREADED_POOL = new SingleThreadedWorkerPool(); 44 45 private boolean isConnectHandler = false; 46 private boolean isDisconnectHandler = false; 47 private boolean isDataHandler = false; 48 private boolean isTimeoutHandler = false; 49 private boolean isLifeCycleHandler = false; 50 private boolean isConnectionScoped = false; 51 private boolean isHandlerThreadSave = false; 52 private boolean isMultithreaded = false; 53 54 private Executor workerpool = null; 55 56 57 62 IoHandlerContext(IHandler appHandler, Executor workerpool) { 63 updateAppHandler(appHandler); 64 updateWorkerpool(workerpool); 65 } 66 67 68 void updateAppHandler(IHandler appHandler) { 69 introspectHandler(appHandler); 70 } 71 72 73 void updateWorkerpool(Executor workerpool) { 74 if (workerpool != null) { 75 this.workerpool = workerpool; 76 isMultithreaded = true; 77 } else { 78 this.workerpool = SINGLE_THREADED_POOL; 79 isMultithreaded = false; 80 } 81 } 82 83 84 public Executor getWorkerpool() { 85 return workerpool; 86 } 87 88 public boolean isAppHandlerListenForConnectEvent() { 89 return isConnectHandler; 90 } 91 92 public boolean isAppHandlerListenForDataEvent() { 93 return isDataHandler; 94 } 95 96 public boolean isAppHandlerListenforDisconnectEvent() { 97 return isDisconnectHandler; 98 } 99 100 public boolean isAppHandlerListenForTimeoutEvent() { 101 return isTimeoutHandler; 102 } 103 104 public boolean isAppHandlerConnectionScoped() { 105 return isConnectionScoped; 106 } 107 108 public boolean isAppHandlerThreadSave() { 109 return isHandlerThreadSave; 110 } 111 112 boolean isConnectionScoped() { 113 return isConnectionScoped; 114 } 115 116 boolean isLifeCycleHandler() { 117 return isLifeCycleHandler; 118 } 119 120 public boolean isAppHandlerThreadSafe() { 121 return isHandlerThreadSave; 122 } 123 124 125 public boolean isMultithreaded() { 126 return isMultithreaded; 127 } 128 129 130 private void introspectHandler(IHandler appHandler) { 131 132 if (appHandler == null) { 133 isDataHandler = true; 134 isConnectHandler = false; 135 isDisconnectHandler = false; 136 isTimeoutHandler = false; 137 isConnectionScoped = false; 138 isHandlerThreadSave = false; 139 isLifeCycleHandler = false; 140 141 } else { 142 143 isConnectHandler = (appHandler instanceof IConnectHandler); 144 isDisconnectHandler = (appHandler instanceof IDisconnectHandler); 145 isDataHandler = (appHandler instanceof IDataHandler); 146 isTimeoutHandler = (appHandler instanceof ITimeoutHandler); 147 isConnectionScoped = (appHandler instanceof IConnectionScoped); 148 isLifeCycleHandler = (appHandler instanceof ILifeCycle); 149 150 Synchronized sync = appHandler.getClass().getAnnotation(Synchronized.class); 151 if (sync != null) { 152 Mode scope = sync.value(); 153 isHandlerThreadSave = (scope == Synchronized.Mode.OFF); 154 } else { 155 isHandlerThreadSave = false; 156 } 157 } 158 } 159 160 161 private static final class SingleThreadedWorkerPool implements Executor { 162 public void execute(Runnable command) { 163 try { 164 165 command.run(); 166 167 } catch (Exception e) { 168 if (LOG.isLoggable(Level.FINE)) { 169 LOG.fine("error occured within worker thread " + e.toString()); 170 } 171 } 172 173 } 174 } 175 } 176 | Popular Tags |