1 20 package org.apache.mina.transport.vmpipe.support; 21 22 import java.util.Iterator ; 23 import java.util.Set ; 24 25 import org.apache.mina.common.IdleStatus; 26 import org.apache.mina.util.IdentityHashSet; 27 28 34 public class VmPipeIdleStatusChecker { 35 private static final VmPipeIdleStatusChecker INSTANCE = new VmPipeIdleStatusChecker(); 36 37 public static VmPipeIdleStatusChecker getInstance() { 38 return INSTANCE; 39 } 40 41 private final Set <VmPipeSessionImpl> sessions = new IdentityHashSet<VmPipeSessionImpl>(); 42 43 private final Worker worker = new Worker(); 44 45 private VmPipeIdleStatusChecker() { 46 worker.start(); 47 } 48 49 public void addSession(VmPipeSessionImpl session) { 50 synchronized (sessions) { 51 sessions.add(session); 52 } 53 } 54 55 private class Worker extends Thread { 56 private Worker() { 57 super("VmPipeIdleStatusChecker"); 58 setDaemon(true); 59 } 60 61 public void run() { 62 for (;;) { 63 try { 64 Thread.sleep(1000); 65 } catch (InterruptedException e) { 66 } 67 68 long currentTime = System.currentTimeMillis(); 69 70 synchronized (sessions) { 71 Iterator <VmPipeSessionImpl> it = sessions.iterator(); 72 while (it.hasNext()) { 73 VmPipeSessionImpl session = it.next(); 74 if (!session.isConnected()) { 75 it.remove(); 76 } else { 77 notifyIdleSession(session, currentTime); 78 } 79 } 80 } 81 } 82 } 83 } 84 85 private void notifyIdleSession(VmPipeSessionImpl session, long currentTime) { 86 notifyIdleSession0(session, currentTime, session 87 .getIdleTimeInMillis(IdleStatus.BOTH_IDLE), 88 IdleStatus.BOTH_IDLE, Math.max(session.getLastIoTime(), session 89 .getLastIdleTime(IdleStatus.BOTH_IDLE))); 90 notifyIdleSession0(session, currentTime, session 91 .getIdleTimeInMillis(IdleStatus.READER_IDLE), 92 IdleStatus.READER_IDLE, Math.max(session.getLastReadTime(), 93 session.getLastIdleTime(IdleStatus.READER_IDLE))); 94 notifyIdleSession0(session, currentTime, session 95 .getIdleTimeInMillis(IdleStatus.WRITER_IDLE), 96 IdleStatus.WRITER_IDLE, Math.max(session.getLastWriteTime(), 97 session.getLastIdleTime(IdleStatus.WRITER_IDLE))); 98 } 99 100 private void notifyIdleSession0(VmPipeSessionImpl session, 101 long currentTime, long idleTime, IdleStatus status, long lastIoTime) { 102 if (idleTime > 0 && lastIoTime != 0 103 && (currentTime - lastIoTime) >= idleTime) { 104 session.increaseIdleCount(status); 105 session.getFilterChain().fireSessionIdle(session, status); 106 } 107 } 108 109 } | Popular Tags |