View Javadoc

1   // ========================================================================
2   // Copyright 2006 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // Licensed under the Apache License, Version 2.0 (the "License");
5   // you may not use this file except in compliance with the License.
6   // You may obtain a copy of the License at 
7   // http://www.apache.org/licenses/LICENSE-2.0
8   // Unless required by applicable law or agreed to in writing, software
9   // distributed under the License is distributed on an "AS IS" BASIS,
10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  // See the License for the specific language governing permissions and
12  // limitations under the License.
13  // ========================================================================
14  
15  package org.mortbay.cometd.ext;
16  
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import org.cometd.Bayeux;
21  import org.cometd.Client;
22  import org.cometd.Extension;
23  import org.cometd.Message;
24  
25  
26  public class TimesyncExtension implements Extension
27  {
28      public TimesyncExtension()
29      {
30      }
31      
32      public Message rcv(Client from, Message message)
33      {
34          return message;
35      }
36  
37      public Message rcvMeta(Client from, Message message)
38      {
39          Map<String,Object> ext=(Map<String,Object>)message.get(Bayeux.EXT_FIELD);
40          if (ext!=null)
41          {
42              Map<String,Object> sync=(Map<String,Object>)ext.get("timesync");
43              if (sync!=null)
44                  sync.put("ts",new Long(System.currentTimeMillis()));
45          }
46          return message;
47      }
48  
49      public Message send(Client from, Message message)
50      {
51          return message;
52      }
53  
54      public Message sendMeta(Client from, Message message)
55      {
56          Message associated = message.getAssociated();
57          if (associated!=null)
58          {
59              Map<String,Object> extIn=associated.getExt(false);
60              
61              if (extIn!=null)
62              {
63                  Map<String,Object> sync=(Map<String,Object>)extIn.get("timesync");
64  
65                  if (sync!=null)
66                  {
67                      final long ts=((Long)sync.get("ts")).longValue();
68                      final long p=System.currentTimeMillis()-ts;
69                      sync.put("p",new Long(p));
70                      
71                      Map<String,Object> extOut=(Map<String,Object>)message.getExt(true);
72                      if (extOut==null)
73                      {
74                          extOut=new HashMap<String, Object>();
75                          message.put(Bayeux.EXT_FIELD,extOut);
76                      }
77                      extOut.put("timesync",sync);
78                  }
79              }
80          }
81          return message;
82      }
83  }