/* ===================== 转发教师端数据到所有学生端 ===================== */ 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; }
/* ===================== 获得数据库表纪录 ===================== */ 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; }
int SendTask::work() { while(true){ Buf* p = NULL; if (0 != SINGLE->sendqueue.dequeue(p, 3)) { continue; } int fd = p->getfd(); if (0 == fd) { printf(" fd = 0 !\n"); continue; } #if 0 MSG_HEAD* pp = (MSG_HEAD*)p->ptr(); if ( pp->cType > 9999 ) { struct sGetAllStudentInfo* xxx = (struct sGetAllStudentInfo*)pp->cData(); printf("id %d pic %s -- len:=%d\n", xxx->iStudentId, xxx->sPicName, pp->cLen); } unsigned int len = 0; int cnt; while (len < p->size()) { cnt = send(fd, (char *)p->ptr() + len, p->size() - len, 0); if (cnt == -1) { cout << "send erroooooooooooooooooooooor" << endl; break; } len += cnt; } printf ("ERRNO: %d, message:%s len = %d, p->size() = %ld\n", errno, strerror (errno), len ,p->size()); #else int bytes_left = p->size (); int written_bytes; char* ptr = (char*) p->ptr(); while (bytes_left > 0) { written_bytes = send (fd, ptr, bytes_left, 0); if (written_bytes <= 0) { if (errno == EINTR) { if (written_bytes < 0) written_bytes = 0; continue; } else if (errno == EAGAIN) { if (written_bytes < 0) written_bytes = 0; sleep (1); continue; } else break; } bytes_left -= written_bytes; ptr += written_bytes; } #endif printf("Send data...finished. packetLength=%ld, from FD=[%d]\n", p->size(), fd); p->reset(); SINGLE->bufpool.free(p); } return 0; }
int SendTask::work () { while (true) { Buf* p = NULL; if (SINGLE->sendqueue.dequeue (p, 3) != 0) { continue; } int fd = p->getfd (); if (fd <= 0) { cout << "[SEND] -- fd of buffer <= 0" << endl; SINGLE->bufpool.free (p); continue; } // #if 0 cout << "Send cType: " << ((MSG_HEAD*)p->ptr())->cType <<endl; printf ("address: %p\n", p); if (((MSG_HEAD*)p->ptr())->cType == 187) { cout << "-----------------------BEGIN-------------------------------------" << endl; cout << "CTYPE: " << ((MSG_HEAD*) (p->ptr()))->cType << endl; cout << "LEN: " << *(unsigned int*) ((char*)p->ptr() + MSG_HEAD_LEN) << endl; cout << "STUDENT_ID: " << *(unsigned int*) ((char*)p->ptr() + MSG_HEAD_LEN + sizeof (unsigned int)) << endl; cout << "STATUS: " << *(unsigned int*) ((char*)p->ptr() + MSG_HEAD_LEN + sizeof (int) + sizeof (int)) << endl; cout << "STUDENT_ID: " << *(unsigned int*) ((char*)p->ptr() + MSG_HEAD_LEN + sizeof (int) + sizeof (int) + sizeof (int)) << endl; cout << "STATUS: " << *(unsigned int*) ((char*)p->ptr() + MSG_HEAD_LEN + sizeof (int) + sizeof (int)+ sizeof (int) + sizeof (int)) << endl; cout << "-------------------------END-----------------------------------" << endl; } #endif // send fck message to clients // must be need written bytes data finished debugProtocol (p); int bytes_left = p->size (); int written_bytes; char* ptr = (char*) p->ptr(); while (bytes_left > 0) { written_bytes = send (fd, ptr, bytes_left, 0); if (written_bytes <= 0) { if (errno == EINTR) { if (written_bytes < 0) { written_bytes = 0; cout << "EINTR......" << endl; continue; } } else if (errno == EAGAIN) { if (written_bytes < 0) { written_bytes = 0; usleep (50); cout << "EAGAIN......" << endl; continue; } } else { break; } } bytes_left -= written_bytes; ptr += written_bytes; } //printf("Send data...finished. packetLength=%ld, from FD=[%d]\n", p->size(), fd); LOG(INFO) << "Send data ... finished. packet len=" << p->size() << ", from FD=" << fd << endl; p->reset (); SINGLE->bufpool.free (p); } return 0; }