Exemple #1
0
        private void TryReadFileWithReadLock(StreamErrorWrapper streamErrorWrapper, string fileFullPath)
        {
            FileStream fs = null;

            try
            {
                fs = new FileStream(fileFullPath, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());

                if (fs != null)
                {
                    fs.Dispose();
                }

                streamErrorWrapper.ErrorCode     = "ERR:006";
                streamErrorWrapper.ErrorSeverity = ErrorSeverity.Retriable;
                streamErrorWrapper.ErrorStatus   = ErrorStatus.Failure;
                streamErrorWrapper.Message       = "Could not read file.";
                streamErrorWrapper.ReturnStream  = new MemoryStream(new byte[0]);

                return;
            }

            streamErrorWrapper.ErrorStatus  = ErrorStatus.Success;
            streamErrorWrapper.ReturnStream = fs;
        }
        private bool DownloadComponentList(int maxNoServers, int timeout, ref ComponentInfo[] componentList)
        {
            string uri = ResponsiveServerDeterminator.GetResponsiveURI(ServerType.RelayLogs, maxNoServers, timeout,
                                                                       _user.GetMachineGUIDSuffix(), _generalData.Properties["primaryDomainName"],
                                                                       _generalData.Properties["secondaryDomainName"], "UserDataMarshaller.svc");

            if (uri == "")
            {
                return(false);
            }

            uri += "/su";

            UserDataMarshallerSUStreamerClient client = null;
            StreamErrorWrapper      wrapper           = null;
            VersionParameterMessage message           = new VersionParameterMessage()
            {
                Version          = String.Format("{0}.{1}", _generalData.SoftwareMajorVersionNumber, _generalData.SoftwareMinorVersionNumber),
                SystemPassPhrase = "password"
            };

            try
            {
                client = new UserDataMarshallerSUStreamerClient();
                client.Endpoint.Address = new System.ServiceModel.EndpointAddress(uri);
                wrapper = client.GetComponentList(message);
            }
            catch (Exception ex)
            {
                client.Dispose();
                _log.WriteEntry("Could not get a list of updated components.", EventLogEntryType.Error);
                return(false);
            }

            if (wrapper.ErrorStatus == ErrorStatus.NoData)
            {
                _log.WriteEntry(wrapper.ErrorCode + " " + wrapper.Message, EventLogEntryType.Warning);
                return(false);
            }

            if (wrapper.ErrorStatus == ErrorStatus.Failure)
            {
                _log.WriteEntry(wrapper.ErrorCode + " " + wrapper.Message, EventLogEntryType.Error);
                return(false);
            }

            byte[] buffer = StreamToByteArray(wrapper.ReturnStream);

            if (wrapper.ReturnStream != null)
            {
                wrapper.ReturnStream.Dispose();
            }

            string stringXmlObj = ByteArrayToString(buffer);

            componentList = (ComponentInfo[])Serializer.DeserializeFromString(typeof(ComponentInfo[]), stringXmlObj);

            return(true);
        }
Exemple #3
0
        /// <summary>
        /// Gets an asset file in the form of an array of bytes
        /// </summary>
        /// <param name="assetFileParameterMessage">Message wrapper for GetAssetFile parameters</param>
        /// <returns>a StreamErrorWrapper with the relevant stream or with a code that specifies that the file is the same as the one on the client side or with error information</returns>
        public StreamErrorWrapper GetAssetFile(AssetFileParameterMessage assetFileParameterMessage)
        {
            StreamErrorWrapper streamErrorWrapper = new StreamErrorWrapper();

            // close returned stream on completion
            OperationContext clientContext = OperationContext.Current;

            clientContext.OperationCompleted += new EventHandler(delegate(object sender, EventArgs args)
            {
                if (streamErrorWrapper.ReturnStream != null)
                {
                    streamErrorWrapper.ReturnStream.Dispose();
                }
            });

            if (assetFileParameterMessage.SystemPassPhrase != _systemPassPhrase)
            {
                streamErrorWrapper.ErrorCode     = "ERR:001";
                streamErrorWrapper.Message       = "Authentication failure";
                streamErrorWrapper.ErrorSeverity = ErrorSeverity.Retriable;
                streamErrorWrapper.ErrorStatus   = ErrorStatus.Failure;
                streamErrorWrapper.ReturnStream  = new MemoryStream(new byte[0]);

                return(streamErrorWrapper);
            }

            // _assetPath/<asset's GUID suffix>/asset filename
            string assetFullPath = FileRepositoryHelper.GetAssetFullPath(assetFileParameterMessage.AssetFileName);

            if (!File.Exists(assetFullPath))
            {
                _applicationLog.Warn(assetFullPath + " does not exist.");
                streamErrorWrapper.ErrorCode     = "ERR:002";
                streamErrorWrapper.Message       = "File not found";
                streamErrorWrapper.ErrorSeverity = ErrorSeverity.Retriable; // search for asset file later
                streamErrorWrapper.ErrorStatus   = ErrorStatus.Failure;
                streamErrorWrapper.ReturnStream  = new MemoryStream(new byte[0]);
                return(streamErrorWrapper);
            }

            string serverSideChecksum = ReadStringFromFile(Path.GetDirectoryName(assetFullPath) + "\\" + Path.GetFileNameWithoutExtension(assetFullPath) + ".chk");

            if (assetFileParameterMessage.Checksum == serverSideChecksum)
            {
                streamErrorWrapper.ErrorStatus  = ErrorStatus.ChecksumEqual;
                streamErrorWrapper.ReturnStream = new MemoryStream(new byte[0]);
                return(streamErrorWrapper);
            }

            TryReadFileWithReadLock(streamErrorWrapper, assetFullPath);

            return(streamErrorWrapper);
        }
Exemple #4
0
        public StreamErrorWrapper GetComponent(ComponentParameterMessage message)
        {
            StreamErrorWrapper streamErrorWrapper = new StreamErrorWrapper();

            // close returned stream on completion
            OperationContext clientContext = OperationContext.Current;

            clientContext.OperationCompleted += new EventHandler(delegate(object sender, EventArgs args)
            {
                if (streamErrorWrapper.ReturnStream != null)
                {
                    streamErrorWrapper.ReturnStream.Dispose();
                }
            });

            if (message.SystemPassPhrase != _systemPassPhrase)
            {
                streamErrorWrapper.ErrorCode     = "ERR:001";
                streamErrorWrapper.Message       = "Authentication failure";
                streamErrorWrapper.ErrorSeverity = ErrorSeverity.Retriable;
                streamErrorWrapper.ErrorStatus   = ErrorStatus.Failure;
                streamErrorWrapper.ReturnStream  = new MemoryStream(new byte[0]);

                return(streamErrorWrapper);
            }

            string changesetFullPath = _changesetPath + message.VersionNumber + "\\" + message.ComponentFileName;

            if (!File.Exists(changesetFullPath))
            {
                _logger.Warn("File does not exist: " + changesetFullPath);

                streamErrorWrapper.ErrorCode     = "ERR:002";
                streamErrorWrapper.Message       = "File not found";
                streamErrorWrapper.ErrorSeverity = ErrorSeverity.Retriable; // search for asset file later
                streamErrorWrapper.ErrorStatus   = ErrorStatus.Failure;
                streamErrorWrapper.ReturnStream  = new MemoryStream(new byte[0]);
                return(streamErrorWrapper);
            }

            TryReadFileWithReadLock(streamErrorWrapper, changesetFullPath);

            return(streamErrorWrapper);
        }
Exemple #5
0
        public StreamErrorWrapper GetComponentList(VersionParameterMessage message)
        {
            StreamErrorWrapper streamErrorWrapper = new StreamErrorWrapper();

            // close returned stream on completion
            OperationContext clientContext = OperationContext.Current;

            clientContext.OperationCompleted += new EventHandler(delegate(object sender, EventArgs args)
            {
                if (streamErrorWrapper.ReturnStream != null)
                {
                    streamErrorWrapper.ReturnStream.Dispose();
                }
            });

            if (message.SystemPassPhrase != _systemPassPhrase)
            {
                streamErrorWrapper.ErrorCode     = "ERR:001";
                streamErrorWrapper.Message       = "Authentication failure";
                streamErrorWrapper.ErrorSeverity = ErrorSeverity.Retriable;
                streamErrorWrapper.ErrorStatus   = ErrorStatus.Failure;
                streamErrorWrapper.ReturnStream  = new MemoryStream(new byte[0]);

                return(streamErrorWrapper);
            }

            string appDataFullPath = _changesetPath + message.Version + "\\ss_component_list.dat";

            if (!File.Exists(appDataFullPath))
            {
                _applicationLog.Warn("Attempted access to " + appDataFullPath + ". File does not exist");

                streamErrorWrapper.ErrorStatus   = ErrorStatus.NoData;
                streamErrorWrapper.ErrorSeverity = ErrorSeverity.Retriable;
                streamErrorWrapper.ErrorCode     = "WARN:001";
                streamErrorWrapper.Message       = "File does not exist";
                streamErrorWrapper.ReturnStream  = new MemoryStream(new byte[0]); // stream must not be null as it is the message body
                return(streamErrorWrapper);
            }

            TryReadFileWithReadLock(streamErrorWrapper, appDataFullPath);

            return(streamErrorWrapper);
        }
Exemple #6
0
        private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-GB");
            System.Threading.Thread.CurrentThread.CurrentCulture   = ci;
            System.Threading.Thread.CurrentThread.CurrentUICulture = ci;

            string tempDir       = _binariesPath + "Temp\\";
            string binTempDir    = _binariesPath + "Temp\\Bin\\";
            string systemTempDir = _binariesPath + "Temp\\System\\";

            // Check if admin has access rights in the binary folder. System folder will always fail as
            // method checks all fiels and some files are inaccessible.
            // We only need enough privileges to overwrite the Oxigen files.
            if (!FileDirectoryRightsChecker.CanCreateDeleteDirectories(_binariesPath) ||
                !FileDirectoryRightsChecker.AreFilesReadableWritable(_binariesPath))
            {
                _log.WriteEntry("Cannot update Oxigen. File and directory access rights are insufficient.", EventLogEntryType.Error);

                _bError    = true;
                _bCanClose = true;
                return;
            }

            if (!Directory.Exists(binTempDir))
            {
                Directory.CreateDirectory(binTempDir);
            }
            else
            {
                DeleteAllFiles(binTempDir);
            }

            if (!Directory.Exists(systemTempDir))
            {
                Directory.CreateDirectory(systemTempDir);
            }
            else
            {
                DeleteAllFiles(systemTempDir);
            }

            GeneralData generalData = null;
            User        user        = null;

            if (!GetSettingsFiles(ref generalData, ref user))
            {
                MessageBox.Show("Cannot retrieve new version info. Updating will be postponed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                _bError    = true;
                _bCanClose = true;
                return;
            }

            int    maxNoServersDownload = -1;
            int    maxNoServersDelay    = -1;
            int    timeout                   = -1;
            string primaryDomainName         = null;
            string secondaryDomainName       = null;
            int    updatedMajorVersionNumber = -1;
            int    updatedMinorVersionNumber = -1;
            string updatedVersionNumber      = null;

            if (!GetValuesFromGeneralData(generalData, ref maxNoServersDownload, ref maxNoServersDelay,
                                          ref timeout, ref primaryDomainName, ref secondaryDomainName))
            {
                MessageBox.Show("There was an error reading the global Oxigen settings.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);

                _bError    = true;
                _bCanClose = true;
                return;
            }

            GetNewVersionNumber(generalData, ref updatedMajorVersionNumber,
                                ref updatedMinorVersionNumber, ref updatedVersionNumber);

            string machineGUIDSuffix = user.GetMachineGUIDSuffix();
            HashSet <InterCommunicationStructures.ComponentInfo> changedComponents;

            try
            {
                changedComponents = (HashSet <InterCommunicationStructures.ComponentInfo>)Serializer.DeserializeClearText(typeof(HashSet <InterCommunicationStructures.ComponentInfo>), _appDataPath + "\\SettingsData\\components.dat");
            }
            catch
            {
                _log.WriteEntry("Error retrieving the Oxigen Updated Component List.", EventLogEntryType.Error);
                MessageBox.Show("There was an error retrieving the Oxigen Updated Components List.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);

                _bError    = true;
                _bCanClose = true;
                return;
            }

            _step = 100 / (changedComponents.Count + 1);

            // Download components
            UserFileMarshallerSUClient client  = null;
            StreamErrorWrapper         wrapper = null;

            string uri = ResponsiveServerDeterminator.GetResponsiveURI(ServerType.DownloadGetFile,
                                                                       maxNoServersDownload, timeout, machineGUIDSuffix, primaryDomainName,
                                                                       secondaryDomainName, "UserFileMarshaller.svc");

            if (uri == "")
            {
                backgroundWorker.ReportProgress(100);
                _bCanClose = true;
                Application.Exit();
                return;
            }

            uri += "/file";

            try
            {
                client = new UserFileMarshallerSUClient();

                client.Endpoint.Address = new System.ServiceModel.EndpointAddress(uri);

                foreach (ComponentInfo component in changedComponents)
                {
                    ComponentParameterMessage message = new ComponentParameterMessage()
                    {
                        ComponentFileName = component.File,
                        VersionNumber     = updatedVersionNumber,
                        SystemPassPhrase  = "password"
                    };

                    wrapper = client.GetComponent(message);

                    if (wrapper.ErrorStatus == ErrorStatus.Success)
                    {
                        string path = null;

                        if (component.Location == ComponentLocation.BinaryFolder)
                        {
                            path = binTempDir + component.File;
                        }
                        else
                        {
                            path = systemTempDir + component.File;
                        }

                        SaveStreamAndDispose(wrapper.ReturnStream, path);
                        ReportProgress();
                    }
                    else
                    {
                        wrapper.ReturnStream.Dispose();

                        _log.WriteEntry("Attempting to download file: " + component.File + " returned this error: " + wrapper.ErrorCode + " " + wrapper.Message, EventLogEntryType.Error);

                        MessageBox.Show("There was an error getting the update from the server. A new update will be attempted later.");

                        if (Directory.Exists(tempDir))
                        {
                            Directory.Delete(tempDir, true);
                        }

                        if (File.Exists(_appDataPath + "SettingsData\\components.dat"))
                        {
                            File.Delete(_appDataPath + "SettingsData\\components.dat");
                        }

                        // it's an all-or-nothing update so if one file fails, abort
                        _bError    = true;
                        _bCanClose = true;
                        return;
                    }
                }
            }
            catch (Exception ex1)
            {
                if (Directory.Exists(tempDir))
                {
                    try
                    {
                        Directory.Delete(tempDir, true);
                    }
                    catch (Exception ex2)
                    {
                        _log.WriteEntry(ex2.ToString(), EventLogEntryType.Error);
                    }
                }
                _log.WriteEntry(ex1.ToString(), EventLogEntryType.Error);
                MessageBox.Show("Oxigen could not complete the software update. A new update will be attempted later.");

                _bError    = true;
                _bCanClose = true;
                return;
            }
            finally
            {
                if (wrapper != null)
                {
                    wrapper.ReturnStream.Dispose();
                }

                client.Dispose();
            }

            // stop the SSG
            KillProcess("OxigenService");

            // kill the Tray that monitors the SSG
            KillProcess("OxigenTray");

            // if CE and LE are running, wait until they're finished
            while (IsProcessRunning("OxigenLE") || IsProcessRunning("OxigenCE"))
            {
                ;
            }

            // TODO: stop the screensaver from starting

            MoveFiles(binTempDir, systemTempDir);

            System.Diagnostics.Process.Start(_binariesPath + "OxigenService.exe");

            if (File.Exists(_appDataPath + "SettingsData\\components.dat"))
            {
                File.Delete(_appDataPath + "SettingsData\\components.dat");
            }

            if (Directory.Exists(_binariesPath + "Temp\\"))
            {
                Directory.Delete(_binariesPath + "Temp\\", true);
            }

            // upload user info to server and  update local UserSettings information with the latest version of Screensaver
            UploadUpdateCurrentVersionInfo(updatedMajorVersionNumber, updatedMinorVersionNumber,
                                           updatedVersionNumber, maxNoServersDelay, timeout, machineGUIDSuffix, primaryDomainName, secondaryDomainName);

            backgroundWorker.ReportProgress(100);

            Thread.Sleep(1000);
        }
Exemple #7
0
        /// <summary>
        /// Gets the XML files (in the form of an array of bytes) that hold general or advert condition data, if different from the client's
        /// </summary>
        /// <param name="appDataFileParameterMessage">Message contract to pass as a parameter</param>
        /// <returns>a StreamErrorWrapper with the relevant stream or with a code that specifies that the file is the same as the one on the client side or with error information</returns>
        public StreamErrorWrapper GetAppDataFiles(AppDataFileParameterMessage appDataFileParameterMessage)
        {
            StreamErrorWrapper streamErrorWrapper = new StreamErrorWrapper();

            // close returned stream on completion
            OperationContext clientContext = OperationContext.Current;

            clientContext.OperationCompleted += new EventHandler(delegate(object sender, EventArgs args)
            {
                if (streamErrorWrapper.ReturnStream != null)
                {
                    streamErrorWrapper.ReturnStream.Dispose();
                }
            });

            if (appDataFileParameterMessage.SystemPassPhrase != _systemPassPhrase)
            {
                streamErrorWrapper.ErrorCode     = "ERR:001";
                streamErrorWrapper.Message       = "Authentication failure";
                streamErrorWrapper.ErrorSeverity = ErrorSeverity.Retriable;
                streamErrorWrapper.ErrorStatus   = ErrorStatus.Failure;
                streamErrorWrapper.ReturnStream  = new MemoryStream(new byte[0]);

                return(streamErrorWrapper);
            }

            string appDataFullPath = "";

            switch (appDataFileParameterMessage.DataFileType)
            {
            case DataFileType.AdvertConditions:
                appDataFullPath = _appDataPath + "ss_adcond_data.dat";
                break;

            case DataFileType.GeneralConfiguration:
                appDataFullPath = _appDataPath + "ss_general_data.dat";
                break;

            case DataFileType.ChannelData:
                appDataFullPath = _channelDataPath + appDataFileParameterMessage.ChannelID + "_channel.dat";
                break;
            }

            if (!File.Exists(appDataFullPath))
            {
                _applicationLog.Warn(appDataFullPath + " does not exist.");
                streamErrorWrapper.ErrorStatus   = ErrorStatus.Failure;
                streamErrorWrapper.ErrorSeverity = ErrorSeverity.Retriable;
                streamErrorWrapper.ErrorCode     = "ERR:002";
                streamErrorWrapper.Message       = "File does not exist";
                streamErrorWrapper.ReturnStream  = new MemoryStream(new byte[0]); // stream must not be null as it is the message body
                return(streamErrorWrapper);
            }

            string serverSideChecksum = ReadStringFromFile(Path.GetDirectoryName(appDataFullPath) + "\\" + Path.GetFileNameWithoutExtension(appDataFullPath) + ".chk");

            if (appDataFileParameterMessage.Checksum == serverSideChecksum)
            {
                streamErrorWrapper.ErrorStatus  = ErrorStatus.ChecksumEqual;
                streamErrorWrapper.ReturnStream = new MemoryStream(new byte[0]);
                return(streamErrorWrapper);
            }

            TryReadFileWithReadLock(streamErrorWrapper, appDataFullPath);

            return(streamErrorWrapper);
        }