001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2013-2015 ForgeRock AS.
026 */
027package org.opends.server.replication.protocol;
028
029import java.util.zip.DataFormatException;
030
031import org.opends.server.replication.common.ServerState;
032import org.opends.server.types.DN;
033
034/**
035 * This message is used by LDAP server when they first connect.
036 * to a replication server to let them know who they are and what is their state
037 * (their RUV)
038 */
039public class ServerStartMsg extends StartMsg
040{
041  /** Id of the LDAP server that sent this message. */
042  private final int serverId;
043  private final String serverURL;
044  private final DN baseDN;
045  private final int maxReceiveQueue;
046  private final int maxSendQueue;
047  private final int maxReceiveDelay;
048  private final int maxSendDelay;
049  private final int windowSize;
050  private final ServerState serverState;
051
052  /**
053   * The time in milliseconds between heartbeats from the replication
054   * server.  Zero means heartbeats are off.
055   */
056  private final long heartbeatInterval;
057
058  /**
059   * Whether to continue using SSL to encrypt messages after the start
060   * messages have been exchanged.
061   */
062  private final boolean sslEncryption;
063
064  /**
065   * Creates a new ServerStartMsg. This message is to be sent by an LDAP
066   * Server after being connected to a replication server for a given
067   * replication domain.
068   *
069   * @param serverId2 The serverId of the server for which the ServerStartMsg
070   *                 is created.
071   * @param serverURL directory server URL
072   * @param baseDN   The base DN.
073   * @param windowSize   The window size used by this server.
074   * @param heartbeatInterval The requested heartbeat interval.
075   * @param serverState  The state of this server.
076   * @param generationId The generationId for this server.
077   * @param sslEncryption Whether to continue using SSL to encrypt messages
078   *                      after the start messages have been exchanged.
079   * @param groupId The group id of the DS for this DN
080   */
081  public ServerStartMsg(int serverId2, String serverURL, DN baseDN,
082      int windowSize, long heartbeatInterval, ServerState serverState,
083      long generationId, boolean sslEncryption,
084      byte groupId)
085  {
086    super((short) -1 /* version set when sending */, generationId);
087
088    this.serverId = serverId2;
089    this.serverURL = serverURL;
090    this.baseDN = baseDN;
091    this.maxReceiveDelay = 0;
092    this.maxReceiveQueue = 0;
093    this.maxSendDelay = 0;
094    this.maxSendQueue = 0;
095    this.windowSize = windowSize;
096    this.heartbeatInterval = heartbeatInterval;
097    this.sslEncryption = sslEncryption;
098    this.serverState = serverState;
099    this.groupId = groupId;
100  }
101
102  /**
103   * Creates a new ServerStartMsg from its encoded form.
104   *
105   * @param in The byte array containing the encoded form of the
106   *           ServerStartMsg.
107   * @throws DataFormatException If the byte array does not contain a valid
108   *                             encoded form of the ServerStartMsg.
109   */
110  ServerStartMsg(byte[] in) throws DataFormatException
111  {
112    final ByteArrayScanner scanner = new ByteArrayScanner(in);
113    decodeHeader(scanner, MSG_TYPE_SERVER_START);
114
115    baseDN = scanner.nextDN();
116    serverId = scanner.nextIntUTF8();
117    serverURL = scanner.nextString();
118    maxReceiveDelay = scanner.nextIntUTF8();
119    maxReceiveQueue = scanner.nextIntUTF8();
120    maxSendDelay = scanner.nextIntUTF8();
121    maxSendQueue = scanner.nextIntUTF8();
122    windowSize = scanner.nextIntUTF8();
123    heartbeatInterval = scanner.nextIntUTF8();
124    sslEncryption = Boolean.valueOf(scanner.nextString());
125    serverState = scanner.nextServerStateMustComeLast();
126  }
127
128  /**
129   * Get the ServerID from the message.
130   * @return the server ID
131   */
132  public int getServerId()
133  {
134    return serverId;
135  }
136
137  /**
138   * Get the Server URL from the message.
139   * @return the server URL
140   */
141  public String getServerURL()
142  {
143    return serverURL;
144  }
145
146  /**
147   * Get the baseDN.
148   *
149   * @return Returns the baseDN.
150   */
151  public DN getBaseDN()
152  {
153    return baseDN;
154  }
155
156  /**
157   * Get the maxReceiveDelay.
158   * @return Returns the maxReceiveDelay.
159   */
160  public int getMaxReceiveDelay()
161  {
162    return maxReceiveDelay;
163  }
164
165  /**
166   * Get the maxReceiveQueue.
167   * @return Returns the maxReceiveQueue.
168   */
169  public int getMaxReceiveQueue()
170  {
171    return maxReceiveQueue;
172  }
173
174  /**
175   * Get the maxSendDelay.
176   * @return Returns the maxSendDelay.
177   */
178  public int getMaxSendDelay()
179  {
180    return maxSendDelay;
181  }
182
183  /**
184   * Get the maxSendQueue.
185   * @return Returns the maxSendQueue.
186   */
187  public int getMaxSendQueue()
188  {
189    return maxSendQueue;
190  }
191
192  /**
193   * Get the ServerState.
194   * @return The ServerState.
195   */
196  public ServerState getServerState()
197  {
198    return serverState;
199  }
200
201  /** {@inheritDoc} */
202  @Override
203  public byte[] getBytes(short protocolVersion)
204  {
205    final ByteArrayBuilder builder = new ByteArrayBuilder();
206    encodeHeader(MSG_TYPE_SERVER_START, builder, protocolVersion);
207
208    builder.appendDN(baseDN);
209    builder.appendIntUTF8(serverId);
210    builder.appendString(serverURL);
211    builder.appendIntUTF8(maxReceiveDelay);
212    builder.appendIntUTF8(maxReceiveQueue);
213    builder.appendIntUTF8(maxSendDelay);
214    builder.appendIntUTF8(maxSendQueue);
215    builder.appendIntUTF8(windowSize);
216    builder.appendLongUTF8(heartbeatInterval);
217    builder.appendString(Boolean.toString(sslEncryption));
218    builder.appendServerStateMustComeLast(serverState);
219    return builder.toByteArray();
220  }
221
222  /**
223   * Get the window size for the ldap server that created the message.
224   *
225   * @return The window size for the ldap server that created the message.
226   */
227  public int getWindowSize()
228  {
229    return windowSize;
230  }
231
232  /**
233   * Get the heartbeat interval requested by the ldap server that created the
234   * message.
235   *
236   * @return The heartbeat interval requested by the ldap server that created
237   * the message.
238   */
239  public long getHeartbeatInterval()
240  {
241    return heartbeatInterval;
242  }
243
244  /**
245   * Get the SSL encryption value for the ldap server that created the
246   * message.
247   *
248   * @return The SSL encryption value for the ldap server that created the
249   *         message.
250   */
251  public boolean getSSLEncryption()
252  {
253    return sslEncryption;
254  }
255
256  /** {@inheritDoc} */
257  @Override
258  public String toString()
259  {
260    return "ServerStartMsg content: " +
261      "\nprotocolVersion: " + protocolVersion +
262      "\ngenerationId: " + generationId +
263      "\ngroupId: " + groupId +
264      "\nbaseDN: " + baseDN +
265      "\nheartbeatInterval: " + heartbeatInterval +
266      "\nmaxReceiveDelay: " + maxReceiveDelay +
267      "\nmaxReceiveQueue: " + maxReceiveQueue +
268      "\nmaxSendDelay: " + maxSendDelay +
269      "\nmaxSendQueue: " + maxSendQueue +
270      "\nserverId: " + serverId +
271      "\nserverState: " + serverState +
272      "\nserverURL: " + serverURL +
273      "\nsslEncryption: " + sslEncryption +
274      "\nwindowSize: " + windowSize;
275  }
276}