//===========================================================================
//文件名称：task_client.c
//功能概要：定义task_client任务
//版权所有：苏州大学飞思卡尔嵌入式中心(sumcu.suda.edu.cn)
//===========================================================================
#include "app_inc.h"

extern _pool_id message_pool;

//===========================================================================
//函数名称：task_client
//参数说明：用户索引号
//函数返回：无
//功能概要：这个任务打开消息队列从消息池申请一个消息，这个消息被发送到server_task并且等待回复，
//等到之后回复消息接着被释放。
//===========================================================================
void task_client(uint32_t index)
{
   SERVER_MESSAGE_PTR msg_ptr;
   _queue_id client_qid;
   
   client_qid=_msgq_open((_queue_number)(CLIENT_QUEUE_BASE +
      index), 0);
   while (TRUE) 
   {
	  //从私有消息池分配一个消息信封
      msg_ptr = (SERVER_MESSAGE_PTR)_msg_alloc(message_pool);
      //填写消息的源地址、目的地址与大小
      msg_ptr->HEADER.SOURCE_QID = client_qid;      
      msg_ptr->HEADER.TARGET_QID = _msgq_get_id(0, SERVER_QUEUE);
      msg_ptr->HEADER.SIZE = sizeof(MESSAGE_HEADER_STRUCT) + 
         strlen((char_ptr)msg_ptr->DATA) + 1;
      msg_ptr->DATA[0] = ('A'+ index);//填写消息内容
      _msgq_send(msg_ptr);//发送消息
      printf("Client Task %ld sended message to server\n", index);
      msg_ptr = _msgq_receive(client_qid, 0);//等待回复消息  
      printf("Client Task %ld received postback message.\n", index);
      _msg_free(msg_ptr);//将收到的消息释放
      _time_delay(1500);
   }
}

