示例#1
0
        public static void WriteBarSettingsToXML(DevComponents.DotNetBar.DotNetBarManager dotNetBarMainManager)
        {
            Util.WriteMethodInfoToConsole();

            if (File.Exists(xmlFile))
            {
                File.Delete(xmlFile);
            }
            // Create a new file
            XmlTextWriter textWriter = new XmlTextWriter(xmlFile, null);

            // Opens the document
            textWriter.WriteStartDocument();
            // Write comments
            //textWriter.WriteComment("First Comment XmlTextWriter Sample Example");
            //textWriter.WriteComment("myXmlFile.xml in root dir");
            //Write the root element
            textWriter.WriteStartElement("BarControls");

            foreach (DevComponents.DotNetBar.Bar bar in dotNetBarMainManager.Bars)
            {
                HConsole.WriteLine(bar.Text + "(" + bar.Name + "): " + " DOCKED=" + bar.Docked + " POSITION=" + bar.DockedSite.Dock +
                                   " STATE:" + bar.BarState + " AUTOHIDESIDE:" + bar.AutoHideSide + " VISIBLE:" + bar.Visible.ToString());

                if (bar.Name.ToUpper().StartsWith("BAR"))
                {
                    #region V1
                    //textWriter.WriteStartElement("Name", bar.Name);
                    //textWriter.WriteStartElement("Docked"); textWriter.WriteString(bar.Docked.ToString()); textWriter.WriteEndElement();
                    //textWriter.WriteStartElement("Position"); textWriter.WriteString(bar.DockedSite.Dock.ToString()); textWriter.WriteEndElement();
                    //textWriter.WriteStartElement("State"); textWriter.WriteString(bar.BarState.ToString()); textWriter.WriteEndElement();
                    //textWriter.WriteStartElement("AutoHide"); textWriter.WriteString(bar.AutoHideSide.ToString()); textWriter.WriteEndElement();
                    //textWriter.WriteStartElement("Visible"); textWriter.WriteString(bar.Visible.ToString()); textWriter.WriteEndElement();
                    //textWriter.WriteEndElement();
                    #endregion

                    #region V2
                    //textWriter.WriteStartElement("DotNetBar");

                    //textWriter.WriteElementString("Name", bar.Name);
                    //textWriter.WriteElementString("Docked", bar.Docked.ToString());
                    //textWriter.WriteElementString("Position", bar.DockedSite.Dock.ToString());
                    //textWriter.WriteElementString("State", bar.BarState.ToString());
                    //textWriter.WriteElementString("AutoHide", bar.AutoHideSide.ToString());
                    //textWriter.WriteElementString("Visible", bar.Visible.ToString());

                    //textWriter.WriteEndElement();
                    #endregion

                    #region V3 (funktioniert)
                    textWriter.WriteStartElement("DotNetBar");

                    textWriter.WriteAttributeString("Name", bar.Name);
                    textWriter.WriteAttributeString("Docked", bar.Docked.ToString());
                    textWriter.WriteAttributeString("Position", bar.DockedSite.Dock.ToString());
                    textWriter.WriteAttributeString("State", bar.BarState.ToString());
                    textWriter.WriteAttributeString("AutoHide", bar.AutoHideSide.ToString());
                    textWriter.WriteAttributeString("Visible", bar.Visible.ToString());

                    textWriter.WriteEndElement();
                    #endregion
                }
            }

            // End the root element
            textWriter.WriteEndElement();
            // Ends the document.
            textWriter.WriteEndDocument();

            // close writer
            textWriter.Close();
        }
示例#2
0
 public void Reset()
 {
     HConsole.Reset();
 }
示例#3
0
 public void SetUp()
 {
     HConsole.Reset();
 }
示例#4
0
        /// https://www.charmmtutorial.org/index.php/Full_example

        public static string[] Hessian
            (IEnumerable <string> toplines
            , IEnumerable <string> parlines
            , IEnumerable <string> psflines
            , IEnumerable <string> crdlines
            )
        {
            string tempbase = @"C:\temp\";

            string[] mincrdlines;

            using (var temp = new HTempDirectory(tempbase, null))
            {
                temp.EnterTemp();
                string topname    = "top.rtf"; HFile.WriteAllLines(topname, toplines);
                string parname    = "par.prm"; HFile.WriteAllLines(parname, parlines);
                string psfname    = "target.psf"; HFile.WriteAllLines(psfname, psflines);
                string crdname    = "target.crd"; HFile.WriteAllLines(crdname, crdlines);
                string mincrdname = "target-minimized.crd";

                string conf_inp = @"* Minimize PDB
* Minimize PDB
*

! read topology and parameter file
read rtf   card name top.rtf
read param card name par.prm
! read the psf and coordinate file
read psf  card name target.psf
read coor card name target.crd

! set up shake
shake bonh param sele all end

! set up electrostatics, since we're not using PME, set up switching
! electrostatics
nbond inbfrq -1 elec fswitch vdw vswitch cutnb 16. ctofnb 12. ctonnb 10.

energy

coor copy comp


! mini sd nstep 100
! mini abnr nstep 1000 nprint 10000 tolg 0.0000
! ! mini abnr nstep 1000 nprint 100 tolg 0.01
! ! mini abnr nstep 10000000 nprint 100 tolg 0.00
! ! mini abnr nstep   1000000  nprint 100 tolg 10.00    ! target-min-step1-tolg-10_00.crd
! ! mini abnr nstep   1000000  nprint 100 tolg 1.00     ! target-min-step2-tolg-1_00.crd
! ! mini abnr nstep   1000000  nprint 100 tolg 0.10
! ! mini sd nstep 10000                                 ! target-min-step3-sd10000.crd
! ! mini abnr nstep   1000000  nprint 100 tolg 0.01     ! target-min-step4-tolg-0_01.crd
! ! mini abnr nstep   10000  nprint 100 tolg 0.0001     ! target-min-step5-tolg-0_0001.crd
! ! mini abnr nstep   10000  nprint 100 tolg 0.000001   ! target-min-step6-tolg-0_000001.crd
! ! mini abnr nstep   10000  nprint 100 tolg 0.000000     ! target-min-step7-tolg-0_000000.crd
! 
! 
! coor rms
! 
! ioform extended
! 
! write coor card name target-minimized.crd
! * Initial minimization, no PME.
! *

! VIBRAN NMOD 20 ATOM FSWITCH rdie eps 4.0 VDW VSHIFT cutnb 13.0 ctofnb 12.0 CTONNB 8.0
! 
! DIMB ITERations 500 TOLErance 0.04 PARDim 200 IUNMode 21 DWIN
! 
! WRITe SECOnd-derivatives card

! BOMLEV -2
! VIBRAN
! DIAG ENTRopy

! VIBRan NMODes 500
! DIAG
! print norm vector dipole

!mini abnr nstep   100  nprint 100 tolg 0.000000     ! target-min-step7-tolg-0_000000.crd
BOMLEV -2
open unit 1 write form name " + " \"second.dat\" " + @" 
REDUce CMPAct
vibran
!diag
write second card unit 1
close unit 1

!calc natom3 ?NATOM *3
!vibran nmode @natom3
!diag
!print norm

!bomlev - 2
!VIBRan NMOD 300
!DIAGonalize


!https://www.charmm.org/charmm/documentation/by-version/c42b1/params/doc/vibran/

stop



";
                conf_inp = conf_inp.Replace("$$topname$$", topname)
                           .Replace("$$parname$$", parname)
                           .Replace("$$psfname$$", psfname)
                           .Replace("$$crdname$$", crdname)
                           .Replace("$$mincrdname$$", mincrdname)
                ;
                HFile.WriteAllText("conf.inp", conf_inp);

                System.Console.WriteLine("Run the following command at " + temp + " :");
                System.Console.WriteLine("      $ charmm < conf.inp");
                System.Console.WriteLine("   or $ mpd&");
                System.Console.WriteLine("      $ mpirun -n 38 charmm_M < conf.inp");
                System.Console.WriteLine("Then, copy second.dat to " + temp);

                while (true)
                {
                    bool next = HConsole.ReadValue <bool>("Ready for next? ", false, null, false, true);
                    if (next)
                    {
                        if (HFile.ExistsAll(mincrdname))
                        {
                            mincrdlines = HFile.ReadAllLines(mincrdname);
                            /// second.dat has the following format
                            ///
                            /// num-atoms
                            /// pot-energy
                            /// atom1-xforce  atom1-yforce atom1-zforce
                            /// atom2-xforce  atom2-yforce atom2-zforce
                            ///         ...
                            /// atomn-xforce  atomn-yforce atomn-zforce
                            /// upper- or lower-diag elem 1
                            /// upper- or lower-diag elem 2
                            ///   ....
                            /// upper- or lower-diag elem 3n*3n/2
                            /// atom1-xcoord  atom1-ycoord atom1-zcoord
                            /// atom2-xcoord  atom2-ycoord atom2-zcoord
                            ///         ...
                            /// atomn-xcoord  atomn-ycoord atomn-zcoord
                            ///
                            break;
                        }
                        System.Console.WriteLine("DO, copy target.psf and target.crd to " + temp);
                    }
                }

                temp.QuitTemp();
            }
            return(mincrdlines);
        }
示例#5
0
        // this creates members & connect to the cluster, then removes all but 1 member,
        // all while using the client - things should go smoothly
        private async Task Run()
        {
            var totalStopwatch = Stopwatch.StartNew();
            var stopwatch      = new Stopwatch();

            HConsole.WriteLine(this, $"Begin run on cluster {RcCluster.Id.Substring(0, 7)}");

            // add all members
            for (var i = 0; i < MembersTotal; i++)
            {
                HConsole.WriteLine(this, $"Add member #{i}...");
                stopwatch.Restart();
                var member = await AddMember();

                HConsole.WriteLine(this, $"Added {member.Uuid.Substring(0, 7)} at {member.Host}:{member.Port} ({(int)stopwatch.Elapsed.TotalSeconds}s)");
            }

            // prepare options
            var options = new HazelcastOptionsBuilder()
                          .WithHConsoleLogger()
                          .Build();

            options.Networking.Addresses.Clear();
            options.Networking.Addresses.Add("127.0.0.1:5701");
            options.ClusterName = RcCluster.Id;
            options.Messaging.RetryTimeoutSeconds = InvocationTimeoutMinutes * 60;

            // subscribe to members updated
            var membersCount  = 0;
            var createdObject = false;

            options.AddSubscriber(events => events
                                  .MembersUpdated((sender, args) =>
            {
                HConsole.WriteLine(this, $"Handle MembersUpdated ({args.Members.Count} members)");
                membersCount = args.Members.Count;
            })
                                  .ObjectCreated((sender, args) =>
            {
                HConsole.WriteLine(this, $"Object {args.ServiceName}:{args.Name} created");
                createdObject = true;
            }));

            // start client
            HConsole.WriteLine(this, "Start client...");
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            await((HazelcastClient)client).DestroyAsync(ServiceNames.Map, "test-map").CfAwait();

            // immediately begin using the client - while connections to all members are established
            HConsole.WriteLine(this, "Get map...");
            await using var map = await client.GetMapAsync <string, string>("test-map-" + RandomProvider.Random.Next(1000));

            await AssertEx.SucceedsEventually(() =>
            {
                Assert.That(createdObject);
            }, 2000, 200);

            HConsole.WriteLine(this, "Use map...");
            await UseClient(map, 2000, 200);

            // all members are here
            await AssertEx.SucceedsEventually(() =>
            {
                Assert.That(membersCount, Is.EqualTo(MembersTotal));
            }, 2000, 200);

            // keep using the client, but remove members down to 1 last member
            HConsole.WriteLine(this, "Remove members...");
            while (RcMembers.Count > 1)
            {
                var(memberId, member) = RcMembers.First();
                HConsole.WriteLine(this, $"Remove member {member.Uuid.Substring(0, 7)} at {member.Host}:{member.Port}...");
                stopwatch.Restart();
                await RemoveMember(memberId);

                HConsole.WriteLine(this, $"Removed member {member.Uuid.Substring(0, 7)} ({(int)stopwatch.Elapsed.TotalSeconds}s)");

                await Task.Delay(500);

                await UseClientOnce(map);
            }

            // take time for things to stabilize

            // keep using the client
            HConsole.WriteLine(this, "Use map...");
            await UseClient(map, 2000, 200);

            // all members but one are gone
            await AssertEx.SucceedsEventually(() =>
            {
                Assert.That(membersCount, Is.EqualTo(1));
            }, 8000, 200);

            // now terminate the client
            HConsole.WriteLine(this, "Dispose client...");
            await client.DisposeAsync();

            HConsole.WriteLine(this, $"End ({(int)totalStopwatch.Elapsed.TotalSeconds}s)");
        }
示例#6
0
        private async Task CollectSubscriptionsAsync(CancellationToken cancellationToken)
        {
            List <ClusterSubscription> gone = null;

            HConsole.WriteLine(this, "CollectSubscription starting");

            // if canceled, will be awaited properly
            await Task.Delay(_clusterState.Options.Events.SubscriptionCollectDelay, cancellationToken).CfAwait();

            while (true)
            {
                // capture ghosts subscriptions
                List <ClusterSubscription> ghosts;
                lock (_ghostLock)
                {
                    ghosts = _ghostSubscriptions.ToList();
                }

                HConsole.WriteLine(this, $"CollectSubscription loop for {ghosts.Count} subscriptions");

                // try to remove captures subscriptions
                // if canceled, will be awaited properly
                gone?.Clear();
                var timeLimit = DateTime.Now - _clusterState.Options.Events.SubscriptionCollectTimeout;
                foreach (var subscription in ghosts)
                {
                    HConsole.WriteLine(this, "CollectSubscription collects");

                    try
                    {
                        var allRemoved = await RemoveMemberSubscriptionsAsync(subscription, cancellationToken).CfAwait();

                        if (allRemoved || subscription.DeactivateTime < timeLimit)
                        {
                            (gone ??= new List <ClusterSubscription>()).Add(subscription);
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        return; // cancelled - stop everything
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e, "An error occurred while collecting subscriptions.");
                    }
                }

                HConsole.WriteLine(this, $"CollectSubscription collected {gone?.Count??0} subscriptions");

                // update ghost subscriptions
                // none remaining = exit the task
                lock (_ghostLock)
                {
                    if (gone != null)
                    {
                        foreach (var subscription in gone)
                        {
                            _ghostSubscriptions.Remove(subscription);
                        }
                    }

                    if (_ghostSubscriptions.Count == 0)
                    {
                        HConsole.WriteLine(this, "CollectSubscription exits");
                        _ghostTask = null;
                        return;
                    }
                }

                HConsole.WriteLine(this, "CollectSubscription waits");

                // else, wait + loop / try again
                // if canceled, will be awaited properly
                await Task.Delay(_clusterState.Options.Events.SubscriptionCollectPeriod, cancellationToken).CfAwait();
            }
        }
示例#7
0
            public Mode[] GetModesMassReduced(bool delhess, int?numModeReturn, Dictionary <string, object> secs)
            {
                HessMatrix       mwhess_ = GetHessMassWeighted(delhess);
                IMatrix <double> mwhess  = mwhess_;
                bool             bsparse = (mwhess_ is HessMatrixSparse);

                Mode[] modes;
                using (new Matlab.NamedLock(""))
                {
                    string msg = "";
                    {
                        if (bsparse)
                        {
                            Matlab.PutSparseMatrix("V", mwhess_.GetMatrixSparse(), 3, 3);
                        }
                        else
                        {
                            Matlab.PutMatrix("V", ref mwhess, true, true);
                        }
                    }
                    msg += Matlab.Execute("tic;");
                    msg += Matlab.Execute("V = (V+V')/2;                   "); // make symmetric
                    {                                                          // eigen-decomposition
                        if (bsparse)
                        {
                            if (numModeReturn != null)
                            {
                                int    numeig = numModeReturn.Value;
                                string cmd    = "eigs(V," + numeig + ",'sm')";
                                msg += Matlab.Execute("[V,D] = " + cmd + ";        ");
                            }
                            else
                            {
                                msg += Matlab.Execute("[V,D] = eig(full(V));         ");
                            }
                        }
                        else
                        {
                            msg += Matlab.Execute("[V,D] = eig(V);         ");
                        }
                    }
                    msg += Matlab.Execute("tm=toc;                         ");
                    if (secs != null)
                    {
                        int    numcore = Matlab.Environment.NumCores;
                        double tm      = Matlab.GetValue("tm");
                        secs.Clear();
                        secs.Add("num cores", numcore);
                        secs.Add("secs multi-threaded", tm);
                        secs.Add("secs estimated single-threaded", tm * Math.Sqrt(numcore));
                        /// x=[]; for i=1:20; tic; H=rand(100*i); [V,D]=eig(H+H'); xx=toc; x=[x;i,xx]; fprintf('%d, %f\n',i,xx); end; x
                        ///
                        /// http://www.mathworks.com/help/matlab/ref/matlabwindows.html
                        ///     run matlab in single-thread: matlab -nodesktop -singleCompThread
                        ///                    multi-thread: matlab -nodesktop
                        ///
                        /// my computer, single thread: cst1={0.0038,0.0106,0.0277,0.0606,0.1062,0.1600,0.2448,0.3483,0.4963,0.6740,0.9399,1.1530,1.4568,1.7902,2.1794,2.6387,3.0510,3.6241,4.2203,4.8914};
                        ///                    2 cores: cst2={0.0045,0.0098,0.0252,0.0435,0.0784,0.1203,0.1734,0.2382,0.3316,0.4381,0.5544,0.6969,1.0170,1.1677,1.4386,1.7165,2.0246,2.4121,2.8124,3.2775};
                        ///                      scale: (cst1.cst2)/(cst1.cst1)              = 0.663824
                        ///                     approx: (cst1.cst2)/(cst1.cst1)*Sqrt[2.2222] = 0.989566
                        /// my computer, single thread: cst1={0.0073,0.0158,0.0287,0.0573,0.0998,0.1580,0.2377,0.3439,0.4811,0.6612,0.8738,1.0974,1.4033,1.7649,2.1764,2.6505,3.1142,3.5791,4.1910,4.8849};
                        ///                    2 cores: cst2={0.0085,0.0114,0.0250,0.0475,0.0719,0.1191,0.1702,0.2395,0.3179,0.4319,0.5638,0.7582,0.9454,1.1526,1.4428,1.7518,2.0291,2.4517,2.8200,3.3090};
                        ///                      scale: (cst1.cst2)/(cst1.cst1)              = 0.671237
                        ///                     approx: (cst1.cst2)/(cst1.cst1)*Sqrt[2.2222] = 1.00062
                        /// ts4-stat   , singhe thread: cst1={0.0048,0.0213,0.0641,0.1111,0.1560,0.2013,0.3307,0.3860,0.4213,0.8433,1.0184,1.3060,1.9358,2.2699,2.1718,3.0149,3.1081,4.3594,5.0356,5.5260};
                        ///                   12 cores: cst2={0.2368,0.0614,0.0235,0.1321,0.0574,0.0829,0.1078,0.1558,0.1949,0.3229,0.4507,0.3883,0.4685,0.6249,0.6835,0.8998,0.9674,1.1851,1.3415,1.6266};
                        ///                      scale: (cst1.cst2)/(cst1.cst1)                 = 0.286778
                        ///                             (cst1.cst2)/(cst1.cst1)*Sqrt[12*1.1111] = 1.04716
                        /// ts4-stat   , singhe thread: cst1={0.0138,0.0215,0.0522,0.0930,0.1783,0.2240,0.2583,0.4054,0.4603,0.9036,0.9239,1.5220,1.9443,2.1042,2.3583,3.0208,3.5507,3.8810,3.6943,6.2085};
                        ///                   12 cores: cst2={0.1648,0.1429,0.1647,0.0358,0.0561,0.0837,0.1101,0.1525,0.2084,0.2680,0.3359,0.4525,0.4775,0.7065,0.6691,0.9564,1.0898,1.2259,1.2926,1.5879};
                        ///                      scale: (cst1.cst2)/(cst1.cst1)          = 0.294706
                        ///                             (cst1.cst2)/(cst1.cst1)*Sqrt[12] = 1.02089
                        /// ts4-stat   , singhe thread: cst1={0.0126,0.0183,0.0476,0.0890,0.1353,0.1821,0.2265,0.3079,0.4551,0.5703,1.0009,1.2175,1.5922,1.8805,2.1991,2.3096,3.7680,3.7538,3.9216,5.2899,5.6737,7.0783,8.8045,9.0091,9.9658,11.6888,12.8311,14.4933,17.2462,17.5660};
                        ///                   12 cores: cst2={0.0690,0.0117,0.0275,0.0523,0.0819,0.1071,0.1684,0.1984,0.1974,0.2659,0.3305,0.4080,0.4951,0.7089,0.9068,0.7936,1.2632,1.0708,1.3187,1.6106,1.7216,2.1114,2.8249,2.7840,2.8259,3.3394,4.3092,4.2708,5.3358,5.7479};
                        ///                      scale: (cst1.cst2)/(cst1.cst1)          = 0.311008
                        ///                             (cst1.cst2)/(cst1.cst1)*Sqrt[12]  = 1.07736
                        /// Therefore, the speedup using multi-core could be sqrt(#core)
                    }
                    msg += Matlab.Execute("D = diag(D);                    ");

                    if (msg.Trim() != "")
                    {
                        System.Console.WriteLine();
                        bool domanual = HConsole.ReadValue <bool>("possibly failed. Will you do ((('V = (V+V')/2;[V,D] = eig(V);D = diag(D);))) manually ?", false, null, false, true);
                        if (domanual)
                        {
                            Matlab.Clear();
                            Matlab.PutMatrix("V", ref mwhess, true, true);
                            System.Console.WriteLine("cleaning working-space and copying V in matlab are done.");
                            System.Console.WriteLine("do V = (V+V')/2; [V,D]=eig(V); D=diag(D);");
                            while (HConsole.ReadValue <bool>("V and D are ready to use in matlab?", false, null, false, true) == false)
                            {
                                ;
                            }
                            //string path_V = HConsole.ReadValue<string>("path V.mat", @"C:\temp\V.mat", null, false, true);
                            //Matlab.Execute("clear;");
                            //Matlab.PutMatrix("V", ref mwhess, true, true);
                            //Matlab.Execute(string.Format("save('{0}', '-V7.3');", path_V));
                            //while(HConsole.ReadValue<bool>("ready for VD.mat containing V and D?", false, null, false, true) == false) ;
                            //string path_VD = HConsole.ReadValue<string>("path VD.mat", @"C:\temp\VD.mat", null, false, true);
                            //Matlab.Execute(string.Format("load '{0}';", path_V));
                        }
                    }

                    if (numModeReturn != null)
                    {
                        Matlab.PutValue("nmode", numModeReturn.Value);
                        Matlab.Execute("V = V(:,1:nmode);");
                        Matlab.Execute("D = D(1:nmode);");
                    }
                    MatrixByRowCol V = Matlab.GetMatrix("V", MatrixByRowCol.Zeros, true, true);
                    Vector         D = Matlab.GetVector("D");
                    HDebug.Assert(V.RowSize == D.Size);
                    modes = new Mode[D.Size];
                    for (int i = 0; i < D.Size; i++)
                    {
                        Vector eigvec = V.GetColVector(i);
                        double eigval = D[i];
                        modes[i] = new Mode
                        {
                            th     = i,
                            eigval = eigval,
                            eigvec = eigvec,
                        };
                    }
                    V = null;
                }
                System.GC.Collect();

                modes.UpdateMassReduced(mass.ToArray());

                return(modes);
            }
示例#8
0
 public static void Show(Exception ex)
 {
     MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     HConsole.WriteLine(ex.Message, ex);
 }
        /// <summary>
        /// Set the members.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="members">The members.</param>
        /// <returns>The corresponding event arguments, if members were updated; otherwise <c>null</c>.</returns>
        public MembersUpdatedEventArgs SetMembers(int version, ICollection <MemberInfo> members)
        {
            // skip old sets
            if (version < _members.Version)
            {
                return(null);
            }

            // replace the table
            var previous = _members;
            var table    = new MemberTable(version, members);

            lock (_mutex) _members = table;

            // notify the load balancer of the new list of members
            // (the load balancer can always return a member that is not a member
            // anymore, see note in GetMember)
            _loadBalancer.SetMembers(members.Select(x => x.Id));

            // compute changes
            // count 1 for old members, 2 for new members, and then the result is
            // 1=removed, 2=added, 3=unchanged
            // MemberInfo overrides GetHashCode and can be used as a key here
            var diff = new Dictionary <MemberInfo, int>();

            if (previous == null)
            {
                foreach (var m in members)
                {
                    diff[m] = 2;
                }
            }
            else
            {
                foreach (var m in previous.Members)
                {
                    diff[m] = 1;
                }

                foreach (var m in members)
                {
                    if (diff.ContainsKey(m))
                    {
                        diff[m] += 2;
                    }
                    else
                    {
                        diff[m] = 2;
                    }
                }
            }

            // log, if the members have changed (one of them at least is not 3=unchanged)
            if (_logger.IsEnabled(LogLevel.Information) && diff.Any(d => d.Value != 3))
            {
                LogDiffs(table, diff);
            }

            // process changes, gather events
            var added   = new List <MemberInfo>();
            var removed = new List <MemberInfo>();

            foreach (var(member, status) in diff)  // all members, old and new
            {
                switch (status)
                {
                case 1:     // old but not new = removed
                    HConsole.WriteLine(this, $"Removed member {member.Id} at {member.Address}");
                    removed.Add(member);

                    // dequeue the member
                    _memberConnectionQueue?.Remove(member.Id);

                    break;

                case 2:     // new but not old = added
                    HConsole.WriteLine(this, $"Added member {member.Id} at {member.Address}");
                    added.Add(member);

                    // queue the member for connection
                    _memberConnectionQueue?.Add(member);

                    break;

                case 3:     // old and new = no change
                    break;

                default:
                    throw new NotSupportedException();
                }
            }

            var maybeDisconnected = false;

            lock (_mutex)
            {
                // removed members need to have their connection removed and terminated
                foreach (var member in removed)
                {
                    if (_connections.TryGetValue(member.Id, out var c))
                    {
                        _connections.Remove(member.Id);
                        _terminateConnections.Add(c);
                    }
                }

                var isAnyMemberConnected = _members.Members.Any(x => _connections.ContainsKey(x.Id));

                if (!_connected)
                {
                    if (isAnyMemberConnected)
                    {
                        // if we were not connected and now one member happens to be connected then we are now connected
                        // we hold the mutex so nothing bad can happen
                        _logger.LogDebug($"Set members: {removed.Count} removed, {added.Count} added, {members.Count} total and at least one is connected, now connected.");
                        _clusterState.ChangeState(ClientState.Connected, ClientState.Started, ClientState.Disconnected);
                        _connected = true;
                    }
                    else
                    {
                        // remain disconnected
                        _logger.LogDebug($"Set members: {removed.Count} removed, {added.Count} added, {members.Count} total and none is connected, remain disconnected.");
                    }
                }
                else
                {
                    if (isAnyMemberConnected)
                    {
                        // remain connected
                        _logger.LogDebug($"Set members: {removed.Count} removed, {added.Count} added, {members.Count} total and at least one is connected, remain connected.");
                    }
                    else
                    {
                        // we probably are disconnected now
                        // but the connection queue is running and might have re-added a member
                        maybeDisconnected = true;
                    }
                }
            }

            // release _mutex, suspend the queue
            if (maybeDisconnected)
            {
                _memberConnectionQueue?.Suspend();
                var disconnected = false;
                try
                {
                    lock (_mutex)
                    {
                        var isAnyMemberConnected = _members.Members.Any(x => _connections.ContainsKey(x.Id));
                        if (!isAnyMemberConnected)
                        {
                            // no more connected member, we are now disconnected
                            _logger.LogDebug($"Set members: {removed.Count} removed, {added.Count} added, {members.Count} total and none connected, disconnecting.");
                            _clusterState.ChangeState(ClientState.Disconnected, ClientState.Connected);
                            _connected   = false;
                            disconnected = true;
                        }
                        else
                        {
                            _logger.LogDebug($"Set members: {removed.Count} removed, {added.Count} added, {members.Count} total and at least one is connected, remain connected.");
                        }
                    }
                }
                finally
                {
                    _memberConnectionQueue?.Resume(disconnected);
                }
            }

            return(new MembersUpdatedEventArgs(added, removed, members.ToList()));
        }
        public async Task SubscriptionIsCollected()
        {
            var address0 = NetworkAddress.Parse("127.0.0.1:11001");
            var address1 = NetworkAddress.Parse("127.0.0.1:11002");

            var memberId0 = Guid.NewGuid();
            var memberId1 = Guid.NewGuid();

            using var __ = HConsoleForTest();

            HConsole.WriteLine(this, "Begin");
            HConsole.WriteLine(this, "Start servers");

            var loggerFactory = new NullLoggerFactory();

            var state0 = new ServerState
            {
                Id        = 0,
                MemberIds = new[] { memberId0, memberId1 },
                Addresses = new[] { address0, address1 },
                MemberId  = memberId0,
                Address   = address0
            };

            await using var server0 = new Server(address0, ServerHandler, loggerFactory, state0, "0")
                        {
                            MemberId = state0.MemberId,
                        };
            await server0.StartAsync();

            var state1 = new ServerState
            {
                Id        = 1,
                MemberIds = new[] { memberId0, memberId1 },
                Addresses = new[] { address0, address1 },
                MemberId  = memberId1,
                Address   = address1
            };

            await using var server1 = new Server(address1, ServerHandler, loggerFactory, state1, "1")
                        {
                            MemberId  = state1.MemberId,
                            ClusterId = server0.ClusterId
                        };
            await server1.StartAsync();

            HConsole.WriteLine(this, "Start client");

            var options = HazelcastOptions.Build(configure: (configuration, options) =>
            {
                options.Networking.Addresses.Add("127.0.0.1:11001");
                options.Networking.Addresses.Add("127.0.0.1:11002");
                options.Events.SubscriptionCollectDelay = TimeSpan.FromSeconds(4); // don't go too fast
            });

            await using var client = (HazelcastClient) await HazelcastClientFactory.StartNewClientAsync(options);

            HConsole.WriteLine(this, "Get map");

            var map = await client.GetMapAsync <string, string>("name");

            var count = 0;

            var clusterEvents = client.Cluster.Events;

            Assert.That(clusterEvents.Subscriptions.Count, Is.EqualTo(0));           // no client subscription yet
            Assert.That(clusterEvents.CorrelatedSubscriptions.Count, Is.EqualTo(1)); // but the cluster views subscription

            HConsole.WriteLine(this, "Subscribe");

            var sid = await map.SubscribeAsync(events => events
                                               .EntryAdded((sender, args) => Interlocked.Increment(ref count))
                                               );

            Assert.That(clusterEvents.Subscriptions.Count, Is.EqualTo(1));                   // 1 (our) client subscription
            Assert.That(clusterEvents.Subscriptions.TryGetValue(sid, out var subscription)); // can get our subscription

            await AssertEx.SucceedsEventually(() =>
            {
                Assert.That(clusterEvents.CorrelatedSubscriptions.Count, Is.EqualTo(3)); // 2 more correlated
                Assert.That(subscription.Count, Is.EqualTo(2));                          // 2 members
                Assert.That(subscription.Active);
            }, 4000, 200);

            HConsole.WriteLine(this, "Set");

            await map.SetAsync("key", "value");

            await AssertEx.SucceedsEventually(() =>
            {
                Assert.That(count, Is.EqualTo(1)); // event triggered
            }, 2000, 100);

            HConsole.WriteLine(this, "Unsubscribe");

            var unsubscribed = await map.UnsubscribeAsync(sid);

            Assert.That(unsubscribed);

            // we have a 4 sec delay before the collect task actually collects

            await AssertEx.SucceedsEventually(() =>
            {
                Assert.That(subscription.Active, Is.False, "active");
                Assert.That(clusterEvents.Subscriptions.Count, Is.EqualTo(0), "count.1");           // is gone
                Assert.That(clusterEvents.CorrelatedSubscriptions.Count, Is.EqualTo(1), "count.2"); // are gone
                Assert.That(subscription.Count, Is.EqualTo(1), "count.3");                          // 1 remains
                Assert.That(clusterEvents.CollectSubscriptions.Count, Is.EqualTo(1), "count.4");    // is ghost
            }, 4000, 200);

            // get a key that targets server 1 - the one that's going to send the event
            var key = GetKey(1, 2, client.SerializationService);

            HConsole.WriteLine(this, "Set key=" + key);

            await map.SetAsync(key, "value");

            await Task.Delay(100);

            Assert.That(count, Is.EqualTo(1)); // no event

            await AssertEx.SucceedsEventually(() =>
            {
                Assert.That(subscription.Count, Is.EqualTo(0));                       // 0 remains
                Assert.That(clusterEvents.CollectSubscriptions.Count, Is.EqualTo(0)); // is gone
            }, 8000, 200);
        }
示例#11
0
 public static void WriteMethodInfoToConsole(string MethodName)
 {
     HConsole.WriteLine("Aufruf der Methode: " + MethodName + "()");
 }
        private async ValueTask ServerHandler(Server s, ClientMessageConnection conn, ClientMessage msg)
        {
            async Task SendResponseAsync(ClientMessage response)
            {
                response.CorrelationId = msg.CorrelationId;
                response.Flags        |= ClientMessageFlags.BeginFragment | ClientMessageFlags.EndFragment;
                await conn.SendAsync(response).CfAwait();
            }

            async Task SendEventAsync(ClientMessage eventMessage, long correlationId)
            {
                eventMessage.CorrelationId = correlationId;
                eventMessage.Flags        |= ClientMessageFlags.BeginFragment | ClientMessageFlags.EndFragment;
                await conn.SendAsync(eventMessage).CfAwait();
            }

            async Task SendErrorAsync(RemoteError error, string message)
            {
                var errorHolders = new List <ErrorHolder>
                {
                    new ErrorHolder(error, "?", message, Enumerable.Empty <StackTraceElement>())
                };
                var response = ErrorsServerCodec.EncodeResponse(errorHolders);

                await SendResponseAsync(response).CfAwait();
            }

            var state   = (ServerState)s.State;
            var address = s.Address;

            const int partitionsCount = 2;

            switch (msg.MessageType)
            {
            // must handle auth
            case ClientAuthenticationServerCodec.RequestMessageType:
            {
                HConsole.WriteLine(this, $"(server{state.Id}) Authentication");
                var authRequest  = ClientAuthenticationServerCodec.DecodeRequest(msg);
                var authResponse = ClientAuthenticationServerCodec.EncodeResponse(
                    0, address, s.MemberId, SerializationService.SerializerVersion,
                    "4.0", partitionsCount, s.ClusterId, false);
                await SendResponseAsync(authResponse).CfAwait();

                break;
            }

            // must handle events
            case ClientAddClusterViewListenerServerCodec.RequestMessageType:
            {
                HConsole.WriteLine(this, $"(server{state.Id}) AddClusterViewListener");
                var addRequest  = ClientAddClusterViewListenerServerCodec.DecodeRequest(msg);
                var addResponse = ClientAddClusterViewListenerServerCodec.EncodeResponse();
                await SendResponseAsync(addResponse).CfAwait();

                _ = Task.Run(async() =>
                    {
                        await Task.Delay(500).CfAwait();

                        const int membersVersion = 1;
                        var memberVersion        = new MemberVersion(4, 0, 0);
                        var memberAttributes     = new Dictionary <string, string>();
                        var membersEventMessage  = ClientAddClusterViewListenerServerCodec.EncodeMembersViewEvent(membersVersion, new[]
                        {
                            new MemberInfo(state.MemberIds[0], state.Addresses[0], memberVersion, false, memberAttributes),
                            new MemberInfo(state.MemberIds[1], state.Addresses[1], memberVersion, false, memberAttributes),
                        });
                        await SendEventAsync(membersEventMessage, msg.CorrelationId).CfAwait();

                        await Task.Delay(500).CfAwait();

                        const int partitionsVersion = 1;
                        var partitionsEventMessage  = ClientAddClusterViewListenerServerCodec.EncodePartitionsViewEvent(partitionsVersion, new[]
                        {
                            new KeyValuePair <Guid, IList <int> >(state.MemberIds[0], new List <int> {
                                0
                            }),
                            new KeyValuePair <Guid, IList <int> >(state.MemberIds[1], new List <int> {
                                1
                            }),
                        });
                        await SendEventAsync(partitionsEventMessage, msg.CorrelationId).CfAwait();
                    });

                break;
            }

            // create object
            case ClientCreateProxyServerCodec.RequestMessageType:
            {
                HConsole.WriteLine(this, $"(server{state.Id}) CreateProxy");
                var createRequest  = ClientCreateProxyServerCodec.DecodeRequest(msg);
                var createResponse = ClientCreateProxiesServerCodec.EncodeResponse();
                await SendResponseAsync(createResponse).CfAwait();

                break;
            }

            // subscribe
            case MapAddEntryListenerServerCodec.RequestMessageType:
            {
                HConsole.WriteLine(this, $"(server{state.Id}) AddEntryListener");
                var addRequest = MapAddEntryListenerServerCodec.DecodeRequest(msg);
                state.Subscribed = true;
                state.SubscriptionCorrelationId = msg.CorrelationId;
                var addResponse = MapAddEntryListenerServerCodec.EncodeResponse(state.SubscriptionId);
                await SendResponseAsync(addResponse).CfAwait();

                break;
            }

            // unsubscribe
            // server 1 removes on first try, server 2 removes on later tries
            case MapRemoveEntryListenerServerCodec.RequestMessageType:
            {
                HConsole.WriteLine(this, $"(server{state.Id}) RemoveEntryListener");
                var removeRequest = MapRemoveEntryListenerServerCodec.DecodeRequest(msg);
                var removed       = state.Subscribed && removeRequest.RegistrationId == state.SubscriptionId;
                removed &= state.Id == 0 || state.UnsubscribeCount++ > 0;
                if (removed)
                {
                    state.Subscribed = false;
                }
                HConsole.WriteLine(this, $"(server{state.Id}) Subscribed={state.Subscribed}");
                var removeResponse = MapRemoveEntryListenerServerCodec.EncodeResponse(removed);
                await SendResponseAsync(removeResponse).CfAwait();

                break;
            }

            // add to map & trigger event
            case MapSetServerCodec.RequestMessageType:
            {
                HConsole.WriteLine(this, $"(server{state.Id}) Set");
                var setRequest  = MapSetServerCodec.DecodeRequest(msg);
                var setResponse = MapSetServerCodec.EncodeResponse();
                await SendResponseAsync(setResponse).CfAwait();

                HConsole.WriteLine(this, $"(server{state.Id}) Subscribed={state.Subscribed}");

                if (state.Subscribed)
                {
                    HConsole.WriteLine(this, $"(server{state.Id}) Trigger event");
                    var key        = setRequest.Key;
                    var value      = setRequest.Value;
                    var addedEvent = MapAddEntryListenerServerCodec.EncodeEntryEvent(key, value, value, value, (int)MapEventTypes.Added, state.SubscriptionId, 1);
                    await SendEventAsync(addedEvent, state.SubscriptionCorrelationId).CfAwait();
                }
                break;
            }

            // unexpected message = error
            default:
            {
                // RemoteError.Hazelcast or RemoteError.RetryableHazelcast
                var messageName = MessageTypeConstants.GetMessageTypeName(msg.MessageType);
                await SendErrorAsync(RemoteError.Hazelcast, $"MessageType {messageName} (0x{msg.MessageType:X}) not implemented.").CfAwait();

                break;
            }
            }
        }
示例#13
0
 static SslLayer()
 {
     HConsole.Configure(consoleOptions => consoleOptions.Configure <SslLayer>().SetPrefix("SSL"));
 }
        public void WriteLog(LogMessageEntry entry)
        {
#if HZ_CONSOLE
            HConsole.WriteLine(this, $"{entry.TimeStamp}{entry.LevelString}{entry.Message.TrimEnd('\r', '\n')}");
#endif
        }
        public async Task CanRetryAndTimeout()
        {
            var address = NetworkAddress.Parse("127.0.0.1:11001");

            HConsole.Configure(this, config => config.SetIndent(0).SetPrefix("TEST"));
            HConsole.WriteLine(this, "Begin");

            HConsole.WriteLine(this, "Start server");
            await using var server = new Server(address, async(svr, conn, msg) =>
            {
                async Task ResponseAsync(ClientMessage response)
                {
                    response.CorrelationId = msg.CorrelationId;
                    response.Flags        |= ClientMessageFlags.BeginFragment | ClientMessageFlags.EndFragment;
                    await conn.SendAsync(response).CAF();
                }

                async Task EventAsync(ClientMessage eventMessage)
                {
                    eventMessage.CorrelationId = msg.CorrelationId;
                    eventMessage.Flags        |= ClientMessageFlags.BeginFragment | ClientMessageFlags.EndFragment;
                    await conn.SendAsync(eventMessage).CAF();
                }

                switch (msg.MessageType)
                {
                // must handle auth
                case ClientAuthenticationServerCodec.RequestMessageType:
                    var authRequest  = ClientAuthenticationServerCodec.DecodeRequest(msg);
                    var authResponse = ClientAuthenticationServerCodec.EncodeResponse(
                        0, address, Guid.NewGuid(), SerializationService.SerializerVersion,
                        "4.0", 1, Guid.NewGuid(), false);
                    await ResponseAsync(authResponse).CAF();
                    break;

                // must handle events
                case ClientAddClusterViewListenerServerCodec.RequestMessageType:
                    var addRequest  = ClientAddClusterViewListenerServerCodec.DecodeRequest(msg);
                    var addResponse = ClientAddClusterViewListenerServerCodec.EncodeResponse();
                    await ResponseAsync(addResponse).CAF();

                    _ = Task.Run(async() =>
                    {
                        await Task.Delay(500).CAF();
                        var eventMessage = ClientAddClusterViewListenerServerCodec.EncodeMembersViewEvent(1, new[]
                        {
                            new MemberInfo(Guid.NewGuid(), address, new MemberVersion(4, 0, 0), false, new Dictionary <string, string>()),
                        });
                        await EventAsync(eventMessage).CAF();
                    });

                    break;

                default:
                    HConsole.WriteLine(svr, "Respond with error.");
                    var response = CreateErrorMessage(ClientProtocolError.RetryableHazelcast);
                    await ResponseAsync(response).CAF();
                    break;
                }
            }, LoggerFactory);
            await server.StartAsync().CAF();

            HConsole.WriteLine(this, "Start client");
            var options = HazelcastOptions.Build(configure: (configuration, options) =>
            {
                options.Networking.Addresses.Add("127.0.0.1:11001");
            });

            await using var client = (HazelcastClient)HazelcastClientFactory.CreateClient(options);
            await client.StartAsync().CAF();

            HConsole.WriteLine(this, "Send message");
            var message = ClientPingServerCodec.EncodeRequest();

            var token = new CancellationTokenSource(3_000).Token;

            Assert.ThrowsAsync <TaskCanceledException>(async() => await client.Cluster.Messaging.SendAsync(message, token).CAF());

            // TODO dispose the client, the server
            await server.StopAsync().CAF();
        }
 public void WriteLog(LogMessageEntry entry)
 {
     HConsole.WriteLine(this, $"{entry.TimeStamp}{entry.LevelString}{entry.Message}");
 }
        public async Task SchedulerTest()
        {
            // this test verifies that the scheduler works as expected

            using var _ = HConsole.Capture(consoleOptions => consoleOptions
                                           .Configure().SetMinLevel()
                                           .Configure <DistributedEventScheduler>().SetMaxLevel()
                                           .Configure(this).SetMaxLevel().SetPrefix("TEST"));

            using var loggerFactory = LoggerFactory.Create(builder => builder.AddHConsole());
            var logger    = loggerFactory.CreateLogger("TEST");
            var scheduler = new DistributedEventScheduler(loggerFactory);

            var pe = new ConcurrentDictionary <int, long>();

            var exceptionCount = 0;

            var concurrentCount   = 0;
            var maxConcurentCount = 0;
            var concurrentLock    = new object();

            scheduler.HandlerError += (sender, args) =>
            {
                Interlocked.Increment(ref exceptionCount);
                var message = "An event handler has thrown: " + args.Exception.Message;
                if (args.Message.CorrelationId < 25)
                {
                    args.Handled = true;
                    message     += " (handling)";
                }
                HConsole.WriteLine(this, "WARN: " + message);
            };

            var s = new ClusterSubscription(async(clientMessage, state) =>
            {
                HConsole.WriteLine(this, $"Handling event for partition: {clientMessage.PartitionId}, sequence: {clientMessage.CorrelationId}");

                lock (pe)
                {
                    // for each partition, events trigger in the right order
                    if (pe.TryGetValue(clientMessage.PartitionId, out var pv))
                    {
                        Assert.That(clientMessage.CorrelationId, Is.GreaterThan(pv));
                    }
                    pe[clientMessage.PartitionId] = clientMessage.CorrelationId;
                }

                //await Task.Yield();
                lock (concurrentLock) concurrentCount++;
                await Task.Delay(200);
                lock (concurrentLock)
                {
                    maxConcurentCount = Math.Max(maxConcurentCount, concurrentCount);
                    concurrentCount--;
                }

                if (clientMessage.CorrelationId % 10 == 0)
                {
                    throw new Exception($"Throwing for partition: {clientMessage.PartitionId} sequence: {clientMessage.CorrelationId}");
                }
                HConsole.WriteLine(this, $"Handled event for partition: {clientMessage.PartitionId} sequence: {clientMessage.CorrelationId}");
            });

            for (var i = 0; i < 50; i++)
            {
                var capture = i;
                var m       = new ClientMessage(new Frame(new byte[64]))
                {
                    PartitionId   = RandomProvider.Random.Next(4),
                    CorrelationId = capture,
                };

                // can add the events
                Assert.That(scheduler.Add(s, m), Is.True);
            }

            // make sure everything is completed
            // (disposing aborts all queues)
            await AssertEx.SucceedsEventually(() =>
            {
                Assert.That(scheduler.Count, Is.EqualTo(0));
            }, 8000, 200);

            // aborts the scheduler & all current queues!
            await scheduler.DisposeAsync();

            // the exceptions have triggered the OnError handler
            Assert.That(exceptionCount, Is.EqualTo(5));

            // the scheduler counts exceptions
            Assert.That(scheduler.ExceptionCount, Is.EqualTo(5));
            Assert.That(scheduler.UnhandledExceptionCount, Is.EqualTo(2));

            // cannot add events to disposed event scheduler
            Assert.That(scheduler.Add(s, new ClientMessage(new Frame(new byte[64]))), Is.False);

            // some events ran concurrently
            Console.WriteLine(maxConcurentCount);
            Assert.That(maxConcurentCount, Is.GreaterThan(1));

            // all tasks are gone
            Assert.That(scheduler.Count, Is.EqualTo(0));
        }
示例#18
0
 /// <summary>
 /// Handles a connection shutdown.
 /// </summary>
 /// <param name="connection">The connection.</param>
 /// <returns>A task that will complete when the connection shutdown has been handled.</returns>
 private void SocketShutdown(SocketConnectionBase connection)
 {
     HConsole.WriteLine(this, "Removing connection " + connection.Id);
     _connections.TryRemove(connection.Id, out _);
 }
 /// <summary>
 /// Handles a connection shutdown.
 /// </summary>
 /// <param name="connection">The connection.</param>
 /// <returns>A task that will complete when the connection shutdown has been handled.</returns>
 private ValueTask SocketShutdown(SocketConnectionBase connection)
 {
     HConsole.WriteLine(this, "Removing connection " + connection.Id);
     _connections.TryRemove(connection.Id, out _);
     return(default);
示例#20
0
        public static SPsfCrd BuildPsfCrd
            (IEnumerable <string> toplines
            , IEnumerable <string> parlines
            , IEnumerable <string> pdblines
            )
        {
            string tempbase = @"C:\temp\";

            SPsfCrd psfcrd;

            using (var temp = new HTempDirectory(tempbase, null))
            {
                temp.EnterTemp();
                string topname = "top.rtf"; HFile.WriteAllLines(topname, toplines);
                string parname = "par.prm"; HFile.WriteAllLines(parname, parlines);

                string tgtname = "target"; HFile.WriteAllLines(tgtname + ".pdb", pdblines);

                string Setup_inp = @"* Run Segment Through CHARMM
*

! read topology and parameter files

read rtf  card name $$topname$$
read para card name $$parname$$

! Read sequence from the PDB coordinate file
open unit 1 card read name $$tgtname$$.pdb
read sequ pdb unit 1

! now generate the PSF and also the IC table (SETU keyword)
generate setu a-pro first NTER last CTER

rewind unit 1

! set bomlev to -1 to avois sying on lack of hydrogen coordinates
bomlev -1
read coor pdb unit 1
! them put bomlev back up to 0
bomlev 0

close unit 1

! prints out number of atoms that still have undefined coordinates.
define test select segid a-pro .and. ( .not. hydrogen ) .and. ( .not. init ) show end

ic para
ic fill preserve
ic build
hbuild sele all end

! write out the protein structure file (psf) and
! the coordinate file in pdb and crd format.

write psf card name $$tgtname$$.psf
* PSF
*

write coor card name $$tgtname$$.crd
* Coords
*

stop

"
                                   .Replace("$$topname$$", topname)
                                   .Replace("$$parname$$", parname)
                                   .Replace("$$tgtname$$", tgtname);
                HFile.WriteAllText("Setup.inp", Setup_inp);

                System.Console.WriteLine("Run the following command at " + temp + " :");
                System.Console.WriteLine("      $ charmm < Setup.inp");
                System.Console.WriteLine("   or $ mpd&");
                System.Console.WriteLine("      $ mpirun -n 38 charmm_M < Setup.inp");
                System.Console.WriteLine("Then, copy target.psf and target.crd to " + temp);

                while (true)
                {
                    bool next = HConsole.ReadValue <bool>("Ready for next? ", false, null, false, true);
                    if (next)
                    {
                        if (HFile.ExistsAll("target.crd", "target.psf"))
                        {
                            string[] crdlines = HFile.ReadAllLines("target.crd");
                            string[] psflines = HFile.ReadAllLines("target.psf");
                            psfcrd = new SPsfCrd
                            {
                                crdlines = crdlines,
                                psflines = psflines,
                            };
                            break;
                        }
                        System.Console.WriteLine("DO, copy target.psf and target.crd to " + temp);
                    }
                }

                temp.QuitTemp();
            }

            return(psfcrd);
        }
示例#21
0
        /// https://www.charmmtutorial.org/index.php/Full_example

        public static string[] MinimizeCrd
            (IEnumerable <string> toplines
            , IEnumerable <string> parlines
            , IEnumerable <string> psflines
            , IEnumerable <string> crdlines
            , string conf_inp_mini
            )
        {
            string tempbase = @"C:\temp\";

            if (conf_inp_mini == null)
            {
                conf_inp_mini = @"
mini sd nstep 100
mini abnr nstep 1000 nprint 10000 tolg 0.0001
! mini abnr nstep 1000 nprint 100 tolg 0.01
! mini abnr nstep 10000000 nprint 100 tolg 0.00
! mini abnr nstep   1000000  nprint 100 tolg 10.00    ! target-min-step1-tolg-10_00.crd
! mini abnr nstep   1000000  nprint 100 tolg 1.00     ! target-min-step2-tolg-1_00.crd
! mini abnr nstep   1000000  nprint 100 tolg 0.10
! mini sd nstep 10000                                 ! target-min-step3-sd10000.crd
! mini abnr nstep   1000000  nprint 100 tolg 0.01     ! target-min-step4-tolg-0_01.crd
! mini abnr nstep   10000  nprint 100 tolg 0.0001     ! target-min-step5-tolg-0_0001.crd
! mini abnr nstep   10000  nprint 100 tolg 0.000001   ! target-min-step6-tolg-0_000001.crd
! mini abnr nstep   10000  nprint 100 tolg 0.000000     ! target-min-step7-tolg-0_000000.crd
";
            }

            string[] mincrdlines;

            using (var temp = new HTempDirectory(tempbase, null))
            {
                temp.EnterTemp();
                string topname    = "top.rtf"; HFile.WriteAllLines(topname, toplines);
                string parname    = "par.prm"; HFile.WriteAllLines(parname, parlines);
                string psfname    = "target.psf"; HFile.WriteAllLines(psfname, psflines);
                string crdname    = "target.crd"; HFile.WriteAllLines(crdname, crdlines);
                string mincrdname = "target-minimized.crd";

                string conf_inp = @"* Minimize PDB
*

! read topology and parameter file
read rtf   card name $$topname$$
read param card name $$parname$$
! read the psf and coordinate file
read psf  card name $$psfname$$
read coor card name $$crdname$$

! set up shake
shake bonh param sele all end

! set up electrostatics, since we're not using PME, set up switching
! electrostatics
nbond inbfrq -1 elec fswitch vdw vswitch cutnb 16. ctofnb 12. ctonnb 10.

energy

coor copy comp

" + conf_inp_mini + @"

coor rms

ioform extended

write coor card name $$mincrdname$$
* Initial minimization, no PME.
*

stop


";
                conf_inp = conf_inp.Replace("$$topname$$", topname)
                           .Replace("$$parname$$", parname)
                           .Replace("$$psfname$$", psfname)
                           .Replace("$$crdname$$", crdname)
                           .Replace("$$mincrdname$$", mincrdname)
                ;
                HFile.WriteAllText("conf.inp", conf_inp);

                System.Console.WriteLine("Run the following command at " + temp + " :");
                System.Console.WriteLine("      $ charmm < conf.inp");
                System.Console.WriteLine("   or $ mpd&");
                System.Console.WriteLine("      $ mpirun -n 38 charmm_M < conf.inp");
                System.Console.WriteLine("Then, copy target-minimized.crd to " + temp);

                while (true)
                {
                    bool next = HConsole.ReadValue <bool>("Ready for next? ", false, null, false, true);
                    if (next)
                    {
                        if (HFile.ExistsAll(mincrdname))
                        {
                            mincrdlines = HFile.ReadAllLines(mincrdname);
                            break;
                        }
                        System.Console.WriteLine("DO, copy target.psf and target.crd to " + temp);
                    }
                }

                temp.QuitTemp();
            }
            return(mincrdlines);
        }
示例#22
0
        /// <summary>
        /// Assigns a connection to support the cluster view event.
        /// </summary>
        /// <param name="connection">An optional candidate connection.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <returns>A task that will complete when a connection has been assigned to handle the cluster views event.</returns>
        private async Task AssignClusterViewsConnectionAsync(MemberConnection connection, CancellationToken cancellationToken)
        {
            // TODO: consider throttling

            ValueTask <MemberConnection> WaitRandomConnection(CancellationToken token)
            {
                var c = _clusterMembers.GetRandomConnection();

                return(c == null
                    ? WaitRandomConnection2(token)
                    : new ValueTask <MemberConnection>(c));
            }

            async ValueTask <MemberConnection> WaitRandomConnection2(CancellationToken token)
            {
                MemberConnection c = null;

                while (!token.IsCancellationRequested &&
                       ((c = _clusterMembers.GetRandomConnection()) == null || !c.Active))
                {
                    lock (_mutex) _connectionOpened = new TaskCompletionSource <object>();
                    using var reg = token.Register(() => _connectionOpened.TrySetCanceled());
                    await _connectionOpened.Task.CfAwait();

                    lock (_mutex) _connectionOpened = null;
                }
                return(c);
            }

            // this will only exit once a connection is assigned, or the task is
            // cancelled, when the cluster goes down (and never up again)
            while (!cancellationToken.IsCancellationRequested)
            {
                connection ??= await WaitRandomConnection(cancellationToken).CfAwait();

                // try to subscribe, relying on the default invocation timeout,
                // so this is not going to last forever - we know it will end
                var correlationId = _clusterState.GetNextCorrelationId();
                if (!await SubscribeToClusterViewsAsync(connection, correlationId, cancellationToken).CfAwait()) // does not throw
                {
                    // failed => try another connection
                    connection = null;
                    continue;
                }

                // success!
                lock (_clusterViewsMutex)
                {
                    if (connection.Active)
                    {
                        _clusterViewsConnection    = connection;
                        _clusterViewsCorrelationId = correlationId;
                        _clusterViewsTask          = null;
                        HConsole.WriteLine(this, $"ClusterViews: connection {connection.Id.ToShortString()} [{correlationId}]");
                        break;
                    }
                }

                // if the connection was not active anymore, we have rejected it
                // if the connection was active, and we have accepted it, and it de-activates,
                // then ClearClusterViewsConnection will deal with it
            }
        }
 static DistributedEventScheduler()
 {
     HConsole.Configure(x => x.Configure <DistributedEventScheduler>().SetPrefix("EVTS.SCHED"));
 }
示例#24
0
        // body of the subscription collection task
        private async Task CollectSubscriptionsAsync(CancellationToken cancellationToken)
        {
            List <MemberSubscription> removedSubscriptions = null;

            HConsole.WriteLine(this, "CollectSubscription starting");

            // if canceled, will be awaited properly
            await Task.Delay(_clusterState.Options.Events.SubscriptionCollectDelay, cancellationToken).CfAwait();

            while (!cancellationToken.IsCancellationRequested)
            {
                // capture subscriptions to collect
                List <MemberSubscription> subscriptions;
                lock (_collectMutex)
                {
                    subscriptions = _collectSubscriptions.ToList();
                }

                HConsole.WriteLine(this, $"CollectSubscription loop for {subscriptions.Count} member subscriptions");

                // try to remove captured subscriptions
                // if canceled, will be awaited properly
                removedSubscriptions?.Clear();
                var timeLimit = DateTime.Now - _clusterState.Options.Events.SubscriptionCollectTimeout;
                foreach (var subscription in subscriptions)
                {
                    HConsole.WriteLine(this, "CollectSubscription collects");

                    try
                    {
                        var removed = await RemoveSubscriptionAsync(subscription, cancellationToken).CfAwait();

                        if (removed || subscription.ClusterSubscription.DeactivateTime < timeLimit)
                        {
                            subscription.ClusterSubscription.Remove(subscription);
                            (removedSubscriptions ??= new List <MemberSubscription>()).Add(subscription);
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        return; // cancelled - stop everything
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e, "An error occurred while collecting subscriptions.");
                    }
                }

                HConsole.WriteLine(this, $"CollectSubscription collected {removedSubscriptions?.Count ?? 0} subscriptions");

                // update subscriptions to collect
                // none remaining = exit the task
                lock (_collectMutex)
                {
                    if (removedSubscriptions != null)
                    {
                        foreach (var subscription in removedSubscriptions)
                        {
                            _collectSubscriptions.Remove(subscription);
                        }
                    }

                    if (_collectSubscriptions.Count == 0)
                    {
                        HConsole.WriteLine(this, "CollectSubscription exits");
                        _collectTask = null;
                        return;
                    }
                }

                HConsole.WriteLine(this, "CollectSubscription waits");

                // else, wait + loop / try again
                // if canceled, will be awaited properly
                await Task.Delay(_clusterState.Options.Events.SubscriptionCollectPeriod, cancellationToken).CfAwait();
            }
        }
示例#25
0
        /// <summary>
        /// Notifies of a 'members view' event.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="members">The members.</param>
        public async ValueTask <MembersUpdatedEventArgs> NotifyMembersView(int version, ICollection <MemberInfo> members)
        {
            // FIXME could we process two of these at a time?

            // get a new table
            var table = new MemberTable(version, members);

            // compute changes
            // count 1 for old members, 2 for new members, and then the result is
            // that 1=removed, 2=added, 3=unchanged
            // MemberInfo overrides GetHashCode and can be used as a key here
            var diff = new Dictionary <MemberInfo, int>();

            if (_memberTable == null)
            {
                foreach (var m in table.Members.Values)
                {
                    diff[m] = 2;
                }
            }
            else
            {
                foreach (var m in _memberTable.Members.Values)
                {
                    diff[m] = 1;
                }
                foreach (var m in table.Members.Values)
                {
                    if (diff.ContainsKey(m))
                    {
                        diff[m] += 2;
                    }
                    else
                    {
                        diff[m] = 2;
                    }
                }
            }

            // replace the table
            _memberTable = table;

            // notify the load balancer of the new list of members
            _loadBalancer.NotifyMembers(members.Select(x => x.Id));

            // signal once
            if (Interlocked.CompareExchange(ref _firstMembersViewed, 1, 0) == 0)
            {
                _firstMembersView.Release();
            }

            // process changes, gather events
            var added   = new List <MemberInfo>();
            var removed = new List <MemberInfo>();

            foreach (var(member, status) in diff)
            {
                switch (status)
                {
                case 1:     // old but not new = removed
                    HConsole.WriteLine(this, $"Removed member {member.Id}");
                    removed.Add(member);
                    if (_connections.TryGetValue(member.Id, out var client))
                    {
                        await client.TerminateAsync().CfAwait();     // TODO: consider dying in the background?
                    }
                    break;

                case 2:     // new but not old = added
                    HConsole.WriteLine(this, $"Added member {member.Id}");
                    added.Add(member);
                    break;

                case 3:     // old and new = no change
                    break;

                default:
                    throw new NotSupportedException();
                }
            }

            return(new MembersUpdatedEventArgs(added, removed, table.Members.Values));
        }
示例#26
0
        private Task _collectTask; // the task that collects ghost subscriptions

        static ClusterEvents()
        {
            HConsole.Configure(x => x.Configure <ClusterEvents>().SetPrefix("CLUST.EVTS"));
        }
示例#27
0
        public void Configure()
        {
            HConsole.Configure(x => x.Set <object>(xx => xx
                                                   .SetIndent(4)
                                                   .SetPrefix("XX")
                                                   .SetLevel(3)));

            var config = HConsole.Options.Get(new object());

            Assert.That(config.Prefix, Is.EqualTo("XX"));
            Assert.That(config.Indent, Is.EqualTo(4));
            Assert.That(config.Level, Is.EqualTo(3));

            HConsole.Configure(x => x.Set(xx => xx
                                          .SetIndent(33)
                                          .SetPrefix("YY")
                                          .SetLevel(44)));

            config = HConsole.Options.Get(new object());

            Assert.That(config.Prefix, Is.EqualTo("YY"));
            Assert.That(config.Indent, Is.EqualTo(33));
            Assert.That(config.Level, Is.EqualTo(44));

            HConsole.Configure(x => x.Set <object>(xx => xx
                                                   .ClearIndent()
                                                   .ClearPrefix()
                                                   .ClearLevel()));

            config = HConsole.Options.Get(new object());

            Assert.That(config.Prefix, Is.Null);
            Assert.That(config.Indent, Is.EqualTo(0));
            Assert.That(config.Level, Is.EqualTo(-1));

            HConsole.Configure(x => x.Clear <object>());

            var o = new object();

            HConsole.Configure(x => x.Set(o, xx => xx
                                          .SetIndent(4)
                                          .SetPrefix("XX")
                                          .SetLevel(3)));

            config = HConsole.Options.Get(o);

            Assert.That(config.Prefix, Is.EqualTo("XX"));
            Assert.That(config.Indent, Is.EqualTo(4));
            Assert.That(config.Level, Is.EqualTo(3));

            HConsole.Configure(x => x.Clear(o));

            config = HConsole.Options.Get(o);

            Assert.That(config.Prefix, Is.Null);
            Assert.That(config.Indent, Is.EqualTo(0));
            Assert.That(config.Level, Is.EqualTo(-1));

            HConsole.Reset();
            config = HConsole.Options.Get(new object());

            Assert.That(config.Prefix, Is.Null);
            Assert.That(config.Indent, Is.EqualTo(0));
            Assert.That(config.Level, Is.EqualTo(-1));

            HConsole.Configure(x => x.Set(xx => xx.Verbose()));
            config = HConsole.Options.Get(new object());
            Assert.That(config.Level, Is.EqualTo(int.MaxValue));

            HConsole.Configure(x => x.Set(xx => xx.Quiet()));
            config = HConsole.Options.Get(new object());
            Assert.That(config.Level, Is.EqualTo(-1));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="Authenticator"/> class.
 /// </summary>
 public Authenticator(AuthenticationOptions options)
 {
     _options = options ?? throw new ArgumentNullException(nameof(options));
     HConsole.Configure(this, config => config.SetIndent(4).SetPrefix("AUTH"));
 }
示例#29
0
        public async Task SampleClient()
        {
            using var _ = HConsoleForTest();

            HConsole.WriteLine(this, "Hazelcast Cloud Client");
            var stopwatch = Stopwatch.StartNew();

            HConsole.WriteLine(this, "Build options...");
            var options = new HazelcastOptionsBuilder()
                          .WithHConsoleLogger()
                          .With("Logging:LogLevel:Hazelcast", "Debug")
                          .WithUserSecrets(GetType().Assembly, SecretsKey)
                          .Build();

            // log level must be a valid Microsoft.Extensions.Logging.LogLevel value
            //   Trace | Debug | Information | Warning | Error | Critical | None

            // enable metrics
            options.Metrics.Enabled = true;

            // enable reconnection
            options.Networking.ReconnectMode = ReconnectMode.ReconnectAsync;

            // instead of using Visual Studio secrets, configuration via code is
            // possible, by uncommenting some of the blocks below - however, this
            // is not recommended as it increases the risk of leaking private
            // infos in a Git repository.

            // uncomment to run on localhost

            /*
             * options.Networking.Addresses.Clear();
             * options.Networking.Addresses.Add("localhost:5701");
             * options.ClusterName = "dev";
             */

            // uncomment to run on cloud

            /*
             * options.ClusterName = "...";
             * options.Networking.Cloud.DiscoveryToken = "...";
             * options.Networking.Cloud.Url = new Uri("https://...");
             */

            HConsole.WriteLine(this, "Get and connect client...");
            HConsole.WriteLine(this, $"Connect to cluster \"{options.ClusterName}\"{(options.Networking.Cloud.Enabled ? " (cloud)" : "")}");
            if (options.Networking.Cloud.Enabled)
            {
                HConsole.WriteLine(this, $"Cloud Discovery Url: {options.Networking.Cloud.Url}");
            }
            var client = await HazelcastClientFactory.StartNewClientAsync(options).ConfigureAwait(false);

            HConsole.WriteLine(this, "Get map...");
            var map = await client.GetMapAsync <string, string>("map").ConfigureAwait(false);

            HConsole.WriteLine(this, "Put value into map...");
            await map.PutAsync("key", "value").ConfigureAwait(false);

            HConsole.WriteLine(this, "Get value from map...");
            var value = await map.GetAsync("key").ConfigureAwait(false);

            HConsole.WriteLine(this, "Validate value...");
            if (!value.Equals("value"))
            {
                HConsole.WriteLine(this, "Error: check your configuration.");
                return;
            }

            HConsole.WriteLine(this, "Put/Get values in/from map with random values...");
            var random = new Random();
            var step   = IterationCount / 10;

            for (var i = 0; i < IterationCount; i++)
            {
                var randomValue = random.Next(100_000);
                await map.PutAsync("key_" + randomValue, "value_" + randomValue).ConfigureAwait(false);

                randomValue = random.Next(100_000);
                await map.GetAsync("key" + randomValue).ConfigureAwait(false);

                if (i % step == 0)
                {
                    HConsole.WriteLine(this, $"[{i:D3}] map size: {await map.GetSizeAsync().ConfigureAwait(false)}");
                }

                if (IterationPauseMilliseconds > 0)
                {
                    await Task.Delay(IterationPauseMilliseconds).ConfigureAwait(false);
                }
            }

            HConsole.WriteLine(this, "Destroy the map...");
            await map.DestroyAsync().ConfigureAwait(false);

            HConsole.WriteLine(this, "Dispose map...");
            await map.DisposeAsync().ConfigureAwait(false);

            HConsole.WriteLine(this, "Dispose client...");
            await client.DisposeAsync().ConfigureAwait(false);

            HConsole.WriteLine(this, $"Done (elapsed: {stopwatch.Elapsed.ToString("hhmmss\\.fff\\ ", CultureInfo.InvariantCulture)}).");
        }
示例#30
0
 public void HazelcastTestBaseOneTimeRootTearDown()
 {
     // write console output & clear
     HConsole.WriteAndClear();
 }