Changeset 2411


Ignore:
Timestamp:
Jan 21, 2011, 6:32:48 PM (11 years ago)
Author:
schwittmann
Message:

Shutdown external client server gracefully

Location:
trunk/CrypPlugins/KeySearcher
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPlugins/KeySearcher/ExternalClient/Cryptool.cpp

    r2259 r2411  
    313313        unsigned long timeDiffMicroSec = (now.tv_sec - lastSubbatchCompleted.tv_sec)*1000000 + (now.tv_usec - lastSubbatchCompleted.tv_usec);
    314314        lastSubbatchCompleted = now;
    315         printf("% .2f%% done. %'u keys/sec\n", ((i+1)*subbatch)/(float)size*100, (unsigned int)(subbatch/((float)timeDiffMicroSec/1000000)));
     315        float keysPerSecond = subbatch/((float)timeDiffMicroSec/1000000);
     316        printf("% .2f%% done. %'u keys/sec %u seconds remaining\n", ((i+1)*subbatch)/(float)size*100, (unsigned int)keysPerSecond,
     317                (unsigned int)((float)(size-(subbatch*(i+1)))/keysPerSecond));
    316318    }
    317319
  • trunk/CrypPlugins/KeySearcher/KeySearcher.cs

    r2359 r2411  
    849849                {
    850850                    //stop server here!
    851                     serverThread.Interrupt();
     851                    cryptoolServer.Shutdown();
    852852                    cryptoolServer.OnJobCompleted -= server_OnJobCompleted;
    853853                    cryptoolServer.OnClientConnected -= server_OnClientConnected;
     
    13781378        internal void WriteStatistics(String dataIdentifier)
    13791379        {
    1380             if (settings.CsvPath == "")
     1380            //using the chosen csv file
     1381            String path = settings.CsvPath;
     1382
     1383            if (path == "")
    13811384            {
    13821385                //using the default save folder %APPDATA%\Local\Cryptool2
    1383                 using (StreamWriter sw = new StreamWriter(string.Format("{0}\\UserRanking{1}.csv", DirectoryHelper.DirectoryLocal, dataIdentifier)))
     1386                path = string.Format("{0}\\UserRanking{1}.csv", DirectoryHelper.DirectoryLocal, dataIdentifier);
     1387            }
     1388
     1389            try
     1390            {
     1391                using (StreamWriter sw = new StreamWriter(path))
    13841392                {
    13851393                    sw.WriteLine("Avatarname" + ";" + "MaschineID" + ";" + "Hostname" + ";" + "Pattern Count" + ";" + "Last Update");
     
    13931401                }
    13941402            }
    1395             else
    1396             {
    1397                 //using the chosen csv file
    1398                 using (StreamWriter sw = new StreamWriter(settings.CsvPath))
    1399                 {
    1400                     sw.WriteLine("Avatarname" + ";" + "MaschineID" + ";" + "Hostname" + ";" + "Pattern Count" + ";" + "Last Update");
    1401                     foreach (string avatar in statistic.Keys)
    1402                     {
    1403                         foreach (long mID in statistic[avatar].Keys)
    1404                         {
    1405                             sw.WriteLine(avatar + ";" + mID.ToString() + ";" + statistic[avatar][mID].Hostname + ";" + statistic[avatar][mID].Count + ";" + statistic[avatar][mID].Date);
    1406                         }
    1407                     }
    1408                 }
    1409             }
     1403            catch (Exception)
     1404            {
     1405                GuiLogMessage(string.Format("Failed to write statistics to {0}", path), NotificationLevel.Debug);
     1406            }
     1407
    14101408
    14111409            /*
  • trunk/CrypPlugins/KeySearcher/Server/Server.cs

    r2214 r2411  
    2121
    2222    private Dictionary<EndPoint, TcpClient> connectedClients = new Dictionary<EndPoint, TcpClient>();
     23    private TcpListener tcpListener;
     24    private bool running = false;
    2325
    2426    ///<summary>
    25     /// Starts the server. Will block forever, you might want to start this in an additional thread.
     27    /// Starts the server. Will block as long as the server runs, you might want to start this in an additional thread.
    2628    ///</summary>
    2729    public void Run()
    2830    {
    29         TcpListener tcpListener = null;
     31        lock (this)
     32        {
     33            if (running)
     34            {
     35                throw new Exception("Invalid state: Already running");
     36            }
     37            running = true;
     38        }
     39
    3040        try
    3141        {
     
    3343            tcpListener.Start();
    3444            Console.WriteLine("Listening for client on port " + Port);
    35             while (true)
     45            while (running)
    3646            {
    3747                TcpClient client = tcpListener.AcceptTcpClient();
     
    4858        {
    4959        }
     60        catch (SocketException e)
     61        {
     62            if (running)
     63            {
     64                Console.WriteLine("CryptoolServer: Got SocketException while running");
     65            }
     66        }
     67
    5068
    5169        tcpListener.Stop();
     
    137155    }
    138156
     157    /// <summary>
     158    /// Closes this server. Any concurrent call to Run() in any other thread will return.
     159    /// </summary>
     160    public void Shutdown()
     161    {
     162        lock (this)
     163        {
     164            running = false;
     165            tcpListener.Stop();
     166        }
     167    }
    139168}
Note: See TracChangeset for help on using the changeset viewer.