示例#1
0
/*
=====================
 转发教师端数据到所有学生端
=====================
*/
bool CHandleMessage::postTeacherToAllStudent (Buf* p, enum CommandType iCommandType)
{
#if 0
    cout << "postTeacherToAllStudent ------------------------ " <<endl;
    if (p == NULL)
        return false;

    int iLen = ((MSG_HEAD*)p->ptr())->cLen;
    int iHeadLen = sizeof (MSG_HEAD);
    Buf* pbuf = NULL;

    CRoom* pc = ROOMMANAGER->get_room_by_fd (p->getfd());
    if (pc != NULL) {

        int i = 0;
        CRoom::STUDENTMAP::iterator it;
        for (it = pc->m_student_map.begin(); \
             it != pc->m_student_map.end (); ++it)
        {
            i++;
            pbuf = SINGLE->bufpool.malloc ();
            if (pbuf != NULL) {
                MSG_HEAD* head = (MSG_HEAD*)pbuf->ptr();
                head->cLen = iLen;
                head->cType = iCommandType;
                if (iLen > iHeadLen) {
                    memcpy (head->cData(), (char*)p->ptr() + iHeadLen, iLen - iHeadLen);
                }
                pbuf->setsize (head->cLen);
                pbuf->setfd (it->first);
                SINGLE->sendqueue.enqueue (pbuf);
            }
            else {
                cout << "Error: out of memory" << endl;
                p->reset();
                pbuf->reset();
                SINGLE->bufpool.free(p);
                SINGLE->bufpool.free(pbuf);
                return false;
            }
        }
        if ( 0 == i ) {
            printf("send to %d students!\n", i);
        }
    }
    else {
        cout << __FILE__ << ":" <<__FUNCTION__<< ":" << __LINE__<<"Error: not found 'teacher_fd' in Room" << endl;
        //p->reset();
        SINGLE->bufpool.free(p);
        return false;
    }

    p->reset();
    SINGLE->bufpool.free(p);
#endif
    return true;
}
示例#2
0
bool
epClass::sendtoAllStudent(Buf* pBuf) {
        EPSTUDENT_MAP::iterator it = studentMap_.begin();
        EPSTUDENT_MAP::const_iterator cie = studentMap_.end();
        Buf* p = NULL;
        for (; cie!=it; ++it) {
                if (pBuf->getfd() == it->first) { // not send to self.
                        continue;
                }
                p = pBuf;
                p->setfd(it->first);
                SINGLE->sendqueue.enqueue(p);
        }

        SINGLE->bufpool.free(pBuf);
        return true;
}
示例#3
0
/*
=====================
 获得数据库表纪录
=====================
*/
bool CHandleMessage::postDBRecord (Buf* buf, int iCase)
{
#if 0
    if (NULL == buf) {
        printf("null buf\n");
        return false;
    }

    MSG_HEAD head;

    try {
        MutexLockGuard guard(DATABASE->m_mutex);
        PreparedStatement* pstmt = NULL;

        if (iCase == 1)
            pstmt = DATABASE->preStatement (SQL_SELECT_COURSE_DB);
        else if (iCase == 2)
            pstmt = DATABASE->preStatement (SQL_SELECT_GRADE_DB);
        else if (iCase == 3)
            pstmt = DATABASE->preStatement (SQL_SELECT_CLASS_DB);
        else if (iCase == 4)
            pstmt = DATABASE->preStatement (SQL_SELECT_CLASSROOM_DB);
        else if (iCase == 5)
            pstmt = DATABASE->preStatement (SQL_SELECT_STUDENT_DB);
        else if (iCase == 6) {
            pstmt = DATABASE->preStatement (SQL_SELECT_COURSEITEM_DB);
#if 1   // send count of all selected course by teacher
            #ifdef _TEACHER_NOLOGIN
            // only for h**king test............
            pstmt->setString (1, "拼图");
            pstmt->setString (2, "造房子");
            pstmt->setString (3, "暖身操");
            pstmt->setString (4, "动画片");
            #else
            CRoom* room = ROOMMANAGER->get_room_by_fd (buf->getfd());
            if (room != NULL)
            {
                CRoom::COURSELIST::iterator it;
                int ii = 1;
                for (it = room->m_course_list.begin (); it != room->m_course_list.end (); ++it)
                    pstmt->setString (ii++, (*it)->getName());
            }
            #endif
#else
            sGetCourseItem* ci = (sGetCourseItem *) ((char*)((MSG_HEAD*)buf->ptr()) + sizeof (MSG_HEAD));
            pstmt->setString (1, ci->sCourseName);
#endif
        }
        else {
            cout << "error: index" << endl;
            return false;
        }

        ResultSet* prst = pstmt->executeQuery();
        unsigned int index = 0, type = 0;
        while(prst->next()) {
            //printf ("index = %d------------------------------------------------------\n", index);
            if (iCase == 1) {
                memset (&head, 0x00, sizeof (head));
                //head.cType = CT_GetCourseDB * 100 + index++;
                type = 5000 + index++;
                memcpy (&head.cType, &type, sizeof (unsigned int));
                head.cLen = sizeof(MSG_HEAD) + sizeof(struct sGetCourseDB);
                struct sGetCourseDB course_info;
                (void) memset (&course_info, 0x00, sizeof (sGetCourseDB));

                strcpy(course_info.sGradeName, prst->getString ("grade_name").c_str());
                strcpy(course_info.sGroupName, prst->getString ("group_name").c_str());
                strcpy(course_info.sCourseName, prst->getString("course_name").c_str());

                course_info.iLanguage   = prst->getInt ("language");
                course_info.iArt        = prst->getInt ("art");
                course_info.iCommunity  = prst->getInt ("community");
                course_info.iHealth     = prst->getInt ("health");
                course_info.iScience    = prst->getInt ("science");

                Buf* p = SINGLE->bufpool.malloc ();
                memcpy(p->ptr(), &head, sizeof(MSG_HEAD));
                memcpy((char*)p->ptr() + sizeof(MSG_HEAD), &course_info, sizeof(struct sGetCourseDB));
                p->setfd(buf->getfd());
                p->setsize(head.cLen);
                SINGLE->sendqueue.enqueue(p);
            }

            else if (iCase == 2) {
                memset (&head, 0x00, sizeof (head));
                type = 3000 + index++;
                memcpy (&head.cType, &type, sizeof (unsigned int));
                //head.cType = CT_GetGradeDB;
                head.cLen = sizeof (MSG_HEAD)+ sizeof (struct sGetGradeDB);
                struct sGetGradeDB grade_info;
                (void) memset (&grade_info, 0x00, sizeof (grade_info));

                strcpy (grade_info.sGradeName, prst->getString ("grade_name").c_str());

                Buf* p = SINGLE->bufpool.malloc ();
                memcpy (p->ptr(), &head, sizeof(MSG_HEAD));
                memcpy ((char *)p->ptr() + sizeof(MSG_HEAD), &grade_info, sizeof(struct sGetGradeDB));
                p->setfd (buf->getfd ());
                p->setsize(head.cLen);
                SINGLE->sendqueue.enqueue (p);
            }

            else if (iCase == 3) {
                memset (&head, 0x00, sizeof (head));
                type = 4000 + index++;
                memcpy (&head.cType, &type, sizeof (unsigned int));
                //head.cType = CT_GetClassDB;
                head.cLen = sizeof(MSG_HEAD)+ sizeof (struct sGetClassDB);
                struct sGetClassDB class_info;
                (void) memset (&class_info, 0x00, sizeof (class_info));

                strcpy (class_info.sClassName, prst->getString ("class_name").c_str());

                Buf* p = SINGLE->bufpool.malloc ();
                memcpy (p->ptr(), &head, sizeof (MSG_HEAD));
                memcpy ((char *)p->ptr() + sizeof (MSG_HEAD), &class_info, sizeof (struct sGetClassDB));
                p->setfd (buf->getfd ());
                p->setsize(head.cLen);
                SINGLE->sendqueue.enqueue (p);
            }

            else if (iCase == 4) {
                memset (&head, 0x00, sizeof (head));
                //head.cType = CT_GetClassRoomDB;
                type = 6000 + index++;
                memcpy (&head.cType, &type, sizeof (unsigned int));
                head.cLen = sizeof(MSG_HEAD) + sizeof (struct sGetClassRoomDB);
                struct sGetClassRoomDB room_info;
                (void) memset (&room_info, 0x00, sizeof (room_info));

                strcpy (room_info.sClassRoomName, prst->getString ("classroom_name").c_str());

                Buf* p = SINGLE->bufpool.malloc ();
                memcpy (p->ptr(), &head, sizeof (MSG_HEAD));
                memcpy ((char *)p->ptr() + sizeof (MSG_HEAD), &room_info, sizeof (struct sGetClassRoomDB));
                p->setfd (buf->getfd ());
                p->setsize(head.cLen);
                SINGLE->sendqueue.enqueue (p);
            }

            else if (iCase == 5) {
                memset (&head, 0x00, sizeof (MSG_HEAD));
                type = 10000 + index++;
                memcpy (&head.cType, &type, sizeof (unsigned int));
                /// cout << "begin:-head.cType = " << head.cType << endl;
                head.cLen = sizeof (MSG_HEAD) + sizeof (struct sGetAllStudentInfo);
                struct sGetAllStudentInfo stu_info;
                (void) memset (&stu_info, 0x00, sizeof (stu_info));

                strcpy (stu_info.sPicName, prst->getString ("picture_name").c_str());
                strcpy (stu_info.sStudentName, prst->getString ("student_name").c_str());
                stu_info.iStudentId= prst->getInt ("student_id");

                /// cout << "stu_info.iStudentId:" << stu_info.iStudentId << endl;
                /// cout << "stu_info.sPicName:" << stu_info.sPicName << endl;
                /// cout << "stu_info.sStudentName:" << stu_info.sStudentName << endl;

                Buf* p = SINGLE->bufpool.malloc ();
                memcpy (p->ptr(), &head, sizeof (MSG_HEAD));
                memcpy ((char *)p->ptr() + sizeof (MSG_HEAD), &stu_info, sizeof (struct sGetAllStudentInfo));
                p->setfd (buf->getfd ());
                p->setsize(head.cLen);
                SINGLE->sendqueue.enqueue (p);
                /// cout << "address = " << p << endl;
                /// cout << "ended:-head.cLen = " << head.cLen << endl;
                /// cout << "ended:-head.cType = " << head.cType << endl;
            }

            else if (iCase == 6) {
                memset (&head, 0x00, sizeof (head));
                type = 7000 + index++;
                memcpy (&head.cType, &type, sizeof (unsigned int));
                //head.cType = CT_GetCourseItem;
                head.cLen = sizeof(MSG_HEAD) + sizeof (struct sCourseItem);
                struct sCourseItem course_item;
                (void) memset (&course_item, 0x00, sizeof (sCourseItem));

                strcpy (course_item.sCourseName, prst->getString ("course_name").c_str());
                strcpy (course_item.sItemName, prst->getString ("item_name").c_str());
                strcpy (course_item.sDesc, prst->getString ("fck_desc").c_str());

                cout << "courseName: " << course_item.sCourseName << endl;
                cout << "itemName: " << course_item.sItemName << endl;
                cout << "Desc: " << course_item.sDesc << endl;
                Buf* p = SINGLE->bufpool.malloc ();
                memcpy (p->ptr(), &head, sizeof(MSG_HEAD));
                memcpy ((char *)p->ptr() + sizeof(MSG_HEAD), &course_item, sizeof (struct sCourseItem));
                p->setfd (buf->getfd ());
                p->setsize(head.cLen);
                SINGLE->sendqueue.enqueue (p);
            }
            else {
                cout << "error: index" << endl;
                return false;
            }

        }
#if 1   // send finished flags
        do {
            sleep (1);
            //cout << "send finished flags -----------" << endl;
            Buf* p = SINGLE->bufpool.malloc ();
            MSG_HEAD* phead = (MSG_HEAD*)p->ptr();
            struct sDBRecordFinished finished;
            memset (&finished, 0x00, sizeof (sDBRecordFinished));
            if (iCase == 5)
                finished.iFlagFinished = 10000;
            else
                finished.iFlagFinished = 1;

            phead->cLen = sizeof (MSG_HEAD) + sizeof (struct sDBRecordFinished);
            phead->cType = ST_GetDBRecordFinished;
            memcpy (((char*)p->ptr()) + MSG_HEAD_LEN, &finished, sizeof (struct sDBRecordFinished));
            p->setfd (buf->getfd());
            p->setsize (phead->cLen);
            SINGLE->sendqueue.enqueue (p);
        } while (0);
#endif
    }
    catch(SQLException e){
        LOG(ERROR) << e.what() << std::endl;
    }

    buf->reset();
    SINGLE->bufpool.free(buf);
#endif
    return true;
}