/* JFlexiPix - Java implementation of FlexiPix output library
 *
 * Copyright 2010-2011 Stefan Schuermans <stefan schuermans info>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.blinkenarea.JFlexiPix;

import java.net.*;
import java.util.Arrays;

/// FlexiPix distributor
class Distri
{
  /**
   * @brief constructor
   * @param[in] distri number of this distributor
   * @param[in] outputCnt number of outputs
   * @param[in] pixelCnt number pixels connected to every output
   */
  Distri(int distri, int outputCnt, int pixelCnt)
  {
    // save constructor parameters
    m_distri    = distri;
    m_outputCnt = outputCnt;
    m_pixelCnt  = pixelCnt;

    // initialize address to default
    try {
      int intIp = Constants.destIpBase +
                  m_distri * Constants.destIpStep;
      byte [] byteIp = {
        (byte)(intIp >> 24),
        (byte)(intIp >> 16),
        (byte)(intIp >>  8),
        (byte)intIp
      };
      m_addr = new InetSocketAddress(InetAddress.getByAddress(byteIp),
                                     Constants.destPort);
    } catch (java.net.UnknownHostException e) {
      m_addr = null;
    } catch (java.io.IOException e) {
      m_addr = null;
    }

    // allocate default mappings
    m_mapRed   = new Mapping();
    m_mapGreen = new Mapping();
    m_mapBlue  = new Mapping();

    // allocate pixel array (no pixels configured yet)
    m_pixels = new Pixel [m_outputCnt * m_pixelCnt];

    // allocate and initialize message buffer
    m_msgBuf = Arrays.copyOf(Constants.mcufHdr,
                             Constants.mcufHdr.length +
                             m_outputCnt * m_pixelCnt * 3);
    m_msgBuf[Constants.mcufOfsOutputs] = (byte)m_outputCnt;
    m_msgBuf[Constants.mcufOfsPixels]  = (byte)m_pixelCnt;
  }

  int               m_distri;    ///< number of this distributor
  int               m_outputCnt; ///< number of outputs
  int               m_pixelCnt;  ///< number pixels connected to every output
  InetSocketAddress m_addr;      ///< network address of distributor
  Mapping           m_mapRed;    ///< mapping information for red channel
  Mapping           m_mapGreen;  ///< mapping information for Green channel
  Mapping           m_mapBlue;   ///< mapping information for blue channel
  Pixel []          m_pixels;    /**< information about pixels of this
                                      distributor,
                                      index = output * m_pixelCnt + pixel */
  byte []           m_msgBuf;    /**< buffer for current message to send to
                                      distributor */
}

