View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
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  /**
29   * Dectects idle sessions and fires <tt>sessionIdle</tt> events to them. 
30   * 
31   * @author The Apache Directory Project (mina-dev@directory.apache.org)
32   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $
33   */
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 }