
  
package com.fuse.projects.fuse_light;

/*
	FUSE Light Server - multiuser server application
	Copyright (C) 2000 Aapo Kyrola / Sulake Oy  Helsinki, Finland

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


import java.net.*;
import java.io.*;
import java.text.*;
import java.util.*;

import com.fuse.*;
import com.fuse.net.*;
import com.fuse.storage.*;
import com.fuse.projects.fuse_light.net.*;


public class FUSELight extends Thread {
	FUSEConnectionManager fcm;
	
	/**
	  * Constructor: initializes everything
	  */
	public FUSELight() throws Exception {
		/* Initialize database facade */
		FUSEDatabase db = FUSEEnvironment.getDatabaseProxy().getDatabase();
		Log.status("Database initialized:" + db);
		
		/* Initialize connection manager */
		fcm = new FUSEConnectionManager(
						FUSEEnvironment.getIntProperty("listener.maxConnections", 100), new FUSELightConnectionFactory());
		
		/* Host to bind */
		InetAddress bindAddress = InetAddress.getByName(FUSEEnvironment.getProperty("listener.host"));
		FUSEConnectionListener listener = 
			new FUSEConnectionListener(fcm, FUSEEnvironment.getIntProperty("listener.port", 50078),
					 bindAddress);
			
		this.start();
	}
	
	/**
	  * Regularly send clients STATUS-message so they know to respond with STATUSOK.
	  * This is so project-dependent that it is better to keep this here than move it 
	  * to FUSEConnectionManager.
	  */
	public void run() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
		while(true) {
			
			try {
				/* Send status to clients */
				Log.status("Number of active connections: " + fcm.getBusyConnections().size());
				String date = sdf.format(new Date());
				fcm.broadcastToAll("STATUS", date);
				
				this.sleep(10 * 1000);
			} catch (Exception e) {
				Log.error(e);
			}
		} 
	   
	}
	
	/**
	  * Starts the server
	  */
	public static void main(String[] args) {
		Log.status("Starting Fuse light");
		try {
			/* Environment variables etc */
			FUSEEnvironment.init(args[0]);
		
			new FUSELight();
		} catch (Exception e) {
			Log.error(e);
		}
	}
}


