void GChannel::SendPacket(GeneralPacket& packet)
{
	if (!m_isOpen)
	{
		THROW_EXCEPTION1(ChannelException, "Channel not open!");
	}

	const util::NetworkOrder& network = util::NetworkOrder::Instance();

	packet.version = 3;  //we support version 3

	std::basic_ostringstream<boost::uint8_t> buffer;
	util::binary_write(buffer, (boost::uint8_t) packet.version, network);
	util::binary_write(buffer, (boost::uint8_t) packet.serviceType, network);

	if(packet.version == 3)
	{
		util::binary_write(buffer, (boost::uint32_t) packet.sessionID, network);
	}
	else
	{
		THROW_EXCEPTION1(ChannelException, "Invalid packet version (!= 3)!");
	}

	util::binary_write(buffer, packet.trackingID, network);
	util::binary_write(buffer, (boost::uint32_t) packet.dataSize, network);
	
	//CRC32C
	{
		std::basic_string<boost::uint8_t> my_string(buffer.str());
		packet.headerCRC = GenerateCRC32C(my_string.c_str(), my_string.size());
		packet.headerCRC = htonl(packet.headerCRC);
		buffer.write((boost::uint8_t*)&packet.headerCRC, sizeof(unsigned int));
	}

	if (packet.dataSize > 0)
		buffer.write(packet.data.c_str(), packet.dataSize);

	//
	std::basic_string<boost::uint8_t> string(buffer.str());
	try
	{
		m_socket->SendBytes((const char*) string.c_str(), string.size());

		if (LOG_DEBUG_ENABLED())
		{
			std::ostringstream dataBytes;
			dataBytes << std::hex << std::uppercase << std::setfill('0');
			for (int i = 0; i < (int) string.size(); i++)
			{
				dataBytes << std::setw(2) << (int) string.at(i) << ' ';
			}
			LOG_DEBUG("SendPacket:packet=" << packet.ToString() << ",  bytes=<" << dataBytes.str() << ">");
		}
	}
	catch (std::exception& ex)
	{
		LOG_ERROR("SendPacket: Failed to write on socket. error=" << ex.what());
		THROW_EXCEPTION1(ChannelException, "Failed to write on channel!");
	}
	catch (...)
	{
		LOG_ERROR("SendPacket: Failed to write on socket. unknown error!");
		THROW_EXCEPTION1(ChannelException, "Failed to write on channel!");
	}
}
AbstractAppCommandPtr AppCommandsFactory::Create(DBCommand& command, DevicesManager& devices)
{
	LOG_DEBUG_APP("[AppCommand]: create for db command:" << command);

	switch ( command.commandCode )
	{
	case DBCommand::ccGetTopology:
		CheckUsrCmdInputVal(devices).TestUsrParams_ccGetTopology(command);
		return AbstractAppCommandPtr(new AppTopologyCommand());

	case DBCommand::ccNotifSubscribe:
	{
		DevicePtr dev;
		CheckUsrCmdInputVal(devices).TestUsrParams_ccNotifSubscribe(command, dev);
		return AbstractAppCommandPtr(new AppSetBurstNotificationCmd(dev->GetPublisherInfo().channelList, 
			dev->GetPublisherInfo().burstMessageList, dev->GetPublisherInfo().triggersList));
	}
	case DBCommand::ccNotifUnSubscribe:
		CheckUsrCmdInputVal(devices).TestUsrParams_ccNotifUnSubscribe(command);
		return AbstractAppCommandPtr(new AppUnsetBurstNotificationCmd());
	
	case DBCommand::ccTopologyNotify:
		CheckUsrCmdInputVal(devices).TestUsrParams_ccTopologyNotify(command);
		return AbstractAppCommandPtr(new AppSetTopoNotificationCmd());

	case DBCommand::ccGeneralCmd:
	{
		int retValueCmdNo;  /* cmdNo */
		std::string retValueDataBytes; /* dataBytes */ 
		bool bypassIOCache;
		CheckUsrCmdInputVal(devices).TestUsrParams_ccGeneralCmd(command, retValueCmdNo, retValueDataBytes, bypassIOCache);
		return AbstractAppCommandPtr(new AppGeneralCommand(retValueCmdNo, retValueDataBytes, command.commandID, command.deviceID, bypassIOCache));	
	}
	case DBCommand::ccReadValue:
		{
		/*compute the channel list for the req cmd*/
		PublishChannel channel;
		bool bypassIOCache;
		CheckUsrCmdInputVal(devices).TestUsrParams_ccReadValue(command, channel, bypassIOCache);
		PublishChannelSetT channelsSet;
		channelsSet.insert(channel);
		return AbstractAppCommandPtr(new AppReadValueCmd( channel.cmdNo, command.deviceID, DeviceReading::ReadValue, channelsSet, bypassIOCache));
		}
	case DBCommand::ccRoutesReport:
		return AbstractAppCommandPtr(new AppRoutesReportCmd(devices.RegisteredDevicesNo()));
	case DBCommand::ccServicesReport:
		return AbstractAppCommandPtr(new AppServicesReportCmd(devices.RegisteredDevicesNo()));
	case DBCommand::ccDeviceHealthReport:
	{
		std::list<std::pair<int, MAC> > devicesList;
		CheckUsrCmdInputVal(devices).TestUsrParams_ccDevHealthReport(command, devicesList);
		return AbstractAppCommandPtr(new AppDeviceHealthReportCmd(devicesList));
	}
	case DBCommand::ccSuperframesReport:
		return AbstractAppCommandPtr(new AppSuperframesReportCmd(devices.RegisteredDevicesNo()));
	case DBCommand::ccDeviceScheduleLinkReport:
	{
		std::list<std::pair<int,MAC> > devicesList;
		CheckUsrCmdInputVal(devices).TestUsrParams_ccDeviceScheduleLinkReport(command, devicesList);
		return AbstractAppCommandPtr(new AppDeviceScheduleLinksReportCmd(devicesList));
	}		
	case DBCommand::ccNeighborHealthReport:
	{
		std::list<std::pair<int, MAC> > devicesList;
		CheckUsrCmdInputVal(devices).TestUsrParams_ccNeighborHealthReport(command, devicesList);
		return AbstractAppCommandPtr(new AppNeighborHealthReportCmd(devicesList));
	}
	case DBCommand::ccAutodetectBurstsConfig:
	{
		DevicePtr dev;
		std::string pubConfFileName;
		CheckUsrCmdInputVal(devices).TestUsrParams_ccBurstConfiguration(command, dev, pubConfFileName);
		//for cancelling command
		dev->configBurstDBCmdID = command.commandID;
		return AbstractAppCommandPtr(new AppSetBurstConfigurationCmd(pubConfFileName, dev));
	}
    case DBCommand::ccReadBurstConfig:
    {
        DevicePtr dev;
        CheckUsrCmdInputVal(devices).TestUsrParams_ccReadBurstConfig(command, dev);
        MAC mac = dev->Mac();
        return AbstractAppCommandPtr(new AppDiscoveryBurstConfigCmd(mac));
    }
	default:
		break;
	}

	LOG_ERROR_APP("[AppCommand]: create with Unknown CommandCode=" << command.commandCode);
	THROW_EXCEPTION1(InvalidCommandException, "unknown command");
}
Example #3
0
void ConfigApp::Load()
{
    CConfigExt oConf ;
    if ( ! oConf.Load(configFilePath.c_str()) )
    {   char szLocal[ 128 ];
        snprintf( szLocal, sizeof(szLocal), "Unable to load config file[%s]\n", configFilePath.c_str());
        szLocal[sizeof(szLocal) - 1] = 0;
        THROW_EXCEPTION1(InvalidConfigAppException, szLocal );
    }
    oConf.GetVar( "LogConfigPath", logConfigPath, MHOST_LOG_INI );
    oConf.GetVar( "DatabasePath", databasePath,   MHOST_DB3 );
    oConf.GetVar( "DatabaseServer", databaseServer, "127.0.0.1" );
    oConf.GetVar( "DatabaseName", databaseName, "Monitor_Host" );
    oConf.GetVar( "DatabaseUser", databaseUser, "root" );
    oConf.GetVar( "DatabasePassword", databasePassword, "" );
    oConf.GetVar( "DatabaseTimeout", databaseTimeout, 10 );
    oConf.GetVar( "DatabaseVacuumPeriodMinutes", databaseVacuumPeriodMinutes, 30 );
    oConf.GetVar( "DatabaseRemoveEntriesCheckPeriodMinutes", databaseRemoveEntriesCheckPeriodMinutes, 10 );
    oConf.GetVar( "DatabaseRemoveEntriesOlderThanMinutes", databaseRemoveOlderEntriesThanMinutes, 30 );

    oConf.GetVar( "DatabaseRemoveAlarmEntriesOlderThanDays", databaseRemoveOlderAlarmEntriesThanDays , 4 );

    oConf.GetVar( "GatewayHost", gatewayHost, "127.0.0.1" );
    oConf.GetVar( "GatewayPort", gatewayPort, 4900 );
    oConf.GetVar( "GatewayListenMode", gatewayListenMode, false );
    oConf.GetVar( "GatewayPacketVersion", gatewayPacketVersion, 1 );
    oConf.GetVar( "CommandsTimeout", commandsTimeout, 60 );
    oConf.GetVar( "CommandsRetryCountIfTimeout", retryCountIfTimeout, 3 );
    oConf.GetVar( "TopologyPooling", topologyPool, 500 );
    oConf.GetVar( "DevicesListPooling", devicesListPool, 500 );
    oConf.GetVar( "firmDlContractsPooling", firmDlContractsPeriod, 30 );
    oConf.GetVar( "ScheduleReportPooling", ScheduleReportPool, 500 );
    oConf.GetVar( "DeviceHealthReportPooling", DeviceReportPool, 500 );
    oConf.GetVar( "NetworkHealthReportPooling", NetworkReportPool, 500 );
    oConf.GetVar( "NeighbourHealthReportPooling", NeighbourReportPool, 500 );
    oConf.GetVar( "ContractsAndRoutesPooling", ContractsAndRoutesPool, 500 );
    oConf.GetVar( "CommandsCheckPeriod", threadCheckPeriod, 2 );
    oConf.GetVar( "UseReadingsHistory", m_nDeviceReadingsHistoryEnable, 0 );
    oConf.GetVar( "ReadingsSavePeriod", readingsBulkSaverPeriod, 10 );
    oConf.GetVar( "ReadingsMaxEntriesBeforeSave", readingsBulkSaverMaxReadings, 10 );
    oConf.GetVar( "BulkDataTransferRate", bulkDataTransferRate, 85 );
    oConf.GetVar( "DelayPeriodBeforeFirmwareRetry", delayPeriodBeforeFirmwareRetry, 1 );
    oConf.GetVar( "PathToFiles", pathToFirmwareFiles, "/usr/local/NISA/Data/DeviceFirmwares/" );
    oConf.GetVar( "PubConfigPath",strPubConfigFile, "" );

    oConf.GetVar( "HistoryStatusSize", history_status_size, 6);

    //security
    oConf.GetVar( "UseEncryption", m_UseEncryption, 0 );
    oConf.GetVar( "ClientCertifFile", m_sslClientCertifFile, "/access_node/activity_files/clientcert.pem" );
    oConf.GetVar( "ClientKeyFile", m_sslClientKeyFile, "/access_node/activity_files/clientkey.pem" );
    oConf.GetVar( "CACertFile", m_sslCACertFile, "/access_node/activity_files/cacert.pem" );


    oConf.GetVar( "LeasePeriod", m_leasePeriod, 15*60 /* 15 minutes */);
    oConf.GetVar( "LeaseCommittedBurst", m_leaseCommittedBurst, 0 /*default for gw*/ );

    oConf.GetVar( "SavePublishPeriod", (int&)m_savePublishPeriod, 2000 /*msec default*/ );

    //added by Cristian.Guef
    if ( InitLogEnv(logConfigPath.c_str()) == false)
    {
        char szLocal[ 128 ];
        snprintf( szLocal, sizeof(szLocal), "Unable to load LOG config file[%s]\n", logConfigPath.c_str());
        szLocal[sizeof(szLocal) - 1] = 0;
        THROW_EXCEPTION1(InvalidConfigAppException,	szLocal);
    }

    //added by Cristian.Guef read publisher list -it is delayed now because there should be done alert_subscription
    //PublisherConf().LoadPublishers(strPubConfigFile.c_str(), PublishersMapStored);
    //processSignal2 = true;
}