📄 timeserverimpl.java
字号:
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
class Interval{
int value;
public Interval(){
value = 0;
}
public Interval(int i){
value =i;
}
public int DescValue(){
value--;
return value;
}
public int getValue()
{
return value;
}
public void setValue(int value){
this.value = value;
}
}
public class timeServerImpl extends UnicastRemoteObject implements timeServerInterface{
private Vector clientList ;//客户端的IP--url
private Vector intervalList ;
private Vector tempInterval ;
Timer time1;
timeTask time_task;
static int count;
public timeServerImpl()throws RemoteException{
super();
clientList = new Vector();
intervalList = new Vector();
tempInterval = new Vector();
time1 = new Timer();
time_task = new timeTask();
time1.schedule(time_task,0,1000);
}
class timeTask extends TimerTask{
public synchronized void run(){////
for(int i = 0 ; i <clientList.size();i++){///
timeClientInterface clientObj = (timeClientInterface)clientList.elementAt(i);
Interval temp = (Interval)tempInterval.elementAt(i);
int interval = temp.DescValue();
if(interval==0){//
Interval inter = (Interval)intervalList.elementAt(i);
int val = inter.getValue();
temp.setValue(val);
try{
Date date = getTime();
clientObj.updateTime(date);
}catch(RemoteException re){
System.out.println("RemoteExeption in timeTask of timeServerImpl: "+re);
}
}//
}///
}////
}
public Date getTime()throws RemoteException{
Date date = new Date();
return date;
}
public synchronized void register(timeClientInterface clientObj , int interval)throws RemoteException{
System.out.println("register vector's size:\t" + clientList.size());
if(!(clientList.contains(clientObj))){
System.out.println("Hellow,"+interval);
clientList.addElement(clientObj);
count++;
Interval intervalTime =new Interval(interval);
intervalList.addElement(intervalTime);
Interval intervalTemp = new Interval(interval);
tempInterval.addElement(intervalTemp);
}
System.out.println("Registered the "+count+"th new client and the interval second is : "+interval);
}
public synchronized void unregister(timeClientInterface clientObj)throws RemoteException{
int index = clientList.indexOf(clientObj);
//System.out.println("unregister index:\t"+index);
if(clientList.removeElement(clientObj)){
intervalList.removeElementAt(index) ;
tempInterval.removeElementAt(index) ;
System.out.println("Unregistered client ");
//System.out.println("Vector's Size:\t"+clientList.size());
}else{
System.out.println("unregister: client wasn't registered.");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -