Ejemplo n.º 1
0
    private umdsrequest(System.String[] args) : base("")
    {
        int Creates = 0;
        int Closes  = 0;

        /* Process the command line arguments */
        try
        {
            process_cmdline(args);
            if (sendAppName)
            {
                setProperty("appl-name", appl_name);
            }
        }
        catch (System.Exception e)
        {
            if (!help)
            {
                //UPGRADE_TODO: Method 'java.io.PrintStream.println' was converted to 'System.Console.Error.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintStreamprintln_javalangObject'"
                //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                System.Console.Error.WriteLine(e);
            }
            System.Console.Error.WriteLine(UMDS.version());
            System.Console.Error.WriteLine(purpose);
            System.Console.Error.WriteLine(usage);
            System.Environment.Exit(1);
        }

        /* If a user name was provided, get the password */
        if (username != null)
        {
            get_password();
        }

        UMDSServerConnection svrconn = this;

        /*
         * If an application config file is provided, read it and set the server
         * connection properties.
         */
        if (conffname != null)
        {
            if (read_config(svrconn, conffname) == false)
            {
                System.Environment.Exit(1);
            }
        }

        /* Set the list of servers, user name and password in the connection */
        try
        {
            svrconn.setProperty("server-list", server_list);
            if (username != null)
            {
                svrconn.setProperty("user", username);
                svrconn.setProperty("password", password);
            }

            // Set the linger value to set a timeout on draining data when
            // closing
            if (linger > 0)
            {
                svrconn.setProperty("client-linger", "" + (linger * 1000));
            }
        }
        catch (UMDSException ex)
        {
            //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
            System.Console.Error.WriteLine("Error setting UMDS configuration: " + ex.ToString());
            System.Environment.Exit(1);
        }

        /*
         * Now start the connection to a server. If server-reconnect is enabled
         * it will return asynchronously. If server-reconnect is disabled,
         * start() will return when the connection is authenticated
         */
        System.String ServerReconnect;
        try
        {
            ServerReconnect = svrconn.getProperty("server-reconnect");
        }
        catch (UMDSException e)
        {
            ServerReconnect = "1";
        }

        System.Console.Error.WriteLine("Property: server-reconnect" + ServerReconnect);
        try
        {
            svrconn.start();
        }
        catch (UMDSAuthenticationException e)
        {
            System.Console.Error.WriteLine("Authentication failed - check user name/password requirements of the server");
            System.Environment.Exit(1);
        }
        catch (UMDSException e)
        {
            //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
            System.Console.Error.WriteLine("Failed to create the server connection:" + e);
            System.Environment.Exit(1);
        }

        while (!svrconn.Authenticated && auth_failed == false)
        {
            System.Console.Out.WriteLine("Not Authorized");
            try
            {
                //UPGRADE_TODO: Method 'java.lang.Thread.sleep' was converted to 'System.Threading.Thread.Sleep' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javalangThreadsleep_long'"
                System.Threading.Thread.Sleep(100);
            }
            catch (System.Threading.ThreadInterruptedException e)
            {
            }
        }

        if (auth_failed)
        {
            System.Environment.Exit(1);
        }

        /* Now the process of sending data begins... */
        if (0 == msglen)
        {
            System.Console.Out.Write("Sending NO messages: Only " + msgs + " requests");
        }
        else
        {
            System.Console.Out.Write("Sending " + msgs + " messages of size " + msglen + " bytes to topic [" + topic + "]");
        }

        if (pause == 0)
        {
            System.Console.Out.WriteLine(" as fast as the system can");
        }
        else
        {
            System.Console.Out.WriteLine(" pausing " + pause + "ms");
        }

        /*
         * Create a payload with the string "UMDS" and set every K to contain
         * and offset for payload verification
         */
        byte[] message  = null;
        byte[] dummymsg = System.Text.UTF8Encoding.UTF8.GetBytes("UMDS");
        if (0 != msglen)
        {
            message = new byte[msglen];
            Array.Copy(dummymsg, 0, message, 0, dummymsg.Length < message.Length?dummymsg.Length:message.Length);


            for (int k = 1; (k * 1024) < message.Length && k < 256; k++)
            {
                message[k * 1024] = (byte)k;
            }
        }

        byte[] dummy_req    = System.Text.UTF8Encoding.UTF8.GetBytes("UMDS REQ");
        byte[] request_data = new byte[(reqlen < dummy_req.Length) ? dummy_req.Length : reqlen];
        Array.Copy(dummy_req, 0, request_data, 0, (dummy_req.Length < request_data.Length)?dummy_req.Length:request_data.Length);

        /*
         * A simple loop to send messages is used. This warning indicates a long
         * pause interval was provided relative to the stats interval.
         */
        if ((pause > stats_ms) && (stats_ms > 0))
        {
            System.Console.Out.WriteLine("Warning - Pause rate " + pause + " exceeds stats interval : " + stats_ms + " (will see intervals of 0 sends)");
        }

        /*
         * Create an array of topic names (and sources) to send on. If -N was
         * provided, each topic will be used in round robin fashion.
         */
        System.String[] topicnames = new System.String[num_topics];
        srcs        = new appRequest[num_topics];
        Creates     = num_topics;
        createCount = 0;
        try
        {
            if (num_topics > 1)
            {
                for (int n = 0; n < num_topics; n++)
                {
                    topicnames[n] = topic + "." + n;
                    if (connsend == false)
                    {
                        srcs[n] = new appRequest(svrconn, topicnames[n], null);
                        srcs[n].setVerbose(verbose);
                    }
                    else
                    {
                        srcs[n] = null;
                    }
                }
            }
            else
            {
                topicnames[0] = topic;
                if (connsend == false)
                {
                    srcs[0] = new appRequest(svrconn, topicnames[0], null);
                    srcs[0].setVerbose(verbose);
                }
                else
                {
                    srcs[0] = null;
                }
            }
        }
        catch (UMDSException e)
        {
            //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
            System.Console.Error.WriteLine("Failed to create the source:" + e);
            System.Environment.Exit(1);
        }

        if (false)
        {
            while (createCount < Creates)
            {
                System.Threading.Thread.Sleep(10);
                System.Console.Error.WriteLine("Sources Created: " + createCount);
            }
            System.Console.Error.WriteLine("Sources Created: " + createCount);
        }

        /* Data used in sending */
        int  req_counter = 0;
        bool req_sent;
        long seq_counter = 0;
        bool seq_sent;
        long msg_counter = 0;
        long seq_end = msg_counter + msgs;
        long last_msgs_sent = 0;
        long last_bytes_sent = 0;
        long bytes_sent = 0;
        long start_time = UMDS.TimeAsMillis();
        long end_time = 0, last_time = start_time;
        long elapsed_time = 0;

        /*
         * A simple loop is run to send messages and dump stats. Calculate the
         * time to wait for each iteration and use ms times decrementing each
         * iteration to trigger sending of messages and dumping of stats.
         */
        long pause_togo = pause;
        long stats_togo = stats_ms;
        int  iter_time;

        if (pause > 0)
        {
            if (pause == stats_ms)
            {
                iter_time = pause;
            }
            else
            {
                if (pause < stats_ms)
                {
                    iter_time = pause % (stats_ms - pause);
                }
                else
                {
                    if (pause > stats_ms)
                    {
                        iter_time = stats_ms;
                    }
                    else
                    {
                        iter_time = stats_ms / (stats_ms / (pause - stats_ms));
                    }
                }
            }
        }
        else
        {
            iter_time = 0;
        }

        System.Console.Out.WriteLine("stats_ms " + stats_ms + " pause " + pause + " iteration " + iter_time);

        /* Start sending messages! */
        int  topic_idx = 0;
        long procd     = UMDS.TimeAsMillis();

        while (msg_counter < seq_end && auth_failed == false)
        {
            req_sent = false;
            seq_sent = false;
            if (stats_togo <= 0 || iter_time == 0)
            {
                end_time = UMDS.TimeAsMillis();

                elapsed_time = end_time - last_time;
                if (elapsed_time >= stats_ms)
                {
                    /* New stats interval */
                    double secs = (elapsed_time) / 1000.0;
                    print_bw(secs, (int)last_msgs_sent, last_bytes_sent, seq_counter);
                    last_time       = end_time;
                    last_msgs_sent  = 0;
                    last_bytes_sent = 0;
                    elapsed_time    = 0;
                }
                stats_togo = stats_ms;
            }

            if ((pause_togo <= 0 || iter_time == 0) &&
                svrconn.Authenticated)
            {
                pause_togo = pause;
                /* Send a message! */
                if (0 != msglen)
                {
                    try
                    {
                        if (srcs[topic_idx].canSend())
                        {
                            if (verbose)
                            {
                                System.Console.Out.WriteLine("Send: topic " + topic_idx + " msg: " + seq_counter);
                            }
                            srcs[topic_idx].send(message);

                            seq_counter++;
                            last_msgs_sent++;
                            bytes_sent      += msglen;
                            last_bytes_sent += msglen;
                            seq_sent         = true;
                        }
                    }
                    catch (UMDSAuthenticationException ex)
                    {
                        /* This can occur while auto-reconnecting */
                        try
                        {
                            // Give the connection time to reestablish
                            //UPGRADE_TODO: Method 'java.lang.Thread.sleep' was converted to 'System.Threading.Thread.Sleep' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javalangThreadsleep_long'"
                            System.Threading.Thread.Sleep(100);
                        }
                        catch (System.Threading.ThreadInterruptedException e)
                        {
                        }
                    }
                    catch (UMDSBadStateException ex)
                    {
                        System.Console.Error.WriteLine("Error sending message: Bad State:" + ex.ToString() + " seq " + seq_counter);
                        try
                        {
                            // Give the source time to establish
                            //UPGRADE_TODO: Method 'java.lang.Thread.sleep' was converted to 'System.Threading.Thread.Sleep' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javalangThreadsleep_long'"
                            System.Threading.Thread.Sleep(100);
                        }
                        catch (System.Threading.ThreadInterruptedException e)
                        {
                        }
                    }
                    catch (UMDSDisconnectException ex)
                    {
                        try
                        {
                            // Give the connection time to reestablish
                            //UPGRADE_TODO: Method 'java.lang.Thread.sleep' was converted to 'System.Threading.Thread.Sleep' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javalangThreadsleep_long'"
                            System.Threading.Thread.Sleep(100);
                        }
                        catch (System.Threading.ThreadInterruptedException e)
                        {
                        }
                    }
                    catch (UMDSException ex)
                    {
                        //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                        System.Console.Error.WriteLine("Error sending message: " + ex.ToString() + " seq " + seq_counter);
                        break;
                    }
                }

                // Now send some requests
                try
                {
                    if (srcs[topic_idx].canSend())
                    {
                        if (verbose)
                        {
                            System.Console.Out.WriteLine("Req:  topic " + topic_idx + " reqid " + req_counter + " msgc " + msg_counter);
                        }

                        System.String temp = "UMDS REQ " + req_counter + " :";
                        dummy_req    = System.Text.UTF8Encoding.UTF8.GetBytes(temp);
                        request_data = new byte[(reqlen < dummy_req.Length) ? dummy_req.Length : reqlen];
                        Array.Copy(dummy_req, 0,
                                   request_data, 0,
                                   (dummy_req.Length < request_data.Length) ? dummy_req.Length : request_data.Length);

                        srcs[topic_idx].request(req_counter, request_data);
                        req_counter++;
                        req_sent = true;
                    }
                    else
                    {
                        try {
                            // Give the connection time to settle down
                            // Comes from running the server under valgrind.
                            System.Threading.Thread.Sleep(100);
                        }
                        catch (System.Threading.ThreadInterruptedException e)
                        {
                        }
                    }
                }
                catch (UMDSAuthenticationException ex)
                {
                    /* This can occur while auto-reconnecting */
                    try
                    {
                        // Give the connection time to reestablish
                        //UPGRADE_TODO: Method 'java.lang.Thread.sleep' was converted to 'System.Threading.Thread.Sleep' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javalangThreadsleep_long'"
                        System.Threading.Thread.Sleep(100);
                    }
                    catch (System.Threading.ThreadInterruptedException e)
                    {
                    }
                }
                catch (UMDSBadStateException ex)
                {
                    System.Console.Error.WriteLine("Error sending message: Bad State:" + ex.ToString() + " REQ " + req_counter);
                    try
                    {
                        // Give the source time to establish
                        //UPGRADE_TODO: Method 'java.lang.Thread.sleep' was converted to 'System.Threading.Thread.Sleep' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javalangThreadsleep_long'"
                        System.Threading.Thread.Sleep(100);
                    }
                    catch (System.Threading.ThreadInterruptedException e)
                    {
                    }
                }
                catch (UMDSDisconnectException ex)
                {
                    try
                    {
                        // Give the connection time to reestablish
                        //UPGRADE_TODO: Method 'java.lang.Thread.sleep' was converted to 'System.Threading.Thread.Sleep' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javalangThreadsleep_long'"
                        System.Threading.Thread.Sleep(100);
                    }
                    catch (System.Threading.ThreadInterruptedException e)
                    {
                    }
                }
                catch (UMDSException ex)
                {
                    //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                    System.Console.Error.WriteLine("Error sending request: " + ex.ToString() + " req " + req_counter);
                    break;
                }

                if (num_topics > 1)
                {
                    topic_idx++;
                    if (topic_idx == topicnames.Length)
                    {
                        topic_idx = 0;
                    }
                }

                // Can only send messages from within this if
                if (req_sent || seq_sent)
                {
                    msg_counter++;
                }
            }

            if (!svrconn.Authenticated)
            {
                // Defensive sleep while waiting for reconnection
                if (ServerReconnect.Equals("1"))
                {
                    try
                    {
                        //UPGRADE_TODO: Method 'java.lang.Thread.sleep' was converted to 'System.Threading.Thread.Sleep' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javalangThreadsleep_long'"
                        System.Threading.Thread.Sleep(500);
                    }
                    catch (System.Threading.ThreadInterruptedException e)
                    {
                    }
                }
                else
                {
                    auth_failed = true;
                }
            }
            /* If the user is trying to send slowly, take a break */
            if (iter_time > 0)
            {
                try
                {
                    //UPGRADE_TODO: Method 'java.lang.Thread.sleep' was converted to 'System.Threading.Thread.Sleep' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javalangThreadsleep_long'"
                    System.Threading.Thread.Sleep(iter_time);
                }
                catch (System.Threading.ThreadInterruptedException e)
                {
                }
            }

            long newprocd = UMDS.TimeAsMillis();
            long procdiff = (newprocd - procd);
            if (pause > 0)
            {
                pause_togo -= procdiff;
            }
            if (stats_ms > 0)
            {
                stats_togo -= procdiff;
            }
            procd = newprocd;
        }               // send loop

        // This block of code adds a variable length delay to closing the src(s) to allow
        // the responder time to respond; by waiting until all the requests have timedout.
        bool fOKtoClose = false;
        int  CloseCount = 0;

        if (true == fOKtoClose)
        {
            fOKtoClose = true;
        }
        while (!fOKtoClose)
        {
            long ReqCanceled = 0;
            for (int n = 0; n < srcs.Length; n++)
            {
                ReqCanceled += srcs[n].getRequestsCanceled();
            }

            if ((msgs == ReqCanceled))                  // Wait until all requests timeout.
            {
                fOKtoClose = true;
                if (verbose)
                {
                    getUMDS().log(0, "OK to Close");                            // log also places the message inthe debug log: BONUS!
                }
            }
            else
            {
                fOKtoClose = false;
                if (verbose)
                {
                    getUMDS().log(0, "NOT OK to Close srcs: m: " + msgs + " / C: " + ReqCanceled);
                }
            }

            // Wait for the last request to time out
            if (!fOKtoClose)
            {
                try {
                    System.Threading.Thread.Sleep(100);
                    CloseCount++;
                } catch (System.Threading.ThreadInterruptedException e) {
                }

                if (100 < CloseCount)                                   // This gives us a maximum wait of 10 seconds; the default request timeout.
                {
                    fOKtoClose = true;
                    if (verbose)
                    {
                        getUMDS().log(0, "Too much waiting");
                    }
                }
            }
        }

        if (auth_failed == false)                               // Have to sill be connected to delet resources
        {
            Closes = 0;
            for (int n = 0; n < srcs.Length; n++)
            {
                if (null != srcs[n])
                {
                    try
                    {
                        srcs[n].close();
                        srcs[n] = null;
                        Closes++;
                    }
                    catch (UMDSException ex)
                    {
                        //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                        System.Console.Error.WriteLine("Error closing source[" + n + "]: " + ex.ToString());
                    }
                }
            }

            while (closeCount < Closes)
            {
                if (verbose)
                {
                    System.Console.Error.WriteLine("Sources: " + Closes + "  Closed: " + closeCount);
                }
                System.Threading.Thread.Sleep(100);
            }
            if (verbose)
            {
                System.Console.Error.WriteLine("All sources closed: Sources: " + Closes + "  Closed: " + closeCount);
            }

            /* Close the connection, we are done */
            try
            {
                System.Console.Error.WriteLine("Closing Connection ");
                end_time = UMDS.TimeAsMillis();                 // Have to get end_time here; close invalidates UMDS
                svrconn.close();
            }
            catch (UMDSException ex)
            {
                //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                System.Console.Error.WriteLine("Error closing server connection: " + ex.ToString());
            }
            System.Console.Error.WriteLine("Connection Closed");
        }

        /* Finished sending, Dump the summary statistics */
        double secs2 = (end_time - start_time) / 1000.0;

        System.Console.Out.WriteLine("Sent " + seq_counter + " messages of size " + msglen + " bytes in " + secs2 + " seconds.");
        System.Console.Out.WriteLine("Sent " + req_counter + " requests of size " + reqlen + " bytes in " + secs2 + " seconds.");
        print_bw(secs2, (int)seq_counter, bytes_sent, seq_counter);

        System.Console.Out.WriteLine("Done");
    }
    private umdspersistentreceive(System.String[] args) : base("")
    {
        int Creates = 0;
        int Closes  = 0;

        /* Process the command line arguments */
        try
        {
            process_cmdline(args);
            if (sendAppName)
            {
                setProperty("appl-name", appl_name);
            }
        }
        catch (System.Exception e)
        {
            if (!help)
            {
                System.Console.Error.WriteLine("Error:" + e.Message);
            }
            System.Console.Error.WriteLine(UMDS.version());
            System.Console.Error.WriteLine(purpose);
            System.Console.Error.WriteLine(usage);
            System.Environment.Exit(1);
        }

        /* If a user name was provided, get the password */
        if (username != null)
        {
            get_password();
        }

        /*
         * Create the server connection object for this application. The
         * application name is registered on the server when connected. The
         * server is not connected until start() is called.
         */
        UMDSServerConnection svrconn = this;

        /*
         * If an application config file is provided, read it and set the server
         * connection properties.
         */
        if (conffname != null)
        {
            if (read_config(svrconn, conffname) == false)
            {
                System.Environment.Exit(1);
            }
        }

        /* Set the list of servers, user name and password in the connection */
        try
        {
            svrconn.setProperty("server-list", server_list);
            if (username != null)
            {
                svrconn.setProperty("user", username);
                svrconn.setProperty("password", password);
            }
            if (useTls)
            {
                svrconn.setProperty("use-tls", "1");
            }
            if (trustStoreFile != null)
            {
                svrconn.setProperty("truststore", trustStoreFile);
            }
            if (trustStorePassword != null)
            {
                svrconn.setProperty("truststore-password", trustStorePassword);
            }
        }
        catch (UMDSException ex)
        {
            System.Console.Error.WriteLine("Error setting UMDS configuration: " + ex.ToString());
            System.Environment.Exit(1);
        }

        /*
         * Now start the connection to a server.
         * If server-reconnect is enabled it will return asynchronously.
         * If server-reconnect is disabled, start() will return
         * when the connection is authenticated
         */
        try
        {
            svrconn.start();
        }
        catch (UMDSAuthenticationException)
        {
            System.Console.Error.WriteLine("Authentication failed - check user name/password requirements of the server");
            System.Environment.Exit(1);
        }
        catch (UMDSException e)
        {
            System.Console.Error.WriteLine("Failed to create the server connection:" + e);
            System.Environment.Exit(1);
        }

        while (!svrconn.Authenticated && auth_failed == false)
        {
            System.Threading.Thread.Sleep(100);
        }

        if (auth_failed)
        {
            try
            {
                svrconn.close();
            }
            catch (UMDSException ex)
            {
                System.Console.Error.WriteLine("Error closing connection: " + ex.ToString());
            }
            return;
        }

        /* Create an array of receivers based on -N if it was used. */
        AppReceiver[] rcv = new AppReceiver[numtopics];

        Creates     = numtopics;
        createCount = 0;
        try
        {
            for (int n = 0; n < numtopics; n++)
            {
                System.String fulltopic;

                /* Create a topic string. Append .N to the name if -N was used */
                if (numtopics == 1)
                {
                    fulltopic = topic;
                }
                else
                {
                    fulltopic = topic + "." + n;
                }

                /*
                 * Create the receiver object for this topic AppReceiver derives
                 * from UMDSReceiver which on creation registers with the server.
                 */
                if (startSeqOffset > 0)
                {
                    rcv[n] = new AppReceiver(svrconn, fulltopic, n, wildcard,
                                             new UMDSRecoverySequenceNumberCallbackimpl(startSeqOffset), (Object)startSeqOffset);
                }
                else
                {
                    rcv[n] = new AppReceiver(svrconn, fulltopic, n, wildcard, null, null);
                }

                /* Copy the verbose flag from the command line */
                rcv[n].verbose = verbose;
            }
        }
        catch (UMDSException e)
        {
            System.Console.Out.WriteLine("Failed to create receiver object:" + e);
            System.Environment.Exit(5);
        }

        /* Loop over all the receivers and start timing */
        for (int n = 0; n < numtopics; n++)
        {
            rcv[n].newInterval();
        }

        /*
         * Now start sending data until the number of requested messages have
         * been received or the server is no longer authenticated. (Most likely
         * disconnected)
         */
        System.Console.Out.WriteLine("Printing Statistics every:" + stats_ms + " ms ");
        while (auth_failed == false)
        {
            /* Dump stats for each receiver object and reset the timing */
            for (int n = 0; n < numtopics; n++)
            {
                if (stats_ms > 0)
                {
                    rcv[n].print_bw();
                }
                rcv[n].newInterval();
            }

            /* Check the server connection hasn't encountered any
             * errors and exit if it has. This might come from
             * a receiver too.
             */
            if (svrconn.Error != UMDS.ERRCODE.NO_ERROR)
            {
                System.Console.Error.WriteLine("Server Connection Error detected:" + svrconn.ErrorStr);
                break;
            }

            /* Wait for the statistics interval */
            try
            {
                // Sleep the interval or 1 second.
                System.Threading.Thread.Sleep(((stats_ms > 0) ? stats_ms : 1000));
            }
            catch (System.Threading.ThreadInterruptedException e)
            {
            }

            /* Count all the received messages on all the receivers */
            int total_msg_count = 0;
            for (int n = 0; n < numtopics; n++)
            {
                total_msg_count = (int)(total_msg_count + rcv[n].total_msg_count);
            }

            /* If we received enough messages, end */
            if (end_msgs > 0 && total_msg_count >= end_msgs)
            {
                break;
            }
        }

        /* Summarise the results and output */
        print_summary(rcv);

        /* Close all the receivers and the connection, we are done */
        Closes = 0;
        if (auth_failed == false)
        {
            try
            {
                for (int n = 0; n < numtopics; n++)
                {
                    if (rcv[n] != null)
                    {
                        rcv[n].close();
                        Closes++;
                    }
                }

                while (closeCount < Closes)
                {
                    if (verbose)
                    {
                        System.Console.Error.WriteLine("Receivers: " + Closes + "  Closed: " + closeCount);
                    }
                    System.Threading.Thread.Sleep(100);
                }
                if (verbose)
                {
                    System.Console.Error.WriteLine("All receivers closed: Receivers: " + Closes + "  Closed: " + closeCount);
                }

                System.Console.Error.WriteLine("Closing Connection ");
                svrconn.close();
            }
            catch (UMDSException ex)
            {
                System.Console.Error.WriteLine("Error closing receiver: " + ex.ToString());
            }
        }
        else
        {
            System.Console.Error.WriteLine("Exiting - no longer authenticated.");
        }

        System.Console.Error.WriteLine("Connection Closed");

        System.Console.Error.WriteLine("Done");
    }
Ejemplo n.º 3
0
    private umdssend(System.String[] args) : base("")
    {
        int Creates = 0;
        int Closes  = 0;

        /* Process the command line arguments */
        try
        {
            process_cmdline(args);
            if (sendAppName)
            {
                setProperty("appl-name", appl_name);
            }
        }
        catch (System.Exception e)
        {
            if (!help)
            {
                System.Console.Error.WriteLine("Error:" + e.Message);
            }
            System.Console.Error.WriteLine(UMDS.version());
            System.Console.Error.WriteLine(purpose);
            System.Console.Error.WriteLine(usage);
            System.Environment.Exit(1);
        }

        /* If a user name was provided, get the password */
        if (username != null)
        {
            get_password();
        }

        UMDSServerConnection svrconn = this;

        /*
         * If an application config file is provided, read it and set the server
         * connection properties.
         */
        if (conffname != null)
        {
            if (read_config(svrconn, conffname) == false)
            {
                System.Environment.Exit(1);
            }
        }

        /* Set the list of servers, user name and password in the connection */
        try
        {
            svrconn.setProperty("server-list", server_list);
            if (username != null)
            {
                svrconn.setProperty("user", username);
                svrconn.setProperty("password", password);
            }

            // Set the linger value to set a timeout on draining data when
            // closing
            if (linger > 0)
            {
                svrconn.setProperty("client-linger", "" + (linger * 1000));
            }
        }
        catch (UMDSException ex)
        {
            System.Console.Error.WriteLine("Error setting UMDS configuration: " + ex.ToString());
            System.Environment.Exit(1);
        }

        /*
         * Now start the connection to a server. If server-reconnect is enabled
         * it will return asynchronously. If server-reconnect is disabled,
         * start() will return when the connection is authenticated
         */
        bool ServerReconnect;

        try {
            ServerReconnect = (svrconn.getProperty("server-reconnect").CompareTo("1") == 0) ? true : false;
        } catch (UMDSException e) {
            ServerReconnect = true;
        }
        try
        {
            svrconn.start();
        }
        catch (UMDSAuthenticationException)
        {
            System.Console.Error.WriteLine("Authentication failed - check user name/password requirements of the server");
            System.Environment.Exit(1);
        }
        catch (UMDSException e)
        {
            System.Console.Error.WriteLine("Failed to create the server connection:" + e);
            System.Environment.Exit(1);
        }

        while (!svrconn.Authenticated && auth_failed == false)
        {
            System.Threading.Thread.Sleep(100);
        }

        if (auth_failed)
        {
            System.Environment.Exit(1);
        }

        /* Now the process of sending data begins... */
        System.Console.Out.Write("Sending " + msgs + " messages of size " + msglen + " bytes to topic [" + topic + "]");

        if (pause == 0)
        {
            System.Console.Out.WriteLine(" as fast as the system can");
        }
        else
        {
            System.Console.Out.WriteLine(" pausing " + pause + "ms");
        }

        /*
         * Create a payload with the string "UMDS" and set every K to contain
         * and offset for payload verification
         */
        byte[] message  = new byte[msglen];
        byte[] dummymsg = System.Text.UTF8Encoding.UTF8.GetBytes("UMDS");
        Array.Copy(dummymsg, 0, message, 0, dummymsg.Length < message.Length?dummymsg.Length:message.Length);

        for (int k = 1; (k * 1024) < message.Length && k < 256; k++)
        {
            message[k * 1024] = (byte)k;
        }

        /*
         * A simple loop to send messages is used. This warning indicates a long
         * pause interval was provided relative to the stats interval.
         */
        if ((pause > stats_ms) && (stats_ms > 0))
        {
            System.Console.Out.WriteLine("Warning - Pause rate " + pause + " exceeds stats interval : " + stats_ms + " (will see intervals of 0 sends)");
        }

        /*
         * Create an array of topic names (and sources) to send on. If -N was
         * provided, each topic will be used in round robin fashion.
         */
        System.String[] topicnames = new System.String[num_topics];
        UMDSSource[]    srcs       = new UMDSSource[num_topics];
        Creates     = num_topics;
        createCount = 0;
        try
        {
            if (num_topics > 1)
            {
                for (int n = 0; n < num_topics; n++)
                {
                    topicnames[n] = topic + "." + n;
                    if (connsend == false)
                    {
                        srcs[n] = new UMDSSource(svrconn, topicnames[n], null);
                    }
                    else
                    {
                        srcs[n] = null;
                    }
                }
            }
            else
            {
                topicnames[0] = topic;
                if (connsend == false)
                {
                    srcs[0] = new UMDSSource(svrconn, topicnames[0], null);
                }
                else
                {
                    srcs[0] = null;
                }
            }
        }
        catch (UMDSException e)
        {
            System.Console.Error.WriteLine("Failed to create the source:" + e);
            System.Environment.Exit(1);
        }

        if (false)
        {
            while (createCount < Creates)
            {
                System.Threading.Thread.Sleep(10);
                System.Console.Error.WriteLine("Sources Created: " + createCount);
            }
            System.Console.Error.WriteLine("Sources Created: " + createCount);
        }


        /* Data used in sending */
        long seq_counter = 0;
        long seq_end = seq_counter + msgs;
        long last_msgs_sent = 0;
        long last_bytes_sent = 0;
        long bytes_sent = 0;
        long start_time = UMDS.TimeAsMillis();
        long end_time = 0, last_time = start_time;
        long elapsed_time = 0;

        /*
         * A simple loop is run to send messages and dump stats. Calculate the
         * time to wait for each iteration and use ms times decrementing each
         * iteration to trigger sending of messages and dumping of stats.
         */
        long pause_togo = pause;
        long stats_togo = stats_ms;
        int  iter_time;

        if (pause > 0)
        {
            if (pause == stats_ms)
            {
                iter_time = pause;
            }
            else
            {
                if (pause < stats_ms)
                {
                    iter_time = pause % (stats_ms - pause);
                }
                else
                {
                    if (pause > stats_ms)
                    {
                        iter_time = stats_ms;
                    }
                    else
                    {
                        iter_time = stats_ms / (stats_ms / (pause - stats_ms));
                    }
                }
            }
        }
        else
        {
            iter_time = 0;
        }

        System.Console.Out.WriteLine("stats_ms " + stats_ms + " pause " + pause + " iteration " + iter_time);

        /* Start sending messages! */
        int  topic_idx = 0;
        long procd     = UMDS.TimeAsMillis();

        while (seq_counter < seq_end && auth_failed == false)
        {
            if (stats_togo <= 0 || iter_time == 0)
            {
                end_time = UMDS.TimeAsMillis();

                elapsed_time = end_time - last_time;
                if (elapsed_time >= stats_ms)
                {
                    /* New stats interval */
                    double secs = (elapsed_time) / 1000.0;
                    print_bw(secs, (int)last_msgs_sent, last_bytes_sent, seq_counter);
                    last_time       = end_time;
                    last_msgs_sent  = 0;
                    last_bytes_sent = 0;
                    elapsed_time    = 0;
                }
                stats_togo = stats_ms;
            }

            bool sent = false;
            if ((pause_togo <= 0 || iter_time == 0) && svrconn.Authenticated)
            {
                pause_togo = pause;
                /* Send a message! */
                try
                {
                    sent = false;
                    if (connsend)
                    {
                        svrconn.send(topicnames[topic_idx], message);
                        sent = true;
                    }
                    else
                    {
                        if (srcs[topic_idx].canSend())
                        {
                            srcs[topic_idx].send(message);
                            sent = true;
                        }
                    }
                    if (sent)
                    {
                        seq_counter++;
                        last_msgs_sent++;
                        bytes_sent      += msglen;
                        last_bytes_sent += msglen;
                        if (num_topics > 1)
                        {
                            topic_idx++;
                            if (topic_idx == topicnames.Length)
                            {
                                topic_idx = 0;
                            }
                        }
                    }
                }
                catch (UMDSAuthenticationException)
                {
                    /* This can occur while auto-reconnecting */
                    // Give the connection time to reestablish.
                    System.Threading.Thread.Sleep(10);
                }
                catch (UMDSBadStateException ex)
                {
                    System.Console.Error.WriteLine("Error sending message: Bad State:" + ex.ToString() + " seq " + seq_counter);
                    // Give the source time to establish.
                    System.Threading.Thread.Sleep(10);
                }
                catch (UMDSDisconnectException)
                {
                    // Give the connection time to reestablish.
                    System.Threading.Thread.Sleep(10);
                }
                catch (UMDSException ex)
                {
                    System.Console.Error.WriteLine("Error sending message: " + ex.ToString() + " seq " + seq_counter);
                    break;
                }
            }
            if (!svrconn.Authenticated)
            {
                // Defensive sleep while waiting for reconnection
                if (ServerReconnect)
                {
                    System.Threading.Thread.Sleep(500);
                }
                else
                {
                    auth_failed = true;
                }
            }
            /* If the user is trying to send slowly, take a break */
            if (iter_time > 0)
            {
                System.Threading.Thread.Sleep(iter_time);
            }

            long newprocd = UMDS.TimeAsMillis();
            long procdiff = (newprocd - procd);
            if (procdiff < 0)
            {
                procdiff = -procdiff;                                                   // When this is negative (we crossed a second boundary); make it positive.
            }
            if (pause > 0)
            {
                pause_togo -= procdiff;
            }
            if (stats_ms > 0)
            {
                stats_togo -= procdiff;
            }
            procd = newprocd;
        }

        if (true != auth_failed)
        {
            Closes = 0;
            for (int n = 0; n < srcs.Length; n++)
            {
                if (null != srcs[n])
                {
                    try
                    {
                        srcs[n].close();
                        srcs[n] = null;
                        Closes++;
                    }
                    catch (UMDSException ex)
                    {
                        System.Console.Error.WriteLine("Error closing source[" + n + "]: " + ex.ToString());
                    }
                }
            }

            while (closeCount < Closes)
            {
                if (verbose)
                {
                    System.Console.Error.WriteLine("Sources: " + Closes + "  Closed: " + closeCount);
                }
                System.Threading.Thread.Sleep(100);
            }
            if (verbose)
            {
                System.Console.Error.WriteLine("All sources closed: Sources: " + Closes + "  Closed: " + closeCount);
            }

            /* Close the connection, we are done */
            try
            {
                System.Console.Error.WriteLine("Closing Connection ");
                end_time = UMDS.TimeAsMillis();                 // Have to get end_time here; close invalidates UMDS
                svrconn.close();
            }
            catch (UMDSException ex)
            {
                //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                System.Console.Error.WriteLine("Error closing server connection: " + ex.ToString());
            }
            System.Console.Error.WriteLine("Connection Closed");
        }

        /* Finished sending, Dump the summary statistics */
        double secs2 = (end_time - start_time) / 1000.0;

        System.Console.Out.WriteLine("Sent " + seq_counter + " messages of size " + msglen + " bytes in " + secs2 + " seconds.");
        print_bw(secs2, (int)seq_counter, bytes_sent, seq_counter);

        System.Console.Out.WriteLine("Done");
    }