/*
 * 这是主处理申请的过程
 */
void Process::Execute(void * arg) {
    cout << "[Process " << GetPid() << "]: Started." << endl;

    /*
     * mPlugins内容应该只有一个,值为"cudart"
     * GetHandler_t是一个指向函数的指针,具体见定义
     */
    GetHandler_t h;
    for(vector<string>::iterator i = mPlugins.begin(); i != mPlugins.end();
            i++) {
        if((h = LoadModule((*i).c_str())) != NULL)//h是指向GetHandler()函数的地址的指针
            mHandlers.push_back(h());
    }
// -----2016-02-29-------

    string routine;
    Buffer * input_buffer = new Buffer();
    /*
     * 这个循环是不断的从mpCommunicator(一个TcpCommunicator类)的mpInput输入流中每次读入一个字符串,直到读到文件结束符EOF为止
     * 这个循环就是整个的处理总过程
     */
    int loop_count = 0; //Sandy 2016.05.17
    while (getstring(mpCommunicator, routine))//mpCommunicator此时的值应该为client,即包含客户信息的TcpCommunicator
    {
    	cout<<"Process loop :"<< loop_count++ <<endl; //Sandy 2016.05.17
        input_buffer->Reset(mpCommunicator);
        Handler *h = NULL;
        for(vector<Handler *>::iterator i = mHandlers.begin();
                i != mHandlers.end(); i++) {
            if((*i)->CanExecute(routine)) {
                h = *i;
                break;
            }
        }
        Result * result;
        if(h == NULL)
        {
            cout << "[Process " << GetPid() << "]: Requested unknown routine "
                    << routine << "." << endl;
            result = new Result(-1, new Buffer());//初始化mExitCode=-1
        } else
            result = h->Execute(routine, input_buffer);

        result->Dump(mpCommunicator);
        if (result->GetExitCode() != 0 && routine.compare("cudaLaunch"))
        {
            cout << "[Process " << GetPid() << "]: Requested '" << routine
                    << "' routine." << endl;
            cout << "[Process " << GetPid() << "]: Exit Code '"
                    << result->GetExitCode() << "'." << endl;
        }
        delete result;
    }
    delete input_buffer;
    Notify("process-ended");
    delete this;
}
void Process::Execute(void * arg) {
    cout << "[Process " << GetPid() << "]: Started." << endl;

    GetHandler_t h;
    for(vector<string>::iterator i = mPlugins.begin(); i != mPlugins.end();
            i++) {
        if((h = LoadModule((*i).c_str())) != NULL)
            mHandlers.push_back(h());
    }

    string routine;
    Buffer * input_buffer = new Buffer();
    while (getstring(mpCommunicator, routine)) {
#ifdef DEBUG
        cout<< "Received routine "<<routine<<endl;
#endif
        input_buffer->Reset(mpCommunicator);
        Handler *h = NULL;
        for(vector<Handler *>::iterator i = mHandlers.begin();
                i != mHandlers.end(); i++) {
            if((*i)->CanExecute(routine)) {
                h = *i;
                break;
            }
        }
        Result * result;
        if(h == NULL) {
            cout << "[Process " << GetPid() << "]: Requested unknown routine "
                    << routine << "." << endl;
            result = new Result(-1, new Buffer());
        } else
            result = h->Execute(routine, input_buffer);
        result->Dump(mpCommunicator);
        if (result->GetExitCode() != 0 && routine.compare("cudaLaunch")) {
            cout << "[Process " << GetPid() << "]: Requested '" << routine
                    << "' routine." << endl;
            cout << "[Process " << GetPid() << "]: Exit Code '"
                    << result->GetExitCode() << "'." << endl;
        }
        delete result;
    }
    delete input_buffer;
    Notify("process-ended");
    delete this;
}