Esempio n. 1
0
ssap_message* generateBulkMessage(const char* sessionKey, const char* ontology, bulkRequest* request){
    ssap_message* bulkMessage = allocateSsapMessage();
    cJSON *body;
    
    body = cJSON_CreateArray();
    size_t i;
    for (i = 0; i < request->size; i++){
      cJSON *ssapBulkItem = cJSON_CreateObject();
      ssap_message* msg = request->payload[i];
      cJSON_AddItemToObject(ssapBulkItem, "type", cJSON_CreateString(messageTypeToString(msg->messageType)));
      cJSON_AddItemToObject(ssapBulkItem, "body", cJSON_CreateString(msg->body));
      cJSON_AddItemToObject(ssapBulkItem, "ontology", cJSON_CreateString(msg->ontology));
      cJSON_AddItemToArray(body, ssapBulkItem);
    }    
    
    bulkMessage->body=cJSON_PrintUnformatted(body);
    bulkMessage->direction = REQUEST;
    bulkMessage->messageId=NULL;
    bulkMessage->messageType=BULK;
    bulkMessage->ontology=(char*) malloc((strlen(ontology)+1)*sizeof(char));
    strcpy(bulkMessage->ontology, ontology);
    bulkMessage->sessionKey=(char*) malloc((strlen(sessionKey)+1)*sizeof(char));
    strcpy(bulkMessage->sessionKey, sessionKey);
    bulkMessage->persistenceType=MONGODB;
 
    cJSON_Delete(body);
    freeBulkRequest(request);
    return bulkMessage;
}
Esempio n. 2
0
void MigratorBase::logMessage(MigratorBase::MessageType type, const QString &msg)
{
    if (mLogFile) {
        mLogFile->write(QString(QLatin1Char('[') + QDateTime::currentDateTime().toString() + QStringLiteral("] ")
                                + messageTypeToString(type) + QStringLiteral(": ") + msg + QLatin1Char('\n')).toUtf8());
        mLogFile->flush();
    }
}
Esempio n. 3
0
bool NETlogEntry(const char *str,UDWORD a,UDWORD b)
{
    static const char star_line[] = "************************************************************\n";
    static UDWORD lastframe = 0;
    UDWORD frame= frameGetFrameNumber();
    time_t aclock;
    struct tm *newtime;
    char buf[256];

    if (!pFileHandle)
    {
        return false;
    }

    time( &aclock );					/* Get time in seconds */
    newtime = localtime( &aclock );		/* Convert time to struct */

    if (!newtime || !str || !pFileHandle)
    {
        debug(LOG_ERROR, "Fatal error averted in NETlog");
        return false;
    }

    // check to see if a new frame.
    if(frame != lastframe)
    {
        static const char dash_line[] = "-----------------------------------------------------------\n";

        lastframe = frame;
        PHYSFS_write(pFileHandle, dash_line, strlen(dash_line), 1);
    }

    if (a < NUM_GAME_PACKETS)
        // replace common msgs with txt descriptions
        snprintf(buf, sizeof(buf), "%s \t: %s \t:%d\t\t%s", str, messageTypeToString(a), b, asctime(newtime));
    else if (a == SYNC_FLAG)
        snprintf(buf, sizeof(buf), "%s \t: %d \t(Sync) \t%s", str, b, asctime(newtime));
    else
        snprintf(buf, sizeof(buf), "%s \t:%d \t\t\t:%d\t\t%s", str, a, b, asctime(newtime));

    if (a == NET_PLAYER_LEAVING || a == NET_PLAYER_DROPPED )
    {
        // Write a starry line above NET_LEAVING messages
        PHYSFS_write(pFileHandle, star_line, strlen(star_line), 1);
        PHYSFS_write(pFileHandle, buf, strlen( buf ), 1);
        PHYSFS_write(pFileHandle, star_line, strlen(star_line), 1);
    }
    else
    {
        PHYSFS_write(pFileHandle, buf, strlen( buf ), 1);
    }

    PHYSFS_flush(pFileHandle);
    return true;
}
Esempio n. 4
0
char* ssap_messageToJson(ssap_message* message){
    cJSON *root;
    
    root=cJSON_CreateObject();  
    
    if(message->body==NULL || strcmp(message->body, "")==0){
        cJSON_AddItemToObject(root, "body", cJSON_CreateNull());
    }else{
         cJSON_AddItemToObject(root, "body", cJSON_CreateString(message->body));
    }
    
    switch(message->direction){
        case REQUEST: cJSON_AddItemToObject(root, "direction", cJSON_CreateString("REQUEST"));
                      break;
        case RESPONSE: cJSON_AddItemToObject(root, "direction", cJSON_CreateString("RESPONSE"));
                      break;
        default:      cJSON_AddItemToObject(root, "direction", cJSON_CreateNull());
                      break;
    }
    
    cJSON_AddItemToObject(root, "messageType", cJSON_CreateString(messageTypeToString(message->messageType)));
    
    if(message->messageId==NULL || strcmp(message->messageId, "")==0){
        cJSON_AddItemToObject(root, "messageId", cJSON_CreateNull());
    }else{
         cJSON_AddItemToObject(root, "messageId", cJSON_CreateString(message->messageId));
    }
    
    if(message->ontology==NULL || strcmp(message->ontology, "")==0){
        cJSON_AddItemToObject(root, "ontology", cJSON_CreateNull());
    }else{
         cJSON_AddItemToObject(root, "ontology", cJSON_CreateString(message->ontology));
    }
    
    
    if(message->sessionKey==NULL || strcmp(message->sessionKey, "")==0){
        cJSON_AddItemToObject(root, "sessionKey", cJSON_CreateNull());
    }else{
         cJSON_AddItemToObject(root, "sessionKey", cJSON_CreateString(message->sessionKey));
    }  
    char* retorno = cJSON_PrintUnformatted(root);
    
    cJSON_Delete(root); 
    
    return retorno;
}
Esempio n. 5
0
bool NETstopLogging(void)
{
    static const char dash_line[] = "-----------------------------------------------------------\n";
    char buf[256];
    int i;
    UDWORD totalBytessent = 0, totalBytesrecv = 0, totalPacketsent = 0, totalPacketrecv = 0;

    if (!pFileHandle)
    {
        return false;
    }

    /* Output stats */
    for (i = 0; i < NUM_GAME_PACKETS; i++)
    {
        snprintf(buf, sizeof(buf), "%-24s:\t received %u times, %u bytes; sent %u times, %u bytes\n", messageTypeToString(i),
                 packetcount[1][i], packetsize[1][i], packetcount[0][i], packetsize[0][i]);
        PHYSFS_write(pFileHandle, buf, strlen(buf), 1);
        totalBytessent += packetsize[0][i];
        totalBytesrecv += packetsize[1][i];
        totalPacketsent += packetcount[0][i];
        totalPacketrecv += packetcount[1][i];
    }
    snprintf(buf, sizeof(buf), "== Total bytes sent %u, Total bytes received %u ==\n", totalBytessent, totalBytesrecv);
    PHYSFS_write(pFileHandle, buf, strlen(buf), 1);
    snprintf(buf, sizeof(buf), "== Total packets sent %u, recv %u ==\n", totalPacketsent, totalPacketrecv);
    PHYSFS_write(pFileHandle, buf, strlen(buf), 1);
    snprintf(buf, sizeof(buf), "\n-Sync statistics -\n");
    PHYSFS_write(pFileHandle, buf, strlen(buf), 1);
    PHYSFS_write(pFileHandle, dash_line, strlen(dash_line), 1);
    snprintf(buf, sizeof(buf), "joins: %hhu, kicks: %hhu, drops: %hhu, left %hhu\n", sync_counter.joins, sync_counter.kicks, sync_counter.drops, sync_counter.left );
    PHYSFS_write(pFileHandle, buf, strlen(buf), 1);
    snprintf(buf, sizeof(buf), "banned: %hhu, cantjoin: %hhu, rejected: %hhu\n", sync_counter.banned, sync_counter.cantjoin, sync_counter.rejected );
    PHYSFS_write(pFileHandle, buf, strlen(buf), 1);
    if (sync_counter.banned && IPlist)
    {
        snprintf(buf, sizeof(buf), "Banned list:\n");
        PHYSFS_write(pFileHandle, buf, strlen(buf), 1);
        for (i = 0; i < MAX_BANS; i++)
        {
            if (IPlist[i].IPAddress[0] != '\0')
            {
                snprintf(buf, sizeof(buf), "player %s, IP: %s\n", IPlist[i].pname, IPlist[i].IPAddress);
                PHYSFS_write(pFileHandle, buf, strlen(buf), 1);
            }
        }

    }
    PHYSFS_write(pFileHandle, dash_line, strlen(dash_line), 1);
    PHYSFS_write(pFileHandle, dash_line, strlen(dash_line), 1);

    if (!PHYSFS_close(pFileHandle))
    {
        debug(LOG_ERROR, "Could not close net log: %s", PHYSFS_getLastError());
        return false;
    }
    pFileHandle = NULL;

    return true;
}
Esempio n. 6
0
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
// Recv Messages. Get a message and dispatch to relevant function.
bool recvMessage(void)
{
	NETQUEUE queue;
	uint8_t type;

	while (NETrecvNet(&queue, &type) || NETrecvGame(&queue, &type))          // for all incoming messages.
	{
		bool processedMessage1 = false;
		bool processedMessage2 = false;

		if (queue.queueType == QUEUE_GAME)
		{
			syncDebug("Processing player %d, message %s", queue.index, messageTypeToString(type));
		}

		// messages only in game.
		if(!ingame.localJoiningInProgress)
		{
			processedMessage1 = true;
			switch(type)
			{
			case GAME_DROIDINFO:					//droid update info
				recvDroidInfo(queue);
				break;
			case NET_TEXTMSG:					// simple text message
				recvTextMessage(queue);
				break;
			case NET_DATA_CHECK:
				recvDataCheck(queue);
				break;
			case NET_AITEXTMSG:					//multiplayer AI text message
				recvTextMessageAI(queue);
				break;
			case NET_BEACONMSG:					//beacon (blip) message
				recvBeacon(queue);
				break;
			case GAME_DROIDDISEMBARK:
				recvDroidDisEmbark(queue);           //droid has disembarked from a Transporter
				break;
			case GAME_GIFT:						// an alliance gift from one player to another.
				recvGift(queue);
				break;
			case GAME_LASSAT:
				recvLasSat(queue);
				break;
			case GAME_DEBUG_MODE:
				recvProcessDebugMappings(queue);
				break;
			case GAME_DEBUG_ADD_DROID:
				recvDroid(queue);
				break;
			case GAME_DEBUG_ADD_STRUCTURE:
				recvBuildFinished(queue);
				break;
			case GAME_DEBUG_ADD_FEATURE:
				recvMultiPlayerFeature(queue);
				break;
			case GAME_DEBUG_REMOVE_DROID:
				recvDestroyDroid(queue);
				break;
			case GAME_DEBUG_REMOVE_STRUCTURE:
				recvDestroyStructure(queue);
				break;
			case GAME_DEBUG_REMOVE_FEATURE:
				recvDestroyFeature(queue);
				break;
			case GAME_DEBUG_FINISH_RESEARCH:
				recvResearch(queue);
				break;
			default:
				processedMessage1 = false;
				break;
			}
		}

		// messages usable all the time
		processedMessage2 = true;
		switch(type)
		{
		case GAME_TEMPLATE:					// new template
			recvTemplate(queue);
			break;
		case GAME_TEMPLATEDEST:				// template destroy
			recvDestroyTemplate(queue);
			break;
		case NET_PING:						// diagnostic ping msg.
			recvPing(queue);
			break;
		case NET_OPTIONS:
			recvOptions(queue);
			break;
		case NET_PLAYER_DROPPED:				// remote player got disconnected
		{
			uint32_t player_id;

			NETbeginDecode(queue, NET_PLAYER_DROPPED);
			{
				NETuint32_t(&player_id);
			}
			NETend();

			if (whosResponsible(player_id) != queue.index && queue.index != NET_HOST_ONLY)
			{
				HandleBadParam("NET_PLAYER_DROPPED given incorrect params.", player_id, queue.index);
				break;
			}

			debug(LOG_INFO,"** player %u has dropped!", player_id);

			if (NetPlay.players[player_id].allocated)
			{
				MultiPlayerLeave(player_id);		// get rid of their stuff
				NET_InitPlayer(player_id, false);
			}
			NETsetPlayerConnectionStatus(CONNECTIONSTATUS_PLAYER_DROPPED, player_id);
			break;
		}
		case NET_PLAYERRESPONDING:			// remote player is now playing
		{
			uint32_t player_id;

			resetReadyStatus(false);

			NETbeginDecode(queue, NET_PLAYERRESPONDING);
				// the player that has just responded
				NETuint32_t(&player_id);
			NETend();
			if (player_id >= MAX_PLAYERS)
			{
				debug(LOG_ERROR, "Bad NET_PLAYERRESPONDING received, ID is %d", (int)player_id);
				break;
			}
			// This player is now with us!
			ingame.JoiningInProgress[player_id] = false;
			break;
		}
		// FIXME: the next 4 cases might not belong here --check (we got two loops for this)
		case NET_COLOURREQUEST:
			recvColourRequest(queue);
			break;
		case NET_POSITIONREQUEST:
			recvPositionRequest(queue);
			break;
		case NET_TEAMREQUEST:
			recvTeamRequest(queue);
			break;
		case NET_READY_REQUEST:
			recvReadyRequest(queue);

			// if hosting try to start the game if everyone is ready
			if(NetPlay.isHost && multiplayPlayersReady(false))
			{
				startMultiplayerGame();
			}
			break;
		case GAME_ALLIANCE:
			recvAlliance(queue, true);
			break;
		case NET_KICK:	// in-game kick message
		{
			uint32_t player_id;
			char reason[MAX_KICK_REASON];
			LOBBY_ERROR_TYPES KICK_TYPE = ERROR_NOERROR;

			NETbeginDecode(queue, NET_KICK);
				NETuint32_t(&player_id);
				NETstring(reason, MAX_KICK_REASON);
				NETenum(&KICK_TYPE);
			NETend();

			if (player_id == NET_HOST_ONLY)
			{
				char buf[250]= {'\0'};

				ssprintf(buf, "Player %d (%s : %s) tried to kick %u", (int) queue.index, NetPlay.players[queue.index].name, NetPlay.players[queue.index].IPtextAddress, player_id);
				NETlogEntry(buf, SYNC_FLAG, 0);
				debug(LOG_ERROR, "%s", buf);
				if (NetPlay.isHost)
				{
					NETplayerKicked((unsigned int) queue.index);
				}
				break;
			}
			else if (selectedPlayer == player_id)  // we've been told to leave.
			{
				debug(LOG_ERROR, "You were kicked because %s", reason);
				setPlayerHasLost(true);
			}
			else
			{
				debug(LOG_NET, "Player %d was kicked: %s", player_id, reason);
				NETplayerKicked(player_id);
			}
			break;
		}
		case GAME_RESEARCHSTATUS:
			recvResearchStatus(queue);
			break;
		case GAME_STRUCTUREINFO:
			recvStructureInfo(queue);
			break;
		case NET_PLAYER_STATS:
			recvMultiStats(queue);
			break;
		case GAME_PLAYER_LEFT:
			recvPlayerLeft(queue);
			break;
		default:
			processedMessage2 = false;
			break;
		}

		if (processedMessage1 && processedMessage2)
		{
			debug(LOG_ERROR, "Processed %s message twice!", messageTypeToString(type));
		}
		if (!processedMessage1 && !processedMessage2)
		{
			debug(LOG_ERROR, "Didn't handle %s message!", messageTypeToString(type));
		}

		NETpop(queue);
	}

	return true;
}