PageRenderTime 62ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/tomcat-7.0.2/java/org/apache/catalina/tribes/transport/ReceiverBase.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 588 lines | 384 code | 103 blank | 101 comment | 18 complexity | 24d7ad05fc497fcd2384896c5bbc433a MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.catalina.tribes.transport;
  18. import java.io.IOException;
  19. import java.net.DatagramSocket;
  20. import java.net.InetAddress;
  21. import java.net.InetSocketAddress;
  22. import java.net.ServerSocket;
  23. import java.util.concurrent.ExecutorService;
  24. import java.util.concurrent.ThreadFactory;
  25. import java.util.concurrent.TimeUnit;
  26. import java.util.concurrent.atomic.AtomicInteger;
  27. import org.apache.catalina.tribes.ChannelMessage;
  28. import org.apache.catalina.tribes.ChannelReceiver;
  29. import org.apache.catalina.tribes.MessageListener;
  30. import org.apache.catalina.tribes.io.ListenCallback;
  31. import org.apache.catalina.tribes.util.ExecutorFactory;
  32. import org.apache.juli.logging.Log;
  33. import org.apache.juli.logging.LogFactory;
  34. /**
  35. * <p>Title: </p>
  36. *
  37. * <p>Description: </p>
  38. *
  39. * <p>Company: </p>
  40. *
  41. * @author not attributable
  42. * @version 1.0
  43. */
  44. public abstract class ReceiverBase implements ChannelReceiver, ListenCallback, RxTaskPool.TaskCreator {
  45. public static final int OPTION_DIRECT_BUFFER = 0x0004;
  46. private static final Log log = LogFactory.getLog(ReceiverBase.class);
  47. private MessageListener listener;
  48. private String host = "auto";
  49. private InetAddress bind;
  50. private int port = 4000;
  51. private int udpPort = -1;
  52. private int securePort = -1;
  53. private int rxBufSize = 43800;
  54. private int txBufSize = 25188;
  55. private int udpRxBufSize = 43800;
  56. private int udpTxBufSize = 25188;
  57. private boolean listen = false;
  58. private RxTaskPool pool;
  59. private boolean direct = true;
  60. private long tcpSelectorTimeout = 5000;
  61. //how many times to search for an available socket
  62. private int autoBind = 100;
  63. private int maxThreads = 15;
  64. private int minThreads = 6;
  65. private int maxTasks = 100;
  66. private int minTasks = 10;
  67. private boolean tcpNoDelay = true;
  68. private boolean soKeepAlive = false;
  69. private boolean ooBInline = true;
  70. private boolean soReuseAddress = true;
  71. private boolean soLingerOn = true;
  72. private int soLingerTime = 3;
  73. private int soTrafficClass = 0x04 | 0x08 | 0x010;
  74. private int timeout = 3000; //3 seconds
  75. private boolean useBufferPool = true;
  76. private boolean daemon = true;
  77. private long maxIdleTime = 60000;
  78. private ExecutorService executor;
  79. public ReceiverBase() {
  80. }
  81. public void start() throws IOException {
  82. if ( executor == null ) {
  83. //executor = new ThreadPoolExecutor(minThreads,maxThreads,60,TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>());
  84. TaskThreadFactory tf = new TaskThreadFactory("Tribes-Task-Receiver-");
  85. executor = ExecutorFactory.newThreadPool(minThreads, maxThreads, maxIdleTime, TimeUnit.MILLISECONDS, tf);
  86. }
  87. }
  88. public void stop() {
  89. if ( executor != null ) executor.shutdownNow();//ignore left overs
  90. executor = null;
  91. }
  92. /**
  93. * getMessageListener
  94. *
  95. * @return MessageListener
  96. * TODO Implement this org.apache.catalina.tribes.ChannelReceiver method
  97. */
  98. public MessageListener getMessageListener() {
  99. return listener;
  100. }
  101. /**
  102. *
  103. * @return The port
  104. * TODO Implement this org.apache.catalina.tribes.ChannelReceiver method
  105. */
  106. public int getPort() {
  107. return port;
  108. }
  109. public int getRxBufSize() {
  110. return rxBufSize;
  111. }
  112. public int getTxBufSize() {
  113. return txBufSize;
  114. }
  115. /**
  116. * @deprecated use getMinThreads()/getMaxThreads()
  117. * @return int
  118. */
  119. @Deprecated
  120. public int getTcpThreadCount() {
  121. return getMaxThreads();
  122. }
  123. /**
  124. * setMessageListener
  125. *
  126. * @param listener MessageListener
  127. * TODO Implement this org.apache.catalina.tribes.ChannelReceiver method
  128. */
  129. public void setMessageListener(MessageListener listener) {
  130. this.listener = listener;
  131. }
  132. /**
  133. * @deprecated use setPort
  134. * @param tcpListenPort int
  135. */
  136. @Deprecated
  137. public void setTcpListenPort(int tcpListenPort) {
  138. setPort(tcpListenPort);
  139. }
  140. /**
  141. * @deprecated use setAddress
  142. * @param tcpListenHost String
  143. */
  144. @Deprecated
  145. public void setTcpListenAddress(String tcpListenHost) {
  146. setAddress(tcpListenHost);
  147. }
  148. public void setRxBufSize(int rxBufSize) {
  149. this.rxBufSize = rxBufSize;
  150. }
  151. public void setTxBufSize(int txBufSize) {
  152. this.txBufSize = txBufSize;
  153. }
  154. /**
  155. * @deprecated use setMaxThreads/setMinThreads
  156. * @param tcpThreadCount int
  157. */
  158. @Deprecated
  159. public void setTcpThreadCount(int tcpThreadCount) {
  160. setMaxThreads(tcpThreadCount);
  161. setMinThreads(tcpThreadCount);
  162. }
  163. /**
  164. * @return Returns the bind.
  165. */
  166. public InetAddress getBind() {
  167. if (bind == null) {
  168. try {
  169. if ("auto".equals(host)) {
  170. host = java.net.InetAddress.getLocalHost().getHostAddress();
  171. }
  172. if (log.isDebugEnabled())
  173. log.debug("Starting replication listener on address:"+ host);
  174. bind = java.net.InetAddress.getByName(host);
  175. } catch (IOException ioe) {
  176. log.error("Failed bind replication listener on address:"+ host, ioe);
  177. }
  178. }
  179. return bind;
  180. }
  181. /**
  182. * recursive bind to find the next available port
  183. * @param socket ServerSocket
  184. * @param portstart int
  185. * @param retries int
  186. * @return int
  187. * @throws IOException
  188. */
  189. protected int bind(ServerSocket socket, int portstart, int retries) throws IOException {
  190. InetSocketAddress addr = null;
  191. while ( retries > 0 ) {
  192. try {
  193. addr = new InetSocketAddress(getBind(), portstart);
  194. socket.bind(addr);
  195. setPort(portstart);
  196. log.info("Receiver Server Socket bound to:"+addr);
  197. return 0;
  198. }catch ( IOException x) {
  199. retries--;
  200. if ( retries <= 0 ) {
  201. log.info("Unable to bind server socket to:"+addr+" throwing error.");
  202. throw x;
  203. }
  204. portstart++;
  205. try {Thread.sleep(25);}catch( InterruptedException ti){Thread.interrupted();}
  206. retries = bind(socket,portstart,retries);
  207. }
  208. }
  209. return retries;
  210. }
  211. /**
  212. * Same as bind() except it does it for the UDP port
  213. * @param socket
  214. * @param portstart
  215. * @param retries
  216. * @return int
  217. * @throws IOException
  218. */
  219. protected int bindUdp(DatagramSocket socket, int portstart, int retries) throws IOException {
  220. InetSocketAddress addr = null;
  221. while ( retries > 0 ) {
  222. try {
  223. addr = new InetSocketAddress(getBind(), portstart);
  224. socket.bind(addr);
  225. setUdpPort(portstart);
  226. log.info("UDP Receiver Server Socket bound to:"+addr);
  227. return 0;
  228. }catch ( IOException x) {
  229. retries--;
  230. if ( retries <= 0 ) {
  231. log.info("Unable to bind UDP socket to:"+addr+" throwing error.");
  232. throw x;
  233. }
  234. portstart++;
  235. try {Thread.sleep(25);}catch( InterruptedException ti){Thread.interrupted();}
  236. retries = bindUdp(socket,portstart,retries);
  237. }
  238. }
  239. return retries;
  240. }
  241. public void messageDataReceived(ChannelMessage data) {
  242. if ( this.listener != null ) {
  243. if ( listener.accept(data) ) listener.messageReceived(data);
  244. }
  245. }
  246. public int getWorkerThreadOptions() {
  247. int options = 0;
  248. if ( getDirect() ) options = options | OPTION_DIRECT_BUFFER;
  249. return options;
  250. }
  251. /**
  252. * @param bind The bind to set.
  253. */
  254. public void setBind(java.net.InetAddress bind) {
  255. this.bind = bind;
  256. }
  257. /**
  258. * @deprecated use getPort
  259. * @return int
  260. */
  261. @Deprecated
  262. public int getTcpListenPort() {
  263. return getPort();
  264. }
  265. public boolean getDirect() {
  266. return direct;
  267. }
  268. public void setDirect(boolean direct) {
  269. this.direct = direct;
  270. }
  271. public String getAddress() {
  272. getBind();
  273. return this.host;
  274. }
  275. public String getHost() {
  276. return getAddress();
  277. }
  278. public long getSelectorTimeout() {
  279. return tcpSelectorTimeout;
  280. }
  281. /**
  282. * @deprecated use getSelectorTimeout
  283. * @return long
  284. */
  285. @Deprecated
  286. public long getTcpSelectorTimeout() {
  287. return getSelectorTimeout();
  288. }
  289. public boolean doListen() {
  290. return listen;
  291. }
  292. public MessageListener getListener() {
  293. return listener;
  294. }
  295. public RxTaskPool getTaskPool() {
  296. return pool;
  297. }
  298. /**
  299. * @deprecated use getAddress
  300. * @return String
  301. */
  302. @Deprecated
  303. public String getTcpListenAddress() {
  304. return getAddress();
  305. }
  306. public int getAutoBind() {
  307. return autoBind;
  308. }
  309. public int getMaxThreads() {
  310. return maxThreads;
  311. }
  312. public int getMinThreads() {
  313. return minThreads;
  314. }
  315. public boolean getTcpNoDelay() {
  316. return tcpNoDelay;
  317. }
  318. public boolean getSoKeepAlive() {
  319. return soKeepAlive;
  320. }
  321. public boolean getOoBInline() {
  322. return ooBInline;
  323. }
  324. public boolean getSoLingerOn() {
  325. return soLingerOn;
  326. }
  327. public int getSoLingerTime() {
  328. return soLingerTime;
  329. }
  330. public boolean getSoReuseAddress() {
  331. return soReuseAddress;
  332. }
  333. public int getSoTrafficClass() {
  334. return soTrafficClass;
  335. }
  336. public int getTimeout() {
  337. return timeout;
  338. }
  339. public boolean getUseBufferPool() {
  340. return useBufferPool;
  341. }
  342. public int getSecurePort() {
  343. return securePort;
  344. }
  345. public int getMinTasks() {
  346. return minTasks;
  347. }
  348. public int getMaxTasks() {
  349. return maxTasks;
  350. }
  351. public ExecutorService getExecutor() {
  352. return executor;
  353. }
  354. public boolean isListening() {
  355. return listen;
  356. }
  357. /**
  358. * @deprecated use setSelectorTimeout
  359. * @param selTimeout long
  360. */
  361. @Deprecated
  362. public void setTcpSelectorTimeout(long selTimeout) {
  363. setSelectorTimeout(selTimeout);
  364. }
  365. public void setSelectorTimeout(long selTimeout) {
  366. tcpSelectorTimeout = selTimeout;
  367. }
  368. public void setListen(boolean doListen) {
  369. this.listen = doListen;
  370. }
  371. public void setAddress(String host) {
  372. this.host = host;
  373. }
  374. public void setHost(String host) {
  375. setAddress(host);
  376. }
  377. public void setListener(MessageListener listener) {
  378. this.listener = listener;
  379. }
  380. public void setPool(RxTaskPool pool) {
  381. this.pool = pool;
  382. }
  383. public void setPort(int port) {
  384. this.port = port;
  385. }
  386. public void setAutoBind(int autoBind) {
  387. this.autoBind = autoBind;
  388. if ( this.autoBind <= 0 ) this.autoBind = 1;
  389. }
  390. public void setMaxThreads(int maxThreads) {
  391. this.maxThreads = maxThreads;
  392. }
  393. public void setMinThreads(int minThreads) {
  394. this.minThreads = minThreads;
  395. }
  396. public void setTcpNoDelay(boolean tcpNoDelay) {
  397. this.tcpNoDelay = tcpNoDelay;
  398. }
  399. public void setSoKeepAlive(boolean soKeepAlive) {
  400. this.soKeepAlive = soKeepAlive;
  401. }
  402. public void setOoBInline(boolean ooBInline) {
  403. this.ooBInline = ooBInline;
  404. }
  405. public void setSoLingerOn(boolean soLingerOn) {
  406. this.soLingerOn = soLingerOn;
  407. }
  408. public void setSoLingerTime(int soLingerTime) {
  409. this.soLingerTime = soLingerTime;
  410. }
  411. public void setSoReuseAddress(boolean soReuseAddress) {
  412. this.soReuseAddress = soReuseAddress;
  413. }
  414. public void setSoTrafficClass(int soTrafficClass) {
  415. this.soTrafficClass = soTrafficClass;
  416. }
  417. public void setTimeout(int timeout) {
  418. this.timeout = timeout;
  419. }
  420. public void setUseBufferPool(boolean useBufferPool) {
  421. this.useBufferPool = useBufferPool;
  422. }
  423. public void setSecurePort(int securePort) {
  424. this.securePort = securePort;
  425. }
  426. public void setMinTasks(int minTasks) {
  427. this.minTasks = minTasks;
  428. }
  429. public void setMaxTasks(int maxTasks) {
  430. this.maxTasks = maxTasks;
  431. }
  432. public void setExecutor(ExecutorService executor) {
  433. this.executor = executor;
  434. }
  435. public void heartbeat() {
  436. //empty operation
  437. }
  438. public int getUdpPort() {
  439. return udpPort;
  440. }
  441. public void setUdpPort(int udpPort) {
  442. this.udpPort = udpPort;
  443. }
  444. public int getUdpRxBufSize() {
  445. return udpRxBufSize;
  446. }
  447. public void setUdpRxBufSize(int udpRxBufSize) {
  448. this.udpRxBufSize = udpRxBufSize;
  449. }
  450. public int getUdpTxBufSize() {
  451. return udpTxBufSize;
  452. }
  453. public void setUdpTxBufSize(int udpTxBufSize) {
  454. this.udpTxBufSize = udpTxBufSize;
  455. }
  456. // ---------------------------------------------- ThreadFactory Inner Class
  457. class TaskThreadFactory implements ThreadFactory {
  458. final ThreadGroup group;
  459. final AtomicInteger threadNumber = new AtomicInteger(1);
  460. final String namePrefix;
  461. TaskThreadFactory(String namePrefix) {
  462. SecurityManager s = System.getSecurityManager();
  463. group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
  464. this.namePrefix = namePrefix;
  465. }
  466. public Thread newThread(Runnable r) {
  467. Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement());
  468. t.setDaemon(daemon);
  469. t.setPriority(Thread.NORM_PRIORITY);
  470. return t;
  471. }
  472. }
  473. public boolean isDaemon() {
  474. return daemon;
  475. }
  476. public long getMaxIdleTime() {
  477. return maxIdleTime;
  478. }
  479. public void setDaemon(boolean daemon) {
  480. this.daemon = daemon;
  481. }
  482. public void setMaxIdleTime(long maxIdleTime) {
  483. this.maxIdleTime = maxIdleTime;
  484. }
  485. }