private void IncrementQueueIndex(QueueInstallationData data)
        {
            if (!_queueIndex.ContainsKey(data.Key))
            {
                _queueIndex.Add(data.Key, 0);
            }

            _queueIndex[data.Key]++;
        }
Beispiel #2
0
        public InstallStatusData Create(string driverModel)
        {
            string key = driverModel;

            lock (_lock)
            {
                if (!ContainsKey(key))
                {
                    _queueInstallData = new QueueInstallationData();

                    InstallStatusData statusData = new InstallStatusData();

                    base.Add(key, statusData);
                }
            }
            return(this[key]);
        }
Beispiel #3
0
        public InstallStatusData Create(QueueInstallationData queueData)
        {
            string key = queueData.QueueName;

            lock (_lock)
            {
                if (!ContainsKey(key))
                {
                    _queueInstallData = queueData;

                    InstallStatusData statusData = new InstallStatusData();

                    base.Add(key, statusData);
                }
            }

            return(this[key]);
        }
Beispiel #4
0
        private bool SetupSharing(InstallerPrintDevice printDevice, StatusRecord record, QueueInstallationData queueData)
        {
            bool status = true;

            try
            {
                UpdateStatus("Configuring sharing...");
                record.Start("SHARING");

                Retry.WhileThrowing
                (
                    () => SetupSharingAction(printDevice, record),
                    3,
                    TimeSpan.FromSeconds(3),
                    new List <Type>()
                {
                    typeof(Win32Exception)
                }
                );

                record.End();
            }
            catch (PrintQueueException ex)
            {
                RecordError(record, queueData, ex);
                status = false;
            }

            return(status);
        }
Beispiel #5
0
        private bool SetupClientRendering(InstallerPrintDevice printDevice, StatusRecord record, QueueInstallationData queueData)
        {
            bool status = true;

            try
            {
                UpdateStatus(@"Configuring ""Render on Client"" setting...");
                record.Start("RENDERING");

                Retry.WhileThrowing
                (
                    () => SetupClientRenderingAction(printDevice, record),
                    3,
                    TimeSpan.FromSeconds(3),
                    new List <Type>()
                {
                    typeof(ArgumentNullException),
                    typeof(SecurityException),
                    typeof(ObjectDisposedException),
                    typeof(UnauthorizedAccessException),
                    typeof(IOException)
                }
                );

                record.End();
            }
            catch (PrintQueueException ex)
            {
                RecordError(record, queueData, ex);
                status = false;
            }

            return(status);
        }
Beispiel #6
0
        private bool InstallQueue(InstallerPrintDevice printDevice, StatusRecord record, QueueInstallationData queueData)
        {
            bool status = true;

            try
            {
                UpdateStatus("Creating queue... " + queueData.QueueName);
                record.Start("QUEUE");

                Retry.WhileThrowing
                (
                    () => InstallQueueAction(printDevice, record),
                    3,
                    TimeSpan.FromSeconds(2),
                    new List <Type>()
                {
                    typeof(Win32Exception)
                }
                );

                record.End();
            }
            catch (PrintQueueException ex)
            {
                RecordError(record, queueData, ex);
                status = false;
            }
            catch (Win32Exception ex)
            {
                RecordError(record, queueData, ex);
                status = false;
            }
            catch (InvalidOperationException ex)
            {
                RecordError(record, queueData, ex);
                status = false;
            }

            return(status);
        }
Beispiel #7
0
        private bool SetupPort(InstallerPrintDevice printDevice, StatusRecord record, QueueInstallationData queueData)
        {
            bool status = true;

            try
            {
                UpdateStatus("Creating port... " + printDevice.Port.PortName);
                record.Start("PORT");

                Retry.WhileThrowing
                (
                    () => SetupPortAction(printDevice, record),
                    3,
                    TimeSpan.FromSeconds(3),
                    new List <Type>()
                {
                    typeof(Win32Exception)
                }
                );

                record.End();
            }
            catch (PrintQueueException ex)
            {
                RecordError(record, queueData, ex);
                status = false;
            }

            return(status);
        }
Beispiel #8
0
 private void RecordError(StatusRecord record, QueueInstallationData queueData, Exception ex)
 {
     UpdateStatus("Error, aborting... " + ex.Message);
     record.Post("ERR - ABORT: {0}".FormatWith(ex.Message));
     queueData.Progress = "ERROR";
 }
Beispiel #9
0
        private void CreatePrintQueue(object state)
        {
            _installThreads.Add(Thread.CurrentThread);

            QueueInstallationData queueData = state as QueueInstallationData;
            InstallStatusData     status    = _installStatus.Create(queueData);

            try
            {
                if (queueData.QueueIsInstalled)
                {
                    // This queue is already installed, so return
                    return;
                }

                if (string.IsNullOrEmpty(queueData.Address))
                {
                    UpdateStatus("NO ADDRESS - SKIPPING");
                    status.Record("NO ADDRESS - SKIPPING");
                    return;
                }

                // Install the initial Print Driver for this queue if needed
                PrintDeviceDriver driver = queueData.Driver;

                InstallDriver(driver);

                DateTime queueInstallStart = DateTime.Now;
                status.Record("NEW ENTRY START", out queueInstallStart);

                TcpIPPortInstaller port = TcpIPPortInstaller.CreateRawPortManager
                                          (
                    queueData.Address,
                    portNumber: queueData.Port,
                    portName: "IP_{0}:{1}".FormatWith(queueData.Address, queueData.Port),
                    snmpEnabled: queueData.SnmpEnabled
                                          );

                queueData.Progress = "Working...";
                UpdateStatus("Installing... " + queueData.QueueName);

                TraceFactory.Logger.Debug("UseConfigurationFile: {0}".FormatWith(queueData.UseConfigurationFile));
                if (!queueData.UseConfigurationFile)
                {
                    // Make sure there are no CFM files sitting in the driver directory
                    RemoveConfigFiles(status);
                    RestoreDriverDefaults(driver, status);
                }

                InstallerPrintDevice printDevice = new InstallerPrintDevice(driver, port);
                printDevice.ConfigFile         = (queueData.UseConfigurationFile) ? queueData.ConfigurationFilePath : string.Empty;
                printDevice.QueueName          = queueData.QueueName;
                printDevice.IsSharedQueue      = queueData.Shared;
                printDevice.IsRenderedOnClient = queueData.ClientRender;

                bool         queueCreated = true;
                StatusRecord record       = new StatusRecord(status);
                while (true)
                {
                    if (!SetupPort(printDevice, record, queueData))
                    {
                        queueCreated = false;
                        break;
                    }

                    if (!InstallQueue(printDevice, record, queueData))
                    {
                        queueCreated = false;
                        break;
                    }

                    if (!SetupClientRendering(printDevice, record, queueData))
                    {
                        queueCreated = false;
                        break;
                    }

                    if (!SetupSharing(printDevice, record, queueData))
                    {
                        queueCreated = false;
                        break;
                    }

                    break;
                }

                DateTime queueInstallEnd = DateTime.Now;
                status.Record("NEW ENTRY END", out queueInstallEnd);
                TimeSpan totalTime = queueInstallEnd.Subtract(queueInstallStart);
                status.Record("NEW ENTRY TOTAL", totalTime);

                if (queueCreated)
                {
                    queueData.Progress = "{0:D2}:{1:D2}.{2:D3}".FormatWith(totalTime.Minutes, totalTime.Seconds, totalTime.Milliseconds);
                    UpdateStatus("1");
                    UpdateStatus("Queue creation complete.");
                }
                else
                {
                    queueData.Progress = "ERROR";
                    UpdateStatus("1");
                    UpdateStatus("Queue creation failed.");
                }

                _threadSemaphore.Release();
            }
            catch (Win32Exception ex)
            {
                status.Record("FAILED: " + ex.Message);
                string message = new Win32Exception(ex.NativeErrorCode).Message;
                UpdateStatus("Queue creation failed: {0}".FormatWith(message));
                FireComplete();
                return;
            }
            finally
            {
                _installThreads.Remove(Thread.CurrentThread);
            }
        }
Beispiel #10
0
 public void Reset()
 {
     this.Clear();
     _queueInstallStatus.Clear();
     _queueInstallData = null;
 }
        /// <summary>
        /// Constructs the queue definitions based on the provided list of printer Ids.
        /// </summary>
        /// <param name="printerIds">The printer ids.</param>
        /// <param name="sourceDriver">The driver.</param>
        /// <param name="currentDriver">The driver.</param>
        /// <param name="description">The additional description.</param>
        /// <param name="queueCount">The queue count.</param>
        /// <returns></returns>
        public Collection <QueueInstallationData> Create
        (
            Collection <string> printerIds,
            PrintDeviceDriver sourceDriver,
            PrintDeviceDriver currentDriver,
            string description,
            int queueCount = 1,
            bool fullName  = true
        )
        {
            Collection <QueueInstallationData> queueData = new Collection <QueueInstallationData>();
            StringBuilder queueName = new StringBuilder();

            if (printerIds.Count == 0)
            {
                return(queueData);
            }

            using (AssetInventoryContext context = DbConnect.AssetInventoryContext())
            {
                foreach (Asset asset in context.Assets.Where(n => printerIds.Contains(n.AssetId)))
                {
                    for (int i = 0; i < queueCount; i++)
                    {
                        QueueInstallationData data = new QueueInstallationData();

                        // Assign the driver install data from a collected list of loaded package data
                        // based on the version.
                        data.Driver = sourceDriver;

                        data.AssetId   = asset.AssetId;
                        data.QueueType = asset.AssetType;
                        data.Shared    = true;

                        queueName.Clear();

                        if (asset.AssetType == "Printer")
                        {
                            Printer printer = (Printer)asset;

                            data.Port         = printer.PortNumber;
                            data.Address      = printer.Address1;
                            data.SnmpEnabled  = true;
                            data.ClientRender = false;
                            data.Shared       = true;
                            data.Description  = printer.Description;

                            queueName.Append(printer.Product);
                            queueName.Append(" ").Append(data.AssetId);

                            if (fullName)
                            {
                                queueName.Append(" ").Append(currentDriver.DriverType);
                                queueName.Append(" ").Append(Regex.Replace(currentDriver.VerifyPdl, @"\s+", " "));
                            }

                            if (!string.IsNullOrEmpty(currentDriver.Release))
                            {
                                queueName.Append(" ").Append(Regex.Replace(currentDriver.Release, @"\s+", " "));
                            }

                            // Append the additional description data if it exists
                            if (!string.IsNullOrEmpty(description))
                            {
                                queueName.Append(" ").Append(Regex.Replace(description, @"\s+", " "));
                            }
                        }
                        else if (asset.AssetType == "VirtualPrinter")
                        {
                            VirtualPrinter virtualPrinter = (VirtualPrinter)asset;

                            //Build a shortened version of the asset ID
                            string        addressCode = asset.AssetId.Split('-')[0];
                            AddressParser assetIP     = new AddressParser(virtualPrinter.Address);

                            data.Port         = virtualPrinter.PortNumber;
                            data.Address      = virtualPrinter.Address;
                            data.SnmpEnabled  = virtualPrinter.SnmpEnabled;
                            data.Description  = "Virtual Printer";
                            data.ClientRender = false;
                            data.Shared       = true;

                            queueName.Append(addressCode);
                            queueName.Append("-").Append(assetIP.GetOctet(2));
                            queueName.Append("-").Append(assetIP.GetOctet(3));
                            queueName.Append(" ").Append(currentDriver.DriverType);
                            queueName.Append(" ").Append(Regex.Replace(currentDriver.VerifyPdl, @"\s+", " "));

                            if (!string.IsNullOrEmpty(currentDriver.Release))
                            {
                                queueName.Append(" ").Append(Regex.Replace(currentDriver.Release, @"\s+", " "));
                            }

                            // Append the additional description data if it exists
                            if (!string.IsNullOrEmpty(description))
                            {
                                queueName.Append(" ").Append(Regex.Replace(description, @"\s+", " "));
                            }
                        }

                        data.Key = queueName.ToString();
                        IncrementQueueIndex(data);

                        if (queueCount > 1)
                        {
                            queueName.Append(" ").Append(_queueIndex[data.Key].ToString("D3"));
                        }

                        data.QueueName = queueName.ToString();
                        queueData.Add(data);
                    }
                }
            }

            return(queueData);
        }
        /// <summary>
        /// Constructs the queue definitions based on user provided configuration.
        /// </summary>
        /// <param name="properties">The driver.</param>
        /// <param name="additionalDescription">The additional description.</param>
        /// <param name="ipStartValue">The start value of the last IP octet.</param>
        /// <param name="ipEndValue">The end value of the last IP octet.</param>
        /// <param name="hostName">The hostname.</param>
        /// <param name="numberOfQueues">The number of queues.</param>
        /// <param name="addressCode">The address code.</param>
        /// <param name="incrementIP">if set to <c>true</c> increment IP octet values.</param>
        /// <param name="enableSnmp">if set to <c>true</c> SNMP will be enable on the port.</param>
        /// <param name="renderOnClient">if set to <c>true</c> the render on client option will be set on the queue.</param>
        /// <param name="shareQueues">if set to <c>true</c> the queue will be shared.</param>
        /// <returns>Collection of QueueInstallationData</returns>
        public Collection <QueueInstallationData> Create
        (
            PrintDeviceDriver properties,
            string additionalDescription,
            int ipStartValue,
            int ipEndValue,
            string hostName,
            int numberOfQueues,
            string addressCode,
            bool incrementIP,
            bool enableSnmp,
            bool renderOnClient,
            bool shareQueues
        )
        {
            if (properties == null)
            {
                throw new ArgumentNullException("properties");
            }
            if (numberOfQueues < 1)
            {
                throw new ArgumentException("numberOfQueues must be a positive, non-zero integer.");
            }
            if (ipStartValue < 1)
            {
                throw new ArgumentException("ipStartValue must be a positive, non-zero integer.");
            }

            Collection <QueueInstallationData> queueData = new Collection <QueueInstallationData>();
            int             currentIPNumber = ipStartValue;
            FrameworkServer vPrintServer    = null;

            using (AssetInventoryContext context = DbConnect.AssetInventoryContext())
            {
                vPrintServer = context.FrameworkServers.FirstOrDefault(n => n.HostName.StartsWith(hostName));
            }

            AddressParser serverIP  = ParseAddress(vPrintServer);
            StringBuilder queueName = new StringBuilder();

            for (int i = 0; i < numberOfQueues; i++)
            {
                queueName.Clear();

                QueueInstallationData data = new QueueInstallationData();
                data.Driver       = properties;
                data.QueueType    = "VirtualPrinter";
                data.AssetId      = "{0}-{1:D5}".FormatWith(addressCode, currentIPNumber);
                data.Address      = serverIP.Prefix + currentIPNumber.ToString();
                data.SnmpEnabled  = enableSnmp;
                data.ClientRender = renderOnClient;
                data.Shared       = shareQueues;

                queueName.Append(addressCode);
                queueName.Append("-").Append(serverIP.GetOctet(2));
                queueName.Append("-").Append(currentIPNumber.ToString("D3"));
                queueName.Append(" ").Append(properties.DriverType);
                queueName.Append(" ").Append(Regex.Replace(properties.VerifyPdl, @"\s+", " "));

                if (!string.IsNullOrEmpty(properties.Release))
                {
                    queueName.Append(" ").Append(Regex.Replace(properties.Release, @"\s+", " "));
                }

                // Append the additional description data if it exists
                if (!string.IsNullOrEmpty(additionalDescription))
                {
                    queueName.Append(" ").Append(Regex.Replace(additionalDescription, @"\s+", " "));
                }

                // Append a queue index if we're reusing the Virtual Printer for multiple queues
                if (incrementIP == false || numberOfQueues > 255)
                {
                    data.Key = queueName.ToString();
                    IncrementQueueIndex(data);
                    queueName.Append(" ").Append(_queueIndex[data.Key].ToString("D3"));
                }

                data.QueueName = queueName.ToString();
                queueData.Add(data);

                if (currentIPNumber == ipEndValue)
                {
                    //Restart the IP number, don't increment
                    currentIPNumber = ipStartValue;
                }
                else if (incrementIP)
                {
                    currentIPNumber++;
                }
            }

            return(queueData);
        }