Exemplo n.º 1
0
        private void CreateScriptHandler(string MainScript)
        {
            // Selecting service provider...
            string    SPProviderFileName;
            AppDomain ad = AppDomain.CreateDomain("SP Files Search");

            {
                System.Type spsType = typeof(ServiceProviders);

                ServiceProviders sps = (ServiceProviders)ad.CreateInstanceAndUnwrap(spsType.Assembly.FullName,
                                                                                    spsType.Namespace + "." + spsType.Name);
                sps.FindProviders(System.Windows.Forms.Application.StartupPath);
                this.opt.SetSPTypesDinamically(sps.GetProviders());

                SPProviderFileName = sps.GetProviderFileName(this.opt.ServiceProviderType);
            }
            AppDomain.Unload(ad);

            if (SPProviderFileName == null)
            {
                TraceOut.Put("No Service provider is found - Exiting!!");
                return;
            }
            this.SPType = ServiceProviders.GetProviderType(SPProviderFileName);

            TraceOut.Put("Creating " + "\"" + MainScript + "\"" + " script handler...");
            ScriptHandler sh = new ScriptHandler(MainScript, this.ServiceName, this.SPType, this.opt);

            this.ActiveScripts.Add(sh);
            this.spStatus.Status         = "OK";
            this.spStatus.AdditionalInfo = "Initializing";
            sh.SPStatusChanged          += new Diacom.APCService.OnSPStatusChange(SPStatusChangedHandler);
            TraceOut.Put("Script handler for " + "\"" + MainScript + "\"" + " created.");
            sh.Start();
        }
Exemplo n.º 2
0
 /// <summary>
 /// Reads options for APCService.
 /// </summary>
 private void ReadConfig()
 {
     System.IO.FileStream fs = null;
     try
     {
         TraceOut.Put("Reading configuration file...");
         // File exists - read from it.
         fs           = new System.IO.FileStream(iniConfigFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
         optFormatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
         APCServiceOptions _opt = ((APCServiceOptions)(optFormatter.Deserialize(fs)));
         _opt.CopyTo(this.opt);
         TraceOut.Put("Acting with following options: " + Environment.NewLine + this.opt.ToString());
     }
     catch (Exception ix)
     {
         // No file - setting default options.
         TraceOut.Put(ix);
         TraceOut.Put("Service Provider will be set by default...");
         APCServiceOptions _opt = new APCServiceOptions();
         _opt.CopyTo(this.opt);
     }
     finally
     {
         if (fs != null)
         {
             fs.Close();
         }
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Gets the type of the service provider given it's name.
 /// </summary>
 /// <param name="ProviderFileName">A type of the Service provider to instantiate.</param>
 /// <returns></returns>
 public static System.Type GetProviderType(string ProviderFileName)
 {
     try
     {
         System.Reflection.Assembly asmbl = System.Reflection.Assembly.LoadFrom(ProviderFileName);
         foreach (System.Reflection.Module module in asmbl.GetModules())
         {
             // Checking each module in assembly.
             foreach (System.Type type in module.GetTypes())
             {
                 if (type.IsNotPublic)
                 {
                     continue;
                 }
                 // Checking each interface in module.
                 foreach (System.Type interf in type.GetInterfaces())
                 {
                     // Checking each implemented interface in class.
                     if (interf.Equals(typeof(ISP)))
                     {
                         TraceOut.Put("Type that implements interface: " + type.ToString());
                         return(type);
                     }
                 }
             }
         }
     }
     catch (Exception x)
     {
         TraceOut.Put(x);
     }
     return(null);
 }
Exemplo n.º 4
0
        /// <summary>
        /// Cleans up any resources being used.
        /// </summary>
        public void Dispose()
        {
            TraceOut.Put("Shutting down script " + this.ScriptFileName);
            try
            {
                TraceOut.Put("Calling this.APCStCtrl.Dispose");
                if (this.APCStCtrl != null)
                {
                    this.APCStCtrl.Dispose();
                }
            }
            catch (Exception ix)
            {
                TraceOut.Put(ix);
            }

            try
            {
                TraceOut.Put("Calling this.SPCoreHandle.Disconnect");
                if (this.SPHandle != null)
                {
                    this.SPHandle.SPStatusEvent -= new Diacom.SPStatusEventHandler(this.ServiceProviderStatusEventHandler);
                    this.SPHandle.Disconnect();
                    this.SPHandle.Dispose();
                }
            }
            catch (Exception ix)
            {
                TraceOut.Put(ix);
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Login to Server.
 /// </summary>
 /// <returns>True if success, otherwise false.</returns>
 private bool LoginToServer()
 {
     try
     {
         TraceOut.Put("Starting login...");
         // Connecting to server.
         SPHandle.Connect(opt.ServerIPAddress, opt.ServerPort, opt.ControlLineLogonType, opt.ControlLineID, opt.ControlLinePassword, opt.LogonTimeout);
         // Connection established.
         TraceOut.Put("Connection to server established... checking for connection status...");
         StatusText = this.SPHandle.Status().ToString();
         TraceOut.Put("Status of Service Provider is " + StatusText);
         if (StatusText != Diacom.SPStatus.OK.ToString())
         {
             StatusInfo = this.ServiceName +
                          " Error " + this.opt.ServiceProviderType.ToString() + " with : " +
                          Environment.NewLine + this.opt.ToString() + Environment.NewLine +
                          "Service provider status: " + this.SPHandle.Status();
         }
         else
         {
             StatusInfo = "Success";
         }
         ReportStatus();
         TraceOut.Put("Connection complete.");
     }
     catch (Exception x)
     {
         TraceOut.Put(x);
         StatusText = Diacom.SPStatus.ERROR_CONNECTION.ToString();
         StatusInfo = this.ServiceName + ": Login Exception :" + Environment.NewLine + this.opt.ToString() + Environment.NewLine + "Service provider status: " + this.SPHandle.Status();
         ReportStatus();
         return(false);
     }
     return(true);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Stop this service.
 /// </summary>
 protected override void OnStop()
 {
     try
     {
         TraceOut.Put("[APCService] Stopping ... Calling Dispose method...");
         WriteConfig();
         TraceOut.Put("Disposing all script handlers...");
         foreach (ScriptHandler sc in this.ActiveScripts)
         {
             try
             {
                 sc.SPStatusChanged -= new Diacom.APCService.OnSPStatusChange(SPStatusChangedHandler);
                 sc.Dispose();
             }
             catch (Exception _e)
             {
                 TraceOut.Put(_e);
             }
         }
         ActiveScripts.Clear();
     }
     catch (Exception _e)
     {
         TraceOut.Put(_e);
     }
     finally
     {
         base.OnStop();
     }
 }
Exemplo n.º 7
0
 /// <summary>
 /// Executes when timer ticks.
 /// </summary>
 /// <param name="state">Parameter, which is the name of the script file.</param>
 private void RestartTimerCallbackEntry(object state)
 {
     TraceOut.Put("RestartTimerCallbackEntry: restarting timer activated...");
     this.RestartTimer.Dispose();
     this.RestartTimer = null;
     ReadConfig();
     CreateScriptHandler((string)state);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Initializes all class components.
 /// </summary>
 public APCService()
 {
     TraceOut.Put("Service Constructor called.");
     this.AutoLog     = false;
     this.ServiceName = "APCService";
     ReadConfig();
     RegisterTCPChannel();
     RegisterRemoteTypes();
 }
Exemplo n.º 9
0
 private void ServiceProviderStatusEventHandler(object source, Diacom.SPStatusEventArgs e)
 {
     if (e == null)
     {
         return;
     }
     StatusText = e.Status.ToString();
     StatusInfo = e.Info;
     TraceOut.Put("SPStatusEvent: Event: " + StatusText + Environment.NewLine + "Info: " + StatusInfo);
     ReportStatus();
 }
Exemplo n.º 10
0
 /// <summary>
 /// Main thread for APCService application.
 /// </summary>
 private void StartHandlers()
 {
     TraceOut.Put(this.TraceMessagesSeparator + " Starting execution...");
     // Reading configuration...
     ReadConfig();
     // Creating new instances for each main script file...
     foreach (string Script in this.opt.APCScriptFileName.Split(';'))
     {
         CreateScriptHandler(Script);
     }
     // All done.
     TraceOut.Put("All done.");
 }
Exemplo n.º 11
0
 private void UnRegisterTCPChannel()
 {
     try
     {
         ChannelServices.UnregisterChannel(this.TCPChannel);
         this.TCPChannel = null;
         TraceOut.Put("TCP channel unregistered...");
     }
     catch (Exception ix)
     {
         TraceOut.Put(ix);
     }
 }
Exemplo n.º 12
0
 /// <summary>
 /// Unegisters remote types.
 /// </summary>
 private void UnRegisterRemoteTypes()
 {
     try
     {
         // Disconnect existing remote objects
         RemotingServices.Disconnect(this.opt);
         RemotingServices.Disconnect(this.spStatus);
         TraceOut.Put("Remote types unregistered...");
     }
     catch (Exception ix)
     {
         TraceOut.Put(ix);
     }
 }
Exemplo n.º 13
0
 private void Run()
 {
     try
     {
         // Creating new instance of SP.
         TraceOut.Put("Creating new ISP instance...");
         this.SPHandle = ((ISP)(Activator.CreateInstance(this.SPType)));
         this.SPHandle.SPStatusEvent += new SPStatusEventHandler(this.ServiceProviderStatusEventHandler);
         TraceOut.Put("Event handler added...");
         // Creating control class.
         TraceOut.Put("Creating APCControl...");
         this.APCStCtrl = new APCStates.APCStateControl(this.SPHandle);
         this.APCStCtrl.APCControlEvent += new SPStatusEventHandler(this.ServiceProviderStatusEventHandler);
         TraceOut.Put("APCControl created...");
         // Adding states.
         TraceOut.Put("Added APC service states folders: " + this.opt.APCServiceStatesFolders + ".");
         this.APCStCtrl.AddStates(this.opt.APCServiceStatesFolders);
         // Adding script.
         this.APCStCtrl.AddScript(this.ScriptFileName);
         TraceOut.Put("Main script file defined...");
         // Check if where is no connection.
         if (this.SPHandle.Status() == Diacom.SPStatus.DISCONNECTED)
         {
             TraceOut.Put("Main did not connect to the Server - Connecting ...");
             // No connection means the script uses default service provider.
             if (!LoginToServer())
             {
                 TraceOut.Put("Connection failed.");
                 return;
             }
             TraceOut.Put("Connected.");
         }
         // Adding lines.
         TraceOut.Put("Adding lines...");
         foreach (SPLine line in this.SPHandle.GetLines())
         {
             this.APCStCtrl.AddLine(line);
         }
         TraceOut.Put("Lines added.");
         // Initializating lines.
         this.APCStCtrl.InitLines();
         TraceOut.Put("Lines initialization complete.");
     }
     catch (Exception x)
     {
         TraceOut.Put("Exception during creating class");
         TraceOut.Put(x);
     }
 }
Exemplo n.º 14
0
 /// <summary>
 /// Initiates a search for all nested service providers starting from a specified directory.
 /// </summary>
 /// <param name="ProvidersDirectory">The Directory to start search in.</param>
 public void FindProviders(string ProvidersDirectory)
 {
     try
     {
         // Creating list of all assemblies in current directory.
         System.IO.DirectoryInfo startupDir = new System.IO.DirectoryInfo(ProvidersDirectory);
         // Generating possible service provider types list.
         ExploreDirs(startupDir);
         ScanDirs();
     }
     catch (Exception x)
     {
         TraceOut.Put(x);
     }
 }
Exemplo n.º 15
0
        private void SPStatusChangedHandler(object sender, SPStatusChangedEventArgs data)
        {
            TraceOut.Put("SPStatusChanged Event arrived. Status = " + data.spStatus.Status + ". Info = " + data.spStatus.AdditionalInfo);
            this.spStatus.Status         = data.spStatus.Status;
            this.spStatus.AdditionalInfo = data.spStatus.AdditionalInfo;

            if (!spStatus.Status.ToUpper().Equals("OK"))
            {
                ScriptHandler currentSH = (ScriptHandler)sender;
                TraceOut.Put("Restarting connection: disposing objects, creating timer...");
                currentSH.SPStatusChanged -= new Diacom.APCService.OnSPStatusChange(SPStatusChangedHandler);
                this.ActiveScripts.Remove(currentSH);
                currentSH.Dispose();
                this.RestartTimer = new System.Threading.Timer(new System.Threading.TimerCallback(this.RestartTimerCallbackEntry), currentSH.FileName, 20000L, System.Threading.Timeout.Infinite);
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Registers remote types.
        /// </summary>
        private void RegisterRemoteTypes()
        {
            try
            {
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(APCServiceProviderStatus),
                                                                   "APCServiceProviderStatus.rem", WellKnownObjectMode.Singleton);
                RemotingServices.Marshal(this.spStatus, "APCServiceProviderStatus.rem");

                RemotingConfiguration.RegisterWellKnownServiceType(typeof(APCServiceOptions),
                                                                   "APCService.rem", WellKnownObjectMode.Singleton);
                RemotingServices.Marshal(this.opt, "APCService.rem");
                TraceOut.Put("Remote types registered...");
            }
            catch (Exception ix)
            {
                TraceOut.Put(ix);
            }
        }
Exemplo n.º 17
0
 private void ScanDirs()
 {
     // Check if ISP interface is somewhere implemented.
     TraceOut.Put("Getting service providers...");
     foreach (string asmblPath in files)
     {
         TraceOut.Put("Loading Assembly :" + asmblPath + " ....");
         // Checking each assembly.
         try
         {
             System.Reflection.Assembly asmbl = System.Reflection.Assembly.LoadFrom(asmblPath);
             foreach (System.Reflection.Module module in asmbl.GetModules())
             {
                 // Checking each module in assembly.
                 foreach (System.Type type in module.GetTypes())
                 {
                     if (type.IsNotPublic)
                     {
                         continue;
                     }
                     // Checking each interface in module.
                     foreach (System.Type interf in type.GetInterfaces())
                     {
                         // Checking each implemented interface in class.
                         if (interf.Equals(typeof(ISP)))
                         {
                             TraceOut.Put("Class that implements interface: " + type.ToString());
                             // ISP interface imlpemented, adding the class.
                             string SPTitle = ((System.Reflection.AssemblyTitleAttribute)(asmbl.GetCustomAttributes(typeof(System.Reflection.AssemblyTitleAttribute), true)[0])).Title;
                             types.Add(SPTitle);
                             providers[SPTitle] = asmblPath;
                             TraceOut.Put("Collecting service providers: \"" + SPTitle + "\" added...");
                         }
                     }
                 }
             }
         }
         catch (Exception x)
         {
             TraceOut.Put(x);
         }
     }
 }
Exemplo n.º 18
0
 /// <summary>
 /// Set things in motion so your service can do its work.
 /// </summary>
 protected override void OnStart(string[] args)
 {
     try
     {
         TraceOut.Put("[APCService] Starting.. Creating and starting main thread...");
         // Creating and starting main thread.
         this.mainThreadHandle = new System.Threading.Thread(new System.Threading.ThreadStart(this.StartHandlers));
         this.mainThreadHandle.SetApartmentState(System.Threading.ApartmentState.MTA);
         this.mainThreadHandle.Priority     = System.Threading.ThreadPriority.Normal;
         this.mainThreadHandle.IsBackground = false;
         this.mainThreadHandle.Name         = "APCSMT";
         this.mainThreadHandle.Start();
     }
     catch (Exception _e)
     {
         TraceOut.Put(_e);
     }
     finally
     {
         base.OnStart(args);
     }
 }
Exemplo n.º 19
0
 /// <summary>
 /// Cleans up any resources being used.
 /// </summary>
 protected override void Dispose(bool disposing)
 {
     TraceOut.Put("Service Dispose called. Disposing = " + disposing);
     if (disposed)
     {
         return;
     }
     disposed = true;
     GC.SuppressFinalize(this);
     if (disposing)
     {
         if (components != null)
         {
             this.components.Dispose();
         }
         if (this.RestartTimer != null)
         {
             this.RestartTimer.Dispose();
             this.RestartTimer = null;
         }
         UnRegisterRemoteTypes();
         UnRegisterTCPChannel();
         TraceOut.Put("Disposing all script handlers...");
         foreach (ScriptHandler sc in this.ActiveScripts)
         {
             try
             {
                 sc.SPStatusChanged -= new Diacom.APCService.OnSPStatusChange(SPStatusChangedHandler);
                 sc.Dispose();
             }
             catch (Exception _e)
             {
                 TraceOut.Put(_e);
             }
         }
     }
     base.Dispose(disposing);
 }
Exemplo n.º 20
0
        /// <summary>
        /// Creats TCP channel for sharing APCServiceCore::GetOptions()
        /// and APCServiceCore::SetOptions(...) functions.
        /// </summary>
        private void RegisterTCPChannel()
        {
            try
            {
                BinaryClientFormatterSinkProvider clientProvider = null;
                BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
                serverProvider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
                IDictionary props = new Hashtable();
                props["port"] = opt.MachinePort;
                props["exclusiveAddressUse"] = false;
                props["typeFilterLevel"]     = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;

                this.TCPChannel = new TcpChannel(props, clientProvider, serverProvider);
                ChannelServices.RegisterChannel(this.TCPChannel, false);

                this.TCPPort = opt.MachinePort;
                TraceOut.Put("TCP channel registered...");
            }
            catch (Exception ix)
            {
                TraceOut.Put(ix);
            }
        }
Exemplo n.º 21
0
 private void WriteConfig()
 {
     System.IO.FileStream fs = null;
     try
     {
         // Saving settings.
         fs           = new System.IO.FileStream(iniConfigFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
         optFormatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
         optFormatter.Serialize(fs, opt);
         TraceOut.Put("Saving " + this.opt.ToString());
     }
     catch (Exception _e)
     {
         TraceOut.Put(_e);
         TraceOut.Put("Cannot save service configuration...");
     }
     finally
     {
         if (fs != null)
         {
             fs.Close();
         }
     }
 }
Exemplo n.º 22
0
 public void Start()
 {
     this.StartThreadHandle.Start();
     TraceOut.Put("[Script Handler]: thread started...");
 }
Exemplo n.º 23
0
 /// <summary>
 /// The main entry point for the process. Starts the service.
 /// </summary>
 static void Main()
 {
     TraceOut.Put("Service Main called.");
     ServiceBase.Run(new APCService());
 }