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