📄 informclient.java
字号:
/*
* Author: Naizheng Bian
* Version:1.0
* Date: 11/01/2001
*/
package serverPackage;
import java.util.*;
import java.io.*;
//import java.awt.*;
//import java.net.*;
import mediaPackage.*;
//import serverPackage.*;
/**
* This Class informs other users about the events
*/
public class InformClient {
/**
* This method informs other users about a event
* @param type The type of the event
* @param msg The chat message to be delivered
* @param userList The hashtable of the current logined users' configs
* @param outputStreamTable The hashtable of users' outputStreams
* @param UserConfig The config of the user who wants to inform others
* @return The vector of crashed users
*/
public Vector informUser(int type, String msg,Hashtable usersList, Hashtable outputStreamTable,
UserConfig userConfig) {
Vector failedUsers = null;
Enumeration e = usersList.elements();
while (e.hasMoreElements()) {
UserConfig next = (UserConfig) e.nextElement();
if (userConfig != null){
if(next.getUserName().equals(userConfig.getUserName())) {
continue;
}
}
try {
try{
String userName = next.getUserName();
ObjectOutputStream ostream =
(ObjectOutputStream)outputStreamTable.get(userName);
ostream.writeObject(new Integer(type));
if(type == Constants.LOGIN_USER)
ostream.writeObject(userConfig);
else if(type == Constants.LOGOUT_USER){
ostream.writeObject(userConfig.getUserName());
}
else if(type == Constants.PUBLIC_CHAT){
ostream.writeObject(msg);
ostream.writeObject(userConfig.getUserName());
ostream.writeObject(next.getUserName());
}
else if(type == Constants.PRIVATE_CHAT){
ostream.writeObject(msg);
ostream.writeObject(userConfig.getUserName());
ostream.writeObject(next.getUserName());
}
else if(type == Constants.START_SESSION ){
;
}
ostream.flush();
ostream.reset();
}catch(IOException ie ){
}
}catch (Exception ee) {
System.out.println("Exception");
if (failedUsers == null)
failedUsers = new Vector();
failedUsers.add(next.getUserName());
System.out.println("failed user is:"+ next.getUserName());
}
}
return failedUsers;
}
/**
* This method informs another user saying YES
* @param contentManagerList The table of contents (Redundant)
* @param usersList The list of other users
* @param strURL The new URL that was added
* @return The list of users that cannot be informed (assumed crashed)
*/
public Vector addToContentManager(Hashtable contentManagerList, Hashtable usersList,String strURL,
Hashtable outputStreamTable) {
Vector crashedUsers = null;
Enumeration e = usersList.elements();
while (e.hasMoreElements()) {
UserConfig uList = (UserConfig) e.nextElement();
try {
String userName = uList.getUserName();
ObjectOutputStream ostream = (ObjectOutputStream)outputStreamTable.get(userName);
ostream.writeObject(new Integer(Constants.UPDATE_CONTENT_MANAGER));
ostream.writeObject(strURL);
ostream.flush();
}catch (IOException ie) {
if (crashedUsers == null) {
crashedUsers = new Vector();
}
crashedUsers.add(uList.getUserName());
}
}
return crashedUsers;
}
/**
* This method informs users about a URL being changed by the presenter
* @param contentManagerList The contents of the ContentManager (Redundant)
* @param usersList The list of other users
* @return The list of users that cannot be informed (assumed crashed)
*/
public Vector notifyURLChanged(Hashtable contentManagerList, Hashtable usersList, String newURL,
Hashtable outputStreamTable)
{
Vector crashedUsers = null;
String key = getFileName(newURL);
String strURL = (String) contentManagerList.get(key);
Enumeration e = usersList.elements();
while (e.hasMoreElements())
{
UserConfig uList = (UserConfig) e.nextElement();
int curUserCategory = uList.getUserCategory();
if (curUserCategory == Constants.USER)
{
try
{
String userName = uList.getUserName();
ObjectOutputStream ostream =
(ObjectOutputStream)outputStreamTable.get(userName);
ostream.writeObject(new Integer(Constants.URL_CHANGED));
ostream.writeObject(strURL);
ostream.flush();
}catch (IOException ie)
{
if (crashedUsers == null) {
crashedUsers = new Vector();
}
crashedUsers.add(uList.getUserName());
}
}
}
return crashedUsers;
}
/**
* This private method extract the name of the file from the URL
* @param urlStr The URL from which name has to be extracted
* @return The extracted name
*/
private String getFileName(String urlStr)
{
if (urlStr == "" || urlStr == null)
{
showMessage("In ReceiveFile 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 is a utility method that shows messages
* @param msg The messages to be shown
*/
private void showMessage(String msg)
{
System.err.println("UserManager: " + msg);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -