📄 rmiclient.java
字号:
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;
import java.io.*;
/*
*
* TODO this class is the client running on the client
*/
/**
* @version 1.0.0
*
*/
public class RMIClient{
private String registry;
private String registration;
//the RMIServerInt
private RMIServerInt service;
//the common user name
private String userName;
//the common password
private String passWord;
//the common action
//private String action;
public RMIClient(String serverName,int portNumber) throws MalformedURLException, RemoteException, NotBoundException{
this.registry = serverName;
//this.portNumber = portNumber;
registration = "rmi://" + registry + "/RMIServerInt";
service = (RMIServerInt)Naming.lookup(registration);
}
/*
*
*/
public void doService(){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
displayMenu();
try{
int actionID = Integer.parseInt(br.readLine());
if(actionID == 6)
return;
else if(actionID == 1){
System.out.println("Please input the user name!");
userName = br.readLine();
System.out.println("Please input the password!");
passWord = br.readLine();
register();
}
else if(actionID == 2){
//[username] [password] [otherusername] [start] [end] [title]
System.out.println("Please input the user name!");
userName = br.readLine();
System.out.println("Please input the password!");
passWord = br.readLine();
System.out.println("Please input the user name you will meet with!");
String otherUserName = br.readLine();
//get the start and end time input
Date start = getTimeInput("Please input the start time of the meeting!");
Date end = getTimeInput("Please input the end time of the meeting!");
System.out.println("Please input the title of the meeting!");
String title = br.readLine();
add(otherUserName,start,end,title);
}else if(actionID == 3){
//[username] [password] [start] [end]
System.out.println("Please input the user name!");
userName = br.readLine();
System.out.println("Please input the password!");
passWord = br.readLine();
Date start = getTimeInput("Please input the start time of the meeting!");
Date end = getTimeInput("Please input the end time of the meeting!");
query(start,end);
}else if(actionID == 4){
//[username] [password] [meetingid]
System.out.println("Please input the user name!");
userName = br.readLine();
System.out.println("Please input the password!");
passWord = br.readLine();
System.out.println("Please input the meeting ID!");
int meetingID = Integer.parseInt(br.readLine());
delete(meetingID);
}else if(actionID == 5){
//[servername] [portnumber] clear [username] [password]
System.out.println("Please input the user name!");
userName = br.readLine();
System.out.println("Please input the password!");
passWord = br.readLine();
clear();
}else{
System.out.println("Invalid input!");
}
}
catch(NumberFormatException nfe){
System.out.println("Invalid input!");
}
catch(Exception e){
e.printStackTrace();
}
}
}
/*
* display the menu for user to choose
*/
public void displayMenu(){
System.out.println("");
System.out.println("Please input a number between 1 and 5 to seclect the OPERATION:");
System.out.println("");
System.out.println("******************* MENU *****************");
System.out.println(" 1 : Register User");
System.out.println(" 2 : Add meeting");
System.out.println(" 3 : Querry meeting");
System.out.println(" 4 : Delete meeting");
System.out.println(" 5 : Clear meeting");
System.out.println(" 6 : Exit");
System.out.println("********************************************");
}
/*
* get the time input in the certain format
* and redo it when invalid
* @parament warning : give the message to tell what kind of time is to input
*/
public Date getTimeInput(String warning){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//the time format
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
//to signify whether the input is valid
boolean validTime = false;
Date date = new Date();
while(!validTime){
try{
System.out.println(warning);
System.out.println(" First input the Date(Format: yyyy-MM-dd)!");
String startDate = br.readLine();
if(startDate.equals("Exit"))
return date;
System.out.println(" Then input the Time(Format: HH-mm-ss)!");
String startTime = br.readLine();
if(startDate.equals("Exit"))
return date;
date= sdf.parse(startDate + " " + startTime);
validTime = true;
}
catch(Exception e){
//when the input is not according to the given format!
System.out.println("Invalid time format, please input again!");
validTime = false;
}
}
return date;
}
/*
* do register
*/
public void register() throws RemoteException{
//[servername] [portnumber] register [username] [password]
String echo = service.register(userName,passWord);
System.out.println(echo);
}
/*
* do adding meet
*/
public void add(String otherUserName,Date start,Date end,String title) throws RemoteException{
//[username] [password] [otherusername] [start] [end] [title]
String echo = service.add(userName,passWord,otherUserName,start,end,title);
System.out.println(echo);
}
/*
* do querying meet
*/
public void query(Date start,Date end) throws RemoteException{
//[username] [password] [start] [end]
Vector meetingDetails = service.query(userName,passWord,start,end);
for(Iterator i = meetingDetails.iterator();i.hasNext();){
String echo = (String) i.next();
System.out.println(echo);
}
}
/*
* do deleting meet
*/
public void delete(int meetingID) throws RemoteException{
//[username] [password] [meetingid]
String echo = service.delete(userName,passWord,meetingID);
System.out.println(echo);
}
/*
* clear a certain client's meetings
*/
public void clear() throws RemoteException{
//[servername] [portnumber] clear [username] [password]
Vector echoStr = service.clear(userName,passWord);
for (Iterator i = echoStr.iterator() ; i.hasNext(); ){
String echo = (String)i.next();
System.out.println(echo);
}
}
/* get the running host name
* contruct a object and doservice
*
*/
public static void main(String[] args) throws Exception {
int port = 1099;
if(args.length < 1){
System.out.println("Usage: servername [portnumber]!");
return;
}
//when get the port input
if(args.length > 1){
port = Integer.parseInt(args[1]);
}
RMIClient rmiClient = new RMIClient(args[0],port);
rmiClient.doService();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -