/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            multicast_configuration.cc
 *
 *  Wed Oct 12 10:12:11 CEST 2005
 *  Copyright  2005 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of MIaV.
 *
 *  MIaV 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; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  MIaV 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 General Public License
 *  along with MIaV; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "config.h"
#include "multicast_configuration.h"

MulticastConfiguration::MulticastConfiguration(Info *info, char *file)
  : MiavConfig(file)
{
  mcastconf_t conf;

  // Create the default values.
  global_conf.addr = "224.0.0.1";
  global_conf.port = 1234;
  global_conf.enabled = false;
  global_conf.with_audio = false;

  bool global = true;

  _cfg *cfg = configs;

  // Build the client list
  while(cfg) {
    if(strcmp(cfg->name->c_str(), "client") == 0) {
      if(!global) confs.push_back(conf);

      // Reset the configuration to the defaults
      conf.client = *(cfg->stringval);
      conf.addr = global_conf.addr;
      conf.port = global_conf.port;
      conf.enabled = global_conf.enabled;
      conf.with_audio = global_conf.with_audio;

      global = false;
    }
    if(strcmp(cfg->name->c_str(), "address") == 0) {
      if(global) global_conf.addr = *(cfg->stringval);
      else conf.addr = *(cfg->stringval);
    }
    if(strcmp(cfg->name->c_str(), "port") == 0) {
      if(global) global_conf.port = cfg->intval;
      else conf.port = cfg->intval;
    }
    if(strcmp(cfg->name->c_str(), "enabled") == 0) {
      if(global) global_conf.enabled = cfg->boolval;
      else conf.enabled = cfg->boolval;
    }
    if(strcmp(cfg->name->c_str(), "with_audio") == 0) {
      if(global) global_conf.with_audio = cfg->boolval;
      else conf.with_audio = cfg->boolval;
    }
    cfg = cfg->next;
  }
  if(!global) confs.push_back(conf);

  // Show the configuration in the log file  .
  info->info("Global - Enabled: %s - Addr: %s - Port: %d - WithAudio: %s", 
             global_conf.enabled?"Yes\0":"No\0",
             global_conf.addr.c_str(),
             global_conf.port,
             global_conf.with_audio?"Yes\0":"No\0");

  for(unsigned int cnt = 0; cnt < confs.size(); cnt++) {
    info->info("Client: %s - Enabled: %s - Addr: %s - Port: %d - WithAudio: %s", 
               confs[cnt].client.c_str(),
               confs[cnt].enabled?"Yes\0":"No\0",
               confs[cnt].addr.c_str(),
               confs[cnt].port,
               confs[cnt].with_audio?"Yes\0":"No\0");
  }

  info->info("Chosing:");
}

MulticastConfiguration::~MulticastConfiguration()
{
}

mcastconf_t &MulticastConfiguration::getConf(char *client)
{
  for(unsigned int cnt = 0; cnt < confs.size(); cnt++) {
    if(strcmp(confs[cnt].client.c_str(), client) == 0)
      return confs[cnt];
  }
 
  return global_conf;
}

#ifdef __TEST_MULTICAST_CONFIGURATION
#include "info_simple.h"

int main(int argc, char *argv[]) {
  if(argc < 2) {
    fprintf(stderr, "usage:\n\t%s [filename]\n", argv[0]);
    return 1;
  }

  InfoSimple info;
  MulticastConfiguration conf(&info, argv[1]);

  mcastconf_t mcconf = conf.getConf("192.168.0.11");

  info.warn("Client: %s - Enabled: %s - Addr: %s - Port: %d", 
            mcconf.client.c_str(),
            mcconf.enabled?"Yes\0":"No\0",
            mcconf.addr.c_str(),
            mcconf.port);

  mcconf = conf.getConf("192.168.0.0");

  info.warn("Client: %s - Enabled: %s - Addr: %s - Port: %d", 
            mcconf.client.c_str(),
            mcconf.enabled?"Yes\0":"No\0",
            mcconf.addr.c_str(),
            mcconf.port);
}

#endif/* __TEST_MULTICAST_CONFIGURATION*/