예제 #1
0
        protected void RunMyRemoteExecExample(int ipversion, string theServer)
        {
            Console.WriteLine("@@@ [tid {0}] TestRemoteExec - start unit test. ", Thread.CurrentThread.ManagedThreadId);

            // server
            using (MyThreadPoolManager tpm = new MyThreadPoolManager(1, 1))
            {
                Console.WriteLine("@@@ [tid {0}] TestRemoteExec - starting server on [{1}]. ", Thread.CurrentThread.ManagedThreadId, theServer);

                tpm.Queue(new TestRemoteExecThreadPoolItem(ipversion));
                tpm.WaitUntilAllStarted();

                // client
                Console.WriteLine("@@@ [tid {0}] TestRemoteExec - starting client to [{1}]. ", Thread.CurrentThread.ManagedThreadId, theServer);

                RemoteExecClient rec;                  //= new RemoteExecClient6("localhost", TestRemoteExecThreadPoolItem.Port);
                if (ipversion == 4)
                {
                    rec = new RemoteExecClient4(theServer, TestRemoteExecThreadPoolItem.Port);
                }
                else
                {
                    rec = new RemoteExecClient6(theServer, TestRemoteExecThreadPoolItem.Port);
                }

                rec.ExecuteCommand("ls", "-al", true);
                Console.WriteLine(rec.Stdout);

                Console.WriteLine("@@@ [tid {0}] TestRemoteExec - finishing client. ", Thread.CurrentThread.ManagedThreadId);

                File.WriteAllText(TestRemoteExecThreadPoolItem.StopFile, "you can finish now");
                Console.WriteLine("@@@ [tid {0}] TestRemoteExec - finishing server. ", Thread.CurrentThread.ManagedThreadId);
            }
            Console.WriteLine("@@@ [tid {0}] TestRemoteExec - finish unit test. ", Thread.CurrentThread.ManagedThreadId);
        }
예제 #2
0
        public void RunMyExample()
        {
            Example1MyThreadPoolItem.Go = false;

            Helper.WriteLine("@@@  RunMyExample - start");

            using (MyThreadPoolManager tpm = new MyThreadPoolManager(100, 100))                 // max 100 threads, queue 200
            {
                Console.WriteLine("@@@  RunMyExample - creating threads");

                for (int i = 1; i <= 100; i++)
                {
                    tpm.Queue(new Example1MyThreadPoolItem());
                }

                Console.WriteLine("@@@  RunMyExample - all threads created, waiting for all threads to start");
                tpm.WaitUntilAllStarted();

                Console.WriteLine("@@@  RunMyExample - all threads started, sleeping 5 sec");
                ST.Thread.Sleep(5000);

                Console.WriteLine("@@@  RunMyExample - all threads created - setting go flag");
                Example1MyThreadPoolItem.Go = true;
                Console.WriteLine("@@@  RunMyExample - all threads created - go flag set");

                Console.WriteLine("@@@  RunMyExample - sleeping for 2 sec");
                ST.Thread.Sleep(2000);

                //Helper.WriteLine( "@@@  RunMyExample - finish - waiting for threads to finish" ) ;
                //tpm.WaitUntilAllFinished() ;
                Helper.WriteLine("@@@  RunMyExample - finish - SHUTDOWN notification.");
            }

            Helper.WriteLine("@@@  RunMyExample - finish - threads finished after SHUTDOWN notification");
        }
예제 #3
0
        protected void RunMyFileWarpExample(int ipversion, string theServer)
        {
            string sourceFile         = @"alp-ch04-threads.txt";
            string sourceFileFullPath = TestFileWarpThreadPoolItem.ServerFileSite + @"\" + sourceFile;

            Assert.IsTrue(File.Exists(sourceFileFullPath));
            FileInfo fiSource = new FileInfo(sourceFileFullPath);

            Assert.IsTrue(fiSource.Length > 100000);

            alby.core.crypto.Hashing hashing = new alby.core.crypto.Hashing();
            byte[] hashSource = hashing.Sha512HashFile(sourceFileFullPath);

            string destFile = @"c:\temp\unittestcopy.txt";

            File.Delete(destFile);
            Assert.IsFalse(File.Exists(destFile));

            Console.WriteLine("@@@ [tid {0}] TestFileWarp - start unit test. ", Thread.CurrentThread.ManagedThreadId);

            // server
            using (MyThreadPoolManager tpm = new MyThreadPoolManager(1, 1))
            {
                Console.WriteLine("@@@ [tid {0}] TestFileWarp - starting server [{1}]. ", Thread.CurrentThread.ManagedThreadId, theServer);

                tpm.Queue(new TestFileWarpThreadPoolItem(ipversion));
                tpm.WaitUntilAllStarted();

                // client
                Console.WriteLine("@@@ [tid {0}] TestFileWarp - starting client to server [{1}]. ", Thread.CurrentThread.ManagedThreadId, theServer);

                FileWarpClient fwc;
                if (ipversion == 4)
                {
                    fwc = new FileWarpClient4(theServer, TestFileWarpThreadPoolItem.Port);
                }
                else
                {
                    fwc = new FileWarpClient6(theServer, TestFileWarpThreadPoolItem.Port);
                }

                fwc.DownloadFile(sourceFile, destFile);
                Console.WriteLine("@@@ [tid {0}] TestFileWarp - finishing client. ", Thread.CurrentThread.ManagedThreadId);

                File.WriteAllText(TestFileWarpThreadPoolItem.StopFile, "you can finish now");
                Console.WriteLine("@@@ [tid {0}] TestFileWarp - finishing server. ", Thread.CurrentThread.ManagedThreadId);
            }

            Assert.IsTrue(File.Exists(destFile));
            FileInfo fiDest = new FileInfo(destFile);

            Assert.AreEqual(fiSource.Length, fiDest.Length);

            byte[] hashDest = hashing.Sha512HashFile(destFile);
            Assert.AreEqual(Convert.ToBase64String(hashDest), Convert.ToBase64String(hashSource));

            Console.WriteLine("@@@ [tid {0}] TestFileWarp - finish unit test. ", Thread.CurrentThread.ManagedThreadId);
        }
예제 #4
0
        // create an Assert.#.cs file for each table
        // containing the function Assert.#( bool insert, # lhs, # rhs )

        protected void Assert(Program p, string theclass, string baseclass)
        {
            Helper h = new Helper();
            List <UnitTestGeneratorAssetPerTableParameters> threadParamList = new List <UnitTestGeneratorAssetPerTableParameters>();

            if (_unitTestTables.Count >= 1)
            {
                using (MyThreadPoolManager tpm = new MyThreadPoolManager(p._threads, _unitTestTables.Count))                     // max threads: _threads, queue length: no of tables
                {
                    int i = 0;
                    foreach (string fqtable in _unitTestTables)
                    {
                        i++;
                        UnitTestGeneratorAssetPerTableParameters utgaptp = new UnitTestGeneratorAssetPerTableParameters();
                        threadParamList.Add(utgaptp);

                        utgaptp.p                = p;
                        utgaptp.theclass         = theclass;
                        utgaptp.baseclass        = baseclass;
                        utgaptp.fqtable          = fqtable;
                        utgaptp.i                = i;
                        utgaptp.tablecount       = _unitTestTables.Count;
                        utgaptp.identitycolumns  = _identityColumnsMap [fqtable];
                        utgaptp.computedcolumns  = _computedColumnsMap [fqtable];
                        utgaptp.timestampcolumns = _timestampColumnsMap[fqtable];
                        utgaptp.columns          = _columnsMap             [fqtable];

                        tpm.Queue(new UnitTestGeneratorAssetPerTableThreadPoolItem(utgaptp));
                    }
                    tpm.WaitUntilAllStarted();
                    tpm.WaitUntilAllFinished();
                }
            }

            // handle any thread exceptions
            foreach (UnitTestGeneratorAssetPerTableParameters utgaptp in threadParamList)
            {
                if (utgaptp.exception != null)
                {
                    throw new ApplicationException("Unit test Assert() worker thread exception", utgaptp.exception);
                }
            }
        } // end Assert
예제 #5
0
        public void DoTables(Program p)
        {
            Helper h = new Helper();

            List <TableGeneratorParameters> list = new List <TableGeneratorParameters>();

            h.MessageVerbose("### Generating code gen tables ###");

            List <string> tables = p._di.Tables.Get();

            if (tables.Count >= 1)                                                                  // anything to do ?
            {
                using (MyThreadPoolManager tpm = new MyThreadPoolManager(p._threads, tables.Count)) // max threads: _threads, queue length: no of tables
                {
                    foreach (string table in tables)
                    {
                        TableGeneratorParameters tgp = new TableGeneratorParameters();
                        list.Add(tgp);

                        tgp.p       = p;
                        tgp.fqtable = table;

                        tpm.Queue(new TableGeneratorThreadPoolItem(tgp));
                    }
                    tpm.WaitUntilAllStarted();
                    tpm.WaitUntilAllFinished();
                }
            }

            h.MessageVerbose("### Generating code gen tables - done ###");

            // handle any thread exceptions
            foreach (TableGeneratorParameters tgp in list)
            {
                if (tgp.exception != null)
                {
                    throw new ApplicationException("DoTables() worker thread exception", tgp.exception);
                }
            }
        } // end do tables
예제 #6
0
        public void DoViews(Program p)
        {
            Helper h = new Helper();

            List <ViewGeneratorParameters> threadParamList = new List <ViewGeneratorParameters>();

            h.MessageVerbose("### Generating code gen views ###");

            List <string> views = p._di.Views.Get();

            if (views.Count >= 1)                                                                  // anything to do ?
            {
                using (MyThreadPoolManager tpm = new MyThreadPoolManager(p._threads, views.Count)) // max threads: _threads, queue length: no of tables
                {
                    foreach (string fqview in views)
                    {
                        ViewGeneratorParameters vgp = new ViewGeneratorParameters();
                        threadParamList.Add(vgp);

                        vgp.p      = p;
                        vgp.fqview = fqview;

                        tpm.Queue(new ViewGeneratorThreadPoolItem(vgp));
                    }
                    tpm.WaitUntilAllStarted();
                    tpm.WaitUntilAllFinished();
                }
            }

            h.MessageVerbose("### Generating code gen views - done ###");

            // handle any thread exceptions
            foreach (ViewGeneratorParameters vgp in threadParamList)
            {
                if (vgp.exception != null)
                {
                    throw new ApplicationException("DoViews() worker thread exception", vgp.exception);
                }
            }
        }
예제 #7
0
        public void DoQueries(Program p)
        {
            Helper h = new Helper();
            List <QueryGeneratorParameters> threadParamList = new List <QueryGeneratorParameters>();

            h.MessageVerbose("### Generating code gen queries ###");

            if (p._queries.Count >= 1)                                                                  // anything to do ?
            {
                using (MyThreadPoolManager tpm = new MyThreadPoolManager(p._threads, p._queries.Count)) // max threads: _threads, queue length: no of tables
                {
                    foreach (XmlNode query in p._queries)
                    {
                        QueryGeneratorParameters qgp = new QueryGeneratorParameters();
                        threadParamList.Add(qgp);

                        qgp.p     = p;
                        qgp.query = query;

                        tpm.Queue(new QueryGeneratorThreadPoolItem(qgp));
                    }
                    tpm.WaitUntilAllStarted();
                    tpm.WaitUntilAllFinished();
                }
            }

            h.MessageVerbose("### Generating code gen queries - done ###");

            // handle any thread exceptions
            foreach (QueryGeneratorParameters qgp in threadParamList)
            {
                if (qgp.exception != null)
                {
                    throw new ApplicationException("DoQueries() worker thread exception", qgp.exception);
                }
            }
        }
예제 #8
0
        protected void DoStoredProcs(Program p)
        {
            Helper h = new Helper();

            List <StoredProcGeneratorParameters> threadParamList = new List <StoredProcGeneratorParameters>();

            h.MessageVerbose("### Generating code gen stored procs ###");
            _storedProcsSubDirectory = _codegen.SelectSingleNode("/CodeGen/StoredProcs/@SubDirectory").Value;

            List <string> storedprocedures = p._di.StoredProcedures.Get();

            if (storedprocedures.Count >= 1)               // anything to do ?
            {
                // create a tt class and ttlist class for each table type

                foreach (var fqtabletype in p._di.TableTypes.Get())
                {
                    Tuple <string, string> schematabletype = h.SplitSchemaFromTable(fqtabletype);

                    string csharpnamespace     = p._namespace + "." + p._storedProcsSubDirectory;
                    string csharptabletype     = h.GetCsharpClassName(p._prefixObjectsWithSchema, schematabletype.Item1, schematabletype.Item2);
                    string thetabletypeclass   = csharptabletype + h.IdentifierSeparator + "tt";
                    string csharptabletypefile = p._directory + @"\" + p._storedProcsSubDirectory + @"\" + csharptabletype + ".tt.cs";

                    string thetabletypelistclass   = csharptabletype + h.IdentifierSeparator + "ttlist";
                    string csharptabletypelistfile = p._directory + @"\" + p._storedProcsSubDirectory + @"\" + csharptabletype + ".ttlist.cs";
                    string ttlistbaseclass         = "scg.List< " + thetabletypeclass + " >, scg.IEnumerable< mss.SqlDataRecord >";

                    List <Tuple <string, string, Int16, Byte, Byte> > columns = p._di.TableTypeColumns.Get(fqtabletype);

                    // tt file
                    h.MessageVerbose("[{0}]", csharptabletypefile);
                    using (StreamWriter sw = new StreamWriter(csharptabletypefile, false, UTF8Encoding.UTF8))
                    {
                        int tab = 0;

                        // header
                        h.WriteCodeGenHeader(sw);
                        h.WriteUsing(sw, p._namespace);

                        // namespace
                        using (NamespaceBlock nsb = new NamespaceBlock(sw, tab++, csharpnamespace))
                            using (ClassBlock cb = new ClassBlock(sw, tab++, thetabletypeclass, "acr.RowBase"))
                                using (StoredProcedureRowConstructorBlock mb = new StoredProcedureRowConstructorBlock(sw, tab, thetabletypeclass, columns, true))
                                {}
                    }

                    // tt list file
                    h.MessageVerbose("[{0}]", csharptabletypelistfile);
                    using (StreamWriter sw = new StreamWriter(csharptabletypelistfile, false, UTF8Encoding.UTF8))
                    {
                        int tab = 0;

                        // header
                        h.WriteCodeGenHeader(sw);
                        h.WriteUsing(sw, p._namespace);

                        // namespace
                        using (NamespaceBlock nsb = new NamespaceBlock(sw, tab++, csharpnamespace))
                            using (ClassBlock cb = new ClassBlock(sw, tab++, thetabletypelistclass, ttlistbaseclass))
                                using (StoredProcedureTableTypeEnumeratorBlock mb = new StoredProcedureTableTypeEnumeratorBlock(sw, tab, thetabletypeclass, columns))
                                {}
                    }
                }

                // write each stored procedure in a file

                using (MyThreadPoolManager tpm = new MyThreadPoolManager(p._threads, storedprocedures.Count))                     // max threads: _threads, queue length: no of sp's
                {
                    foreach (string storedprocedure in storedprocedures)
                    {
                        StoredProcGeneratorParameters tgp = new StoredProcGeneratorParameters();
                        threadParamList.Add(tgp);

                        tgp.p = p;
                        tgp.fqstoredprocedure = storedprocedure;

                        tpm.Queue(new StoredProcGeneratorThreadPoolItem(tgp));
                    }
                    tpm.WaitUntilAllStarted();
                    tpm.WaitUntilAllFinished();
                }
            }
            h.MessageVerbose("### Generating code gen stored procs - done ###");

            // handle any thread exceptions
            foreach (StoredProcGeneratorParameters tgp in threadParamList)
            {
                if (tgp.exception != null)
                {
                    throw new ApplicationException("DoStoredProcs() worker thread exception", tgp.exception);
                }
            }
        }
예제 #9
0
        public void RunMyExample()
        {
            string sourceDirectory = @"c:\temp\spacePictures";
            string destDirectory   = @"c:\temp\spacePicturesCopy";
            int    maxFiles        = 10;

            // how many files to copy
            DirectoryInfo sourceDirectoryInfo = new DirectoryInfo(sourceDirectory);
            int           sourceFileCount     = 0;
            long          bytesSource         = 0;

            // recreate dest directory ;
            //if ( Directory.Exists(destDirectory) )
            //	Directory.Delete(destDirectory, true ) ;
            if (!Directory.Exists(destDirectory))
            {
                Directory.CreateDirectory(destDirectory);
            }

            Assert.IsTrue(Directory.Exists(destDirectory));

            Array.ForEach(
                Directory.GetFiles(destDirectory),
                x => File.Delete(x));

            Assert.IsTrue(Directory.GetFiles(destDirectory).Length == 0);

            //-------------------------------------------

            DateTime start = DateTime.Now;

            Console.WriteLine("*** RunMyExample - start, copying [{0}] files \nfrom [{1}] \nto [{2}]", sourceFileCount, sourceDirectory, destDirectory);

            using (MyThreadPoolManager tpm = new MyThreadPoolManager(100, 100))                // max 100 threads, queue 100
            {
                int i = 0;
                sourceFileCount = 0;
                foreach (FileInfo fi in sourceDirectoryInfo.GetFiles())
                {
                    Example2ThreadPoolItem mtpi = new Example2ThreadPoolItem();
                    mtpi.SourceMachinePath      = sourceDirectory + @"\" + fi.Name;
                    mtpi.DestinationMachinePath = destDirectory + @"\" + fi.Name;
                    tpm.Queue(mtpi);

                    sourceFileCount++;
                    bytesSource += fi.Length;

                    i++;
                    if (i > maxFiles)
                    {
                        break;
                    }
                }

                Helper.WriteLine("*** RunMyExample - waiting for all threads to start...");
                tpm.WaitUntilAllStarted();

                Helper.WriteLine("*** RunMyExample - finish - waiting for threads to finish...");
            }

            Helper.WriteLine("*** RunMyExample - finish - threads finished");
            DateTime finish = DateTime.Now;

            // see if all files copied over
            DirectoryInfo destDirectoryInfo = new DirectoryInfo(destDirectory);
            int           destFileCount     = destDirectoryInfo.GetFiles().Length;

            Assert.AreEqual(sourceFileCount, destFileCount);

            // see if all files copied over
            long bytesDest = 0;

            foreach (FileInfo fi in destDirectoryInfo.GetFiles())
            {
                bytesDest += fi.Length;
            }
            Assert.AreEqual(bytesSource, bytesDest);

            // dump timing info
            double seconds = ((double)finish.Subtract(start).TotalMilliseconds) / 1000.0;
            double rate    = Math.Round(((double)bytesDest) / ((double)seconds) / 1000000.0, 2);

            Helper.MtWriteLine("*** RunMyExample - finish - [{0}] files copied in [{1}] sec - rate [{2}] bytes/sec",
                               sourceFileCount,
                               seconds,
                               rate);
        }
예제 #10
0
        //
        //
        //
        public void Listen(SNS.AddressFamily addressFamily, int port, int timeoutSec, string stopFile, Dictionary <string, string> parameters)
        {
            DateTime startTime = System.DateTime.Now;

            SNS.Socket listenSocket = null;

            try
            {
                // delete stop file
                File.Delete(stopFile);

                // create the main listening socket
                listenSocket = new SNS.Socket(addressFamily, SNS.SocketType.Stream, SNS.ProtocolType.Tcp);

                // set socket options
                listenSocket.SetSocketOption(SNS.SocketOptionLevel.Socket, SNS.SocketOptionName.ReuseAddress, 1);
                listenSocket.SetSocketOption(SNS.SocketOptionLevel.Socket, SNS.SocketOptionName.Linger, new SNS.LingerOption(false, 0));
                listenSocket.SetSocketOption(SNS.SocketOptionLevel.Tcp, SNS.SocketOptionName.NoDelay, 1);

                // bind the listening socket
                if (addressFamily == SNS.AddressFamily.InterNetworkV6)
                {
                    listenSocket.Bind(new IPEndPoint(IPAddress.IPv6Any, port));
                }
                else
                if (addressFamily == SNS.AddressFamily.InterNetwork)
                {
                    listenSocket.Bind(new IPEndPoint(IPAddress.Any, port));
                }
                else
                {
                    throw new SocketException("Cant bind server socket. Unknown address family " + addressFamily.ToString());
                }

                // listen
                listenSocket.Listen(SocketConstants.SOCKET_BACKLOG);
                Console.WriteLine("SocketServer - listening on port [{0}] family [{1}]", port, addressFamily.ToString());

                // accept client connections - run in a thread pool thread
                using (MyThreadPoolManager tpm = new MyThreadPoolManager(50, 5000))                     // max 50 concurrent clients
                {
                    while (true)
                    {
                        // see if it is time to stop the server

                        if (File.Exists(stopFile))                             // stop listening if stop file exists
                        {
                            _stop = true;
                        }
                        else
                        if (timeoutSec > 0)                           // stop listening if server timed out
                        {
                            TimeSpan ts = System.DateTime.Now.Subtract(startTime);
                            if (ts.TotalSeconds >= timeoutSec)
                            {
                                _stop = true;
                            }
                        }

                        if (_stop)                           // time to go
                        {
                            tpm.Shutdown();
                            Console.WriteLine("SocketServer - listening on port [{0}] stopping...", port);
                            break;
                        }

                        if (listenSocket.Poll(200000, SNS.SelectMode.SelectRead))                             // ready to accept - 0.2 sec wait max
                        {
                            SNS.Socket clientSocket = listenSocket.Accept();

                            T tpi = new T();
                            tpi.Parameters = parameters;
                            tpi.SetRawSocket(clientSocket);
                            tpm.Queue(tpi);
                        }
                    }

                    //tpm.WaitUntilAllStarted() ; // servers alive until all queued sockets execute
                }
            }
            finally             // tidy up
            {
                Console.WriteLine("SocketServer [{0}] - listening on port [{1}] stopped", addressFamily, port);

                try
                {
                    listenSocket.Shutdown(SNS.SocketShutdown.Both);
                }
                catch (Exception) {}

                try
                {
                    listenSocket.Close();
                }
                catch (Exception) {}
            }
        } // end method