Esempio n. 1
0
std::string Customer::toString() const {
    char data[4096];

    std::string str;

    sprintf(data,"Cust %s)  SetupTime=%.1f   Cap=%-6.0f  IniQty=%-6.0f   Safety=%-6.0f",
            getIndexStr().c_str(),getSetupTime(),capacity_,initialQuantity_,safetyLevel_);

    sprintf(data,"%s Trailers=[",data);
    bool first=true;
    for(Trailer* t: allowedTrailers_){
        sprintf(data,"%s%s%d",data,(first?"":"-"),t->getIndex());
        first = false;
    }
    sprintf(data,"%s]",data);


    int tam = (int)forecast_.size();
    int n = 0, day = 0;
    double dailyAmount;
    sprintf(data,"%s\n         ",data);
    sprintf(data,"%s  Forecast=[",data);
    for(double d: forecast_){
        dailyAmount += d;
        if( n % 24 == 23 ) {
            sprintf(data,"%s%6.0f%s",data,dailyAmount,(n==tam-1?"":";"));
            if( (n/24)%10 == 9 && n < tam-1 )
                sprintf(data,"%s\n                     ",data);
            dailyAmount = 0.0;
        }
        n++;
    }
    sprintf(data,"%s]",data);

    str = data;
    return str;
}
Esempio n. 2
0
void
DServerCmdWrite::run()
{ 
#if DS_CMD_WRITE_DEBUG
  cout << "DServerCmdWrite::run" << getIndexStr() << endl;
#endif

  if (!checkWorkerAvailable())
    return;

  if (!startWorkerStreamCommunication())
    {
      setErrorText("Could not initiate communication!");
      return;
    }

  int algID,typID;
  arrayalgebra::extractIds(getWorker() -> getTType(),algID,typID);
  TypeConstructor* tc = am->GetTC(algID,typID);

  while (nextIndex())
    {
      const int curIdx = getIndex();

      string master_port =int2Str((1800+curIdx));
          
      string sendCmd = 
        "let r" + getWorker() -> getName() + int2Str(curIdx) +
        " = " + "receiveD(" + getWorker() -> getMasterHostIP_() + 
        ",p" + master_port + ")";

#if DS_CMD_WRITE_DEBUG
      cout << "Sending:" << sendCmd << endl;
#endif
      //The sendD-operator on the worker is started 
      if (!sendSecondoCmdToWorkerSOS(sendCmd, true))
        {
          string errMsg;
          if (hasCmdError())
            errMsg = getCmdErrorText();
          else
            errMsg = "Could not write data from worker!";

          setErrorText(errMsg);
        
          waitForSecondoResultThreaded();
          return;
        }

      // open communication with receiveD - TypeMap
      DServerCmdCallBackCommunication callBack(getWorker() ->getMasterHostIP(),
                                               master_port);
      callBack.startSocket();
      callBack.startSocketCommunication();

      // send TYPE to receiveD - TypeMap
      if (!callBack.sendTextToCallBack("TYPE", getWorker() -> getTTypeStr()))
        {;
          waitForSecondoResultThreaded();
          return;
        }

      // await "CLOSE" tag
      if (!callBack.getTagFromCallBack("CLOSE"))
        {
          waitForSecondoResultThreaded();
          return;
        }
      // stop communication w/ reveive TypeMap
      callBack.closeSocketCommunication();

      // type map has finished

      //The callback connection from the value-mapping 
      // is opened and stored
      if (!(callBack.startSocketCommunication()))
        {
          setErrorText(callBack.getErrorText());
          waitForSecondoResultThreaded();
          return;
        }
      
      // send TYPE to receiveD - TypeMap
      if (!callBack.sendTextToCallBack("TYPE", getWorker() -> getTTypeStr()))
        {
          setErrorText(callBack.getErrorText());
          waitForSecondoResultThreaded();
          return;
        }

      if (!callBack.getTagFromCallBack("GOTTYPE"))
        { 
          setErrorText(callBack.getErrorText());
          waitForSecondoResultThreaded();
          return;
        }

      //The element is converted into a binary stream of data
      SmiRecordFile recF(false,0);
      SmiRecord rec;
      SmiRecordId recID;
      Cmd_Mutex.acquire();       
      recF.Open("send");
      recF.AppendRecord(recID,rec);
      size_t size = 0;
      am->SaveObj(algID,typID,rec,size,getWorker() -> getTType(),
                  (*(getInElements()))[curIdx]);
      char* buffer = new char[size]; 
      rec.Read(buffer,size,0);
      //rec.Truncate(3);
      recF.DeleteRecord(recID);
      recF.Close();
      Cmd_Mutex.release();

      //Size of the binary data is sent
      if (!callBack.sendTextToCallBack("SIZE", size))
        { 
          waitForSecondoResultThreaded();
          return;
        }

#if DS_CMD_WRITE_DEBUG
      cout << "Send Size:" << size << endl;
#endif

      //The actual data are sent
      if (!callBack.Write(buffer,size))
        { 
          waitForSecondoResultThreaded();
          return;
        }
 
      delete [] buffer ;

      Attribute* a;
      if(tc->NumOfFLOBs() > 0 ) 
        a = static_cast<Attribute*>((am->Cast(algID,typID))
                (((*(getInElements()))[curIdx]).addr));
         
      //Flobs are sent to worker
      for(int i = 0; i < tc->NumOfFLOBs(); i++)
        {
          //send FLOB Tag as info
          if (!callBack.sendTagToCallBack("FLOB"))
            { 
              waitForSecondoResultThreaded();
              return;
            }

          Flob* f = a->GetFLOB(i);
         
          //Flob is converted to binary data
          SmiSize si = f->getSize();
          int n_blocks = si / 1024 + 1;
          char* buf = new char[n_blocks*1024];
          memset(buf,0,1024*n_blocks);
         
          f->read(buf,si,0);
 
#if DS_CMD_WRITE_DEBUG
          cout << "Send Flob - Size:" << si << endl;
#endif
          //Size of the binary data is sent
          if (!callBack.sendTextToCallBack("FLOBSIZE", si))
            { 
              waitForSecondoResultThreaded();
              return;
            }

         
          //Flob data is sent
          for(int j = 0; j<n_blocks;j++)
            callBack.Write(buf+j*1024,1024);
            
          delete [] buf;

          //send FLOB Tag as info
          bool noErr = true;
          if (!callBack.getTagFromCallBackTF("GOTFLOB", "ERROR", noErr))
            { 
              waitForSecondoResultThreaded();
              return;
            }

          if (!noErr)
            { 
              waitForSecondoResultThreaded();
              return;
            }
        } //
         

      if (!callBack.sendTagToCallBack("CLOSE"))
        { 
          waitForSecondoResultThreaded();
          return;
        }

      if (!callBack.getTagFromCallBack("FINISH")) 
        { 
          waitForSecondoResultThreaded();
          return;
        }
              
      callBack.closeCallBackCommunication();

      if (!waitForSecondoResultThreaded())
        {
          string errMsg;
          if (hasCmdError())
            errMsg = getCmdErrorText();
          else
            errMsg = "Could not write data to worker!";
       
          setErrorText(errMsg);
        }
    } // while(nextIndex())

  if (!closeWorkerStreamCommunication())
    {
      setErrorText("Could not stop communication!");
      return;
    }

#if DS_CMD_WRITE_DEBUG
  cout << "DServerCmdWrite::run DONE" << endl;
#endif
} // run()