void Java_com_ysaito_shogi_BonanzaJNI_computerMove( JNIEnv *env, jclass unused_bonanza_class, jint instance_id, jobject result) { int status = R_OK; pthread_mutex_lock(&g_lock); if (AnotherInstanceStarted(env, instance_id, result)) { pthread_mutex_unlock(&g_lock); return; } CHECK2_GE(com_turn_start(&tree, 0), 0, "error: %s", str_error); unsigned int move = last_pv_save.a[1]; const char* move_str = NULL; if (move != 0) { move_str = str_CSA_move( move ); LOG_DEBUG("Comp: %x %s", move, move_str); } else { // Computer likely have resigned } status = GameStatusToReturnCode(); FillResult("Computer", env, status, NULL, move_str, move, &tree, result); pthread_mutex_unlock(&g_lock); }
static int AnotherInstanceStarted(JNIEnv* env, int instance_id, jobject result) { if (instance_id != g_instance_id) { FillResult("Human", env, R_INSTANCE_DELETED, "Another game already started", NULL, 0, NULL, result); return 1; } return 0; }
void Java_com_ysaito_shogi_BonanzaJNI_humanMove( JNIEnv *env, jclass unused_bonanza_class, jint instance_id, jstring jmove_str, jobject result) { pthread_mutex_lock(&g_lock); if (AnotherInstanceStarted(env, instance_id, result)) { pthread_mutex_unlock(&g_lock); return; } MoveBuf move_str_buf; char* move_str = NULL; unsigned int move = 0; int status = R_OK; const char* error = NULL; int r = ParseCsaMove(env, jmove_str, &move, move_str_buf); if (r < 0) { LOG_DEBUG("Failed to parse move: %s: %s", move_str, str_error); status = R_ILLEGAL_MOVE; error = str_error; } else { r = make_move_root(&tree, move, (flag_history | flag_time | flag_rep | flag_detect_hang | flag_rejections)); if (r < 0) { LOG_DEBUG("Failed to make move: %s: %s", move_str_buf, str_error); move_str = NULL; move = 0; status = R_ILLEGAL_MOVE; error = str_error; } else { LOG_DEBUG("Human: %s", move_str_buf); move_str = move_str_buf; status = GameStatusToReturnCode(); error = NULL; } } FillResult("Human", env, status, error, move_str, move, &tree, result); pthread_mutex_unlock(&g_lock); }
void Java_com_ysaito_shogi_BonanzaJNI_undo( JNIEnv *env, jclass unused_bonanza_class, jint instance_id, jint undo_cookie1, jint undo_cookie2, jobject result) { pthread_mutex_lock(&g_lock); if (AnotherInstanceStarted(env, instance_id, result)) { pthread_mutex_unlock(&g_lock); return; } MoveBuf move_str; unsigned int move; CHECK2_GE(undo_cookie1, 1, "Cookie: %x", undo_cookie1); unmake_move_root(&tree, undo_cookie1); if (undo_cookie2 >= 0) { unmake_move_root(&tree, undo_cookie2); } LOG_DEBUG("Undo: %x %x", undo_cookie1, undo_cookie2); FillResult("Undo", env, R_OK, NULL, NULL, 0, &tree, result); pthread_mutex_unlock(&g_lock); }
DWORD WINAPI CommMonitor( LPSTR lpData) { CommData SYSTEMTIME SystemTime; int i, nItemCount, nPackType, nTranType, nDataOff,nResultLen; BYTE btCalCheckSum, btRxCheckSum; WORD wSampleNo; WORD wRSampleNo; BYTE btResultType; LPSTR lpPointer; Init() nItemCount =0; ResetOpPointer() FillDevName() PurgeComm( hComm, PURGE_RXCLEAR ); SetCommWaitMask(EV_RXFLAG) while (TRUE) { ResetRxPointer() do { GetInQueueLength(dwReadLength) if(dwReadLength==0 &&dwRxBufferLength==0) { WaitEvent(EV_RXFLAG) GetInQueueLength(dwReadLength) } ReadCommBlock(dwReadLength) if ( dwRxBufferLength>=2) { if( (*(lpRxPointer-2)== ETX) ) break; } }while(TRUE); nPackType = lpOpPointer[c_PackTypeOff]; btResultType =0xff; nItemCount =0; switch (nPackType) { case 'R': break; case 'D': nTranType =lpOpPointer[c_TranTypeOff]; switch (nTranType) { case 'B': break; case 'E': if (lpOpPointer[c_TranTypeOff+1]==ETX) break; default: GetLocalTime(&SystemTime); switch (*(lpOpPointer+c_SampleIdOff)) { case '0': wSampleNo =StrToInt(lpOpPointer+c_SampleIdOff, c_SampleIDLen+1); btResultType =c_TypeSample; break; case '1': wSampleNo =StrToInt(lpOpPointer+c_SampleIdOff, c_SampleIDLen+1); btResultType =c_TypeSample; break; case '2': wSampleNo =StrToInt(lpOpPointer+c_SampleIdOff, c_SampleIDLen+1); btResultType =c_TypeSample; break; case '3': wSampleNo =StrToInt(lpOpPointer+c_SampleIdOff, c_SampleIDLen+1); btResultType =c_TypeSample; break; case 'Q': wRSampleNo =StrToInt(lpOpPointer+c_SampleNoOff, c_SampleIDLen); wSampleNo = wRSampleNo+8100; btResultType =c_TypeQC; break; case 'P': wRSampleNo =StrToInt(lpOpPointer+c_SampleNoOff, c_SampleIDLen); wSampleNo = wRSampleNo+6000; btResultType =c_TypeStat; break; case 'E': wRSampleNo =StrToInt(lpOpPointer+c_SampleNoOff, c_SampleIDLen); wSampleNo = wRSampleNo+4000; btResultType =c_TypeEmergency; break; case 'U': wRSampleNo =StrToInt(lpOpPointer+c_SampleNoOff, c_SampleIDLen); wSampleNo = wRSampleNo+5000; btResultType =c_TypeUrine; break; } } if(btResultType != 0xff) for(nDataOff =c_DataOff; *(lpOpPointer+nDataOff)!=ETX ;) { FillSampleID(nItemCount, wSampleNo) FillItemName(nItemCount, lpOpPointer+nDataOff+c_ItemIDOff, c_ItemIDLen) DeleZero(lpOpPointer, c_ResultLen) FillResult(nItemCount, lpPointer ,nResultLen ) FillDate(nItemCount, SystemTime.wYear, SystemTime.wMonth, SystemTime.wDay, SystemTime.wHour) FillResultType(nItemCount, btResultType) FillComment(nItemCount, lpOpPointer+nDataOff+c_ErrorCodeOff, c_ErrorCodeLen) //填状态注解 FillErrorCode(nItemCount, c_Reliable) nItemCount++; nDataOff+=c_OneDataLen; } break; } if (nItemCount>0) { (*lpResultProcessRoutine)(lpDevice->nDevNO, OutResult, nItemCount); lpDevice->dwRecordCount+=nItemCount; } ResetRxPointer() PurgeComm( hComm, PURGE_RXCLEAR ); WriteCommChar(ACK) } return TRUE; }
jint Java_com_ysaito_shogi_BonanzaJNI_startGame( JNIEnv *env, jclass unused_bonanza_class, jint resume_instance_id, jobject initial_board, jint next_turn, // 0: black 1: white jint difficulty, jint total_think_time_secs, jint per_turn_think_time_secs, jobject result) { int instance_id = -1; pthread_mutex_lock(&g_lock); if (!g_initialized) { FillResult("Init", env, R_INITIALIZATION_ERROR, "Bonanza not yet initialized", NULL, 0, NULL, result); } else if (g_initialization_error != NULL) { FillResult("Init", env, R_INITIALIZATION_ERROR, g_initialization_error, NULL, 0, NULL, result); } else if (resume_instance_id != 0 && resume_instance_id == g_instance_id) { LOG_DEBUG("Resuming game %d", g_instance_id); instance_id = resume_instance_id; } else { instance_id = ++g_instance_id; LOG_DEBUG("Starting game: d=%d t=%d p=%d", difficulty, total_think_time_secs, per_turn_think_time_secs); jclass board_class = (*env)->GetObjectClass(env, initial_board); jfieldID fid = (*env)->GetFieldID(env, board_class, "mSquares", "[I"); jintArray jarray = (jintArray)((*env)->GetObjectField( env, initial_board, fid)); min_posi_t initial_pos = min_posi_no_handicap; initial_pos.turn_to_move = next_turn; jint tmp_array[nsquare]; (*env)->GetIntArrayRegion(env, jarray, 0, nsquare, tmp_array); for (int i = 0; i < nsquare; ++i) { initial_pos.asquare[i] = tmp_array[i]; } fid = (*env)->GetFieldID(env, board_class, "mCapturedBlack", "I"); initial_pos.hand_black = (*env)->GetIntField(env, initial_board, fid); fid = (*env)->GetFieldID(env, board_class, "mCapturedWhite", "I"); initial_pos.hand_white = (*env)->GetIntField(env, initial_board, fid); if (ini_game(&tree, &initial_pos, flag_history, NULL, NULL) < 0) { LOG_FATAL("Failed to initialize game: %s", str_error); } LOG_DEBUG("Initialized Bonanza successfully"); // Disable background thinking; we can't send a interrupt Bonanza // in the middle of thinking otherwise. // // TODO(saito) I must be missing something. Enable background // thinking. SetDifficulty(difficulty, total_think_time_secs, per_turn_think_time_secs); } FillResult("Init", env, R_OK, NULL, NULL, 0, &tree, result); pthread_mutex_unlock(&g_lock); return instance_id; }
void RunTest(Test* test) { int args_length = strlen(test->args_to_test); //char* executable_name = "SnoopyG.exe"; char* executable_name = __argv[1]; int len1 = strlen(executable_name); int len2 = strlen(test->args_to_test); char* result_output; int test_id = test->test_id; const char* expected_file = test->expected_file; const char* generated_file = test->output_file_name; char* command_with_exe = (char*)malloc(sizeof(char)*(len1 + len2 + 1)); PROCESS_INFORMATION procinfo; STARTUPINFO startinfo; DWORD waitcode; DWORD exitCode = 0; LPSTR command = NULL; if (command_with_exe == NULL) { printf("Failed to allocate memory for RunTest function, command with exe parameter \n"); exit(1); } sprintf(command_with_exe,"""\%s\ %s",executable_name,test->args_to_test); command = command_with_exe; ZeroMemory( &startinfo, sizeof(startinfo) ); startinfo.cb = sizeof(startinfo); ZeroMemory( &procinfo, sizeof(procinfo) ); if( !CreateProcess( NULL, // No module name (use command line) command, // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance to FALSE 0, // No creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &startinfo, // Pointer to STARTUPINFO structure &procinfo ) // Pointer to PROCESS_INFORMATION structure ) { printf( "CreateProcess failed (%d).\n", GetLastError() ); return; } Sleep(100); waitcode = WaitForSingleObject(procinfo.hProcess, INFINITE); if (waitcode != WAIT_OBJECT_0) { printf("Unexpected output value of 0x%x from WaitForSingleObject(). " "Ending function.\n", waitcode); CloseHandle(procinfo.hProcess); return; } FillRunTimeLog(procinfo.hProcess,test_id); if (GetExitCodeProcess(procinfo.hProcess,&exitCode)) { if(!exitCode) { if(CompareExpectedToGenerated(expected_file,generated_file)) { result_output = "Succeeded"; FillResult(test_id,result_output); } else { result_output = "Failed"; FillResult(test_id,result_output); } } else { if(CompareExpectedErrorToGeneratedError(exitCode,expected_file)) { FillErrorResult(test_id,exitCode); } else { printf("If you are here probably you have a bug in your program!\n"); } } } else { printf("GetExitCodeProcess() failed, error code %d\n", GetLastError()); return; } CloseHandle(procinfo.hProcess); CloseHandle(procinfo.hThread); }
DWORD WINAPI CommMonitor( LPSTR lpData) { //监控串口接收数据,并完成数据格式分析,将结果放入结果队列 CommData SYSTEMTIME SystemTime; int nItemCount,TrayNum,CupNum;//样本项目号,盘号,杯号 BYTE SampleIDLen ;//数据包功能号,样本ID长度 WORD wSampleNo; bool blogfile; int i,ByteRecieved; bool isdataPacket=FALSE; char DataBuffer[200];// 数据缓冲区! CHAR TempWorkingID[c_MaxWorkingIDLength+1];//样本号,chj char LSampleID[5]; Init() ResetOpPointer() ResetRxPointer() nItemCount =0; FillDevName() SetCommWaitMask(EV_RXCHAR) PurgeComm( hComm, PURGE_RXCLEAR ); blogfile=NewLogFile("Syn_ELISE.txt"); WriteCommChar(XON) while (TRUE) { WaitEvent(EV_RXCHAR) do { ReadFile(hComm, &RxChar, 1, &dwReadLength, NULL); if(RxChar==STX) isdataPacket=TRUE; if(isdataPacket) { DataBuffer[i]=RxChar; i++; } if(RxChar==ENQ||RxChar==EOT) { break; } if(((RxChar==ETX)|(RxChar==ETB))&(isdataPacket)) { //DataBuffer[i]=RxChar; ByteRecieved=i; break; } }while(dwReadLength==1); i=0; if((RxChar==ETX)) { while(DataBuffer[i]!=STX) i++; } lpOpPointer=&DataBuffer[i]; if (blogfile) { WriteLogData(hLogFile,lpOpPointer,ByteRecieved); } switch (RxChar) { case ENQ: // Sleep(300); TransmitCommChar(hComm,ACK); break; case ETB: case ETX: //先进行校验的计算,若为正常接受则进行结果处理! //目前,由于资料校验和的计算方法与结果包中数据不符!尚未加校验计算 /* 需要到仪器上读出数据包进行分析 */ switch(*(lpOpPointer+2)) { case 'H': break; case 'P': break; case 'O': nItemCount=0; TrayNum=(int)StrToInt(lpOpPointer+c_TrayNumEOff,c_TrayNumLen); CupNum=(int)StrToInt(lpOpPointer+c_CupNumEOff,c_CupNumLen); wSampleNo =(int)StrToInt(lpOpPointer+c_SequenceNoOff,c_SequenceNoLen); _itoa(wSampleNo,LSampleID,10); TempWorkingID[0]=lpDevice->DevItemType; memset(&TempWorkingID[1],0x30,5); SampleIDLen=strlen(LSampleID); strncpy(&TempWorkingID[6-SampleIDLen],&LSampleID[0],SampleIDLen); TempWorkingID[6]='\0'; break; //继续接收 case 'R': FillSampleID(nItemCount, wSampleNo) strncpy(OutResult[nItemCount].WorkingID,TempWorkingID,7);//填写工作单号 OutResult[nItemCount].ItemNo=nItemCount+1; FillItemName(nItemCount,lpOpPointer+c_ItemNameOff ,c_ItemNameLen) if (OutResult[nItemCount].ItemID[1]==124)//'|' 如果项目名称长度为1则添加字符串尾标志-ASCII 码值为 0 { OutResult[nItemCount].ItemID[1]='\0'; FillResult(nItemCount,lpOpPointer+c_ResultOff-2,c_ResultLen) } else FillResult(nItemCount,lpOpPointer+c_ResultOff,c_ResultLen) OutResult[nItemCount].ItemNo=nItemCount+1; //填日期 GetSystemTime(&SystemTime); FillDate(nItemCount, SystemTime.wYear, SystemTime.wMonth, SystemTime.wDay, SystemTime.wHour,SystemTime.wMinute,SystemTime.wSecond) nItemCount++; (*lpResultProcessRoutine)(lpDevice->nDevNO, &OutResult[nItemCount-1], 1); lpDevice->dwRecordCount+=1; break; case 'L': break; } // Sleep(300); TransmitCommChar(hComm,ACK); break; case EOT: //结果入队列 default: TransmitCommChar(hComm,ACK); break; }
DWORD WINAPI CommMonitor( LPSTR lpData) { //监控串口接收数据,并完成数据格式分析,将结果放入结果队列 int nItemCount,SampleIDLen,nDataOff,nResultLen,iSampleID; bool bLogFile; char TempWorkingID[7]; LPSTR lpPointer; //应答包 char * p_Ack="\x2\x6\x3"; char * p_Nak="\x2\x16\x3"; char * p_IntSessionTXT="\x2I\x20\x3"; char * p_ReleaseMaster="\x2M\x20\x20\x20\x20\x20\x3"; char PackageType; char SampleID[10]; SYSTEMTIME SystemTime; CommData //通信类型1'、1''、2、3、4、5 Init() ResetOpPointer() FillDevName() SetCommWaitMask(EV_RXFLAG) PurgeComm( hComm, PURGE_RXCLEAR ); memset(lpOpPointer , 0, c_RxBufferLength ); if(NewLogFile("AEROSET.txt")) { bLogFile=TRUE; } while (TRUE) { WaitEvent(EV_RXFLAG)//等待接受数据包 GetInQueueLength(dwReadLength)//分析数据 ReadCommBlock(dwReadLength) PackageType=*(lpOpPointer+1);//数据包的类型 if(bLogFile) { WriteLogData(hLogFile,lpOpPointer,dwReadLength); } //校验接收到的数据,若正确,则处理,否则给否定应答。 /* checksum calculation */ /*结果的数据块中可能包含病人信息、ID信息、结果数据等组。*/ PackageType=*(lpOpPointer+1); switch(PackageType) { case 'Q': WriteCommBlock(p_Ack,3)//肯定应答 //WriteCommChar(ACK) break; case 'R': //结果处理 WriteCommBlock(p_Ack,3)//肯定应答 iSampleID=StrToInt(lpOpPointer+c_SampleIDOff,c_SamlpeIDLen); itoa(iSampleID,&SampleID[0],10); SampleIDLen=strlen(SampleID); TempWorkingID[0]=lpDevice->DevItemType; if(SampleIDLen>=6) { strncpy(&TempWorkingID[1],&SampleID[SampleIDLen-5],5); } else strncpy(&TempWorkingID[6-SampleIDLen],SampleID,SampleIDLen); TempWorkingID[6]='\0'; GetLocalTime(&SystemTime); nItemCount=0; for(nDataOff =c_DataOff; *(lpOpPointer+nDataOff)!=ETB ;) { strncpy(OutResult[nItemCount].WorkingID,TempWorkingID,7); OutResult[nItemCount].ItemNo=nItemCount+1; lpPointer=lpOpPointer+nDataOff; DeleSpace(4) strncpy(OutResult[nItemCount].ItemID,lpPointer,nResultLen); OutResult[nItemCount].ItemID[nResultLen]='\0'; lpPointer=lpOpPointer+nDataOff+c_ResultOff; DeleSpace(c_ResultLen ) FillResult(nItemCount, lpPointer ,nResultLen ) FillDate(nItemCount, SystemTime.wYear, SystemTime.wMonth, SystemTime.wDay, SystemTime.wHour,SystemTime.wMinute,SystemTime.wSecond) nDataOff = nDataOff + c_OneDataLen; nItemCount++; } if(nItemCount>0) { (*lpResultProcessRoutine)(lpDevice->nDevNO, OutResult, nItemCount); lpDevice->dwRecordCount+=nItemCount; } //WriteCommChar(ACK) /// WriteCommBlock(p_Ack,3)//肯定应答 break; case ACK: case NAK: break; } ResetRxPointer() } if(bLogFile) { CloseLogFile(hLogFile); } return TRUE; } // end of CommWatchProc()
DWORD WINAPI CommMonitor( LPSTR lpData) { CommData SYSTEMTIME SystemTime; int i,nItemCount,nPackLen=0; BYTE PackFun ; BOOL OddFlag=FALSE; LPSTR lpPointer,lptemp1,lptemp2,hRXBuffer=(LPSTR)RxBuffer; char *p_ACK1="\x10\x31"; char *p_ACK0="\x10\x30"; Init() ResetOpPointer() ResetRxPointer() nItemCount =0; FillDevName() SetCommWaitMask(EV_RXFLAG) PurgeComm( hComm, PURGE_RXCLEAR ); while (TRUE) { WaitEvent(EV_RXFLAG) GetInQueueLength(dwReadLength) ReadCommBlock(dwReadLength) lptemp1=strbchrnb(lpRxPointer,'[',1,(int)dwReadLength+5); PackFun= *(lptemp1+c_FuncNumOff); { case '0': case 'D': ResetRxPointer() ResetOpPointer() continue; case '1': if (lptemp1!=lpOpPointer) { for (i=0;*hRXBuffer!='[';hRXBuffer++,i++); memmove(lpOpPointer ,hRXBuffer , (int)(lpRxPointer-hRXBuffer)); lpRxPointer=lpRxPointer-i; hRXBuffer=(LPSTR)RxBuffer; } continue; case '5': if ((*lpOpPointer)!='[') { for (i=0;*hRXBuffer!='[';hRXBuffer++,i++); memmove(lpOpPointer ,hRXBuffer , (int)(lpRxPointer-hRXBuffer)); lpRxPointer=lpRxPointer-i; hRXBuffer=(LPSTR)RxBuffer; continue; case '9': break; default : continue; } wSampleID =(int)StrToInt(lpOpPointer+7,5); GetLocalTime(&SystemTime); lpPointer=strchrnb(lpOpPointer+1,'[',2,70); while(*(lpPointer+2)=='2') { FillSampleID(nItemCount, wSampleID) lptemp1=strchrnb(lpPointer,',',1,4)+1; lptemp2=strchrnb(lptemp1,',',1,5); FillItemName(nItemCount, lptemp1,(lptemp2-lptemp1)) lptemp1=strchrnb(lptemp2+1,',',2,20)+1; for (;*lptemp1==' ';lptemp1++); lptemp2=strchrnb(lptemp1,',',1,10); FillResult(nItemCount,lptemp1,(int)(lptemp2-lptemp1)) FillDate(nItemCount, SystemTime.wYear, SystemTime.wMonth, SystemTime.wDay, SystemTime.wHour) lpPointer=strchrnb(lptemp2+1,'[',1,40); nItemCount++; } if(nItemCount>0) { (*lpResultProcessRoutine)(lpDevice->nDevNO, OutResult, nItemCount); lpDevice->dwRecordCount+=nItemCount; } nItemCount=0; // nPackLen=0; ResetRxPointer() ResetOpPointer() hRXBuffer=(LPSTR)RxBuffer; } return TRUE; } // end of CommWatchProc() void WINAPI BeforeSetCommState(DCB *pComDcb) { pComDcb->EvtChar ='['; }