org/sonews/feed/PushFeeder.java
changeset 1 6fceb66e1ad7
child 3 2fdc9cc89502
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/feed/PushFeeder.java	Fri Jun 26 16:48:50 2009 +0200
     1.3 @@ -0,0 +1,107 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package org.sonews.feed;
    1.23 +
    1.24 +import java.io.IOException;
    1.25 +import java.util.concurrent.ConcurrentLinkedQueue;
    1.26 +import org.sonews.daemon.storage.Article;
    1.27 +import org.sonews.daemon.storage.Headers;
    1.28 +import org.sonews.util.Log;
    1.29 +import org.sonews.util.io.ArticleWriter;
    1.30 +
    1.31 +/**
    1.32 + * Pushes new articles to remote newsservers. This feeder sleeps until a new
    1.33 + * message is posted to the sonews instance.
    1.34 + * @author Christian Lins
    1.35 + * @since sonews/0.5.0
    1.36 + */
    1.37 +class PushFeeder extends AbstractFeeder
    1.38 +{
    1.39 +  
    1.40 +  private ConcurrentLinkedQueue<Article> articleQueue = 
    1.41 +    new ConcurrentLinkedQueue<Article>();
    1.42 +  
    1.43 +  @Override
    1.44 +  public void run()
    1.45 +  {
    1.46 +    while(isRunning())
    1.47 +    {
    1.48 +      try
    1.49 +      {
    1.50 +        synchronized(this)
    1.51 +        {
    1.52 +          this.wait();
    1.53 +        }
    1.54 +        
    1.55 +        Article  article = this.articleQueue.poll();
    1.56 +        String[] groups  = article.getHeader(Headers.NEWSGROUPS)[0].split(",");
    1.57 +        Log.msg("PushFeed: " + article.getMessageID(), true);
    1.58 +        for(Subscription sub : this.subscriptions)
    1.59 +        {
    1.60 +          // Circle check
    1.61 +          if(article.getHeader(Headers.PATH)[0].contains(sub.getHost()))
    1.62 +          {
    1.63 +            Log.msg(article.getMessageID() + " skipped for host " 
    1.64 +              + sub.getHost(), true);
    1.65 +            continue;
    1.66 +          }
    1.67 +
    1.68 +          try
    1.69 +          {
    1.70 +            for(String group : groups)
    1.71 +            {
    1.72 +              if(sub.getGroup().equals(group))
    1.73 +              {
    1.74 +                // Delete headers that may cause problems
    1.75 +                article.removeHeader(Headers.NNTP_POSTING_DATE);
    1.76 +                article.removeHeader(Headers.NNTP_POSTING_HOST);
    1.77 +                article.removeHeader(Headers.X_COMPLAINTS_TO);
    1.78 +                article.removeHeader(Headers.X_TRACE);
    1.79 +                article.removeHeader(Headers.XREF);
    1.80 +                
    1.81 +                // POST the message to remote server
    1.82 +                ArticleWriter awriter = new ArticleWriter(sub.getHost(), sub.getPort());
    1.83 +                awriter.writeArticle(article);
    1.84 +                break;
    1.85 +              }
    1.86 +            }
    1.87 +          }
    1.88 +          catch(IOException ex)
    1.89 +          {
    1.90 +            Log.msg(ex, false);
    1.91 +          }
    1.92 +        }
    1.93 +      }
    1.94 +      catch(InterruptedException ex)
    1.95 +      {
    1.96 +        Log.msg("PushFeeder interrupted.", true);
    1.97 +      }
    1.98 +    }
    1.99 +  }
   1.100 +  
   1.101 +  public void queueForPush(Article article)
   1.102 +  {
   1.103 +    this.articleQueue.add(article);
   1.104 +    synchronized(this)
   1.105 +    {
   1.106 +      this.notifyAll();
   1.107 +    }
   1.108 +  }
   1.109 +  
   1.110 +}