/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            server.cc
 *
 *  Mon Nov  8 11:35:01 CET 2004
 *  Copyright  2004 Bent Bisballe
 *  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.
 */

/*
 * $Id$
 */

/*
 * $Log$
 * Revision 1.25  2005/06/19 20:04:43  deva
 * ImgEncoder now uses the file class for output, through jpeg_mem_dest.
 *
 * Revision 1.24  2005/06/19 11:44:14  deva
 * Cleaned up a log of logging.
 * Fixed server queue (shouldn't happen).
 * Added user and group lookup.
 *
 * Revision 1.23  2005/06/14 18:58:35  deva
 * *** empty log message ***
 *
 * Revision 1.22  2005/06/14 12:29:40  deva
 * Incorporated the use of the Info object everywhere... also using the log functionality.
 *
 * Revision 1.21  2005/05/26 21:32:39  deva
 * *** empty log message ***
 *
 * Revision 1.20  2005/05/26 12:48:36  deva
 * *** empty log message ***
 *
 * Revision 1.19  2005/05/22 16:34:44  deva
 * Fix: Connection is now taken down when taking a sanpshot without recording.
 *
 * Revision 1.18  2005/05/22 15:49:22  deva
 * Added multithreaded encoding support.
 *
 * Revision 1.17  2005/05/17 19:16:26  deva
 * Made new mpeg writer work, with proper file permissions.
 *
 * Revision 1.16  2005/05/17 15:12:51  deva
 * Fixed file rights (All read on files and directories, and all execute on directories).
 *
 * Revision 1.15  2005/05/17 14:30:56  deva
 * Added code, preparing threaded encoding.
 *
 * Revision 1.14  2005/05/16 16:00:57  deva
 * Lots of stuff!
 *
 * Revision 1.13  2005/05/09 16:40:20  deva
 * Added optimize yuv conversion code
 *
 * Revision 1.12  2005/05/07 10:56:18  deva
 * Changed print outs
 *
 * Revision 1.11  2005/05/03 08:31:59  deva
 * Removed the error object, and replaced it with a more generic info object.
 *
 * Revision 1.10  2005/05/02 19:47:43  deva
 * Fixed overlapping cpr numbers on the server (now it saves one cpr pr.
 * connection, and ignores any changes sent)
 *
 * Revision 1.9  2005/05/02 18:46:15  deva
 * Files are now saved in a custom folder (defined in miav.conf)
 *
 * Revision 1.8  2005/05/01 09:56:26  deva
 * Added Id and Log tags to all files
 */

#include "server.h"
#include "miav.h"

#include <stdio.h>
#include <stdlib.h>

// For mkdir
#include <sys/stat.h>
#include <sys/types.h>

// For unlink
#include <unistd.h>

// For errno
#include <errno.h>

// For inet_ntoa
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "miav_config.h"

#include "mov_encoder_thread.h"
#include "img_encoder.h"

#include "server_status.h"

#include "dv.h"

void newConnection(Socket *socket, Info *info)
{
  char cpr[256];
  bool hasCpr = false;
  ServerStatus status(info);

  n_savestate savestate = LATER;
  n_header h;
  Frame *frame;
  Frame *freeze_frame = NULL;
  MovEncoderThread *enc = NULL;

  frame = new Frame(NULL, DVPACKAGE_SIZE);

  info->info("New connection (%s)", inet_ntoa(socket->socketaddr.sin_addr));

  Network network = Network(socket, info);
  while(int ret = network.recvPackage(&h, frame->data, frame->size)) {

    status.checkPoint();
    
    if(ret == -1) {
      info->error("A network error ocurred, terminating session");
      break;
    }

    if(!hasCpr) {
      sprintf(cpr, h.header.h_data.cpr);
      hasCpr = true;
    }
    
    //    printf("Read: %d bytes ", ret);
    //    printf("typ: %d ", h.header_type);
//    fprintf(stdout, "cpr: %s ", cpr);
//    fprintf(stdout, "frz: %d ", h.header.h_data.freeze);
//    fprintf(stdout, "sht: %d ", h.header.h_data.snapshot);
//    fprintf(stdout, "save: %d ", h.header.h_data.savestate);
//    fflush(stdout);
    
    if(h.header.h_data.snapshot) {
      if(freeze_frame) {
        ImgEncoder(cpr, info).encode(freeze_frame, 100);
        delete freeze_frame;
        freeze_frame = NULL;
      } else {
        ImgEncoder(cpr, info).encode(frame, 100);
      }
    }

    if(h.header.h_data.savestate) {
      savestate = h.header.h_data.savestate;
    }

    if(h.header.h_data.freeze) {
      if(freeze_frame) delete freeze_frame;
      // copy the frame into another temporary one.
      freeze_frame = new Frame(frame->data, frame->size);
    }

    // This one must be last!
    if(h.header.h_data.record) {
      //      if(!enc) enc = newMovEncoder(cpr);
      if(!enc) enc = new MovEncoderThread(cpr, info);
      enc->encode(frame);
    }

    frame = new Frame(NULL, DVPACKAGE_SIZE);
  }

  // TODO: Use save state

  info->info("Closing connection...");

  if(enc) delete enc;

  info->info("Connection closed");
  
}