📄 serverclass.java
字号:
/*
* Author: Naizheng Bian
* Version: 1.0
* Date: 11/01/2001
*/
package serverPackage;
import mediaPackage.*;
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
import java.io.*;
import java.net.*;
//import java.awt.*;
import java.rmi.registry.*;
//import serverPackage.*;
/**
* This is the remote server class that receives request from client and
* do appropriate processing
*/
public class ServerClass extends UnicastRemoteObject implements ServerFace
{
/**
*
*/
private static final long serialVersionUID = 1L;
//true-session started, false-session stoped
//private boolean startSession;
//true-the mic is locked by a person who wants to speak,false-the mic is free
private boolean lockedMic;
private ServerSocket fileSocket;
//the user locked mic
private String locker;
private int portOffset=0;
//instance of class UserLogin
private UserLogin mLogin = null;
//instance of class of UserManager
private UserManager mUserManager = null;
//instance of class of InformClient
private InformClient mClientInformer = null;
//histroy of text chat
private Vector mChatMessages;
//private Hashtable userConfigTable;
private Hashtable outputStreamTable;
//array for values of clarity
private int mClarityValues[] = {0,0,0,0};
//array for values of pace
private int mPaceValues[] = {0,0,0,0};
//instance of AudioRelay.
private AudioRelay mAudioRelay;
/**
* This default constructor
* It creates the various objects needed for normal performing
*/
class FileThread extends Thread{
private FileInputStream in;
private BufferedOutputStream out;
private Socket socket;
private String fileName=null;
public FileThread(Socket s){
this.socket=s;
}
public void run(){
int i=0;
try{
ObjectInputStream input=new ObjectInputStream(socket.getInputStream());
fileName=(String)input.readObject();
System.out.println("in server file name is :"+fileName);
//input.close();
}catch(Exception e0){
i++;
System.err.println("connection "+e0.getMessage());
}
try{
int ch;
in=new FileInputStream("D:/slides/"+fileName);
out=new BufferedOutputStream(socket.getOutputStream());
System.out.println("here in new bufferedstream");
while ((ch=in.read())!=-1) out.write(ch);
//out.write(-1);
in.close();
out.close();
System.out.println("closeed the file in d:");
}catch (IOException e0){
System.err.println("error in read file or write file"+e0.getMessage());
System.exit(-1);
}
}
}
public ServerClass() throws RemoteException {
super();
lockedMic = false;
locker = new String();
//userConfigTable = new Hashtable();
outputStreamTable = new Hashtable();
mLogin = new UserLogin();
if (mLogin == null) {
System.err.println("Error: unable to create a login manager");
System.exit(-1);
}
mUserManager = new UserManager();
if (mUserManager == null) {
System.err.println("Error: unable to create a User manager");
System.exit(-1);
}
mClientInformer = new InformClient();
if (mClientInformer == null) {
System.err.println("Error: unable to create a Clients Informer");
System.exit(-1);
}
mChatMessages = new Vector();
mAudioRelay = new AudioRelay(Constants.AUDIO_SOURCE_PORT);
if (mAudioRelay == null) {
System.err.println("Error: unable to create the Audio Relay.");
}
}
/**
* the main()
* It creates the class server and makes it available through registry
*
* @param args The command line arguments (port number) passed to it
*/
public static void main(String[] args) {
Registry registry = null;
int registryPort = 0;
if (args.length < 1) {
System.out.println("Command usage: serverPackage.ServerClass <portnumber>");
System.exit(-1);
}
try {
registryPort = Integer.parseInt(args[0]);
} catch (NumberFormatException nfe) {
System.out.println("Error: invalid port number");
System.out.println("Command usage: server.ServerClass <portnumber>");
System.exit(-1);
}
try {
registry = LocateRegistry.createRegistry(registryPort);
} catch(RemoteException re){
try {
registry = LocateRegistry.getRegistry(registryPort);
} catch (RemoteException re2) {
System.out.println("Error: Cannot start registry " + re2.getMessage());
System.exit(-1);
}
}
if (registry == null) {
System.out.println("Error: Unable to bind registry");
System.exit(-1);
}
/*if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}8*/
try {
ServerFace server = new ServerClass();
registry.rebind("ServerClass", server);
System.out.println("ServerClass bound");
((ServerClass)server).startAccept();
}
catch (Exception e) {
System.err.println("Error in starting ServerFace Class:" + e.getMessage());
System.exit(-1);
}
}
public void startAccept() {
// TODO Auto-generated method stub
try{
fileSocket=new ServerSocket(Constants.FILEPORT,20);
Socket socket=fileSocket.accept();
new FileThread(socket).start();
}catch(Exception e0){
System.err.println("error in startAccept");
}
}
/**
* This method checks if the user's login is is valid
*
* @param userName The user's login name
* @param password The user's login password
* @param category The user's required category
* @param clientAddress The clinet's IP Address
* @param audioPort The audio port on which client is listening (redundant)
*
* @return The category of the user in which it was logged in (USER or PRESENTER) or FAIL.
*/
private void show(String s){
System.out.println(s);
}
public int loginUser(String userName, String password, int category,
String clientAddress, int audioPort) throws RemoteException
{
if ( outputStreamTable.containsKey(userName))
return Constants.FAIL_LOGIN;
show("new User");
show("IP:"+clientAddress+"name:"+userName+"password:"+password+"category:"+category);
InetAddress clientAddr;
Socket clientSocket=null;
try {
clientAddr = InetAddress.getByName(clientAddress);
} catch (java.net.UnknownHostException uhe) {
return Constants.FAIL_LOGIN;
}
UserInfo uInfo = mLogin.loginUser(userName, password, category);
if (uInfo == null) {System.err.println("failed because password or name or category");return Constants.FAIL_LOGIN;}
try{
clientSocket = new Socket(clientAddr, Constants.LISTEN_PORT+(portOffset++));
ObjectOutputStream ostream = new ObjectOutputStream(clientSocket.getOutputStream());
outputStreamTable.put(userName,ostream);
}catch(IOException e0){
System.out.println("Could not open client socket in ServerClass.");
System.err.println("io exception in loginUser");
}
UserConfig uList = mUserManager.loginUser(uInfo,clientAddr,audioPort,category,lockedMic);
if (uList == null) {System.err.println("failed because some other reason");return Constants.FAIL_LOGIN;}
Vector crashedUsers = mClientInformer.informUser(Constants.LOGIN_USER,userName,
mUserManager.getUserConfigList(),outputStreamTable, uList);
if (crashedUsers != null) {
removeCrashedUsers(crashedUsers);
}
mAudioRelay.addUser(clientAddress);
return uList.getUserCategory();
}
/**
* This function checks if the password is valid
*
* @param userName The username
* @param password The password of the user
*
* @return Constants.FAIL if the user password is incorrect
* Constants.PASS if the user password is correct
*/
public int verifyPassword(String userName, String password)
throws RemoteException
{
UserInfo uInfo = mLogin.loginUser(userName, password,Constants.USER);
if (uInfo == null)
return Constants.FAIL_LOGIN;
else
return Constants.PASS_LOGIN;
}
/**
* This function registers a new user
*
* @param uInfo The information about the new user
*
* @returns <code>true</code> if the user is registered successfully
* <code>false</code> if the user is not registered
*/
public boolean registerUser(UserInfo uInfo)throws RemoteException
{
boolean result = false;
result = mLogin.registerUser(uInfo);
return result;
}
/**
* This method checks if the user successfully logout
*
* @param userName The user's login name
*
* @returns <code>true</code> if the user successfully log out
* <code>false</code> if not.
*/
public boolean logoutUser(String userName) throws RemoteException
{
if(userName.equals(locker)){
locker = "";
lockedMic = false;
}
Hashtable table = mUserManager.getUserConfigList();
UserConfig userConfig = (UserConfig) table.get(userName);
outputStreamTable.remove(userName);
boolean result = mUserManager.logoutUser(userName);
Vector crashedUsers = mClientInformer.informUser(Constants.LOGOUT_USER,userName,
mUserManager.getUserConfigList(),outputStreamTable,userConfig);
if (table.size() > 0) mAudioRelay.deleteUser(userConfig.getIPAddress());
if (crashedUsers != null) {
removeCrashedUsers(crashedUsers);
}
if(!mUserManager.isInSession())
crashedUsers = mClientInformer.informUser(Constants.STOP_SESSION,null,
mUserManager.getUserConfigList(),outputStreamTable,null);
if (crashedUsers != null) {
removeCrashedUsers(crashedUsers);
}
return result;
}
/**
* This method is used to get a hashtable of currently logined user's configs.
*
* @return a hashtable of user's configs.
*/
public Hashtable getUsersConfig() throws RemoteException{
return mUserManager.getUserConfigList();
}
/**
* This method is used to start the session.
*
*/
public void startSession() throws RemoteException{
boolean result = mUserManager.startSession();
if (result==true){};
Vector crashedUsers = mClientInformer.informUser(Constants.START_SESSION,null,
mUserManager.getUserConfigList(),outputStreamTable,null);
if (crashedUsers != null) {
removeCrashedUsers(crashedUsers);
}
}
/**
* This method is used to stop the session.
*
*/
public void stopSession() throws RemoteException{
boolean result = mUserManager.stopSession();
if (result==false){};
Vector crashedUsers = mClientInformer.informUser(Constants.STOP_SESSION,null,
mUserManager.getUserConfigList(),outputStreamTable,null);
if (crashedUsers != null) {
removeCrashedUsers(crashedUsers);
}
}
/**
* This method is used to get the information of session.
*
* @returns <code>true</code> if it is in session
* <code>false</code> if it is out of session.
*/
public boolean getSessionInfo() throws RemoteException
{
return mUserManager.isInSession();
}
/**
* This method is used to get public chat histrory.
*
* @return a vector of public chat messages.
*/
public Vector getChatMessages() throws RemoteException
{
return mChatMessages;
}
/**
* This method is used by the user to indicate yes.
*
* @returns <code>true</code> if the user successfully saying yes
* <code>false</code> if not.
*/
public boolean sayYes(String userName) throws RemoteException
{
Hashtable table = mUserManager.getUserConfigList();
UserConfig userConfig = (UserConfig) table.get(userName);
boolean result= mUserManager.sayYesNo(userName,Constants.YES);
Vector crashedUsers = mClientInformer.informUser(Constants.SAY_YES,userName,
mUserManager.getUserConfigList(),outputStreamTable,userConfig);
if (crashedUsers != null) {
removeCrashedUsers(crashedUsers);
}
return result;
}
/**
* This method is used by the user to indicate no.
*
* @returns <code>true</code> if the user successfully saying no
* <code>false</code> if not.
*/
public boolean sayNo(String userName) throws RemoteException
{
Hashtable table = mUserManager.getUserConfigList();
UserConfig userConfig = (UserConfig) table.get(userName);
boolean result= mUserManager.sayYesNo(userName,Constants.NO);
Vector crashedUsers = mClientInformer.informUser(Constants.SAY_NO,userName,
mUserManager.getUserConfigList(),outputStreamTable,userConfig);
if (crashedUsers != null) {
removeCrashedUsers(crashedUsers);
}
return result;
}
/**
* This method is used by the presenter to clear yes or no.
*
* @returns <code>true</code> if the presenter successfully clears yes/no
* <code>false</code> if not.
*/
public boolean clearYesNo() throws RemoteException
{
boolean result = mUserManager.clearYesNo();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -