View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.mitosis.service.protocol.codec;
21  
22  
23  import java.nio.charset.CharacterCodingException;
24  import java.nio.charset.Charset;
25  import java.nio.charset.CharsetEncoder;
26  import java.util.HashSet;
27  import java.util.Set;
28  
29  import org.apache.mina.common.ByteBuffer;
30  import org.apache.directory.mitosis.common.CSN;
31  import org.apache.directory.mitosis.common.CSNVector;
32  import org.apache.directory.mitosis.service.protocol.Constants;
33  import org.apache.directory.mitosis.service.protocol.message.BaseMessage;
34  import org.apache.directory.mitosis.service.protocol.message.BeginLogEntriesAckMessage;
35  
36  
37  public class BeginLogEntriesAckMessageEncoder extends ResponseMessageEncoder
38  {
39      private final CharsetEncoder utf8encoder;
40  
41  
42      public BeginLogEntriesAckMessageEncoder()
43      {
44          utf8encoder = Charset.forName( "UTF-8" ).newEncoder();
45      }
46  
47  
48      protected void encodeBody( BaseMessage in, ByteBuffer out ) throws Exception
49      {
50          // write out response code
51          super.encodeBody( in, out );
52  
53          BeginLogEntriesAckMessage m = ( BeginLogEntriesAckMessage ) in;
54          if ( m.getResponseCode() != Constants.OK )
55          {
56              return;
57          }
58  
59          writeCSNVector( out, m.getPurgeVector() );
60          writeCSNVector( out, m.getUpdateVector() );
61      }
62  
63  
64      private void writeCSNVector( ByteBuffer out, CSNVector csns )
65      {
66          Set<String> replicaIds = csns.getReplicaIds();
67  
68          int nReplicas = replicaIds.size();
69          out.putInt( nReplicas );
70  
71          for ( String replicaId:replicaIds )
72          {
73              CSN csn = csns.getCSN( replicaId );
74              
75              try
76              {
77                  out.putString( replicaId, utf8encoder );
78                  out.put( ( byte ) 0x00 );
79                  out.putLong( csn.getTimestamp() );
80                  out.putInt( csn.getOperationSequence() );
81              }
82              catch ( CharacterCodingException e )
83              {
84                  throw new RuntimeException( e );
85              }
86          }
87      }
88  
89  
90      public Set<Class<?>> getMessageTypes()
91      {
92          Set<Class<?>> set = new HashSet<Class<?>>();
93          set.add( BeginLogEntriesAckMessage.class );
94          return set;
95      }
96  
97  }