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.IdentityHashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  
26  import org.apache.mina.common.IdleStatus;
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 Map sessions = new IdentityHashMap(); // will use as a set
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.put(session, 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 it = sessions.keySet().iterator();
72                      while (it.hasNext()) {
73                          VmPipeSessionImpl session = (VmPipeSessionImpl) it
74                                  .next();
75                          if (!session.isConnected()) {
76                              it.remove();
77                          } else {
78                              notifyIdleSession(session, currentTime);
79                          }
80                      }
81                  }
82              }
83          }
84      }
85  
86      private void notifyIdleSession(VmPipeSessionImpl session, long currentTime) {
87          notifyIdleSession0(session, currentTime, session
88                  .getIdleTimeInMillis(IdleStatus.BOTH_IDLE),
89                  IdleStatus.BOTH_IDLE, Math.max(session.getLastIoTime(), session
90                          .getLastIdleTime(IdleStatus.BOTH_IDLE)));
91          notifyIdleSession0(session, currentTime, session
92                  .getIdleTimeInMillis(IdleStatus.READER_IDLE),
93                  IdleStatus.READER_IDLE, Math.max(session.getLastReadTime(),
94                          session.getLastIdleTime(IdleStatus.READER_IDLE)));
95          notifyIdleSession0(session, currentTime, session
96                  .getIdleTimeInMillis(IdleStatus.WRITER_IDLE),
97                  IdleStatus.WRITER_IDLE, Math.max(session.getLastWriteTime(),
98                          session.getLastIdleTime(IdleStatus.WRITER_IDLE)));
99      }
100 
101     private void notifyIdleSession0(VmPipeSessionImpl session,
102             long currentTime, long idleTime, IdleStatus status, long lastIoTime) {
103         if (idleTime > 0 && lastIoTime != 0
104                 && (currentTime - lastIoTime) >= idleTime) {
105             session.increaseIdleCount(status);
106             session.getFilterChain().fireSessionIdle(session, status);
107         }
108     }
109 
110 }