Example #1
0
        /// <summary>
        /// Executes LogExchanger steps.
        /// </summary>
        public void Execute()
        {
            if (!_bGlobalsSet)
            {
                return;
            }

            if (!FileAccessRights())
            {
                return;
            }

            if (!TryRecreateDirectoryStructureIfNecessary())
            {
                return;
            }

            ChannelSubscriptions channelSubscriptions = GetChannelSubscriptions();

            _logger.WriteMessage("GetChannelSubscriptions returned.");

            if (channelSubscriptions == null)
            {
                return; // log exchanger cannot run without channel subscriptions
            }
            // lock and load raw logs.
            bool bRawLogsExist = LoadRawAll();

            TimeSpanWrapperClass dateTimeDiff = GetDateTimeDiff();

            foreach (ChannelSubscription channelSubscription in channelSubscriptions.SubscriptionSet)
            {
                if (!AggregateAndUploadNonAdvertSpecificLogs(channelSubscription.ChannelID, channelSubscription.GetGUIDSuffix(), dateTimeDiff, bRawLogsExist))
                {
                    return;
                }
            }

            // adverts: channel 0
            if (!AggregateAndUploadNonAdvertSpecificLogs(0, "", dateTimeDiff, bRawLogsExist))
            {
                return;
            }

            // advert-specific logs (adverts - channel weighting calculations)
            AggregateAndUploadAdvertSpecificLogs(channelSubscriptions, dateTimeDiff, bRawLogsExist);

            // if raw files can't be truncated they will be reprocessed.
            if (!TruncateRawFiles())
            {
                ReleaseRawFiles();
            }

            TryUploadTruncateReleaseUsageCountFile(bRawLogsExist);

            _userDataMarshallerStreamerClient.Dispose();
        }
Example #2
0
        private bool TryReadTimeDiffFromFile(ref TimeSpanWrapperClass timeSpanWrapperClass)
        {
            User user = null;

            try
            {
                user = (User)Serializer.Deserialize(typeof(User), _userSettingsPath, _password);
            }
            catch (Exception ex)
            {
                _logger.WriteError(ex);
                return(false);
            }

            timeSpanWrapperClass = new TimeSpanWrapperClass(user.LastTimeDiff);

            return(true);
        }
Example #3
0
        private bool TryWriteTimeDiffToFile(TimeSpanWrapperClass dateTimeDiff)
        {
            if (dateTimeDiff == null)
            {
                return(true);
            }

            try
            {
                User user = (User)Serializer.Deserialize(typeof(User), _userSettingsPath, _password);

                user.LastTimeDiff = dateTimeDiff.TimeSpan;

                Serializer.Serialize(user, _userSettingsPath, _password);
            }
            catch (Exception ex)
            {
                _logger.WriteError(ex);
                return(false);
            }

            return(true);
        }
Example #4
0
        private TimeSpanWrapperClass GetDateTimeDiff()
        {
            TimeSpanWrapperClass dateTimeDiff = null;

            string timeServerURI = GetResponsiveServer(ServerType.RelayLogs, _maxNoRelayLogServers, _machineGUIDSuffix, "UserDataMarshaller.svc");

            // if responsive server found
            if (timeServerURI != "")
            {
                _userDataMarshallerClient.Endpoint.Address = new EndpointAddress(timeServerURI);

                dateTimeDiff = GetTimeDiff(_dateTimeDiffTolerance);

                // if not able to write time difference to file, don't time-correct
                TryWriteTimeDiffToFile(dateTimeDiff);
            }
            else // if not able to connect, read existing time difference from file, if it exists
            {
                // if not able to read an existing time difference, don't time-correct
                TryReadTimeDiffFromFile(ref dateTimeDiff);
            }

            return(dateTimeDiff);
        }
Example #5
0
        private bool AggregateAndUploadAdvertSpecificLogs(ChannelSubscriptions channelSubscriptions, TimeSpanWrapperClass dateTimeDiff, bool bRawLogsExist)
        {
            // work unit 1: aggregate to flat aggregated files

            bool bAggregatedAdvertSpecificLogsExist = LoadAggregatedAdvertSpecific();

            // if no logs exist, return.
            if (!bRawLogsExist && !bAggregatedAdvertSpecificLogsExist)
            {
                ReleaseAggregatedFiles();
                return(true);
            }

            // if we have the time difference, time-correct
            if (dateTimeDiff != null)
            {
                TimeCorrect(dateTimeDiff.TimeSpan);
            }

            AggregateRawDataToMemoryAdvertSpecific(channelSubscriptions);

            // append aggregated data from memory to files.
            AppendAggregatedDataAdvertSpecific();

            ReleaseAggregatedFiles();

            //
            // Work unit 2: upload data from aggregated files and truncate if successful
            //

            // search again for responsive server
            string sendLogURI = GetResponsiveServer(ServerType.RelayLogAdv, _maxNoRelayLogServers, _machineGUIDSuffix, "UserDataMarshaller.svc");

            // if no responsive servers found, return
            if (sendLogURI == "")
            {
                _logger.WriteTimestampedMessage("AggregateAndUploadNonAdvertSpecificLogs: URL not found");
                return(true);
            }

            _logger.WriteTimestampedMessage("AggregateAndUploadNonAdvertSpecificLogs: URL: " + sendLogURI + "/file");

            _userDataMarshallerStreamerClient.Endpoint.Address = new EndpointAddress(sendLogURI + "/file");

            UploadAdvertSpecificAggregatedFiles();

            // truncate aggregated files. no need to check if there was an error as this is near the end of the LogExchanger
            TruncateAdvertSpecificAggregatedFilesIfUploaded();

            ReleaseAggregatedFiles();

            return(true);
        }
Example #6
0
        // returns false if there's a critical error or application can otherwise not continue (e.g. no data)
        private bool AggregateAndUploadNonAdvertSpecificLogs(long channelID, string channelGUIDSuffix, TimeSpanWrapperClass dateTimeDiff, bool bRawLogsExist)
        {
            //
            // Work unit 1: aggregate to flat aggregated files
            //

            string channelAggregatedLogsPath = _logPath + channelID;

            if (!Directory.Exists(channelAggregatedLogsPath))
            {
                Directory.CreateDirectory(channelAggregatedLogsPath);
            }

            // load and lock aggregated Logs for a specific channel or generic advert logs.
            bool bAggregatedNonAdvertSpecificLogsExist = LoadAggregatedNonAdvertSpecific(channelAggregatedLogsPath);

            // if no logs exist, return .
            if (!bRawLogsExist && !bAggregatedNonAdvertSpecificLogsExist)
            {
                ReleaseAggregatedFiles();
                return(true);
            }

            // if we have the time difference, time-correct
            if (dateTimeDiff != null)
            {
                TimeCorrect(dateTimeDiff.TimeSpan);
            }

            AggregateRawDataToMemoryNonAdvertSpecific(channelID);

            // append aggregated data from memory to files.
            AppendAggregatedDataNonAdvertSpecific();

            ReleaseAggregatedFiles();

            //
            // Work unit 2: upload data from aggregated files and truncate if successful
            //

            // search again for responsive server
            string sendLogURI = null;

            // condition channelGUIDSuffix == "" only if logs pertain to advert logs, as adverts do not have a channel
            if (channelGUIDSuffix == "")
            {
                sendLogURI = GetResponsiveServer(ServerType.RelayLogAdv, _maxNoRelayLogServers, _machineGUIDSuffix, "UserDataMarshaller.svc");
            }
            else
            {
                sendLogURI = GetResponsiveServer(ServerType.RelayLogCont, _maxNoRelayLogServers, channelGUIDSuffix, "UserDataMarshaller.svc");
            }

            // if no responsive servers found, return. at this point data have been aggregated, try next channel
            if (sendLogURI == "")
            {
                _logger.WriteTimestampedMessage("AggregateAndUploadNonAdvertSpecificLogs: URL not found");
                return(true);
            }

            _logger.WriteTimestampedMessage("AggregateAndUploadNonAdvertSpecificLogs: URL: " + sendLogURI + "/file");

            _userDataMarshallerStreamerClient.Endpoint.Address = new EndpointAddress(sendLogURI + "/file");

            UploadNonAdvertSpecificAggregatedFiles(channelID);

            // truncate aggregated files. no need to check if there was an error as this is near the end of the LogExchanger
            TruncateNonAdvertSpecificAggregatedFilesIfUploaded();

            ReleaseAggregatedFiles();

            return(true);
        }