示例#1
0
int CFileListener::Run(CThread * thisThread) {
    CString loop = "File Listener loop";
    bool found = true;
    while (!m_stopped) {
        try {
            if ( !found ) {
                LOG_DEBUG(GetLogger(), ATF_DEBUG, "File listener wait for dir changes");
                DWORD result = WaitForMultipleObjects(2,m_handles,false,INFINITE);
                if ( 1 == result - WAIT_OBJECT_0 ) {
                    break;
                }
                if ( 0 != result - WAIT_OBJECT_0 ) {
                    THROW_SYSTEM_EXCEPTION("Problem during wait changes");
                }

                FindNextChangeNotification(m_handles[ATF_WAIT_HANDLE]);
            }
            CString file = GetNextFile();
            if ( file.IsEmpty() ) {
                found = false;
                continue;
            }
            found = true;
            
            LOG_DEBUG(GetLogger(), ATF_DEBUG, "File found ["+file+"]");

            CMessage * msg = new CMessage(file);
            int pos_point = file.ReverseFind('.');
            int pos_slash = file.ReverseFind('\\');
            if ( pos_point < pos_slash || pos_point == -1 ) {
                msg->SetType("$$$");
            } else {
                CString ext = ((const char*)file)[pos_point+1];
                msg->SetType(ext);
            }
            
            CString tranId;
            if ( pos_point < pos_slash || pos_slash == -1) {
                tranId.Format("%0ld", msg->GetID());
                msg->SetTransactionID(tranId);
            } else {
                for ( int i = pos_slash + 1; i < pos_point; i++ ) {
                    tranId += file[i];
                }
                msg->SetTransactionID(tranId);
            }


            IExecutor * executor = m_executors.Get();
            CThread * proc = m_threads.Get();
            executor->SetTask(msg);
            IRunnable& body = proc->GetBody();
            ((CThreadBodyRunnable&)body).SetTask(executor, &m_executors);
            ((CPoolableThread*)proc)->Run();

        } catch (CSystemException &se) {
            CExceptionLogger::Log(GetLogger(), se);
        } catch (CAtfException &te) {
            CExceptionLogger::Log(GetLogger(), te);
        } catch (...) {
            LOG_ERROR(GetLogger(), ATF_ASSERT_ERR, "Unknown Exception was thrown");
        }
        LOG_DEBUG(GetLogger(), ATF_DEBUG, loop);
    }
    return 0;
};