/*
* Copyright (c) 2006 Manuel Traut and Volker Dahnke
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors: Manuel Traut and Volker Dahnke
*
*/
package YalpServer;
import java.net.Inet4Address;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.util.Properties;
import java.security.*;
import java.sql.SQLException;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
import YalpInterfaces.*;
/*
* Class InitServer
*
* Establishes DBConnection and creates VlcStreamer waits for
* further Instructions via Interfaces
*
* @author Volker Dahnke / Manuel Traut
*
* @version 0.1 20-11-2005
*
* @see Server
*
*/
public class InitServer {
private ServerSettings settings = new ServerSettings();
private ServerControlImpl srvCon;
private ServerControlInterface srv;
private ORB orb;
private String[] orbArgs;
private POA poa;
private String serverIP;
private ArrayList browseList;
/*
* Constructor: starts Server initialization
*/
public InitServer(String[] _orbArgs) {
loadConfig("ServerSettings.xml");
writeConfig("ServerSettings.xml");
this.browseList = new ArrayList();
this.orbArgs = _orbArgs; // t.b.d. read orbargs from config xml
try {
this.serverIP = Inet4Address.getLocalHost().getHostAddress();
} catch(UnknownHostException e) {
/* t.b.d. error handling */
System.out.println("couldn't resolve hostname");
}
/* bind ServerControl to ORB and NamingService */
this.orb = ORB.init(orbArgs, null);
try {
this.poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
} catch(org.omg.CORBA.ORBPackage.InvalidName e) {
/* t.b.d. error handling */
System.out.println("couldn't get name ref of root poa");
}
try {
poa.the_POAManager().activate();
} catch(org.omg.PortableServer.POAManagerPackage.AdapterInactive e) {
/* t.b.d. error handling */
System.out.println("poa inactive");
}
this.srvCon = new ServerControlImpl();
this.srvCon.setORB(this.orb);
try {
org.omg.CORBA.Object ref = poa.servant_to_reference(this.srvCon);
this.srv = ServerControlInterfaceHelper.narrow(ref);
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
String name = "YALP_Server";
NameComponent path[] = ncRef.to_name(name);
ncRef.rebind(path, this.srv);
System.out.println("YALP Server ready");
this.orb.run();
} catch( org.omg.CosNaming.NamingContextPackage.InvalidName e) {
/* t.b.d. error handling */
System.out.println("couldn't narrow ref to path");
} catch( org.omg.CosNaming.NamingContextPackage.NotFound e) {
/* t.b.d. error handling */
System.out.println("naming context not found, couldn't bind server ctl");
} catch(org.omg.PortableServer.POAPackage.ServantNotActive e) {
/* t.b.d. error handling */
System.out.println("couldn't get name ref of root poa");
} catch (org.omg.CORBA.ORBPackage.InvalidName e) {
/* t.b.d. error handling */
System.out.println("couldn't get NameService");
} catch (org.omg.PortableServer.POAPackage.WrongPolicy e) {
/* t.b.d. error handling */
System.out.println("policies not set correctly");
} catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) {
/* t.b.d. error handling */
System.out.println("rebind failed, cannot proceed");
}
}
/*
* write Configuration to XML File
*
* @param fileName
* where to write the configuration file
* @return boolean
* false - if failed
*/
public boolean writeConfig(String fileName) {
try{
FileOutputStream configFile = new FileOutputStream(fileName);
XMLEncoder configWriter = new XMLEncoder(configFile);
configWriter.writeObject(settings.imageDir);
configWriter.writeObject(settings.videoDir);
configWriter.writeObject(settings.soundDir);
configWriter.close();
} catch (FileNotFoundException fnfe){
return false;
}
return true;
}
/*
* tries to load ServerSettings from XML File
* @param fileName
* Configuration file
* @return boolean
* false - if loading failed
*/
public boolean loadConfig(String fileName) {
try{
FileInputStream configFile = new FileInputStream(fileName);
XMLDecoder configLoader = new XMLDecoder(configFile);
settings.imageDir = (String)configLoader.readObject();
settings.videoDir = (String)configLoader.readObject();
settings.soundDir = (String)configLoader.readObject();
configLoader.close();
} catch(FileNotFoundException fnfe) {
System.out.println("Configuration not found, loading defaults...");
return false;
} catch(ClassCastException cce) {
System.out.println("Errors in Configuration, loading defaults...");
return false;
}
return true;
}
/*
* returns actual configuration of the server
* @return ServerSettings
* actual configuration
*/
public ServerSettings getConfig() {
return this.settings;
}
/*
* sets and saves a new ServerConfiguration
*
* @param set
* new ServerSettings
* @return boolean
* true if succesfully saved
*/
public boolean setConfig(ServerSettings set){
this.settings = set;
return this.writeConfig("ServerConfiguration.xml");
}
/*
* t.b.d. session management
*
* Client logon (give him,his fileBrowser)
*
* @param ipAdress
* of the Client
* @param userName
* using the Client
*
public void newClient(String ipAdress,String userName) {
this.browseList.add( new FileBrowser( this.settings.startDir,
ipAdress,
userName ) );
}
*/
/*
* Client logoff (free memory of his fileBrowser
*
* @param ipAdress
* of the Client
* @param userName
* using the Client
*/
public void remClient(String ipAdress, String userName) {
for( FileBrowser aBrowser : this.browseList ) {
if( aBrowser.getOwner().equals(ipAdress) &&
aBrowser.getUserName().equals(userName) ) {
this.browseList.remove(aBrowser);
break;
}
}
}
/*
* change Directory in fileBrowser
*
* @param ip
* of the Client
* @param dir
* to which should be changed
* @return ArrayList
* content of the directory changed to
*/
public ArrayList changeDir(String ip, String dir) {
FileBrowser aBrowser = null;
for(FileBrowser oneMoreBrowser : this.browseList) {
if( oneMoreBrowser.getOwner().equals(ip) ) {
oneMoreBrowser.changeDir(dir);
aBrowser = oneMoreBrowser;
break;
}
}
return aBrowser.getFiles();
}
/*
* Server Shutdown
*/
public void serverShutdown() {
System.out.println("Server shutdown - Server is going down");
System.exit(0);
}
}