📄 server.java
字号:
/* Server.java
*
* The MediaServer stores QuickTime video files from CamProcessors and transmits
* video to CamViewers using RTP Video files are stored on disk, and video
* data is stored in a database.
*
* Copyright (C) 2002 by Frank McCown
* University of Arkansas at Little Rock
* July 2002
*/
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContext.*;
import org.omg.PortableServer.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.sql.*;
public class Server extends JFrame
{
private final int DEFAULT_WINDOW_WIDTH = 350, DEFAULT_WINDOW_HEIGHT = 500;
private static NameComponent ncArray[] = new NameComponent[1];
private static NameComponent ncArray2[] = new NameComponent[1];
private static NamingContext ncRef;
private static org.omg.CORBA.ORB orb;
// Object to register with CORBA
public static MediaStoreImpl mediaStore;
public static RegistrationImpl registrationObj;
//private static JLabel lblInfo, lblImage;
public static Connection connection; // Used by MediaStore for DB access
public static boolean connectedToDB = false;
private static boolean startupError = false;
private final String DBPROP_FILE = "database.conf";
private static final String PROPERTY_FILE = "project.conf";
private MediaServerPanel serverPanel;
// Constructor
public Server()
{
// Make database connection for storing images
DBConnect();
// Start socket server
startFileServer();
// Call constructor AFTER getting ORB up because of ORB-dependent methods in MediaServerPanel
serverPanel = new MediaServerPanel();
JPanel contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(new BorderLayout());
this.setSize(new Dimension(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT));
this.setTitle("Media Server");
contentPane.add(serverPanel, BorderLayout.CENTER);
show();
}
private void DBConnect()
{
java.util.Properties props = new java.util.Properties();
// Load the driver to allow connection to the database
try
{
// Get username and password
FileInputStream f = new FileInputStream(DBPROP_FILE);
props.load(f);
f.close();
}
catch (FileNotFoundException ex)
{
System.err.println("Unable to open file " + DBPROP_FILE + ".");
showError("Unable to open file " + DBPROP_FILE + ".");
ex.printStackTrace();
}
catch (IOException ex)
{
System.err.println("Unable to read from " + DBPROP_FILE + " file.");
showError("Unable to read from " + DBPROP_FILE + " file.");
ex.printStackTrace();
}
try
{
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("Connecting to database...");
String url = "jdbc:microsoft:sqlserver://" + props.getProperty("hostname");
connection = DriverManager.getConnection(
url, props.getProperty("username"),
props.getProperty("password"));
connectedToDB = true;
}
catch (Exception e)
{
e.printStackTrace();
startupError = true;
}
}
private static void initCorba(String[] args)
{
// Initialize the Orbacus ORB
java.util.Properties props = System.getProperties();
props.setProperty("org.omg.CORBA.ORBClass", "com.ooc.CORBA.ORB");
props.setProperty("org.omg.CORBA.ORBSingletonClass", "com.ooc.CORBA.ORBSingleton");
try
{
// Read properties from project property file that indicates location
// of Name and Event Services.
FileInputStream f = new FileInputStream(PROPERTY_FILE);
props.load(f);
f.close();
}
catch (FileNotFoundException e)
{
System.out.println("Could not file file: " + PROPERTY_FILE);
e.printStackTrace();
startupError = true;
}
catch (IOException e)
{
System.out.println("Could not load properties from " + PROPERTY_FILE);
e.printStackTrace();
startupError = true;
}
orb = org.omg.CORBA.ORB.init(args, props);
/***********************************************************/
// The following code is necessary for the ORB to work correctly:
try
{
// get reference to rootpoa & activate the POAManager
POA rootPOA = (POA)orb.resolve_initial_references("RootPOA");
org.omg.CORBA.Policy[] policies = {
rootPOA.create_lifespan_policy(LifespanPolicyValue.PERSISTENT),
rootPOA.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID)
};
POA myPOA = rootPOA.create_POA("blahh", rootPOA.the_POAManager(), policies);
myPOA.the_POAManager().activate();
// Create the servants
mediaStore = new MediaStoreImpl();
rowc.MediaStore href = mediaStore._this(orb);
registrationObj = new RegistrationImpl();
rowc.Registration href2 = registrationObj._this(orb);
// get the root naming context
ncRef = NamingContextHelper.narrow(
orb.resolve_initial_references("NameService"));
// bind the Object References in Naming Service
ncArray[0] = new NameComponent("MediaStore","");
ncRef.rebind(ncArray, href);
ncArray2[0] = new NameComponent("Registration","");
ncRef.rebind(ncArray2, href2);
}
catch (org.omg.CORBA.TRANSIENT e)
{
JOptionPane.showMessageDialog(null,
"Unable to locate Naming Service.\nMake sure Naming Service is running.",
"Media Server Error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
System.exit(0);
}
catch (Exception e)
{
e.printStackTrace();
startupError = true;
}
}
private void startFileServer()
{
// Start FileServer on separate thread to listen for incoming calls from
// CamManagers wanting to store video files.
try
{
// Retrieve video file from client via sockets
FileServer fs = new FileServer();
}
catch (Exception e)
{
e.printStackTrace();
startupError = true;
}
}
public void showError(String error)
{
JOptionPane.showMessageDialog(this, error, "Media Server Error",
JOptionPane.ERROR_MESSAGE);
}
public static void shutDown()
{
try
{
if (connectedToDB)
connection.close();
}
catch (SQLException e)
{
System.err.println("Unable to disconnect from database.");
e.printStackTrace();
}
catch (Exception e)
{
// Probably called because connection was never properly made.
System.err.println("Error closing database connection.");
System.err.println(e);
}
// Must unbind or name will remain in NameService
try
{
ncRef.unbind(ncArray);
ncRef.unbind(ncArray2);
}
catch (Exception e)
{
e.printStackTrace();
}
// ORBacus requires this be done to properly free-up resources:
((com.ooc.CORBA.ORB)orb).destroy();
}
public static void main(String[] args)
{
// Setup CORBA ORB and make remote objects available
initCorba(args);
final Server app = new Server();
app.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
app.shutDown();
System.exit(0);
}
});
if (startupError)
app.serverPanel.setInfo("Error during startup.");
else
app.serverPanel.setInfo("");
System.out.println("Server is ready.");
// Wait for incoming requests
orb.run();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -