Exemple #1
0
        private static void sendEventsFromFile(
            string eventsFilename,
            PerformanceCounter counter,
            StreamWriter sw,
            int eventsInSend,
            int delayTime
            )
        {
            #region Local variables

            Stopwatch stopWatch            = new Stopwatch();
            int       eventsInXML          = 0;
            WBXXml    eventsXMLToSend      = null;
            bool      updateBAMInformation = true;
            string    currBAMType          = null;
            int       eventsRead           = 0;

            #endregion

            // Write the header line in the data collector file
            sw.WriteLine(DATA_COLLECTOR_FILE_HEADER_LINE);

            #region Open the events file

            FileStream fs =
                new FileStream(
                    eventsFilename,
                    FileMode.Open,
                    FileAccess.Read
                    );

            StreamReader sr = new StreamReader(fs);

            #endregion

            #region Read and convert header names to hashtable

            // Read the XML names line in the events
            String[] pairNameArray = sr.ReadLine().Split(',');

            Hashtable pairNameHsh = new Hashtable();

            for (int i = 0; i < pairNameArray.Length; i++)
            {
                pairNameHsh.Add(pairNameArray[i], i);
            }

            #endregion

            // Thread initialization complete
            // Increment the number of threads initialized.
            Interlocked.Increment(ref _threadsAreInitialized);

            // We want all the threads to start working together
            // so all threads will wait for the following variable
            // to be greater than zero.
            while (_threadsAreInitialized < _threadsCount)
            {
                Thread.Sleep(100);
            }

            Int64 totalTimeInMilliseconds;

            // Start reading events
            String currLine = sr.ReadLine();

            // Read until all events are read
            while (currLine != null)
            {
                try {
                    #region Create the XML to send

                    if (eventsInXML == 0)
                    {
                        eventsXMLToSend      = new WBXXml(BASE_EVENT);
                        updateBAMInformation = true;
                    }

                    #endregion

                    // Increment the event counters.
                    eventsInXML++;
                    eventsRead++;
                    //counter.Increment();

                    string[] currLineArray = currLine.Split(',');

                    #region Add a new event element

                    // Create the event element
                    eventsXMLToSend.addElement(
                        null,
                        "event",
                        new Hashtable()
                    {
                        { "eventId", eventsInXML.ToString() }
                    }
                        );

                    string eventXPath =
                        string.Format(
                            "{0}[@{1}='{2}']",
                            "event",
                            "eventId",
                            eventsInXML
                            );

                    // Create the business data element in the event
                    eventsXMLToSend.addElement(
                        eventXPath,
                        "businessdata"
                        );

                    // Create the policy data element in the event
                    eventsXMLToSend.addElement(
                        eventXPath,
                        "policydata"
                        );

                    #endregion

                    #region Update BAM information if needed

                    if (updateBAMInformation)
                    {
                        currBAMType = currLineArray[(int)pairNameHsh["BAMType"]];

                        eventsXMLToSend.updateFirstNameValue(
                            pairNameArray[(int)pairNameHsh["BAMType"]],
                            currBAMType
                            );

                        eventsXMLToSend.updateFirstNameValue(
                            pairNameArray[(int)pairNameHsh["BAMUniqueID"]],
                            currLineArray[(int)pairNameHsh["BAMUniqueID"]]
                            );

                        updateBAMInformation = false;
                    }

                    #endregion

                    #region Add event to the events XML

                    switch (currBAMType.ToLower())
                    {
                    case "sap r3":
                        addSAPR3Event(
                            eventsXMLToSend,
                            eventXPath,
                            pairNameHsh,
                            currLineArray
                            );
                        break;

                    case "wss":
                        addWSSEvent(
                            eventsXMLToSend,
                            eventXPath,
                            pairNameHsh,
                            currLineArray
                            );
                        break;

                    default:
                        Console.WriteLine(
                            "Unknown BAM type - {0}",
                            currBAMType
                            );
                        break;
                    }

                    #endregion

                    #region Report the events to the event collector service

                    // Report Events (maybe multi-threaded ?)
                    if (eventsInXML == eventsInSend)
                    {
                        // Reset the number of events in the XML
                        // before reading the next event
                        eventsInXML = 0;

                        stopWatch.Reset();
                        stopWatch.Start();

                        //Console.Out.WriteLine(eventsXMLToSend.ToString());
                        _serviceClient.reportEvents(eventsXMLToSend.ToString());

                        stopWatch.Stop();

                        totalTimeInMilliseconds =
                            calculateTimeInMilliseconds(
                                stopWatch.Elapsed
                                );

                        // Update performance counter
                        //counter.RawValue = stopWatch.Elapsed.Milliseconds;

                        // Write the data to the data collector file
                        sw.WriteLine(
                            DATA_COLLECTOR_FILE_DATA_LINE,
                            DateTime.Now.ToString(Constants.TIMESTAMP_FORMAT),
                            totalTimeInMilliseconds
                            );

                        // Flush the buffer
                        sw.Flush();

                        Thread.Sleep(delayTime);
                    }

                    #endregion

                    // Read the next event
                    currLine = sr.ReadLine();
                }
                #region Exception handling
                catch (Exception ex) {
                    Console.WriteLine(
                        "Error processing event number {0} from file {1}.\nError: {2}",
                        eventsRead,
                        eventsFilename,
                        ex.Message
                        );
                }
                #endregion
            }

            sr.Close();

            // Increment the number of threads finished.
            Interlocked.Increment(ref _numberOfThreadsFinished);
        }