1 7 package org.jboss.test.remoting.performance.synchronous; 8 9 import org.jboss.remoting.callback.Callback; 10 import org.jboss.remoting.callback.HandleCallbackException; 11 import org.jboss.remoting.callback.InvokerCallbackHandler; 12 13 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt; 14 15 18 public class CallTracker 19 { 20 private String clientSessionId; 21 private int totalCount = 0; 22 private SynchronizedInt duplicateCount = new SynchronizedInt(0); 23 private SynchronizedInt receivedCount = new SynchronizedInt(0); 24 25 private boolean[] counterArray = null; 26 27 private InvokerCallbackHandler callbackHandler; 28 29 public CallTracker(String sessionId, InvokerCallbackHandler callbackHandler) 30 { 31 this.clientSessionId = sessionId; 32 this.callbackHandler = callbackHandler; 33 } 34 35 public synchronized void createTotalCount(int totalCount) 36 { 37 counterArray = new boolean[totalCount + 1]; 38 this.totalCount = totalCount; 39 receivedCount.set(0); 40 duplicateCount.set(0); 41 } 42 43 public void verifyClientInvokeCount(int clientInvokeCallCount) 44 { 45 boolean duplicate = addToReceivedCount(clientInvokeCallCount); 46 if(duplicate) 47 { 48 duplicateCount.increment(); 49 } 50 else 51 { 52 receivedCount.increment(); 53 } 54 55 int currentDuplicateCount = duplicateCount.get(); 56 int currentReceivedCount = receivedCount.get(); 57 if((currentReceivedCount % 100) == 0) 58 { 59 System.out.println(clientSessionId + " -- Received count: " + currentReceivedCount); 60 System.out.println(clientSessionId + " -- Duplicate count: " + currentDuplicateCount); 61 System.out.println(clientSessionId + " -- Total count: " + totalCount); 62 } 63 if((currentReceivedCount + currentDuplicateCount) == totalCount) 64 { 65 System.out.println("\n\n*****************************\n" + 66 " Test Finished\n" + 67 "*****************************\n" + 68 " " + clientSessionId + " -- Received Count = " + currentReceivedCount + "\n" + 69 " " + clientSessionId + " -- Duplicate Count = " + currentDuplicateCount + "\n" + 70 "*****************************\n\n"); 71 72 if(callbackHandler != null) 74 { 75 Callback callback = new Callback(new Integer (currentReceivedCount + currentDuplicateCount)); 76 try 77 { 78 callbackHandler.handleCallback(callback); 79 } 80 catch(HandleCallbackException e) 81 { 82 e.printStackTrace(); 83 } 84 } 85 } 86 87 } 88 89 private synchronized boolean addToReceivedCount(int localClientInvokeCount) 90 { 91 boolean isDuplicate = false; 92 if(counterArray == null) 93 { 94 System.out.println("Error! Have not received invoke for method 'totalCallCount', so can not process count."); 95 throw new RuntimeException ("Error! Have not received invoke for method 'totalCallCount', so can not process count."); 96 } 97 else 98 { 99 try 100 { 101 isDuplicate = counterArray[localClientInvokeCount]; 102 if(!isDuplicate) 103 { 104 counterArray[localClientInvokeCount] = true; 105 } 106 } 107 catch(ArrayIndexOutOfBoundsException e) 108 { 109 System.err.println("Got ArrayIndexOutOfBoundsException"); 110 System.err.println("Counter array size = " + counterArray.length); 111 System.err.println("Received count = " + localClientInvokeCount); 112 e.printStackTrace(); 113 } 114 } 115 return isDuplicate; 116 } 117 118 } | Popular Tags |