ecmserver0.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 475 行 · 第 1/2 页
JAVA
475 行
package ecmAglet;
import com.ibm.aglet.*;
import com.ibm.aglet.event.*;
import java.io.*;
import java.net.URL;
import java.sql.*;
//import java.util.Date;
import java.util.Enumeration;
//import java.util.Date;
public class ecmServer extends Aglet implements Runnable{
tableSql t;
log log;
AgletProxy slaveProxy=null;
AgletID slaveid=null;
//AgletProxy ecmClient;
AgletID self_id=null;
String slavename;
Thread repeatServer;
//long repeattime=0;
boolean isrun=true;
boolean isrepeate=true;
String surl=null;
double currenttime=0;
double cycletime=0;
double servicetime=0;
boolean once=true;
public void onCreation(Object o) {
t=new tableSql();
log = new log();
log.Init("Ecmdb");
self_id=getAgletID();
surl = getAgletContext().getHostingURL().toString();
//repeatServer=new repeatThread(this);
//repeatServer.start();
addMobilityListener(new MobilityAdapter() {
public void onArrival(MobilityEvent ev) {
///////////
//AgletInfo inf=getAgletInfo();
AgletContext cxt=getAgletContext();
try{
Enumeration aglets=cxt.getAgletProxies();
while (aglets.hasMoreElements()) {
AgletProxy tmp=(AgletProxy) aglets.nextElement();
AgletInfo inf=tmp.getAgletInfo();
System.out.println(inf.toString() );
String agName=tmp.getAgletClassName();
if (agName.endsWith("ecmProxy") ){ ////get server id
//slaveid=tmp.getAgletID();
slavename=tmp.getAgletClassName();
}
}
////////////
//System.out.println(inf.toString() );
//System.out.println(cxt.toString() );
//try {
} catch (Exception ex) {
ex.printStackTrace();
}
}
});//end listerner
}///end function
public boolean handleMessage(Message msg) {
System.out.println("*******enter deal with message******");
if (msg.sameKind("connect")) {
//msgg[order++]=msg;
String[] mm=(String[])msg.getArg();
String proxy=(String)mm[0];
String curl=(String)mm[1];
String cid=(String)mm[2];
String mode=(String)mm[3]; ////////mode (repeate or once)
String str=(String)mm[4]; ////command
log.Add("event","ecmserver:recieve command from "+curl+"//id is:"+cid);
slaveid=new AgletID(mm[0]);
try{
slaveProxy=getAgletContext().getAgletProxy(slaveid);
} ////
catch (Exception e){
e.printStackTrace();
System.out.println("connect with server failed");
log.Add("error","ecmserver:connect with server failed");
}
/////////获取系统当前服务时间,及服务周期
ResultSet rs;
try{
t.tableInit("Ecmdb");
rs=t.recordSql("select * from ecmSys");
while (rs.next()){
//repeattime=Long.parseLong(rs.getString("repeattime") );
//System.out.println("the cycle time is:"+rs.getString("cycle") );
//System.out.println("the servicetime of day is:"+rs.getString("servicetime") ); ///day
cycletime=Double.valueOf(rs.getString("cycle")).doubleValue();
//System.out.println("***the cycle time of minutes is:"+cycletime);
servicetime=Double.valueOf(rs.getString("servicetime")).doubleValue();
//currenttime=(double)System.currentTimeMillis()-cycletime;
//System.out.println("***the service currenttime is:"+currenttime);
}
t.closeResult();
t.closeConnect();
}//////end try
catch(NumberFormatException ex){
ex.printStackTrace();
log.Add("error","ecmserver: set repeattime FormatException");
}
catch(SQLException ex){
log.Add("error","ecmserver:get repeattime Exception ");
System.out.println("\n***SQLException caught ***\n");
while (ex != null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
////////
//currenttime=(double)System.currentTimeMillis()-cycletime*1000*60; //////设定第一次系统启动服务时间
currenttime=(double)System.currentTimeMillis(); //////设定第一次系统启动服务时间
System.out.println("***the current system time is:"+System.currentTimeMillis());
System.out.println("***the first service time is:"+currenttime);
///////////
if (isrun){
repeatServer=new repeatThread(this);
repeatServer.start();
isrun=false;
}///end if
/////记录用户查询要求
once=true;
if (mode.equals("repeate")){
System.out.println("the mode is:"+mode);
System.out.println("the command is:"+str);
double s=(double)System.currentTimeMillis()+servicetime*24*60*60*1000;
String ss=new Double(s).toString();
System.out.println("***the cycle time of minutes is:"+cycletime);
//System.out.println("***the service currenttime is:"+currenttime);
System.out.println("***this support service time of repeate is:"+ss);
insertEcm(str,curl,cid,ss);
t.closeResult();
}
else {
double s=(double)System.currentTimeMillis()+30*1000;
//double s=currenttime+100;
String ss=new Double(s).toString();
System.out.println("***the cycle time of minutes is:"+cycletime);
System.out.println("***the service currenttime is:"+currenttime);
System.out.println("***this support service time of once is:"+ss);
insertEcm(str,curl,cid,ss);
t.closeResult();
}
//System.out.println("ecmServer have succeed to save the guest command ");
return true;
}
else if (msg.sameKind("bye")) {
System.out.println("bye and close the server connection");
slaveid=null;
return true;
}
return false;
}
public void insertEcm(String strQuery,String curl,String cid,String servicetime) {
String tmpstr;
int recordNumber;
String[] records=null;
ResultSet rs;
try{
///query db and display result
t.tableInit("Ecmdb"); ////////strQuery :'good1','100','100','5000'
strQuery="insert into service (curl,cid,goodsname,model,price,amount,servetime) values("
+"'"+curl+"','"+cid+"',"+strQuery+",'"+servicetime+"')";
System.out.println(strQuery);
t.recordInsert(strQuery);
rs=t.recordSql("select * from service");
t.display(t.recordPrint(rs) );
slaveProxy.sendAsyncMessage(new Message("bye","kill finderProxy") );
log.Add("event","ecmserver:kill ecmproxy");
t.closeConnect();
}///end try
catch (Exception e){
e.printStackTrace();
log.Add("error","ecmserver:insert new service failed");
}
}//end function
}
//////////
class repeatThread extends Thread{
ecmServer aglet=null;
//long repeattime=0;
double repeattime=0;
boolean isfirst=true;
//boolean once=true;
//Message[] msgg;
//AgletProxy slaveProxy=null;
//AgletID slaveid=null;
AgletProxy ecmClient;
repeatThread(ecmServer aglet){
this.aglet = aglet;
}
public void run(){
System.out.println("*******enter the thread run********");
aglet.log.Add("event","ecmserver:enter recicle service ");
//filter=false;
tableSql tt=new tableSql();
ResultSet rs;
int datetime;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?