package org.postgresql.coreDriver;

import org.postgresql.PG_Stream;
import java.io.IOException;
import org.postgresql.util.FailOverException;

/**
 * Sent to the backend to initialize a newly created connection.
 *
 * $Id: StartupPacket.java,v 1.1 2005/03/02 16:07:32 jsalas Exp $
 */

public class StartupPacket
{
	private static final int SM_DATABASE = 64;
	private static final int SM_USER = 32;
	private static final int SM_OPTIONS = 64;
	private static final int SM_UNUSED = 64;
	private static final int SM_TTY = 64;

	private int protocolMajor;
	private int protocolMinor;
	private String user;
	private String database;
    // Middleware
    private int idConnection;


        // Middleware : Add idConnection
	public StartupPacket(int protocolMajor, int protocolMinor, String user, String database, int idConnection)
	{
		this.protocolMajor = protocolMajor;
		this.protocolMinor = protocolMinor;
		this.user = user;
		this.database = database;
        this.idConnection = idConnection;
	}

	public void writeTo(PG_Stream stream) throws FailOverException
        {
          stream.SendInteger(4 + 4 + SM_DATABASE + SM_USER + SM_OPTIONS + SM_UNUSED +
                             SM_TTY, 4);
          stream.SendInteger(protocolMajor, 2);
          stream.SendInteger(protocolMinor, 2);
          // Middleware
          //System.out.println("idConnection = " + idConnection);
          stream.SendInteger(idConnection, 2);
          // Middleware
          //stream.Send(database.getBytes(), SM_DATABASE);

          // This last send includes the unused fields
          stream.Send(user.getBytes(), SM_USER + SM_OPTIONS + SM_UNUSED + SM_TTY);
	}
}
