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

📄 rmiserverimpl.java

📁 SSD8练习4 传上去大家参考参考 研究研究
💻 JAVA
字号:
import java.rmi.RemoteException;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;

/*

 *
 * TODO this is a class to do the server's
 * 		services
 * 
 */

/**

 * @version 1.0.0
 * 
 */
public class RMIServerImpl extends java.rmi.server.UnicastRemoteObject
		implements RMIServerInt {

	//sequence id
	private static final long serialVersionUID = 1L;

	// store the clients
	private Vector<Client> vec = new Vector<Client>();

	// the id of the meet
	private int meetID = 1;

	public RMIServerImpl() throws RemoteException {

	}

	// java [clientname] [servername] [portnumber] register [username]
	// [password]
	public String register(String un, String p) throws RemoteException {

		// check to see whether the name is registed
		if (isRegisted(un)) {
			return "The user name is used!";
		}

		Client newClient = new Client(un, p);
		vec.add(newClient);

		return "Registed successfully!";
	}

	// java [clientname] [servername] [portnumber] add [username] [password]
	// [otherusername] [start] [end] [title]
	public String add(String un, String p, String oun, Date s, Date e, String t)
			throws java.rmi.RemoteException {

		// check six conditions :
		// whether the two users have registed
		String echo = isValidUser(un, p);
		if (!echo.equals("valid user!"))
			return "Meet adding Failure! Because " + echo;
		if (!isRegisted(oun))
			return "Meet adding Failure! Because the user name " + oun
					+ " has not registed!";
		//when the same user meet with himself
		if(un.equals(oun)){
			return "Meet adding Failure! Because the user " + un + " can't have meet with himself!";
		}
		
		//when the start time is later than the end time
		if(!s.before(e)){
			return "Meet adding Failure! Because the starting time \"" + s + "\" is not before the end time \"" + e + "\" !";
		}

		// whether the two users have meeting then
		if (hasMeeting(un, s, e)) {
			return "Meet adding Failure! Because the user " + un
					+ " has meeting by then!";
		}
		if (hasMeeting(oun, s, e)) {
			return "Meet adding Failure! Because the user " + oun
					+ " has meeting by then!";
		}

		// add the meeting!
		int flag = 0;// flag sign the times meet has been added
		for (Iterator i = vec.iterator(); i.hasNext() && flag != 2;) {
			Client client = (Client) i.next();
			if (client.getName().equals(un)) {
				client.addMeeting(s, e, oun, t, meetID, true);
				flag++;
			} else if (client.getName().equals(oun)) {
				client.addMeeting(s, e, un, t, meetID, false);
				flag++;
			}
		}
		meetID++;

		return "Meet adding SUCCESS! The meeting ID is: " + (meetID - 1);
	}

	// java [clientname] [servername] [portnumber] query [username] [password]
	// [start] [end]
	public Vector query(String un, String p, Date s, Date e)
			throws RemoteException {

		Vector<String> meetingsDetails = new Vector<String>();

		// check if user is valid
		String echo = isValidUser(un, p);
		if (!echo.equals("valid user!")) {
			// "Meets querying Failure! Because " + echo{
			meetingsDetails.add("Meets querying Failure! Because " + echo);
			return meetingsDetails;
		}
		
		//when the start time is later than the end time
		if(!s.before(e)){
			meetingsDetails.add("Meet querying Failure! Because the starting time \"" + s + "\" is not before the end time \"" + e + "\" !");
			return meetingsDetails;
		}

		for (Iterator i = vec.iterator(); i.hasNext();) {
			Client client = (Client) i.next();
			if (client.getName().equals(un)) {
				Vector meetings = client.query(s, e);
				if (meetings.isEmpty()) {
					meetingsDetails.add("There is no meeting by the time!");
				} else {		
					meetingsDetails.add("Meetings as follow:");
					for (Iterator j = meetings.iterator(); j.hasNext();) {
						Meeting meet = (Meeting) j.next();
						meetingsDetails.add("Meeting ID: " + meet.getID());
						meetingsDetails.add("Meeting title: " + meet.getTitle());
						meetingsDetails.add("Meeting start time: " + meet.getStart());
						meetingsDetails.add("Meeting end time: " + meet.getEnd());
						meetingsDetails.add("Meeting partner: " + meet.getOtherUserName());
						meetingsDetails.add("");
					}
				}
				
			}
		}

		return meetingsDetails;
	}

	// java [clientname] [servername] [portnumber] delete [username] [password]
	// [meetingid]
	public String delete(String un, String p, int mid)
			throws java.rmi.RemoteException {

		// check if user is valid
		String echo = isValidUser(un, p);
		if (!echo.equals("valid user!"))
			return "Meet" + mid + " deleting failure! Because " + echo;

		// delete the client's given meeting the method delete return
		// a null if there is no the given meet, otherwise return
		// another one's user name
		String str = "";
		for (Iterator i = vec.iterator(); i.hasNext();) {
			Client client = (Client) i.next();
			if (client.getName().equals(un)) {
				str = client.delete(mid);
				break;
			}
		}

		// delete the meet in another user's agenda if former delete is
		// successful
		if (!str.equals("")) {
			for (Iterator i = vec.iterator(); i.hasNext();) {
				Client client = (Client) i.next();
				if (client.getName().equals(str)) {
					client.delete(mid);
					return "Meet " + mid + " deleting success!";
				}
			}
		}
		return "There is no meet " + mid + " in " + un + "'s agenda!";
	}

	// java [clientname] [servername] [portnumber] clear [username] [password]
	public Vector clear(String un, String p) throws java.rmi.RemoteException {

		Vector<String> echoStr = new Vector<String>();

		// check if user is valid
		String echo = isValidUser(un, p);
		if (!echo.equals("valid user!")) {
			echoStr.add("Meets clearing Failure! Because " + echo);
			return echoStr;
		}
		// the client who will clear his agenda
		Client client = new Client(un, p);
		// meetings in the client's agenda
		Vector meetingIDs = new Vector();

		// get the client reference and its meeting ids as a vector
		for (Iterator i = vec.iterator(); i.hasNext();) {
			client = (Client) i.next();
			if (client.getName().equals(un)) {
				meetingIDs = client.getMeetingIDs();
				break;
			}
		}
		// when there is no meet in the agenda
		if (meetingIDs.isEmpty()) {
			echoStr.add("There is no meet in " + un + "'s agenda!");
			return echoStr;
		}

		// iterator the meeting ids and delete them
		String userName = client.getName();
		String passWord = client.getPassWord();
		for (Iterator i = meetingIDs.iterator(); i.hasNext();) {
			int mid = (int)(Integer)i.next();
			echoStr.add(delete(userName,passWord,mid));
		}
		echoStr.add("All meetings have been cleared in " + un + "'s agenda!");

		return echoStr;
	}

	// check whether the user name is registed
	public boolean isRegisted(String userName) {
		for (Iterator i = vec.iterator(); i.hasNext();) {
			Client client = (Client) i.next();
			if (client.getName().equals(userName)) {
				return true;
			}
		}
		return false;
	}

	// check whether the input user name and password is correct
	public String isValidUser(String userName, String passWord) {

		String echo = "the user name " + userName + " has not registed!";

		for (Iterator i = vec.iterator(); i.hasNext();) {
			Client client = (Client) i.next();
			if (client.getName().equals(userName)) {
				if (!client.getPassWord().equals(passWord)) {
					echo = "the password is wrong!";
					break;
				} else {
					echo = "valid user!";
					break;
				}
			}
		}
		return echo;
	}

	// check if the client has meeting between the given time
	public boolean hasMeeting(String userName, Date start, Date end) {

		for (Iterator i = vec.iterator(); i.hasNext();) {
			Client client = (Client) i.next();
			if (client.getName().equals(userName)) {
				Vector query = client.query(start, end);
				if (!query.isEmpty()) {
					return true;
				}
			}
		}
		return false;
	}
}

⌨️ 快捷键说明

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