HeartbeatDiagnostics.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.client.protocol;
022 
023 class HeartbeatDiagnostics
024 {
025     private static final Diagnostics _impl = init();
026 
027     private static Diagnostics init()
028     {
029         return Boolean.getBoolean("amqj.heartbeat.diagnostics"new On() new Off();
030     }
031 
032     static void sent()
033     {
034         _impl.sent();
035     }
036 
037     static void timeout()
038     {
039         _impl.timeout();
040     }
041 
042     static void received(boolean heartbeat)
043     {
044         _impl.received(heartbeat);
045     }
046 
047     static void init(int delay, int timeout)
048     {
049         _impl.init(delay, timeout);
050     }
051 
052     private static interface Diagnostics
053     {
054         void sent();
055         void timeout();
056         void received(boolean heartbeat);
057         void init(int delay, int timeout);
058     }
059 
060     private static class On implements Diagnostics
061     {
062         private final String[] messages = new String[50];
063         private int i;
064 
065         private void save(String msg)
066         {
067             messages[i++= msg;
068             if(i >= messages.length){
069                 i = 0;//i.e. a circular buffer
070             }
071         }
072 
073         public void sent()
074         {
075             save(System.currentTimeMillis() ": sent heartbeat");
076         }
077 
078         public void timeout()
079         {
080             for(int i = 0; i < messages.length; i++)
081             {
082                 if(messages[i!= null)
083                 {
084                     System.out.println(messages[i]);
085                 }
086             }
087             System.out.println(System.currentTimeMillis() ": timed out");
088         }
089 
090         public void received(boolean heartbeat)
091         {
092             save(System.currentTimeMillis() ": received " (heartbeat ? "heartbeat" "data"));
093         }
094 
095         public void init(int delay, int timeout)
096         {
097             System.out.println(System.currentTimeMillis() ": initialised delay=" + delay + ", timeout=" + timeout);
098         }
099     }
100 
101     private static class Off implements Diagnostics
102     {
103         public void sent()
104         {
105 
106         }
107         public void timeout()
108         {
109 
110         }
111         public void received(boolean heartbeat)
112         {
113 
114         }
115 
116         public void init(int delay, int timeout)
117         {
118 
119         }
120     }
121 }