📄 acremoteservice.java
字号:
package ACRemote;
import java.util.Hashtable;
import java.io.IOException;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import net.jini.discovery.DiscoveryListener;
import net.jini.discovery.DiscoveryEvent;
import net.jini.discovery.LookupDiscovery;
import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceItem;
import net.jini.core.lookup.ServiceRegistration;
class ACRemoteServiceProxy implements Serializable,ACRemoteServiceInterface{
int state;
int temp;
int hh, mm;
public ACRemoteServiceProxy(){
this.state = 2;
this.temp=24;
this.hh=0;
this.mm=0;
}
public void setOnOff(int state){
this.state = state;
}
public void setTemp(int temp){
this.temp = temp;
}
public void setTimer(int hh,int mm){
this.hh = hh;
this.mm = mm;
}
public String getStatus(){
String str = new String(state+"#"+temp+"#"+hh+"#"+mm);
return str;
}
}
public class ACRemoteService implements Runnable{
protected final int LEASE_TIME = 10 * 60 * 1000;
protected Hashtable registrations = new Hashtable();
protected ServiceItem item;
protected LookupDiscovery discovery;
class MyListener implements DiscoveryListener{
public void discovered(DiscoveryEvent de){
System.out.println("Discovered a lookup service!");
ServiceRegistrar newregs[] = de.getRegistrars();
for(int i=0;i<newregs.length;i++){
if(!registrations.containsKey(newregs[i])){
registerWithLookup(newregs[i]);
}
}
}
public void discarded(DiscoveryEvent de){
ServiceRegistrar deadregs[] = de.getRegistrars();
for(int i=0;i<deadregs.length;i++){
registrations.remove(deadregs[i]);
}
}
}
public ACRemoteService() throws IOException{
item = new ServiceItem(null, createProxy(),null);
if(System.getSecurityManager() == null){
System.setSecurityManager(new RMISecurityManager());
}
discovery = new LookupDiscovery(new String[] {""});
discovery.addDiscoveryListener(new MyListener());
}
protected ACRemoteServiceInterface createProxy(){
return new ACRemoteServiceProxy();
}
protected synchronized void registerWithLookup(ServiceRegistrar registrar){
ServiceRegistration registration = null;
try{
registration = registrar.register(item,LEASE_TIME);
}catch(RemoteException e){
System.err.println("Unable to register: "+e.getMessage());
return;
}
registrations.put(registrar,registration);
System.out.println("Service is Registered with Lookup Service.");
}
public void run(){
while(true){
try{
Thread.sleep(1000000);
}catch(Exception e){
}
}
}
public static void main(String args[]){
try{
ACRemoteService remoteservice = new ACRemoteService();
new Thread(remoteservice).start();
}catch(IOException e){
System.err.println("Unable to create service: "+e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -