XSSUpstreamer.cs :  » Bloggers » dasBlog » newtelligence » DasBlog » Web » Services » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » dasBlog 
dasBlog » newtelligence » DasBlog » Web » Services » XSSUpstreamer.cs
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Threading;
using newtelligence.DasBlog.Runtime;
using newtelligence.DasBlog.Runtime.Proxies;
using newtelligence.DasBlog.Web.Services.Rss20;

namespace newtelligence.DasBlog.Web.Services{
  /// <summary>
  /// Summary description for XSSUpstreamer.
  /// </summary>
  public class XSSUpstreamer
  {
    static AutoResetEvent upstreamTrigger = new AutoResetEvent(false);
        string configPath;
        string contentPath;
        string logPath;
        
 
        public XSSUpstreamer(string configPath, string contentPath, string logPath)
    {
            this.configPath = configPath;
            this.contentPath = contentPath;
            this.logPath = logPath;
    }

        public static void TriggerUpstreaming()
        {
            upstreamTrigger.Set();
        }

        public void Run()
        {
            SiteConfig siteConfig = SiteConfig.GetSiteConfig( configPath );
            ILoggingDataService loggingService = LoggingDataServiceFactory.GetService(logPath);
            IBlogDataService dataService = BlogDataServiceFactory.GetService(contentPath, loggingService );
            

            loggingService.AddEvent( new EventDataItem( EventCodes.XSSServiceStart, "",""));

            do
            {
                try
                {
                    // reload on every cycle to get the current settings
                    siteConfig = SiteConfig.GetSiteConfig( configPath );
                    loggingService = LoggingDataServiceFactory.GetService(logPath);
                    dataService = BlogDataServiceFactory.GetService(contentPath, loggingService );
                    
                    SyndicationServiceImplementation syndicator = new SyndicationServiceImplementation(siteConfig, dataService, loggingService);

                    if ( siteConfig.EnableXSSUpstream &&
                         siteConfig.XSSUpstreamPassword != null &&
                         siteConfig.XSSUpstreamUsername != null )
                    {
                        try
                        {
                            MemoryStream memoryStream = new MemoryStream();
                            StreamWriter streamWriter = new StreamWriter( memoryStream, System.Text.Encoding.UTF8 );
                            XmlSerializer ser = new XmlSerializer( typeof( RssRoot ) );
                            RssRoot root = syndicator.GetRss();
                            ser.Serialize( streamWriter, root );
                            
                            
                            
                            XmlStorageSystemClientProxy xssProxy = new XmlStorageSystemClientProxy();
                            if ( siteConfig.XSSUpstreamEndpoint != null && siteConfig.XSSUpstreamEndpoint.Length > 0 )
                            {
                                xssProxy.Url = siteConfig.XSSUpstreamEndpoint;
                            }

                            string rssFileName = "rss-dasblog.xml";
                            if ( siteConfig.XSSRSSFilename != null && siteConfig.XSSRSSFilename.Length > 0 )
                            {
                                rssFileName = siteConfig.XSSRSSFilename;
                            }

                            // the buffer is longer than the output and must be shortened
                            // by copying into a new buffer
                            byte[] rssBuffer = new byte[memoryStream.Position];
                            byte[] memoryStreamBuffer = memoryStream.GetBuffer();
                            for( int l=0;l<rssBuffer.Length;l++)
                            {
                                rssBuffer[l] = memoryStreamBuffer[l];
                            }


                            XSSSaveMultipleFilesReply reply =
                                xssProxy.SaveMultipleFiles(
                                    siteConfig.XSSUpstreamUsername, 
                                    XmlStorageSystemClientProxy.EncodePassword(siteConfig.XSSUpstreamPassword),
                                    new string[]{rssFileName},
                                    new byte[][]{rssBuffer});

                            if ( reply.flError )
                            {
                                loggingService.AddEvent(
                                    new EventDataItem( 
                                    EventCodes.XSSUpstreamError, reply.message, null, null));
                            }
                            else
                            {
                                loggingService.AddEvent(
                                    new EventDataItem( 
                                    EventCodes.XSSUpstreamSuccess, reply.message, rssFileName, xssProxy.Url));
                            }

                        }
                        catch( Exception e )
                        {
                            loggingService.AddEvent(
                                new EventDataItem( 
                                EventCodes.XSSUpstreamError, e.Message, e.StackTrace, null));
                        }
                     }

                    upstreamTrigger.WaitOne(TimeSpan.FromSeconds(siteConfig.XSSUpstreamInterval), false);

          // introduce a little time offset to limit competition for the files.
          Thread.Sleep(500);

                }
                catch( ThreadAbortException abortException )
                {
                    ErrorTrace.Trace(System.Diagnostics.TraceLevel.Info,abortException);
                    loggingService.AddEvent( new EventDataItem( EventCodes.XSSServiceShutdown, "",""));
                    break;
                }
                catch( Exception e )
                {
                    // if the siteConfig can't be read, stay running regardless 
                    // default wait time is 4 minutes in that case
                    Thread.Sleep( TimeSpan.FromSeconds(240));
                    ErrorTrace.Trace(System.Diagnostics.TraceLevel.Error,e);
                }
            }
            while ( true );
            loggingService.AddEvent( new EventDataItem( EventCodes.XSSServiceShutdown, "",""));
        }

  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.