/* Initializes hardware- and software-Uart. Returns true if new module connects at the softSerial, false otherwise. Result value can be use to indicate whether a new module must be enqued in masters topology table. Must be called periodically (in the main loop) */ bool Communication::connect(byte* adress, bool* orientation) { bool result = false; //Upstream interface if(!uartConnected) { // send my adress over the hardware serial towards the potential master as init beacon until initial handshake is completed handShake(); } //Downstream interface if(!softwareUartConnected && uartConnected){ // listen at the downstream interface only, if upstream connection (to the master) is already established result = initSoftwareUart(&*adress, &*orientation); } return result; }
int sampleMethod() { int val,i,j,k; int * retVal; setPropertyFileName(".\\stackgameclient.properties"); printf("Waiting for handshake ... ...\n"); retVal = handShake(); printf("The response is: %d %d %d\n",retVal[0],retVal[1],retVal[2]); if (retVal[1] == RED_COINS) { Op = 4; Iam = 3; } else { Iam = 4; Op = 3; } if(retVal[2] == PLAY_SECOND) { printf("I am playing Second\n"); play =2; retVal = readMoves(); printf("The response is: %d %d %d %d %d %d\n",retVal[0],retVal[1],retVal[2],retVal[3],retVal[4],retVal[5]); retVal[1]-=1; retVal[2]-=1; retVal[4]-=1; retVal[5]-=1; update_board(retVal,1); for(i=0;i<8;i++) for(j=0;j<8;j++) for(k=0;k<5;k++) board[i][j][k] = t_board[i][j][k]; for(i=0;i<8;i++) for(j=0;j<8;j++) top[i][j] = t_top[i][j]; res = t_res; cap = t_cap; opp_res = t_opp_res; opp_cap = t_opp_cap; } else { play=1; printf("I am playing First\n"); } while(1) { val = intelligence(); for(i=0;i<8;i++) for(j=0;j<8;j++) for(k=0;k<5;k++) t_board[i][j][k] = board[i][j][k]; for(i=0;i<8;i++) for(j=0;j<8;j++) t_top[i][j] = top[i][j]; t_res = res; t_cap = cap; t_opp_res = opp_res; t_opp_cap = opp_cap; update_board(all_moves[val],0); for(i=0;i<8;i++) for(j=0;j<8;j++) for(k=0;k<5;k++) board[i][j][k] = t_board[i][j][k]; for(i=0;i<8;i++) for(j=0;j<8;j++) top[i][j] = t_top[i][j]; res = t_res; cap = t_cap; opp_res = t_opp_res; opp_cap = t_opp_cap; writeMoves(all_moves[val][0],all_moves[val][1]+1,all_moves[val][2]+1,all_moves[val][3],all_moves[val][4]+1,all_moves[val][5]+1); printf("Waiting for response ... ...\n"); retVal = readMoves(); printf("The response is: %d %d %d %d %d %d\n",retVal[0],retVal[1],retVal[2],retVal[3],retVal[4],retVal[5]); retVal[1]-=1; retVal[2]-=1; retVal[4]-=1; retVal[5]-=1; for(i=0;i<8;i++) for(j=0;j<8;j++) for(k=0;k<5;k++) t_board[i][j][k] = board[i][j][k]; for(i=0;i<8;i++) for(j=0;j<8;j++) t_top[i][j] = top[i][j]; t_res = res; t_cap = cap; t_opp_res = opp_res; t_opp_cap = opp_cap; update_board(retVal,1); for(i=0;i<8;i++) for(j=0;j<8;j++) for(k=0;k<5;k++) board[i][j][k] = t_board[i][j][k]; for(i=0;i<8;i++) for(j=0;j<8;j++) top[i][j] = t_top[i][j]; res = t_res; cap = t_cap; opp_res = t_opp_res; opp_cap = t_opp_cap; if(retVal[0] !=0 && retVal[0] != 1) { if(retVal[0] == GAME_ENDED) { break; } else if(retVal[0] == GAME_RESET) { break; } else if(retVal[0] == INVALID_MOVE) { break; } } } }
void ServerThread::run(void* dummy) { UInt32 tag; UserNetworkIdentification otherID; StreamSocket * socket; printd(INFO, "ServerThread::run(): waiting for client...\n"); while (!shutdown) { try { socket = NULL; //ExtendedBinaryMessage message; // wait for incomming messages if (!client.waitReadable(5.0)) continue; // open new socket for incomming communication socket = new StreamSocket(client.accept()); printd(INFO, "ServerThread::run(): Client connected!\n"); // update the local IP-address if not set yet if (internalNetwork->myId.address.ipAddress == 0x7F000001) internalNetwork->setLocalIPAddress(socket); printd(INFO, "ServerThread::run(): calling handShake!\n"); // try to do handShake with other side if (!handShake(socket, &otherID, tag)) { printd(ERROR, "ServerThread::run(): handShake FAILED!\n"); socket->close(); continue; } // if internalNetwork->addConnectionToNetwork(socket, otherID); // if (tag != internalNetwork->quickConnectTag) // { // printd(INFO, "ServerThread::run(): creating new SocketListEntry!\n"); // SocketListEntry* entry = new SocketListEntry(); // entry->socketTCP = socket; // entry->id = otherID; // entry->nextMsg = NULL; // entry->prioritizedMsg = NULL; // // internalNetwork->socketListLock->aquire(); // internalNetwork->socketList.push_back(entry); // internalNetwork->socketListLock->release(); // } // if } catch (SocketException &e) { printd(ERROR, "ServerThread::run(): network error: %s\n", e.what()); sleep(1); } } client.close(); #if OSG_MAJOR_VERSION >= 2 // Enter Barrier BarrierRefPtr cleanupBarrier = OSG::dynamic_pointer_cast<OSG::Barrier> (ThreadManager::the()->getBarrier( "cleanupBarrier",false)); #else //OpenSG1: // Enter Barrier Barrier* cleanupBarrier = dynamic_cast<Barrier*> (ThreadManager::the()->getBarrier( "cleanupBarrier")); #endif printd(INFO, "ServerThread::run(): entering cleanupBarrier with counter 3!\n"); cleanupBarrier->enter(3); printd(INFO, "ServerThread::run(): goodby!\n"); }