public void Run()
        {
            EventArgs args = new EventArgs();

            BeginRun?.Invoke(this, args);

            DirectoryInfo directory = new DirectoryInfo(DirectoryFilePath);

            foreach (var file in directory.GetFiles())
            {
                if (Exit)
                {
                    return;
                }

                currentFile = file.Name;
                currentPosition++;

                FixtureAttachFileStartArgs beginAttachArgs = new FixtureAttachFileStartArgs(currentFile);
                BeginAttachFile?.Invoke(this, beginAttachArgs);

                FixtureAttachFileEndArgs endAttachArgs = new FixtureAttachFileEndArgs();
                endAttachArgs.FileName = file.Name;
                endAttachArgs.Objects  = new List <FixtureAttachFileObject>();

                if (Regex.IsMatch(file.Name, @"^\d+"))
                {
                    int idDoc = Convert.ToInt32(Regex.Match(file.Name, @"^\d+").Value);

                    // файл
                    FileStream stream = File.OpenRead(file.FullName);

                    byte[] bytes = new byte[file.Length];
                    stream.Read(bytes, 0, (int)file.Length);

                    // контрольная сумма
                    SHA1   s    = new SHA1CryptoServiceProvider();
                    byte[] hash = s.ComputeHash(bytes);

                    string hashStr = (System.BitConverter.ToString(hash)).Replace("-", "").ToLower();

                    // транзакция
                    OracleConnection connection = obj_lib.Module.OpenSession().Connection as OracleConnection;

                    OracleCommand command = new OracleCommand("pkg_sepo_import_global.attach_fixture_file");
                    command.Connection  = connection;
                    command.CommandType = System.Data.CommandType.StoredProcedure;

                    OracleParameter p_result = new OracleParameter(
                        "p_result", OracleDbType.RefCursor, System.Data.ParameterDirection.Output);

                    OracleParameter p_iddoc   = new OracleParameter("p_iddoc", idDoc);
                    OracleParameter p_docname = new OracleParameter("p_docname", file.Name);
                    OracleParameter p_hash    = new OracleParameter("p_hash", hashStr);
                    OracleParameter p_data    = new OracleParameter("p_data", OracleDbType.Blob);

                    command.Parameters.AddRange(new OracleParameter[] { p_result, p_iddoc, p_docname, p_hash, p_data });

                    p_data.Value = bytes;
                    command.ExecuteNonQuery();

                    OracleRefCursor result = (OracleRefCursor)p_result.Value;

                    using (OracleDataReader rd = result.GetDataReader())
                    {
                        while (rd.Read())
                        {
                            FixtureAttachFileObject obj = new FixtureAttachFileObject();
                            obj.Sign  = rd.GetString(3);
                            obj.State = (rd.GetInt16(4) == 1) ? FixtureAttachFileState.Success : FixtureAttachFileState.None;

                            endAttachArgs.Objects.Add(obj);
                        }
                    }

                    //var result = obj_lib.Module.OpenSession()
                    //    .GetNamedQuery("AttachFixtureFile")
                    //    .SetParameter(0, idDoc)
                    //    .SetParameter(1, file.Name)
                    //    .SetParameter(2, hashStr)
                    //    .SetParameter(3, bytes)
                    //    .List<SEPO_FIXTURE_AF_RESULT_TEMP>();

                    //transaction.Commit();

                    //foreach (var pos in result)
                    //{
                    //    FixtureAttachFileObject obj = new FixtureAttachFileObject();
                    //    obj.Sign = pos.OBJSIGN;
                    //    obj.State = (pos.STATE == 1) ? FixtureAttachFileState.Success : FixtureAttachFileState.None;

                    //    endAttachArgs.Objects.Add(obj);
                    //}
                }

                EndAttachFile?.Invoke(this, endAttachArgs);
            }

            System.Threading.Thread.Sleep(1500);

            EndRun?.Invoke(this, args);
        }
Ejemplo n.º 2
0
        public async Task <ProviderRun> RunAsync(CoreProvider serverProvider, ProviderFixture <CoreProvider> serverFixture, string scopeName = null, string[] tables = null, SyncConfiguration conf = null,
                                                 bool reuseAgent = true)
        {
            // server proxy
            var proxyServerProvider = new WebProxyServerProvider(serverProvider);
            var proxyClientProvider = new WebProxyClientProvider();

            var syncTables = tables ?? serverFixture.Tables;

            // local test, through tcp
            if (NetworkType == NetworkType.Tcp)
            {
                // create agent
                if (this.Agent == null || !reuseAgent)
                {
                    this.Agent = new SyncAgent(ClientProvider, serverProvider, syncTables);
                }

                // copy conf settings
                if (conf != null)
                {
                    serverFixture.CopyConfiguration(this.Agent.Configuration, conf);
                }

                // Add Filers
                if (serverFixture.Filters != null && serverFixture.Filters.Count > 0)
                {
                    serverFixture.Filters.ForEach(f => {
                        if (!Agent.Configuration.Filters.Contains(f))
                        {
                            Agent.Configuration.Filters.Add(f);
                        }
                    });
                }

                // Add Filers values
                if (serverFixture.FilterParameters != null && serverFixture.FilterParameters.Count > 0)
                {
                    foreach (var syncParam in serverFixture.FilterParameters)
                    {
                        if (!this.Agent.Parameters.Contains(syncParam))
                        {
                            this.Agent.Parameters.Add(syncParam);
                        }
                    }
                }

                // sync
                try
                {
                    BeginRun?.Invoke(this.Agent.RemoteProvider);
                    Results = await this.Agent.SynchronizeAsync();

                    EndRun?.Invoke(this.Agent.RemoteProvider);
                }
                catch (Exception ex)
                {
                    Exception = ex;
                    Console.WriteLine(ex);
                }
            }

            // -----------------------------------------------------------------------
            // HTTP
            // -----------------------------------------------------------------------

            // tests through http proxy
            if (NetworkType == NetworkType.Http)
            {
                // client handler
                using (var server = new KestrellTestServer())
                {
                    // server handler
                    var serverHandler = new RequestDelegate(async context =>
                    {
                        SyncConfiguration syncConfiguration = new SyncConfiguration(syncTables);// set proxy conf
                        // copy conf settings

                        if (conf != null)
                        {
                            serverFixture.CopyConfiguration(syncConfiguration, conf);
                        }


                        // set proxy conf
                        proxyServerProvider.Configuration = syncConfiguration;

                        // Add Filers
                        if (serverFixture.Filters != null && serverFixture.Filters.Count > 0)
                        {
                            serverFixture.Filters.ForEach(f => {
                                if (!proxyServerProvider.Configuration.Filters.Contains(f))
                                {
                                    proxyServerProvider.Configuration.Filters.Add(f);
                                }
                            });
                        }


                        // sync
                        try
                        {
                            BeginRun?.Invoke(proxyServerProvider.LocalProvider);
                            await proxyServerProvider.HandleRequestAsync(context);
                            EndRun?.Invoke(proxyServerProvider.LocalProvider);
                        }
                        catch (Exception ew)
                        {
                            Console.WriteLine(ew);
                        }
                    });

                    var clientHandler = new ResponseDelegate(async(serviceUri) =>
                    {
                        // create agent
                        if (this.Agent == null || !reuseAgent)
                        {
                            this.Agent = new SyncAgent(ClientProvider, proxyClientProvider);
                        }

                        if (serverFixture.FilterParameters != null && serverFixture.FilterParameters.Count > 0)
                        {
                            foreach (var syncParam in serverFixture.FilterParameters)
                            {
                                if (!this.Agent.Parameters.Contains(syncParam))
                                {
                                    this.Agent.Parameters.Add(syncParam);
                                }
                            }
                        }

                        ((WebProxyClientProvider)this.Agent.RemoteProvider).ServiceUri = new Uri(serviceUri);

                        try
                        {
                            Results = await Agent.SynchronizeAsync();
                        }
                        catch (Exception ew)
                        {
                            Exception = ew;
                            Console.WriteLine(ew);
                        }
                    });
                    await server.Run(serverHandler, clientHandler);
                }
            }
            return(this);
        }