//@snippet-start cma_init_full
package org.objectweb.proactive.examples.userguide.cmagent.initialized;

import org.objectweb.proactive.Body;
import org.objectweb.proactive.EndActive;
import org.objectweb.proactive.InitActive;
import org.objectweb.proactive.RunActive;
import org.objectweb.proactive.Service;
import org.objectweb.proactive.core.util.wrapper.LongWrapper;
import org.objectweb.proactive.examples.userguide.cmagent.simple.CMAgent;


public class CMAgentInitialized extends CMAgent implements InitActive, RunActive, EndActive {
    private long lastRequestDuration;
    private long startTime;
    private long requestsServed = 0;
    public void initActivity(Body body) {
        System.out.println("### Started Active object " + body.getMBean().getName() + " on " +
            body.getMBean().getNodeUrl());
        //get start time
        startTime = System.currentTimeMillis();
    }

    public void runActivity(Body body) {
        Service service = new Service(body);
        long currentRequestDuration = 0;
        while (body.isActive()) {
            service.waitForRequest(); // block until a request is received 
            currentRequestDuration = System.currentTimeMillis();
            service.serveOldest(); //server the requests in a FIFO manner
            //calculate the total duration
            currentRequestDuration = System.currentTimeMillis() - currentRequestDuration;
            // an intermediary variable is used so 
            // when calling getLastRequestServeTime() 
            // we get the first value before the last request
            // i.e when calling getLastRequestServeTime
            // the lastRequestDuration is update with the 
            // value of the getLastRequestServeTime call 
            // AFTER the previous calculated value has been returned
            lastRequestDuration = currentRequestDuration;
            //increment the number of requests served
            requestsServed++;
        }
    }

    public void endActivity(Body body) {
    	long runningTime = System.currentTimeMillis() - startTime;
        System.out.println("### You have killed the active object. The final" +
        		" resting place is on " + body.getNodeURL() + "\n### It has faithfully served " 
        		+ requestsServed + " requests(including migrations) " +
        		"and has been an upstanding active object for " +
        		runningTime + " ms ");
    }

    public LongWrapper getLastRequestServeTime() {
        // user wrappers for primitive types
        // so the calls are asynchronous
        return new LongWrapper(lastRequestDuration);
    }

}
//@snippet-end cma_init_full
@@ -632,24 +632,25 @@
 Served = 0;%0A
+%0A
     public v
@@ -2021,17 +2021,20 @@
 ) %7B%0A    
-%09
+    
 long run
@@ -2163,27 +2163,17 @@
 final%22 +
-%0A        %09%09
+ 
 %22 restin
@@ -2185,24 +2185,36 @@
 ce is on %22 +
+%0A           
  body.getNod
@@ -2256,27 +2256,16 @@
 erved %22 
-%0A        %09%09
 + reques
@@ -2274,16 +2274,28 @@
 Served +
+%0A           
  %22 reque
@@ -2323,27 +2323,17 @@
 ons) %22 +
-%0A        %09%09
+ 
 %22and has
@@ -2386,10 +2386,12 @@
     
-%09%09
+    
 runn

format whoops