示例#1
0
文件: main.cpp 项目: srgblnch/yat
// ============================================================================
//  main
// ============================================================================
int main(int argc, char* argv[])
{
  yat::Message * m = 0;
  
  YAT_LOG_STATIC("Instanciating Task...");
	
  Consumer * dt = new Consumer(kLO_WATER_MARK, kHI_WATER_MARK);
  
  YAT_LOG_STATIC("Starting Task...");
	
  try
  {
    dt->go(2000); 
  }
  catch (const yat::Exception&)
  {
    YAT_LOG_STATIC("yat exception caught - could not start task. aborting...");
    dt->exit();
    return 0;
  }
  catch (...)
  {
    YAT_LOG_STATIC("unknown exception caught - could not start task. aborting...");
    dt->exit();
    return 0;
  }

 /*
  for (size_t i = 0; i < kNUM_MSGS; i++)
  {
    try
    {
      //- post msg to consumer
      dt->post(new yat::Message(kDUMMY_MSG), kPOST_MSG_TMO);

      //- simulate some time consuming activity
      yat::ThreadingUtilities::sleep(0, 100000);
    }
    catch (const std::bad_alloc&)
    {
      YAT_LOG_STATIC("std::bad_alloc except. caught - could not post msg#" << i);
    }
    catch (const yat::Exception&)
    {
      YAT_LOG_STATIC("tango except. caught - could not post msg#" << i);
    }
    catch (...)
    {
      YAT_LOG_STATIC("unknown except. caught - could not post msg#" << i);
    }
  }
*/
 
  yat::Buffer<double> data(kNUM_MSGS);
  for ( size_t i = 0; i < kNUM_MSGS; i++ )
    data[i] = 1. * i;
  data.force_length(kNUM_MSGS);
  
  for (size_t i = 0; i < kNUM_MSGS; i++)
  {
    try
    {
      SharedBuffer* sb = new SharedBuffer();
      sb->capacity(i + 1);
      sb->memcpy(data.base(), i + 1);
      std::cout << "SharedBuffer* sb.length = " << sb->length() << std::endl;
      dt->post(kDATA_MSG, sb->duplicate(), false);
      sb->release();
      //- simulate some time consuming activity
      yat::ThreadingUtilities::sleep(0, 100000);
    }
    catch (const std::bad_alloc&)
    {
      YAT_LOG_STATIC("std::bad_alloc except. caught - could not post msg#" << i);
    }
    catch (const yat::Exception&)
    {
      YAT_LOG_STATIC("tango except. caught - could not post msg#" << i);
    }
    catch (...)
    {
      YAT_LOG_STATIC("unknown except. caught - could not post msg#" << i);
    }
  }
  
  try
  {
    dt->exit();
  }
  catch (const yat::Exception&)
  {
    YAT_LOG_STATIC("tango except. caught - could stop task. aborting...");
  }
  catch (...)
  {
    YAT_LOG_STATIC("unknown except. caught - could stop task. aborting...");
    return 0;
  }

  return 0;
}
示例#2
0
文件: my_task.cpp 项目: srgblnch/yat
// ============================================================================
// Consumer::handle_message
// ============================================================================
void Consumer::handle_message (yat::Message& _msg)
{
	//- YAT_TRACE("Consumer::handle_message");

	//- handle msg
  switch (_msg.type())
	{
	  //- TASK_INIT ----------------------
	  case yat::TASK_INIT:
	    {
  	    //- "initialization" code goes here
  	    YAT_LOG("Consumer::handle_message::TASK_INIT::task is starting up");
        this->ctrl_msg_counter++;
      } 
		  break;
		//- TASK_EXIT ----------------------
		case yat::TASK_EXIT:
		  {
  			//- "release" code goes here
  			YAT_LOG("Consumer::handle_message::TASK_EXIT::task is quitting");
        this->ctrl_msg_counter++;
      }
			break;
		//- TASK_PERIODIC ------------------
		case yat::TASK_PERIODIC:
		  {
  		  //- code relative to the task's periodic job goes here
  		  YAT_LOG("Consumer::handle_message::handling TASK_PERIODIC msg");
      }
		  break;
		//- TASK_TIMEOUT -------------------
		case yat::TASK_TIMEOUT:
		  {
  		  //- code relative to the task's tmo handling goes here
  		  YAT_LOG("Consumer::handle_message::handling TASK_TIMEOUT msg");
      }
		  break;
		//- USER_DEFINED_MSG -----------------
		case kDUMMY_MSG:
		  {
  		  //- YAT_LOG("Consumer::handle_message::handling kDUMMY_MSG user msg");
        this->user_msg_counter++;
#if defined (YAT_DEBUG)
        if (_msg.id() < last_msg_id)
          this->wrong_order_msg_counter++;
        else
          this->lost_msg_counter += _msg.id() - (this->last_msg_id + 1);
#endif
        //- simulate some time consuming activity
        yat::ThreadingUtilities::sleep(0, 10000);
  		}
  		break;
    //- kDATA_MSG -------------------
    case kDATA_MSG:
      {
        //- code relative to the task's tmo handling goes here
        SharedBuffer* sb = _msg.detach_data<SharedBuffer>();
        YAT_LOG("got a SharedBuffer containing " << sb->length() << " elements");
        sb->release();
      }
      break;
  	default:
  		YAT_LOG("Consumer::handle_message::unhandled msg type received");
  		break;
	}
#if defined (YAT_DEBUG)
  this->last_msg_id = _msg.id();
#endif
}