/************************************************************************************
MRailSim - a model railway simulation program - http://mrailsim.sourceforge.net/
Copyright (C) 2004,2007 Bernd Arnold
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 3 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, see <http://www.gnu.org/licenses/>.
************************************************************************************/
package net.sf.mrailsim.main;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import net.sf.mrailsim.gui.MainWindow;
import net.sf.mrailsim.gui.MapWindow;
import net.sf.mrailsim.gui.SignalWatcherWindow;
import net.sf.mrailsim.gui.TrainWatcherWindow;
import net.sf.mrailsim.shared.Logger;
public class Program {
public static final long VERSION = 30000; // Major * 1000000 + Release * 10000 + Minor * 100 + Patch; 30000 => 0.3.0.0;
public static final String NAME = "MRailSim";
public static final String COPYRIGHT = "(C) 2004,2007 by Bernd Arnold";
public static final String RELEASE_INFO = NAME + " is released under the GNU General Public License";
// TODO: How to declare license?
// public static final String LICENSE = "GNU General Public License version 3 or later - http://www.gnu.org/licenses/gpl.html";
public static Server server;
public static Game game;
private static MapWindow mapWindow;
private MainWindow mainWindow;
// TODO: Unify to watcherWindows
private static ArrayList<TrainWatcherWindow> trainWatcherWindows;
private static ArrayList<SignalWatcherWindow> signalWatcherWindows;
public Program() {
Logger.setLevel( "" );
// Logger.setLevel( "I" );
// Logger.enableComponent( Logger.Component.TRACK );
game = new Game();
server = new Server();
// TODO: Unify to watcherWindows
trainWatcherWindows = new ArrayList<TrainWatcherWindow>();
signalWatcherWindows = new ArrayList<SignalWatcherWindow>();
}
public void start() {
printStartupInfo();
try {
Thread.sleep( 10 );
} catch (InterruptedException e) { }
}
public void startGame() {
mainWindow = new MainWindow( this );
mainWindow.setVisible( true );
mapWindow = new MapWindow();
mapWindow.setSize( 850, 650 );
mapWindow.setLocation( 400, 100 );
mapWindow.setVisible( true );
Thread serverThread = new Thread( server );
serverThread.start();
game.start();
}
public void exit() {
server.setStop( true );
// Hide the windows
mainWindow.setVisible( false );
mapWindow.setVisible( false );
for ( TrainWatcherWindow window : trainWatcherWindows ) {
window.setVisible( false );
}
for ( SignalWatcherWindow window : signalWatcherWindows ) {
window.setVisible( false );
}
// Wait for server thread to be ending
// TODO: 600 should be higher then server sleep
try {
Thread.sleep( 600 );
}
catch ( InterruptedException e ) { }
System.out.println( NAME + " ending." );
System.exit( 0 );
}
void printStartupInfo() {
System.out.println( NAME + " " + COPYRIGHT );
System.out.println( RELEASE_INFO );
System.out.println();
System.out.println( "Current environment:" );
System.out.println( "Java version: " + System.getProperty( "java.version" ) );
System.out.println();
}
public void dispatchMessage( String action, String msg ) {
if ( action.equals( "ADDTRACK" ) ) {
// Examples:
// "Id=1,Type=Straight,Length=100,Nodes=(1,2)"
// "Id=2,Type=Switch_1,Length=100,Nodes=(2,3,103)"
Game.trackManager.addTrack( msg );
}
if ( action.equals( "SETPOSITION" ) ) {
// Example:
// "Id=2,Newpos=1"
Game.trackManager.setPosition( msg );
}
if ( action.equals( "COORDINATES" ) ) {
// Example:
// "Id=2,X=100,Y=50"
Game.nodeManager.setCoordinates( msg );
}
if ( action.equals( "ADDSECTION" ) ) {
// Example:
// "Id=3"
Game.trackManager.addTrackSection( msg );
}
if ( action.equals( "SETSECTION" ) ) {
// Example:
// "Section=1,Track=2"
Game.trackManager.setTrackSection( msg );
}
if ( action.equals( "ADDSIGNAL" ) ) {
// Example:
// "Id=3,Section=1"
Game.trackManager.addSignal( msg );
}
if ( action.equals( "SIGNAL_COORDINATES" ) ) {
// Example:
// "Id=2,X=100,Y=50"
Game.trackManager.setSignalCoordinates( msg );
}
if ( action.equals( "START_MAPREADER" ) ) {
// Example:
// "X=5,Y=2,Grid=10"
game.startMapReader( msg );
}
if ( action.equals( "ADD_MAPLINE" ) ) {
// Example:
// "1 5 5 5 2"
game.passLineToMapReader( msg );
}
if ( action.equals( "END_MAPREADER" ) ) {
// Example:
// ""
game.finishMapReader( this );
}
}
public void loadConfigFromFile( String filename ) {
try {
BufferedReader in =
new BufferedReader( new FileReader( filename ) );
String line;
String splitted[];
while ( ( line = in.readLine() ) != null ) {
if ( line.startsWith( "#" ) ) {
continue;
}
splitted = line.split( ":" );
if ( splitted.length == 2 ) {
dispatchMessage( splitted[ 0 ], splitted[ 1 ].trim() );
}
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.err.println( "Configuration file cannot be found. Terminating program." );
System.exit( 1 );
} catch (IOException e) {
e.printStackTrace();
System.err.println( "Configuration file cannot be read. Terminating program." );
System.exit( 1 );
}
}
static public void refreshMapWindows() {
mapWindow.repaint();
}
public static void refreshTrainWatcherWindows() {
for ( TrainWatcherWindow window : trainWatcherWindows ) {
window.refresh();
}
}
public static void refreshSignalWatcherWindows() {
for ( SignalWatcherWindow window : signalWatcherWindows ) {
window.refresh();
}
}
public static void addTrainWatcherWindow( long trainId ) {
TrainWatcherWindow window = new TrainWatcherWindow( null, Game.trainManager.getTrain( trainId ) );
trainWatcherWindows.add( window );
}
public static void addSignalWatcherWindow( long signalId ) {
SignalWatcherWindow window = new SignalWatcherWindow( null, Game.trackManager.getSignal( signalId ) );
signalWatcherWindows.add( window );
}
}