void Controler::_drawSlot()
{
  int size, val;
  QString listString;

  if(!_seeded)
    {
      ::srand( ::time(NULL) );
      _seeded = true;
    }

  size = _ui->listSizeSpinBox->value();

  _numbers->clear();
  listString = "";

  for(int i = 0; i < size; ++i)
    {
      val = ::rand() % 100;
      _numbers->push_back(val);
      listString += QString::number(val);
      if(i < size-1)
	listString += ", ";
    }
  
  _ui->initialListLineEdit->setText( listString );

  _updateStatus("Random numbers drawn");

}
bool PosixDirectory::read(Attributes* entry)
{
   if (_status != Open)
      return false;

   struct dirent* de = readdir(_handle);
   
   if (!de)
   {
      _status = EndOfFile;
      return false;
   }

   // Skip "." and ".." entries
   if (de->d_name[0] == '.' && (de->d_name[1] == '\0' ||
      (de->d_name[1] == '.' && de->d_name[2] == '\0')))
      return read(entry);

   // The dirent structure doesn't actually return much beside
   // the name, so we must call stat for more info.
   struct stat info;
   String file = _name + "/" + de->d_name;
   
   int error = stat(file.c_str(),&info);
   
   if (error < 0)
   {
      _updateStatus();
      return false;
   }
   copyStatAttributes(info,entry);
   entry->name = de->d_name;
   return true;
}
void Controler::_sigSlot()
{
  _updateStatus("Signal notification received");

  _sigNotifier->setEnabled(false);

  if( ::read(_sigFd[0], &_sigEmitter, sizeof(pid_t)) == -1)
    qFatal("read (1), in Controler::sigSlot");

  if( ::read(_sigFd[0], &_sigCode, sizeof(SortTypes::SigCode)) == -1)
    qFatal("read (2), in Controler::sigSlot");

  switch(_sigCode)
    {
    case SortTypes::RESULTS_SENT:
      _fetchResult();
      _displaySortedList();
      _tell(_sorter_pid, SortTypes::RESULTS_RECEIVED);
      break;

    default:
      qFatal("Controler::_sigSlot, unexpected signal");
    }

  _sigNotifier->setEnabled(true);  

  qDebug() << "Received SigCode " << SortUtility::SigCodeToQString(_sigCode) << "from (" << _sigEmitter << ")" << "\n";
}
Пример #4
0
/**
 * The function used as ZOOUpdateStatus from the JavaScript environment
 * (ZOO-API).
 *
 * @param cx the JavaScript context
 * @param argc the number of parameters
 * @param argv1 the parameter values
 * @return true
 * @see setHeader,_updateStatus
 */
JSBool
JSUpdateStatus(JSContext *cx, uintN argc, jsval *argv1)
{
  jsval *argv = JS_ARGV(cx,argv1);
  JS_MaybeGC(cx);
  int istatus=0;
  char *status=NULL;
  maps *conf;
  if(argc>2){
#ifdef JS_DEBUG
    fprintf(stderr,"Number of arguments used to call the function : %i",argc);
#endif
    return JS_FALSE;
  }
  conf=mapsFromJSObject(cx,argv[0]);
  if(JS_ValueToInt32(cx,argv[1],&istatus)==JS_TRUE){
    char tmpStatus[4];
    sprintf(tmpStatus,"%i",istatus);
    tmpStatus[3]=0;
    status=strdup(tmpStatus);
  }
  if(getMapFromMaps(conf,"lenv","status")!=NULL){
    if(status!=NULL){
      setMapInMaps(conf,"lenv","status",status);
      free(status);
    }
    else
      setMapInMaps(conf,"lenv","status","15");
    _updateStatus(conf);
  }
  freeMaps(&conf);
  free(conf);
  JS_MaybeGC(cx);
  return JS_TRUE;
}
void Controler::_createSorter()
{
  int dad_writes[2];
  int son_writes[2];
  pid_t control_pid;

  control_pid = getpid();

  if( pipe(dad_writes) == -1 || pipe(son_writes) == -1  )
    qFatal("pipe, in Controler::_createSorter");

  _writeChannel = dad_writes[1];
  _readChannel = son_writes[0];

  //make list of numbers available for Sorter
  _sendData();

  switch( _sorter_pid = fork() )
    {
    case -1:
      qFatal("fork, in Controler::_createSorter");

    case 0:

      ::close(dad_writes[1]);
      ::close(son_writes[0]);

      setpgid(_sorter_pid, control_pid);

      if(
	 execl(_execName->toAscii(), _execName->toAscii(),
	       "-p",
	       "sorter",
	       "--control",
	       SortUtility::itos(control_pid).c_str(),
	       "--write-channel",
	       SortUtility::itos(son_writes[1]).c_str(),
	       "--read-channel",
	       SortUtility::itos(dad_writes[0]).c_str(),
	       NULL
	       ) == -1 )
	qFatal("execl, in Controler::_createSorter");

    default:
      ::close(dad_writes[0]);
      ::close(son_writes[1]);

    }

  qDebug() << "write Channnels --- Control (" << control_pid << "): " << dad_writes[1] << ", Sorter (" << _sorter_pid <<"): " << son_writes[1] << "\n";
  qDebug() << "read Channnels --- Control (" << control_pid << "): " << son_writes[0] << ", Sorter (" << _sorter_pid <<"): " << dad_writes[0] << "\n";

  _updateStatus("Sorter view created" );
}
bool PosixDirectory::open()
{
   if ((_handle = opendir(_name)) == 0)
   {
      _updateStatus();
      return false;
   }
   
   _status = Open;
   return true;
}
void Controler::_tell(const pid_t & recipient, const SortTypes::SigCode & sigCode)
{
  union sigval message;

  message.sival_int = sigCode;

  if( ::sigqueue(recipient, SIGUSR1, message) == -1)
    qFatal("sigqueue, in Controler::_tell");

  _updateStatus(QString("Signal ")+SortUtility::SigCodeToQString(sigCode)+" sent" );
}
U32 PosixFile::write(const void* src, U32 size)
{
   if ((_status != Open && _status != EndOfFile) || !size)
      return 0;

   U32 bytesWritten = fwrite(src, 1, size, _handle);
   
   if (bytesWritten != size)
      _updateStatus();
      
   return bytesWritten;
}
bool PosixDirectory::getAttributes(Attributes* attr)
{
   struct stat info;
   if (stat(_name.c_str(),&info))
   {
      _updateStatus();
      return false;
   }

   copyStatAttributes(info,attr);
   attr->name = _path;
   return true;
}
bool PosixFile::getAttributes(Attributes* attr)
{
   struct stat info;
   int error = _handle? fstat(fileno(_handle),&info): stat(_name.c_str(),&info);
   
   if (error < 0)
   {
      _updateStatus();
      return false;
   }

   copyStatAttributes(info,attr);
   attr->name = _path;
   
   return true;
}
U32 PosixFile::read(void* dst, U32 size)
{
   if (_status != Open && _status != EndOfFile)
      return 0;

   U32 bytesRead = fread(dst, 1, size, _handle);
   
   if (bytesRead != size)
   {
      if (feof(_handle))
         _status = EndOfFile;
      else
         _updateStatus();
   }
   
   return bytesRead;
}
Пример #12
0
void SFXSource::stop( F32 fadeOutTime )
{
   _updateStatus();
   
   if( mStatus != SFXStatusPlaying
       && mStatus != SFXStatusPaused )
      return;
      
   if( fadeOutTime != 0.0f && ( fadeOutTime > 0.0f || mFadeOutTime > 0.0f ) )
   {
      // Do a fade-out and then stop.
      
      _clearEffects< SFXFadeEffect >();
      
      if( fadeOutTime == -1.0f )
         fadeOutTime = mFadeOutTime;
         
      mEffects.pushFront( new SFXFadeEffect( this,
                                             getMin( fadeOutTime,
                                                     F32( getDuration() - getPosition() ) / 1000.f ),
                                             0.0f,
                                             getPosition(),
                                             SFXFadeEffect::ON_END_Stop,
                                             true ) );
   }
   else
   {
      // Stop immediately.
      
      _setStatus( SFXStatusStopped );
   
      if ( mVoice )
         mVoice->stop();
      else
         mVirtualPlayTimer.stop();
         
      #ifdef DEBUG_SPEW
      Platform::outputDebugString( "[SFXSource] stopped playback of source '%i'", getId() );
      #endif
   }      
}
Пример #13
0
void Controler::_sendData()
{
  QVector<int> & numbers = *_numbers;

  int datalen = numbers.size();

  //data length
  if( ::write(_writeChannel, &datalen, sizeof(int)) == -1)
    qFatal("write (1), in Controler::_sendData");

  //actual data
  for(int i=0; i < numbers.size(); ++i)
    {

      if( ::write(_writeChannel, &numbers[i], sizeof(int)) == -1)
	qFatal("write (2), in Controler::_sendData");
    }

  _updateStatus("Data sent in pipe" );

}
Пример #14
0
void Controler::_fetchResult()
{
  int datalen, data;

  //data length
  if( ::read(_readChannel, &datalen, sizeof(int)) == -1 )
      qFatal("read (1), in MergeSorter::_fetchResult");

  //actual data
  _numbers->clear();

  for(int i=0; i < datalen; ++i)
    {
      if( ::read(_readChannel, &data, sizeof(int)) == -1 )
	qFatal("read (2), in MergeSorter::_fetchResult");

      _numbers->push_back(data);
    }

  _updateStatus("Received sorted list" );
}
bool PosixFile::open(AccessMode mode)
{
   close();
      
   if (_name.isEmpty())
   {
      return _status;
   }

   #ifdef DEBUG_SPEW
   Platform::outputDebugString( "[PosixFile] opening '%s'", _name.c_str() );
   #endif

   const char* fmode = "r";
   switch (mode)
   {
      case Read:        fmode = "r"; break;
      case Write:       fmode = "w"; break;
      case ReadWrite:
      {
         fmode = "r+";
         // Ensure the file exists.
         FILE* temp = fopen( _name.c_str(), "a+" );
         fclose( temp );
         break;
      }
      case WriteAppend: fmode = "a"; break;
      default:          break;
   }

   if (!(_handle = fopen(_name.c_str(), fmode)))
   {
      _updateStatus();
      return false;
   }
   
   _status = Open;
   return true;
}
Пример #16
0
/**
 * Update the ongoing status of a running service from the Ruby environment
 * (ZOO-API)
 *
 * @param argc the number of parameters
 * @param argv the parameter values given from the Ruby environment
 * @param obj the Ruby object on which we run the method
 * @return a new Ruby string containing the translated value
 * @see _updateStatus
 */
VALUE
RubyUpdateStatus(int argc, VALUE *argv, VALUE obj)
{
  maps* conf;
  VALUE confdict=argv[0];
  int istatus=argv[1];
  char* status;
  if (istatus < 0 || istatus > 100){
    fprintf(stderr,"Status must be a percentage.");
    return Qnil;
  }else{
     char tmpStatus[4];
     snprintf(tmpStatus, 4, "%i", istatus);
     status = zStrdup(tmpStatus);
  }
  /* now update the map */
  {
    VALUE lenv = rb_hash_aref(confdict,rb_str_new2("lenv"));
    if(TYPE(lenv)!=T_NIL){
      VALUE valobj=rb_str_new2(status);
      rb_hash_aset(lenv,rb_str_new2("status"),valobj);
    }
  }
  conf = mapsFromRubyHash(confdict);
  if (getMapFromMaps(conf,"lenv","status") != NULL){
    fprintf(stderr,"STATUS RETURNED : %s\n",status);
    if(status!=NULL){
      setMapInMaps(conf,"lenv","status",status);
      free(status);
    }
    else
      setMapInMaps(conf,"lenv","status","15");
    _updateStatus(conf);
  }
  freeMaps(&conf);
  free(conf);
  return Qnil;
}
Пример #17
0
void SFXSource::play( F32 fadeInTime )
{
   // Update our status once.
   _updateStatus();

   if( mStatus == SFXStatusPlaying )
      return;

   if( mStatus != SFXStatusPaused )
      mPlayStartTick = Platform::getVirtualMilliseconds();
         
   // Add fade-out, if requested.
   
   U32 fadeOutStartsAt = getDuration();
   if( mFadeOutTime )
   {
      fadeOutStartsAt = getMax( getPosition(), getDuration() - U32( mFadeOutTime * 1000 ) );
      mEffects.pushBack( new SFXFadeEffect( this,
                                            getMin( mFadeOutTime,
                                                    F32( getDuration() - getPosition() ) / 1000.f ),
                                            0.0f,
                                            fadeOutStartsAt,
                                            SFXFadeEffect::ON_END_Stop,
                                            true ) );
   }
   
   // Add fade-in, if requested.
   
   if( fadeInTime != 0.0f && ( fadeInTime > 0.0f || mFadeInTime > 0.0f ) )
   {
      // Don't fade from full 0.0f to avoid virtualization on this source.
      
      if( fadeInTime == -1.0f )
         fadeInTime = mFadeInTime;
         
      fadeInTime = getMin( fadeInTime, F32( fadeOutStartsAt ) * 1000.f );
      
      mEffects.pushFront( new SFXFadeEffect( this, 
                                             fadeInTime,
                                             mVolume,
                                             getPosition(),
                                             SFXFadeEffect::ON_END_Nop,
                                             true ) );
      setVolume( 0.01f );
   }

   // Start playback.

   _setStatus( SFXStatusPlaying );
   if( mVoice )
   {
      #ifdef DEBUG_SPEW
      Platform::outputDebugString( "[SFXSource] playing source '%i'", getId() );
      #endif
      
      mVoice->play( mIsLooping );
   }
   else
   {
      // To ensure the fastest possible reaction 
      // to this playback let the system reassign
      // voices immediately.
      SFX->_assignVoices();

      // If we did not get assigned a voice, start the
      // playback timer for virtualized playback.

      if( !mVoice )
      {
         #ifdef DEBUG_SPEW
         Platform::outputDebugString( "[SFXSource] virtualizing playback of source '%i'", getId() );
         #endif
         
         mVirtualPlayTimer.start();
      }
   }
}