//
// Copyright 2003, Juan Segarra Flor
//
// This is part of videocache
//
// Videocache 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.
//
// Videocache 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 Foobar; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <errno.h>

#include "cachetree.h"
#include "config.h"

#define VIDPATH "traces/movie%ld.trc"

inline extern double weightfunction(double popularity,int duration,long rate);
extern long sizefunction(int treedepth,int level);
extern void drawpoints(long max);
extern long storedparts[TREE_DEPTH]; // number of parts stored in each level
extern long storedspace[TREE_DEPTH]; // number of space stored in each level

typedef struct place {
  place *next;
  int nodeid;
} t_place;

typedef struct videopart {
  double popularity;
  int duration; // sec
  long rate; // kbps
  double weight;
  t_place *location;
  videopart *nextpart; // next part in a video
  videopart *nextvideo; // next video in structure
  videopart *nextweight; // next video sorted by weight
} t_videopart;

class videos {
  long maxvideos;
  long meanrate;    // kbps
  int meanduration; // sec
  //int meanpartduration; //sec
  double meanparts;    // parts
  t_videopart *videolist;
  t_videopart *weightedlist;
  t_videopart *readvideoinfo(char* filename,double pop);
  void sortvideos();
  t_videopart *getvideopart(int video,int part);
  void addlevel(t_videopart *video,int level,cachetree &tree);
  int getpartsize(t_videopart *part); // return its size in Mb
 public:
  videos(long numvideos);
  void printvideos();
  int distribute(cachetree &tree);
  int getvideobypopularity(double pop);
  int getnumparts(int video);
  int *getlocation(int video,int part);
  int getduration(int video,int part);
  long getrate(int video,int part); // kbps
  double getprobofrequest(int video); //]0,1]

  long getmeanrate();
  int getmeanduration();
  double getmeanparts();
  // redistribution
  int getpartsize(int video,int part){
    return getpartsize(getvideopart(video,part));}; // return its size in Mb
  void getlastvideopartinlevel(int level,cachetree &tree,
			       int&lastvideo,int&lastpart);
};
