/******************************************************************************
In the readData() we are simply inputting the "xintput.txt" file in a vector. 
we create a object for Scanner class and use a While loop to continuously scan
for text in the file until it has nothing else in it. While the scanner is 
reading in the values this data is now pushed back into a vector called TheData.
We now have a vector that consist of 100 values of stock and it now received.


****/
void MovingAverage::readData(std::string fileName)
{
Scanner inScanner;
double value ;
inScanner.openFile(fileName);
std::cout << "Now receiving Stocks" << std::endl;

  while(inScanner.hasNext())
  {
    value = inScanner.nextDouble(); 
    theData.push_back(value);  
  }
  
 
}
/******************************************************************************
 * Function 'runForever' 
 * This function will do most of the programs work. It will take in all the packets
 * (which realistically, should make this a continuously running function) and call
 * the function for them to be read. It will take these read packets and add them to
 * messages and then add these messages to the  MessagesMap. It will test if a message
 * has been completed upon the addition of a packet and call the messageDump function 
 * to remove and output a message if it is done. In the scale of this program, this function
 * runs until all the messages are complete and there are no more packets to take in.
 *
 * Parameters:
 * scanner - the 'Scanner' from which to read 
 * outStream - the stream to which to write
 *
 * Returns: None
 *
 * Output: Adds each completed message to the OutStream. 
**/
void PacketAssembler::runForever(Scanner& scanner, ofstream& outStream)
{
#ifdef EBUG
  Utils::logStream << "enter runForever\n"; 
#endif

  Packet tempPacket; 
  int tempMessageID; 
  
  //run as long as there are more packets to take in. 
  while(scanner.hasNext()) 
  {
    //create a temporary packet and call the read from the Packet class to assign it values
    tempPacket = Packet(scanner);
    tempMessageID = tempPacket.getMessageID(); 
    //using this packets MessageID, check if we already have this message
    if (messagesMapContains(tempMessageID) == false)
    {
      //if not create a new Message and push it into the messagesMap
      Message tempMessage;
      tempMessage.insert(tempPacket); 
      messagesMap.insert(std::pair<int, Message>(tempMessageID, tempMessage));   
    } 
   
    //otherwise, this message is in the Messages map and we should just add the new
    //packet to the message already in the messages map. 
    else 
    {
      if(messagesMapContains(tempMessageID))
      {
        messagesMap[tempMessageID].insert(tempPacket); 
      }
    }  
  
    //after adding each packet, we should check if that packet completed a message
    if (messagesMap[tempMessageID].isComplete() == true)
    {
      //if it did, dump it and outStream the data. 
      outStream << "The Completed message is: ";
      outStream << "\n";  
      outStream << dumpMessage(tempMessageID); 
    }
  } 

#ifdef EBUG
  Utils::logStream << "leave runForever\n"; 
#endif
}