⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 agent.java

📁 一个简单的Agent例子,Agent在多台主机上移动并收集信息,最终返回主机
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.image.*; 
import java.awt.event.*;
import java.io.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import java.util.*; 
import java.net.*;


public class Agent implements Serializable 
{
	private DisplayFrame df;
	private TravelPlan tp;
	private int counter=0;
	private int port=8000;
	private int hosttraveled=0;
	private String[] information;

	Agent(int s)throws IOException
	{
		this.df=new DisplayFrame(s);
		this.tp=new TravelPlan();
		information=new String[this.tp.IP.length];
	}
	
	public void setInformation(String s)
	{
		information[hosttraveled-1]=s;
	} 
	
	public void viewInformation()
	{
		System.out.println("****************************************************");
		System.out.println("");
		System.out.println("*******OUTPT THE INFORMATION OF HOST TRAVELED*******");
		for(int i=0;i<hosttraveled-1;i++)
		{
			System.out.println("The "+(i+1)+" traveled Host's information:"+information[i]);
		}
		System.out.println("****************************************************");
	}

	public void displayPic()throws Exception
	{
		this.df.setVisible(true);
		this.df.display();
	}
	
	public void readPic(InputStream is)
	{
		this.df.read(is);
	}
	
	public void gotoNextHost()
	{
       	try
		{
   			System.out.println("Prepare to travel to "+this.tp.IP[counter]+"....");
   			Socket connectToNextHost =new Socket (this.tp.IP[counter],port);
			
    		ObjectOutputStream oos = new ObjectOutputStream(
    			connectToNextHost.getOutputStream());			
				
   			this.counter++;
     		this.port++;//用于本地测试
     		hosttraveled++;
    		oos.writeObject(this);
    		oos.flush();//发送对象
			System.out.println("Agent Migrated!");
			System.out.println("****************************************************");
			connectToNextHost.close();
		}
		catch(Exception ex)
		{
			System.out.println("Error! Can't travel to "+this.tp.IP[counter]);
			this.counter++;
   		 	this.port++;//用于本地测试
			this.gotoNextHost();
		}

	}

}


class DisplayFrame extends JFrame implements Serializable
 {
 	transient BufferedImage icon;	
 	int size;
 	byte PicData[];

 
 	DisplayFrame(int s)
 	{
 		setTitle("Image");
 		size = s;
 		PicData=new byte[s+1];
 		
 	}

 	public void paint(Graphics g)
 	{
		g.drawImage(icon,0,30,this);
  	}
  	
  	public void display()throws Exception
  	{
   	   	ByteArrayInputStream in = new ByteArrayInputStream(PicData);
					
     	ImageReader reader = null;
    	Iterator <ImageReader> iter = ImageIO.getImageReadersByFormatName("JPEG");
    	if(iter.hasNext()) reader = iter.next();
    	ImageInputStream imageIn = ImageIO.createImageInputStream(in);
     	reader.setInput(imageIn);
    	icon = reader.read(0);

		setSize(new Dimension(icon.getWidth(), icon.getHeight()));
  		Graphics g=this.getGraphics ();
  		g.drawImage(icon,0,30,this);
  		
  		Thread.sleep(3000);
  	}
  	
  	public void read(InputStream is)
  	{
 		try
		{
        	is.read(PicData);
    	}
   	   	catch(IOException ex)
		{
			System.err.println(ex);
		}  	
	}

}


class TravelPlan implements Serializable
{
	public String []IP;
	
	TravelPlan()throws IOException
	{
		System.out.print("Please input the number of Host in this LAN:");
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String s=br.readLine();
		int num=Integer.parseInt(s);
		System.out.println("************CONSTRUCTING THE TRAVEL PLAN************");
		
		IP=new String[num];
		for(int i=0;i<num-1;i++)
		{
			System.out.print("Please Input The IP of Host "+(i+1)+" To Go:");
			s=br.readLine();
			IP[i]=s;
		}
		System.out.print("Please Input The IP of LocalHost:");
		s=br.readLine();
		IP[num-1]=s;
		System.out.println("************THE TRAVEL PLAN CONSTRUCTED!************");
		System.out.println("");
		System.out.println("****************************************************");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -