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: import java.io.*;
027: import java.net.*;
028: import java.util.*;
029: import javax.net.ssl.*;
030:
031: public abstract class AbstractServer extends HashSet {
032: private Thread m_acceptThread;
033: private ThreadGroup m_threadGroup;
034:
035: protected Acceptor m_acceptor;
036: protected ServerSocket m_serverSocket;
037:
038: public AbstractServer(int port, boolean secure) throws IOException {
039: if (secure == true) {
040: SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory
041: .getDefault();
042: m_serverSocket = (ServerSocket) sslserversocketfactory
043: .createServerSocket(port);
044: } else {
045: m_serverSocket = new ServerSocket(port);
046: }
047:
048: m_serverSocket.setReuseAddress(true);
049:
050: m_acceptor = new Acceptor(this );
051: m_threadGroup = new ThreadGroup(getClass().getName());
052: m_acceptThread = newThread(m_acceptor);
053: }
054:
055: public int getPort() {
056: return m_serverSocket.getLocalPort();
057: }
058:
059: public ThreadGroup getThreadGroup() {
060: return m_threadGroup;
061: }
062:
063: public Thread newThread(Runnable run) {
064: return new Thread(getThreadGroup(), run);
065: }
066:
067: public synchronized void accept() {
068: if (m_acceptThread.isAlive() == false) {
069: m_acceptThread.setName("MyOodb Tcp Accept Thread: "
070: + m_serverSocket);
071: m_acceptThread.start();
072: }
073: }
074:
075: public synchronized void close() throws IOException {
076: Iterator iter = iterator();
077: while (iter.hasNext()) {
078: try {
079: ((AbstractClient) iter.next()).close();
080: } catch (IOException e) {
081: // nothing to do
082: }
083: }
084:
085: m_serverSocket.close();
086: }
087:
088: public synchronized void removeClient(AbstractClient client) {
089: Iterator iter = iterator();
090: while (iter.hasNext()) {
091: AbstractClient tmpClient = (AbstractClient) iter.next();
092:
093: if (tmpClient == client) {
094: iter.remove();
095:
096: try {
097: tmpClient.close();
098: } catch (IOException e) {
099: // nothing to do
100: }
101:
102: return;
103: }
104: }
105:
106: throw new org.myoodb.exception.InternalException(
107: "no such client");
108: }
109:
110: public abstract AbstractClient newClient(Socket socket);
111:
112: public abstract Object processWork(AbstractClient client,
113: Object work);
114:
115: public abstract void handleException(AbstractClient client,
116: Exception e);
117:
118: class Acceptor implements Runnable {
119: protected AbstractServer m_server;
120:
121: protected Acceptor(AbstractServer server) {
122: m_server = server;
123: }
124:
125: public void run() {
126: try {
127: while (MyOodbManager.getTheManagerShuttingdownFlag() == false) {
128: AbstractClient client = m_server
129: .newClient(m_server.m_serverSocket.accept());
130: if (client != null) {
131: m_server.add(client);
132: client.listen();
133: }
134: }
135: } catch (Exception e) {
136: // nothing to do
137: }
138: }
139: }
140: }
|