package main;
import javax.swing.JDialog;
import javax.swing.JFrame;
import common.ClientServerProtocol;
import common.info.Server;
import connection.Listener;
import connection.server.ConnectionManager;
import connection.server.ConnectionManagerInterface;
import bandwidth.network.ControlledInputStream;
import bandwidth.network.ControlledOutputStream;
import main.settings.ServerList;
import main.settings.Settings;
import search.SearchManager;
import share.SharedFilesTable;
import transfer.download.DownloadManager;
import transfer.upload.UploadManager;
import ui.GuiManager;
import ui.MainFrame;
import ui.status.NullStatusManager;
import ui.status.StatusManagerInterface;
public class ClientMain
{
private static volatile StatusManagerInterface statusManager;
private static SharedFilesTable fileTable;
private static ConnectionManager connectionManager;
private static SearchManager searchManager;
private static DownloadManager downloadManager;
private static UploadManager uploadManager;
private static GuiManager guiManager;
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
Settings.initialize();
initializeServerList();
ControlledInputStream.initialize();
ControlledOutputStream.initialize();
statusManager = new NullStatusManager();
connectionManager = new ConnectionManager();
fileTable = new SharedFilesTable();
searchManager = new SearchManager();
uploadManager = new UploadManager();
downloadManager = new DownloadManager();
Runtime.getRuntime().addShutdownHook(new ShutdownHook());
new Listener();
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
guiManager = new GuiManager();
statusManager = guiManager.getStatusManager();
}
});
}
private static void initializeServerList()
{
if (!ServerList.readFromDataFile())
{
ServerList.addServer(new Server("Local Host", "", "127.0.0.1",
ClientServerProtocol.DEFAULT_SERVER_PORT,
ClientServerProtocol.DEFAULT_SECURED_SERVER_PORT));
ServerList.addServer(new Server("TowerShade", "", "sillian.towershade.org",
ClientServerProtocol.DEFAULT_SERVER_PORT,
ClientServerProtocol.DEFAULT_SECURED_SERVER_PORT));
ServerList.writeToDataFile();
}
}
private static class ShutdownHook extends Thread
{
public void run()
{
downloadManager.terminateAllDownloads();
downloadManager.saveDownloadStatus();
}
}
public static GuiManager getGuiManager()
{
return guiManager;
}
public static MainFrame getMainFrame()
{
return guiManager.getMainFrame();
}
public static ConnectionManagerInterface getConnectionManager()
{
return connectionManager;
}
public static SharedFilesTable getSharedFilesTable()
{
return fileTable;
}
public static SearchManager getSearchManager()
{
return searchManager;
}
public static DownloadManager getDownloadManager()
{
return downloadManager;
}
public static UploadManager getUploadManager()
{
return uploadManager;
}
public static StatusManagerInterface getStatusManager()
{
return statusManager;
}
}