移动AGENT系统 ——Aglets 提纲 Aglets简介 Aglets模型 Aglets API Aglets样例 2 AGLETS Aglets IBM http://www.trl.ibm.com/aglets/ http://aglets.sourceforge.net/ Latest version 2.5-alpha Stable version: 2.0.2 3 AGLETS What are mobile agents? Mobile network agents are programs that can be dispatched from one computer and transported to a remote computer for execution. Arriving at the remote computer, they present their credentials and obtain access to local services and data. The remote computer may also serve as a broker by bringing together agents with similar interests and compatible goals, thus providing a meeting place at which agents can interact. 4 AGLETS What is the Aglets Software Development Kit? The Aglets Software Development Kit is an environment for programming mobile Internet agents in Java. (It is what we used to be called Aglets Workbench.) 5 AGLETS What is an aglet? The aglet represents the next leap forward in the evolution of executable content on the Internet, introducing program code that can be transported along with state information. Aglets are Java objects that can move from one host on the Internet to another. That is, an aglet that executes on one host can suddenly halt execution, dispatch itself to a remote host, and resume execution there. When the aglet moves, it takes along its program code as well as its data. 6 AGLETS Aglet Mobile Agent Platform (Aglets MAP): is the core platform, able to manage mobile agents. Tahiti: is the main server in charge of managing the mobility of agents. It comes with a GUI that helps administrators taking care of running agents. Aglets Software Development Kit (Aglets SDK – ASDK): is a library that provides developers all the facilities required to write mobile agents compliant to the Aglets MAP. 7 AGLETS 迁移机制 通信机制 安全保障 8 MOBILITY Strong Mobility vs. Weak Mobility Example for (int i = 0; i<10; i++){ migrate ( remoteHosts[i] ); } 9 MOBILITY Aglets only support weak mobility! In fact, almost all the Java MAP support only the weak mobility. No support in Java to get enough information about the execution flow. The Thread class itself is not serializable, and so cannot be transmitted over the network. 10 MOBILITY Aglets ensures that an agent will keep its Java state (i.e. the value of its inner variable) among migrations (at least if the Java state is serializable). When the agent is going to be executed on the remote machine, the execution will start from a well defined entry point (a method), and no reinitialization will happen (in order to not override the current agent state). 11 COMMUNICATION Based on message passing! Agents cannot directly reference other agents, so it is not possible for an agent to handle a “pointer” to another agent. For security reason 12 COMMUNICATION Proxy Pattern A proxy is an object that masquerades another object (its owner) and that forwarders method calls to its owner. 13 COMMUNICATION Aglets are hidden by proxies, and other aglets can send messages or perform method calls against the proxy, having the proxy to forward such method calls on the owner agent. Advantages: Aglets are always protected, being invisible and that the system can create and manage a lot of proxies for the same aglet without breaking the agent protection. The dispose of a proxy does not causes the disposing of the agent it is masquerading. In Aglets, agents are created and managed by the MAP, while proxies represents “handles” to other agents, thanks to which agents can communicate each other. 14 SECURITY protection of the host against aglets, protection of other aglets, protection of the aglet from the host, and protection of the underlying network. 15 SECURITY 16 SECURITY The Aglet security model supports the flexible definition of various security policies and describes how and where a secure system enforces these policies. Security policies are defined in terms of a set of rules by one administrative authority. 17 SECURITY POLICIES the conditions under which aglets may access objects; the authentication required of users and other principals, which actions an authenticated entity is allowed to perform, and whether entities can delegate their rights; the communications security required between aglets and between contexts, including trust; and the degree of accountability required for each security relevant activity. 18 AGLET 体系结构 Generic Mobile Agent Server Interagent Communication Manager Agent Manager Network Generic Mobile Agent Server Agent Reliability Manager Generic Mobile Agent Server Application Gateway External Application Directory Manager Security Manager 19 AGLET 模型 What is java aglet? Lightweight agent Java object that can move from one host to another A mobile java agent that supports concepts of autonomous execution and dynamic routing on its itinerary. Has its own thread of control Is event-driven Communicates by message passing “Write once, go anywhere” 20 AGLET MODEL(1) Basic Elements: Aglet – a mobile Java object Proxy – a representative of an aglet Context – an aglet’s workplace Identifier – bound to each aglet 21 RELATIONSHIP BETWEEN AGLET AND PROXY Proxy Represents the Aglet A handle for local, remote or deactivated aglet Shields Public Methods for Potential Misuse Can Hide the “actual” Location of Aglet Proxy and Aglet on Different Computing Nodes Proxy :Aglet < = > n : 1 22 Clients Interaction Proxy Aglet H1 Clients Interaction Proxy H2 Aglet 23 CONTEXT A context is an uniform execution environment for aglet. a stationary object provides a means for maintaining and managing running aglets One node in a computer network may run multiple servers and each server may host multiple contexts. Contexts are named and can thus be located by the combination of their server's address and their name. 24 CONTEXT Aglets must be instantiated within a context, and at any time belong to exactly one context. Context can be used for: createAglet retractAglet getAgletProxies / getAgletProxy setProperty/getProperty 25 RELATIONSHIP BETWEEN HOST, SERVER PROCESS(ENGINE), AND CONTEXTS Host Context Context Context Server Process (Engine) 26 Network CONTEXT, PROXY, AGLET aglet Proxy Context 27 AGLET MODEL(2) Basic Elements Aglet : a mobile java object Proxy : a representative of an aglet Context : an aglet’s workplace Identifier : bound to each aglet Message : an object exchanged between aglets Future reply : an asynchronous message-sending 28 AGLET MODEL(2) Message an object exchanged between aglets. It allows for synchronous as well as asynchronous message passing between aglets. Future reply A future reply is used in asynchronous messagesending as a handler to receive a result later asynchronously 29 AGLET : COMMUNICATION MODEL Message passing: Allows aglets to create and exchange messages in flexible ways. Key Components: Aglet, AgletProxy, Message, Future reply, Reply set Aglet/ Application Message Reply Proxy Message Aglet Reply 30 AGLET MODEL(2) Message aglet Proxy Proxy aglet Context 31 AGLET: OPERATIONS Six Basic Operations 1. 2. 3. 4. 5. 6. Creation Cloning Dispatching Retraction Activation and Deactivation Disposal 32 AGLET: OPERATIONS Creation Initializes a New Aglet Assigns Aglet an Identifier Cloning Produce an Identical Copy of an Aglet Differences Between Clone and Original Aglet Identifier Place of Execution 33 AGLET: OPERATIONS Dispatching Aglet is Transported (Pushed) From One Context to Another Retraction Concept that Returns an Aglet Back to its Original Context Original Context Pulls Back the Aglet From the Current Context 34 AGLET: OPERATIONS Deactivation and Activation Deactivation Must Occur Before Activation Deactivation Halts the Execution of an Aglet Activation Starts Execution of an Aglet Activation Must Occur to Restore the Aglet Disposal Stops Execution of an Aglet Removes an Aglet From its Current Context 35 BASIC OPERATIONS Classification Classified as the Life Cycle of an Aglet Observation of Six Basic Aglet Operations of Six Basic Aglet Operations Context Functions Just Like a Place 36 AGENT LIFE-CYCLE MODEL Context A Clone Context B Dispatch Aglet Aglet Dispose Retract Create Class File Deactivate Activate Disk Storage 37 AGENT LIFE-CYCLE MODEL 38 AGLET EVENT MODEL Aglet Programming Model Similar to the Java Event Model Event Based Listeners Used to Perform Event Actions Three Event Model Listeners Clone Listener Mobility Listener Persistence Listener Actions Listen for the Event They are Named After Listeners Can Be Customized to Perform Specific Actions When the Listener Event Occurs 39 RELATIONSHIP BETWEEN AGLET AND ITS LISTENERS Aglet Clone Events CloneListener Mobility Events MobilityListener Persistence Events PersistenceListener 40 EVENT MODEL LISTENERS Clone Listener Listens for Cloning Events Actions Can Be Customized to Occur Before, During, or After the Cloning Mobility Listener Listens for Mobility Events Actions Can Be Customized to Occur When an Aglet is About to Be Dispatched, Retracted, or Arrives in a New Context Persistence Listener Listens for Persistence Events Actions Can Be Customized to Occur When an Aglet is About to Be Deactivated or Has Been Activated 41 EXAMPLE Agent dispatch 42 THE AGLET MODEL Message Handling Aglets exploit a communication system based on message passing. An agent that wants to explicitly manage messages has to override the handleMessage(..) method, returning true in the case that the message is managed by the agent, and false otherwise. 43 MESSAGE HANDLING An aglet is not necessarily aware of message source, it just receives and handles messages. Message source should be included in some way in the message itself, since there is no other way to identify it. Message handling is as complex as the handleMessage(…) implementation. The method handleMessage(Message msg) has one parameter, the message received by the aglet, which can be used to define the aglet message handling behavior. The message type is its classification. 44 MESSAGE HANDLING 45 THE AGLET MODEL Agent Design Patterns Traveling Task Interaction 46 AGENT DESIGN PATTERN (CONT.) Traveling Itinerary: routes among destinations. Forwarding: a host forwards an arrived agent to another host. Ticket: objectifies a destination address and encapsulates the quality of service and permissions needed to dispatch an agent to a host address and to execute it there. 47 AGENT DESIGN PATTERN (CONT.) Task Master-Slave: an agent delegate a task to another Plan: organize multiple tasks to be performed in sequence or in parallel by multiple agents. 48 AGENT DESIGN PATTERN (CONT.) Interaction Meeting: two or more agents initiate local interaction at a given host. Locker: defines a storage and come back for data later. Messenger: an agent carries a remote message from one agent to another. Facilitator: an agent provides services for naming and locating agents. Organized Group: travel together. 49 Aglet Class & API 50 AGLETS Mobile Agents with Java Aglet Package (Classes) Aglet Class Message Class AgletID Class AgletInfo Class AgletStub Class FutureReply Class QoC Class ReplySet Class 51 AGLETS Mobile Agents with Java Aglet Package (Interfaces) AgletProxy Interface AgletContext Interface MessageManager Interface 52 AGLET API: CLASSES Aglet Class Contains All Methods Needed to Perform the Basic Aglet Operations Basis for Building All Aglets Contains All the Elements of the Aglet 53 AGLET CLASS Aglet Creation Create a Customized Aglet import com.ibm.aglet.*; public class MyFirstAglet extends Aglet{ //Put aglet’s methods here } The Dispatch Method Dispatches an Aglet to a Remote Context The Method Call Contains the URL of the Remote Context dispatch(new URL(atp://remote.host.com/context”)); 54 AGLET CLASS Overridden methods: public void onCreation(Object init){ //do some initialization here… } public void run() { //do something here; } //main entry point e.g. call dispatch(new URL(“atp://some.host.com//context”)); 55 AGLET API :AGLET CLASS What happens when “dispatch”? Object Serialization Transfer State Byte Code Host Sending Deserialization and re-create state Host Host Receiving State Byte Code Host 56 MESSAGE CLASS Message Objects are Sent Using the AgletProxy Class Methods Object sendMessage(Message msg) void sendOnewayMessage(Message msg) Code Example proxy.sendMessage(myName); String name = (String)proxy.sendMessage(yourName) ; 57 MESSAGE CLASS handleMessage method: Public boolean handleMessage(Message msg){ if(msg.samekind(“my name”)){ String name=(String)msg.getArg(); return true; //Yes, I handled this message } else if(msg.samekind(“your name?”)) { msg.sendReply(“Yina”); //Return its name return true; //Yes, I handled this message } else return false; //No, I did not handle this message } 58 AGLET API : AGLETPROXY How to get a proxy? Get proxy from a newly created aglet: AgletContext.createAglet() AgletProxy.clone() Get proxy from existing aglets Aglet.getProxy() – get its own proxy AgletContext.getAgletProxies() – retrieve an enum. of proxies in a context AgletContext.getAgletProxy() – get an aglet proxy for a given aglet ID AgletContext.setProperty – put proxy into context property and share it. 59 AGLET API : AGLETCONTEXT An aglet context is the equivalent of a place. An aglet uses AgletContext interface to: Get information about its environment Send message to the environment, including other aglets currently active in that environment How it works: context=getAgletContext(); context.createAglet(); context.retractAglet(remoteContextURL,agletID); 60 EXAMPLE • Aglet Example: Remote File Update • Premise: Large Multiple Remote Files that Must be Updated by Word Replacement One Solution: Move Files to Central Server, Perform Update, and Move Files Back Another Solution: An Aglet that Updates Files by Replacing All Occurrences of One Specified Word in the Files With Another Specified Word Distributes the Load of Updates to Multiple Servers • We’re Moving “Code” Rather Than Files • • • 61 MULTIPLE AGLETS UPDATING FILES IN PARALLEL Host Host F Aglet F F F Aglet Host F F Aglet Aglet Host (Updating) 62 UPDATE FILE AGLET import import import import public com.ibm.aglet.*; com.ibm.aglet.event.*; java.net.*; java.io.*; class UpdateFile extends Aglet{ URL destination = null; File dir = null; String from = null; String to = null; public void onCreation(Object args){ destination = (URL)((Object[])args)[0]; dir = (File)((Object[])args)[1]; from = (String)((Object[])args)[2]; to = (String)((Object[])args)[3]; addMobilityListener(){ new MobilityAdapter(){ 63 UPDATE FILE AGLET Public void onArrival(MobilityEvent e){ replace(args.file,args.from,args.to); dispose(); } } } try{ dispatch(args.destination); }catch (Exception e){ System.out.println(“Failed to dispatch.”); } } void replace(File, file, String, from, Sting to){ //Open ‘file’ and replace ‘from’ with ‘to’ } 64 APPLICATION OF AGLETS Marketplace Architecture Shop agents go to a market from a shop owner’s terminal Customer agents travel around various markets to get more information Market advertisers go to other markets to invite customer agents 65 ANATOMY OF AN AGLET Creation & Disposal Clone Mobility – Dispatch and Retract Persistence – Activate and Deactive 66 CREATION public final AgletContext Aglet.getAgletContext() //Example: getAgletContext().createAglet(getCodeBase(),”SomeAglet”,null); Methods that can be overridden protected Aglet.Aglet() Public void Aglet.onCreation(Object init) Public void Aglet.run() createAglet() Aglet() onCreation run() 67 CLONING public final Object Aglet.clone() public final void Aglet.addCloneListener(CloneListener listener) public final void Aglet.removeCloneListener(CloneListener listener) public CloneAdapter.CloneAdapter() public void CloneAdapter.onCloning(CloneEvent event) public void CloneAdapter.onClone(CloneEvent event) public void CloneAdapter.onCloned(CloneEvent event) 68 CLONING public class CloningExample extends Aglet{ boolean _theClone=false; public void onCreation(Object o) { addCloneListener(new CloneAdapter(){ public void onCloning(CloneEvent e){ // print to the console } public void onClone(CloneEvent e){ // print to the console } public void onCloned(CloneEvent e){ // print to the console } } ); } run() onCloning() onCloned() onClone() run() public void run() { if (! _theClone) { //the original runs here… try{ clone(); } catch (Exception e) { System.out.println(e.getMessage( )); } else {//the clone runs here…} } original clone 69 MOBILITY -- Dispatching run( ) dispatch( ) onDispatching( ) onArrival( ) run( ) Origin Destination -- Retracting retractAglet( ) onArrival( ) run( ) run( ) onReverting( ) Local Remote 70 PERSISTENCE Persistence Aglet Context Activate Deactivate run( ) deactivate( ) onDeactivating( ) onActivation( ) run( ) Before After 71