📄 messagecenterserver.java
字号:
import java.io.*;
import java.util.*;
import java.text.*;
import org.omg.CORBA.Context;
import org.omg.CORBA.ContextList;
import org.omg.CORBA.DomainManager;
import org.omg.CORBA.ExceptionList;
import org.omg.CORBA.NVList;
import org.omg.CORBA.NamedValue;
import org.omg.CORBA.Object;
import org.omg.CORBA.Policy;
import org.omg.CORBA.Request;
import org.omg.CORBA.SetOverrideType;
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
/**
* Run the server to execute the distributed system.
* @author jack
*
*/
public class MessageCenterServer implements MessageCenter {
/*store the name and password of users */
private static HashMap<String, String> user = new HashMap<String, String>();
/*store the message of every user*/
private static ArrayList<MessageContent> messageList = new ArrayList<MessageContent>();
/*store the date formate*/
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* run the main method
* @param args
*/
public static void main(String args[]) {
try{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// create servant and register it with the ORB
MessageCenterServer helloRef = new MessageCenterServer();
orb.connect(helloRef);
// get the root naming context
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
// bind the Object Reference in Naming
NameComponent nc = new NameComponent("MessageCenter", "");
NameComponent path[] = {nc};
ncRef.rebind(path, helloRef);
System.out.println ("servant registered")
// wait for invocations from clients
while(true) {
Thread.sleep(1000);
}
} catch (Exception e) {
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
}
/**
* this method should print each message that was left for the user.
*
* @param userName the name of user
* @param password the password
*/
public String register(String u,String p){
if(!isUserExisted(u)){
user.put(u, p);
return "\nRegister Successfully!\n";
}else
return "\nUsername Already Existed, Please select another!\n";
}
/** Implement the functionality of show users
*
* @return return a list of username or the error message.
*/
public String showusers(){
String returnList = "";
if(!user.isEmpty()){
Iterator i = user.keySet().iterator();
returnList += "\nAll usernames are listing below :\n";
while(i.hasNext()){
returnList += (String)i.next() + "\n";
}
return returnList;
}else
return "\nSorry, There is no user registered now!\n";
}
/** Implement the functionality of check message.
*
* @para u the u express the usersname
* @para p the p express the password
* @return return the error hint or alist of message.
*/
public String checkmessages(String u, String p){
int index = 0;
String returnMessage = "";
if(isUserExisted(u)){
if(canLogin(u, p)){
if(!isUserHasNoMessage(u)){
Iterator i = messageList.iterator();
returnMessage +="\nYour messages are listing below :\n";
while(i.hasNext()){
MessageContent m = (MessageContent)i.next();
if(m.getReceiver().equals(u)){
index++;
returnMessage += "Mail " + index + " :";
returnMessage += m.toString();
}
}
}else
return "\nSorry, There is no message for you!\n";
}else
return "\nYour Password Wrong, Input again!\n";
}else{
return "\nYou haven't registered yet, Please register first\n";
}
return returnMessage;
}
/** Implement the functionality of leave message.
*
* @para u the u express the usersname
* @para r the r express the receiver names.
* @para mt the mt express the message content
* @return return the error message.
*/
public String leavemessage(String u, String r, String mt){
String sendDate = "";
if(isUserExisted(u)){
if(!u.equals(r)){
Date now = new Date();
sendDate = sdf.format(now);
MessageContent mc = new MessageContent(u, r, sendDate, mt);
messageList.add( mc );
return "\nLeave message successfully!\n";
}else
return "\nYou shouldn't send message to yourself\n";
}else
return "\nLogin failed!\n";
}
/** Implement the functionality of count logic value.
*
* @para u the u express the usersname
* @return return a boolean to express whether there
* there is no message for the user.
*/
public boolean isUserHasNoMessage(String u){
Iterator i = messageList.iterator();
while(i.hasNext()){
if(((MessageContent)i.next()).getReceiver().equals(u))
return false;
}
return true;
}
/** Implement the functionality of count logic value.
*
* @para u the u express the usersname
* @para p the p express the password
* @return return a boolean to judge whether the user can login.
*/
public boolean canLogin(String u, String p){
if(isUserExisted(u)){
if(user.get(u).equals(p))
return true;
}
return false;
}
/** Implement the functionality of count logic value.
*
* @para u the u express the usersname
* @return return a boolean to judge whether the username existed.
*/
public boolean isUserExisted(String u){
return user.containsKey(u);
}
@Override
public Request _create_request(Context ctx, String operation,
NVList arg_list, NamedValue result) {
// TODO Auto-generated method stub
return null;
}
@Override
public Request _create_request(Context ctx, String operation,
NVList arg_list, NamedValue result, ExceptionList exclist,
ContextList ctxlist) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object _duplicate() {
// TODO Auto-generated method stub
return null;
}
@Override
public DomainManager[] _get_domain_managers() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object _get_interface_def() {
// TODO Auto-generated method stub
return null;
}
@Override
public Policy _get_policy(int policy_type) {
// TODO Auto-generated method stub
return null;
}
@Override
public int _hash(int maximum) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean _is_a(String repositoryIdentifier) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean _is_equivalent(Object other) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean _non_existent() {
// TODO Auto-generated method stub
return false;
}
@Override
public void _release() {
// TODO Auto-generated method stub
}
@Override
public Request _request(String operation) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object _set_policy_override(Policy[] policies,
SetOverrideType set_add) {
// TODO Auto-generated method stub
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -