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

📄 server.java

📁 SSD8的exam3的答案
💻 JAVA
字号:
/*
 * this is the Server class and MessageCenterImpl class.
 * @author: Chen Yao
 * @studentID:200532580247
 * @date:6.10.2008
 */

import MessageCenterAPP.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import java.util.Date;
import java.io.*;

class MessageCenterImpl extends MessageCenterPOA {
	  private ORB orb;

	  public void setORB(ORB orb_val) {
	    orb = orb_val; 
	  }
	User user[] = new User[999];
	int usertop = 0;//the total number of users.
	String show;//used to keep the methods' return value.
	
	//implement method register.
	public String register(String userName, String password) {
		//make sure every user is unique.
		for(int i = 0; i < usertop; i++){
			if(user[i].userName.equals(userName)){
				show = "This user has existed, please change your register name.";
				return show;
			}
		}
		
		User aUser = new User(userName, password);//create a new user.
		user[usertop] = aUser;
		usertop++;//add one to the total number of users.

		show = "Register Successful";
		System.out.println( userName + " has  registered");
		return show;
	}
	
	//implement method showUsers.
public String showUsers(){
 show="";
		if(usertop == 0){//if there are no users.
      
      show = " There are no users utill now.";
    
		}else{//show all the users who have registered.

      show = "users :" + "\n";
			for(int i=0;i<usertop;i++){

				show +=(i+1) + " : " + user[i].userName + "\n";
			}
		}
		return show;
	}
	
//implement method checkMessage.
	public String checkMessage(String uName,String psw){
       show="";
		if(usertop != 0){

			for(int i = 0; i < usertop; i++){//only the registered users can check their messages.

				if(user[i].userName.equals(uName) && user[i].password.equals(psw)){

                   int j = user[i].MSGtop;//the user's total message number.
                   if(j == 0){//if there are no messages for the user.
          	
          	       show = "No message for you";
          
                   }else{//show all the message for the user.
          	
          	           show = "There are Messages :"+"\n";

						for(int k = 0; k < j; k++){

							show += " From : " + user[i].msg[k].sendUser + " Message :"+user[i].msg[k].messageTexts + " Time: "+user[i].msg[k].date+"\n";

						}
						
                   }
	            }
			}if(show==""){//if the user's name or password is wrong.
				
				show = "Wrong userName or password";
			}
		}else{//if there is no users.
			show = "There are no users.";
		}
		return show;
	} 
	
	//implement the method leaveMessage.
	public String leaveMessage(String sender,String receiver,String message) {
		show="";
    
		for(int i = 0; i < usertop; i++){

			if(user[i].userName.equals(sender)){

				for(int j = 0; j < usertop; j++){

					if(user[j].userName.equals(receiver)){
                        Date d=new Date();//the date and time the message was left.
						Message ml=new Message(sender, receiver, message ,d);//create a new message.
						user[j].msg[user[j].MSGtop] = ml;
						user[j].MSGtop++;//add one to the user's total message number.
						show = "Leave Message";
						}
				}if(show==""){//if the receiver do not exist.
					show="the receiver do not exist.";
				}
			}
		}if(show==""){//if the sender have not registered.
			show="You have to register first!";
		}
  
    return show;
	}
}
	
//the class Server
	public class Server {

		public static void main(String[] args) {
			
			try{
			      // create and initialize the ORB
			      ORB orb = ORB.init(args, null);

			      // get reference to rootpoa & activate the POAManager
			      POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
			      rootpoa.the_POAManager().activate();

			      // create servant and register it with the ORB
			     MessageCenterImpl messageCenterImpl = new MessageCenterImpl();
			     messageCenterImpl.setORB(orb); 

			      // get object reference from the servant
			      org.omg.CORBA.Object ref = rootpoa.servant_to_reference(messageCenterImpl);
			     MessageCenter href = MessageCenterHelper.narrow(ref);
				  
			      // get the root naming context
			      org.omg.CORBA.Object objRef =
			          orb.resolve_initial_references("NameService");
			      // Use NamingContextExt which is part of the Interoperable
			      // Naming Service (INS) specification.
			      NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

			      // bind the Object Reference in Naming
			      String name = "MessageCenter";
			      NameComponent path[] = ncRef.to_name( name );
			      ncRef.rebind(path, href);

			      System.out.println("MessageCenterServer ready and waiting ...");

			      // wait for invocations from clients
			      orb.run();
			    } 
				
			      catch (Exception e) {//handle the exceptions.
			    	  System.err.println();
						System.out.println("ERROR : " + e) ;
						  e.printStackTrace(System.out);
			      }
				  
			      System.out.println("MessageCenterServer Exiting ...");
				}
	}

⌨️ 快捷键说明

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