001: /*
002: * File: Server.java
003: * Project: jMOS, com.aranova.java.jmos
004: * Revision: 0.9 - Inicial
005: * Date: 29-sep-2005 10:59:11
006: *
007: * Copyright (C) Aragón Innovación Tecnológica S.L.L.
008: * All rights reserved.
009: *
010: * This software is distributed under the terms of the Aranova License version 1.0.
011: * See the terms of the Aranova License in the documentation provided with this software.
012: */
013:
014: package com.aranova.java.jmos;
015:
016: import java.io.IOException;
017: import java.net.InetAddress;
018: import java.net.ServerSocket;
019: import java.net.Socket;
020: import java.util.ArrayList;
021: import java.util.LinkedHashMap;
022: import java.util.Map;
023: import java.util.Observable;
024: import java.util.Observer;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: import com.aranova.java.jmos.enums.TypeMOS;
030: import com.aranova.java.jmos.enums.TypeShutdown;
031: import com.aranova.java.jmos.util.Config;
032:
033: /**
034: * Clase Servidor.
035: *
036: * @author <a href="http://www.aranova.net/contactar/">Daniel Sánchez</a>
037: * @version 0.9.1
038: * @since 0.9
039: */
040: public final class Server extends ServerSocket implements Runnable,
041: Observer {
042: private static final Log _log = LogFactory.getLog(Server.class);
043: private static final Map<IServerMOS, MessageFilter[]> _listeners = new LinkedHashMap<IServerMOS, MessageFilter[]>();
044: private static Server _mos;
045: private static Server _ncs;
046: private ArrayList<SocketThread> _clients;
047: private boolean _isShutDown;
048: private static boolean _isAlive;
049:
050: private Server(final TypeMOS type) throws IOException {
051: this (type, null);
052: }
053:
054: private Server(final TypeMOS type, final InetAddress bindAddr)
055: throws IOException {
056: super (Config.getConfig().getInt(type.name() + "Port"), 0,
057: bindAddr);
058: _clients = new ArrayList<SocketThread>();
059: _isShutDown = false;
060: }
061:
062: public void run() {
063: try {
064: while (true) {
065: final Socket clientSocket = accept();
066: final SocketThread st = new SocketThread(clientSocket,
067: _listeners);
068: final Thread thread = new Thread(st);
069: st.addObserver(this );
070: _clients.add(st);
071: thread.start();
072: }
073: } catch (Exception e) {
074: if (!_isShutDown) {
075: _log.warn("Error en el servidor", e);
076: }
077: }
078: }
079:
080: private void close(final TypeShutdown type) throws IOException {
081: _isShutDown = true;
082: this .close();
083:
084: if (type == TypeShutdown.Immediate) {
085: for (SocketThread st : _clients) {
086: st.close();
087: }
088: _clients.clear();
089: }
090: }
091:
092: /**
093: * Arranca el servidor.
094: */
095: public static void startServer() {
096: try {
097: _log.info("Iniciando el servidor");
098: _mos = new Server(TypeMOS.MOS);
099: Thread thMOS = new Thread(_mos, "MOS");
100: thMOS.start();
101:
102: _ncs = new Server(TypeMOS.NCS);
103: Thread thNCS = new Thread(_ncs, "NCS");
104: thNCS.start();
105: _isAlive = true;
106: } catch (IOException e) {
107: stopServer(TypeShutdown.Immediate);
108: }
109: }
110:
111: /**
112: * Para el servidor.
113: * @param type Forma de parar el servidor.
114: */
115: public static void stopServer(final TypeShutdown type) {
116: try {
117: _log.info("Parando el servidor");
118: _mos.close(type);
119: _ncs.close(type);
120: _isAlive = false;
121: } catch (IOException e) {
122: _log.warn("Error stop server", e);
123: }
124: }
125:
126: /**
127: * @return isAlive.
128: */
129: public static boolean isAlive() {
130: return _isAlive;
131: }
132:
133: public static Connection getConnection(final String url,
134: final String name, final TypeMOS type) throws Exception {
135: final Connection conn = new Connection(url, name, type);
136: return conn;
137: }
138:
139: /* (non-Javadoc)
140: * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
141: */
142: public void update(final Observable o, final Object arg) {
143: _clients.remove(o);
144: }
145:
146: /**
147: * Añade un listener al servidor con su conjunto de filtros.
148: * @param serverListener
149: * @param filters
150: */
151: public static void addListener(final IServerMOS serverListener,
152: final MessageFilter... filters) {
153: _listeners.put(serverListener, filters);
154: }
155:
156: /**
157: * Elimina un listener del servidor.
158: * @param serverListener
159: */
160: public static void removeListener(final IServerMOS serverListener) {
161: _listeners.remove(serverListener);
162: }
163: }
|