001:///////////////////////////////////////////////////////////////////////////////
002://
003:// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004://
005:// All Rights Reserved
006://
007:// This program is free software; you can redistribute it and/or modify
008:// it under the terms of the GNU General Public License and GNU Library
009:// General Public License as published by the Free Software Foundation;
010:// either version 2, or (at your option) any later version.
011://
012:// This program is distributed in the hope that it will be useful,
013:// but WITHOUT ANY WARRANTY; without even the implied warranty of
014:// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015:// GNU General Public License and GNU Library General Public License
016:// for more details.
017://
018:// You should have received a copy of the GNU General Public License
019:// and GNU Library General Public License along with this program; if
020:// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021:// MA 02139, USA.
022://
023:///////////////////////////////////////////////////////////////////////////////
024:package org.myoodb.core;
025:
026:public class TunnelManager extends AbstractManager implements Runnable
027:{
028: protected interface Protocol
029: {
030: final static short KEEP_ALIVE = 0;
031: final static short VERIFY_DATA = 1;
032: final static short USER_DATA = 2;
033: final static short USER_DATA_TUNNEL = 3;
034: }
035:
036: private static final org.myoodb.util.Logger LOGGER = org.myoodb.util.Logger.getLogger(TunnelManager.class);
037:
038: private static int s_tunnelTimeout = @tunnelTimeoutMs@;
039:
040: protected static final java.util.HashMap CONNECTIONS = new java.util.HashMap();
041: protected static final java.util.HashMap INVOKE_COMMANDS = new java.util.HashMap();
042:
043: private Thread m_cleanUpThread;
044: private org.myoodb.MyOodbDatabase m_db;
045:
046: public static void setTunnelTimeout(int timeout)
047: {
048: s_tunnelTimeout = timeout;
049: }
050:
051: public static int getTunnelTimeout()
052: {
053: return s_tunnelTimeout;
054: }
055:
056: public TunnelManager(org.myoodb.MyOodbDatabase db)
057: {
058: m_db = db;
059: }
060:
061: private void closeDatabaseConnection(Object[] connectionTuple)
062: {
063: org.myoodb.MyOodbDatabase.close((org.myoodb.core.DatabaseConnection) connectionTuple[0]);
064: }
065:
066: private void closeDatagramConnection(Object[] connectionTuple)
067: {
068: java.net.SocketAddress address = (java.net.SocketAddress) connectionTuple[1];
069:
070: boolean found = false;
071: java.util.ArrayList connnections = new java.util.ArrayList(CONNECTIONS.values());
072: java.util.Iterator iter = connnections.iterator();
073: while (iter.hasNext())
074: {
075: Object[] tuple = (Object[]) iter.next();
076: if (address.equals(tuple[1]) == true)
077: {
078: found = true;
079: break;
080: }
081: }
082:
083: if (found == false)
084: {
085: connnections = new java.util.ArrayList(AbstractDatagramSocket.CONNECTIONS.values());
086: iter = connnections.iterator();
087: while (iter.hasNext())
088: {
089: AbstractDatagramSocket socket = (AbstractDatagramSocket) iter.next();
090: if (address.equals(socket.getRemoteSocketAddress()) == true)
091: {
092: if (LOGGER.isDebugEnabled() == true)
093: {
094: LOGGER.debug("Found gaming connection to close: " + address + ", " + connectionTuple[3]);
095: }
096:
097: try
098: {
099: socket.close();
100: }
101: catch (java.io.IOException e)
102: {
103: // nothing to do
104: }
105: }
106: }
107: }
108: }
109:
110: public void startup() throws Exception
111: {
112: m_cleanUpThread = new Thread(this );
113: m_cleanUpThread.setDaemon(true);
114: m_cleanUpThread.setName("MyOodb Tunnel CleanUp Thread: " + m_cleanUpThread.getId());
115: m_cleanUpThread.start();
116: }
117:
118: public void shutdown() throws Exception
119: {
120: m_cleanUpThread = null;
121: }
122:
123: public org.myoodb.MyOodbDatabase getDatabase()
124: {
125: return m_db;
126: }
127:
128: public void run()
129: {
130: while (m_cleanUpThread != null)
131: {
132: try
133: {
134: Thread.sleep(getTunnelTimeout());
135: }
136: catch (InterruptedException e)
137: {
138: // nothing to do
139: }
140:
141: synchronized(CONNECTIONS)
142: {
143: java.util.Iterator iter = CONNECTIONS.values().iterator();
144: while (iter.hasNext())
145: {
146: Object[] tuple = (Object[]) iter.next();
147: long lastRequestTime = (Long) tuple[2];
148:
149: if ((System.currentTimeMillis() - lastRequestTime) > getTunnelTimeout())
150: {
151: iter.remove();
152:
153: if (LOGGER.isDebugEnabled() == true)
154: {
155: LOGGER.debug("Closing stale tunnel connection: " + tuple[1]);
156: }
157:
158: closeDatabaseConnection(tuple);
159:
160: if (org.myoodb.MyOodbDatabase.getGameConfigurationFlag() == true)
161: {
162: closeDatagramConnection(tuple);
163:
164: synchronized(tuple)
165: {
166: tuple[4] = (long) -1;
167: tuple.notify();
168: }
169: }
170:
171: if (LOGGER.isDebugEnabled() == true)
172: {
173: LOGGER.debug("Closed stale tunnel connection: " + tuple[1]);
174: }
175: }
176: }
177: }
178: }
179: }
180:}
|