⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usermanager.java

📁 一个实现网络会议的软件.包含三个包.其中一个包需JMF的支持.
💻 JAVA
字号:
/*
 * Author: Naizheng Bian
 * Version: 1.0
 * Date: 12/09/2001
 */

package serverPackage;

import java.util.*;
import java.net.*;
//import java.io.*;
import mediaPackage.*;
//import serverPackage.*;

/**
 * This helper class manages user information, and session informatin. 
 */
public class UserManager {
	
	/** the hashtable for content management */
	Hashtable mConManagerTable;
	/** the hashtable of the current present users */
	Hashtable mUsersTable;	
	/** the current entry order for a specific user */
	int mEntryOrder;	
	/** the current order for raised hands */
	int mHandOrder;	
	//private members
        private boolean mInSession;
	private boolean mIsPresenter;
//        private boolean multiPresenter;
	
	/**
	  * This is the default constructor 
	  * It created default values for class members 
	  */
	public UserManager() {

        mUsersTable = new Hashtable();
	mConManagerTable = new Hashtable();
        mEntryOrder = 0;  
	mHandOrder = 1;
        mIsPresenter = false;		
        mInSession = false;
		
	}
	
	
	/**
	 * This method logs in a new user
	 * @param userInfo The UserInfo object for the basic user information
	 * @param clientAddr The client's IP address
	 * @param audioPort The audio port that the client is used for listening
	 * @param category The current user category
	 *
	 * @return true if the user successfully logges in, false if the user cannot log in
	 */
	public UserConfig loginUser(UserInfo userInfo,InetAddress clientAddr,
					int audioPort,int category,boolean miclocked) {

//		int noOfTries = 0;
		String userName = userInfo.getUserName();		
        	UserConfig userPro = null;
		if (mUsersTable.containsKey(userName)) 
			return userPro; 
       		if (userInfo != null) {
			boolean mic = true;
			if(miclocked || mInSession)
				mic = false;
       			if (!mIsPresenter && (userInfo.getCategory() == Constants.PRESENTER) 
						&& (category == Constants.PRESENTER)) {				
	       			userPro = new UserConfig(userInfo,  mEntryOrder, 
							 true,0, Constants.NONE,
							 clientAddr,audioPort, 
	       						 Constants.PRESENTER,true);
	       			mIsPresenter = true;
	       		} else {
	       			userPro = new UserConfig(userInfo,  mEntryOrder, 
							 false,0, Constants.NONE,
							 clientAddr,audioPort, 
	       						 Constants.USER,true);								  
			}       				
	       		mEntryOrder++;
	       		mUsersTable.put(userName, userPro);
	       		return userPro;
		}
       		return null;
	}
	
	    
    	/**
     	  *  This method remove the user from the list
     	  *  @param userName the user's name as a string
     	  *  @return  true if the use successfully logs out, false if not
     	  */
	public boolean logoutUser(String userName) {
		UserConfig uList = (UserConfig) mUsersTable.remove(userName);
		boolean result = false;
		if (uList != null) {
			adjustUserList(uList);
			mEntryOrder--;
			if (uList.getHandOrder() != 0) mHandOrder--;
			if (uList.getUserCategory() == Constants.PRESENTER) {
				UserConfig uConfig = getNextPresenter();				
				if (uConfig == null) {
					mIsPresenter = false;
					if (mInSession){
						stopSession();
					}
				} else {
					uConfig.setUserCategory(Constants.PRESENTER);
					uConfig.setMicAvailable(true);
				}
			}
			result = true;
		}
        	return result;        
    	}
	
    /**
     * This method get the next presenter when the previous one logs out
     */    
    private UserConfig getNextPresenter() {
    	UserConfig uConfig = null;
    	int eOrder = Integer.MAX_VALUE;
    	Enumeration e = mUsersTable.elements();
    	while (e.hasMoreElements()) {
    		UserConfig u = (UserConfig) e.nextElement();
    		if (((UserInfo) u.getUserInfo()).getCategory() == Constants.PRESENTER) {
    			if (u.getEntryOrder() < eOrder) {
    				eOrder = u.getEntryOrder();
    				uConfig = u;
    			}
    		}
    	}
    	return uConfig;
    }
	
	
    /**
     *  This method adjusts the user list after outside change
     *
     *  @param uConfig The user configuration for the removing user
     */
    private void adjustUserList(UserConfig uConfig) {

	Enumeration e = mUsersTable.elements();
    	int hOrder = uConfig.getHandOrder();
    	int eOrder = uConfig.getEntryOrder();
    	
    	while (e.hasMoreElements()) {
    		UserConfig u = (UserConfig) e.nextElement();
    		String username = (u.getUserInfo()).getUserName();
    		mUsersTable.remove(username);
    		int aOrder = u.getEntryOrder();
    		if (aOrder > eOrder) {
    			aOrder--;
    			u.setEntryOrder(aOrder);
    		}
    		if (hOrder == 0) {
    			mUsersTable.put(username, u);
    			continue;
    		}
    		int handOrder = u.getHandOrder();
    		if (handOrder > hOrder) {
    			handOrder--;
    			u.setEntryOrder(handOrder);
    		}
   		mUsersTable.put(username, u);
    	}
    }
	
	
	/**
	 * This method starts a session
	 */    
    public boolean startSession() {

    	mInSession = !mInSession;
    	Enumeration e = mUsersTable.elements();

    	while (e.hasMoreElements()) {
	    	UserConfig uList = (UserConfig) e.nextElement();
	    	if (uList.getUserCategory() != Constants.PRESENTER) {
	    		uList.setMicAvailable(!mInSession);
	    	}
    	}
	return true;
    }
	
 	/**
	 * This method stops a session
	 */    
   public boolean stopSession() {

    	mInSession = false;
    	Enumeration e = mUsersTable.elements();

    	while (e.hasMoreElements()) {
	    	UserConfig uList = (UserConfig) e.nextElement();
	    	uList.setMicAvailable(true);
    	}
		return true;
    }
 	/**
	 * This methods indicates "YES" or "NO"
	 */	
    public boolean sayYesNo(String userName,int YesNo) {
	    UserConfig uList = (UserConfig) mUsersTable.get(userName);
	    if (uList.getYesNo()!=YesNo) uList.setYesNo(YesNo);
	    else uList.setYesNo(Constants.NONE);
		return true;
    }
	
	/**
	 * This method clears up the yes/no option
	 */    
    public boolean clearYesNo(){
    	Enumeration e = mUsersTable.elements();
    	while (e.hasMoreElements()) {
	    	UserConfig uList = (UserConfig) e.nextElement();
	    	uList.setYesNo(Constants.NONE);
    	}
		return true;
    }
	
	/**
	 * This method raises hand
	 * @param userName the user's name
	 * @return true after raising the user's hand, false if not
	 */    
    public boolean raiseHand(String userName) {

    	  UserConfig uList = (UserConfig) mUsersTable.get(userName);
////////////////////////********************modified by me*************************
	  uList.setHandOrder(mHandOrder++);
	  return true;
    }
     
	/**
	 * This method lowers all users' hands
	 */    
    public boolean lowerAllHands(){

		mHandOrder =1;
    		Enumeration e = mUsersTable.elements();
    		while (e.hasMoreElements()) {
	    		UserConfig uList = (UserConfig) e.nextElement();
	    		uList.setHandOrder(0);
    		}
		return true;
    }
	

	/**
	 * This method lowers a user's hand
	 * @param userName the user's name
	 * @return true after lowing the user's hand, false if not
	 */    
     public boolean lowerHand(String userName){
		UserConfig uList = (UserConfig) mUsersTable.get(userName);	
		int hOrder = uList.getHandOrder();
    		Enumeration e = mUsersTable.elements();
    		while (e.hasMoreElements()) {
    			UserConfig u = (UserConfig) e.nextElement();
			String username = (u.getUserInfo()).getUserName();
    			mUsersTable.remove(username);
    			int handOrder = u.getHandOrder();
    			if (handOrder > hOrder) {
    				handOrder--;
    				u.setHandOrder(handOrder);
    			}
			else if(handOrder == hOrder){
				u.setHandOrder(0);
			}
			mUsersTable.put(username, u);
    		}
		mHandOrder--;
		return true;
     }
	
	/**
	 * This method exchanges step in/out
	 * @param userName the user's name
	 * @return true after exchange the step in/out, false if not
	 */    
    public boolean stepInOut(String userName) {
    	 UserConfig uList = (UserConfig) mUsersTable.get(userName);		
	 boolean step = uList.getUserPresent();
	 uList.setUserPresent(!step);
	 return true;
    }
	

	/**
	 * This method locks the microphone
	 * @param userName the user's name
	 * @return true after locking the user's hand, false if not
	 */    
    public boolean lockMic(String userName) {

		Enumeration e = mUsersTable.elements();
    	 	while (e.hasMoreElements()) {
    			UserConfig u = (UserConfig) e.nextElement();
				String username = (u.getUserInfo()).getUserName();
    			mUsersTable.remove(username);
				if (username.equals(userName) || u.getUserCategory() == Constants.PRESENTER){
					u.setMicAvailable(true);
				}
				else
					u.setMicAvailable(false);
				mUsersTable.put(username, u);
    		}
		return true;
    }
	

	/**
	 * This method unlocks the microphone
	 * @param userName the user's name
	 * @return true after unlocking the user's hand, false if not
	 */    
    public boolean unlockMic(String userName) {

    	Enumeration e = mUsersTable.elements();
    	while (e.hasMoreElements()) {
    		UserConfig u = (UserConfig) e.nextElement();
			String username = (u.getUserInfo()).getUserName();			
			if(mInSession){
				if (username.equals(userName) && u.getUserCategory() != Constants.PRESENTER){
					mUsersTable.remove(username);
					u.setMicAvailable(false);
					mUsersTable.put(username, u);
					return true;
				}
			}
			else
				u.setMicAvailable(true);
    	}
	return true;
    }
	

	/**
	 * This method gives the microphone to the next user
	 * @return true after giving the microphone to the next user, false if not
	 */    
    public boolean giveMicToNext(){
	if(mHandOrder <= 1)
		return false;
    	Enumeration e = mUsersTable.elements();
    	while (e.hasMoreElements()) {
	    	UserConfig uList = (UserConfig) e.nextElement();
		int horder = uList.getHandOrder();
		if(uList.getMicAvailable() && uList.getUserCategory() != Constants.PRESENTER)
			uList.setMicAvailable(false);
		if(horder > 0){
			horder--;
			uList.setEntryOrder(horder);
			if(horder == 0)
				uList.setMicAvailable(true);				
		}
	}
	mHandOrder--;
	return true;		
    }


    /**
     *  This method gets the current user configuration list
     */
    public Hashtable getUserConfigList() {
    	
    	return mUsersTable;
    	
    }
	
	
	/**
	 * This method adds a new URL to the content manager hashtable
	 * @param strURL The added URL string
	 * @return true if the url string is successfully stored or if it already exists
	 *         otherwise it returns false 
	 */
	public boolean addURL(String strURL) {
		
		if (strURL == "" || strURL == null)
		{
			showMessage("URL to add is empty");
			return false;
		}
		
		String endName = getFileName(strURL);
		
		if (endName == null)
		{
			showMessage("URL " + strURL + " not in proper format. Should be in the form protocol://[Servername]/Path/Filename");			
			return false;
		}
		if (mConManagerTable.containsKey(endName))
		{
			showMessage("Key " + endName +  " already exists is ContentManager Table");
			return false;
		}
		
		if (strURL.indexOf("file:") != -1)
		{
			mConManagerTable.put(endName, Constants.SERVER_URL + endName);
			
		}
		else
		{
			if (strURL.indexOf("http:") != -1)
			{
				mConManagerTable.put(endName, strURL);
			}
			else
			{
				showMessage("URL " + strURL + " not in proper format. Should be in the form protocol://[Servername]/Path/Filename");			
				return false;
			}
			
		}
			
	   	return true;
	}
	
	/**
	 * This method shows up a message
	 * @param msg The messages to be shown
	 */
	private void showMessage(String msg)
	{
		System.err.println("In UserManager: " + msg);
	}
	
    /**
     *  This method gets the content hashtable 
     */
    public Hashtable getContentManagerTable() {  	
    	return mConManagerTable;
    	
    }
	
	/**
	 * This method gets the file name based on the input url string 
	 * @param urlStr The URL string for returned file name 
	 * @return a string as the obtained name
	 */
	private String getFileName(String urlStr)
	{
		if (urlStr == "" || urlStr == null)
		{
			showMessage("In UserManager class: URL is empty.");
			return null;
		}
		
		int nameStartIndex = urlStr.lastIndexOf("/");
		if(nameStartIndex == -1)
			nameStartIndex = urlStr.lastIndexOf("\\");		
		if(nameStartIndex == -1)
			nameStartIndex = urlStr.lastIndexOf("//");
		
		String fileName = null;
		if(nameStartIndex != -1)
			fileName = urlStr.substring(nameStartIndex + 1);
		else
		{
			showMessage("URL form error: " + urlStr 
					+ "\n URL should be in the form of protocol://Path/FileName");
		}
		return fileName;
	}
	
	/**
	 * This method returns the session state 
	 * @param true if the session is in, othwise returns false
	 */
    public boolean isInSession() {
    	return mInSession;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -