Package net.easymodo.asagi.model

Examples of net.easymodo.asagi.model.Topic


                    oldTopic.lock.readLock().lock();
                    int oldTopicNum = oldTopic.getNum();
                    long oldTopicLastMod = oldTopic.getLastModTimestamp();
                    oldTopic.lock.readLock().unlock();

                    Topic newTopic = threadMap.remove(oldTopicNum);
                    if(newTopic != null) {
                        if(oldTopicLastMod < newTopic.getLastModTimestamp()) {
                            debug(TALK, "modified: " + oldTopicNum);
                            if(!newTopics.contains(newTopic.getNum()))
                                newTopics.add(newTopic.getNum());
                        }

                        oldTopic.lock.writeLock().lock();
                        oldTopic.setLastModTimestamp(newTopic.getLastModTimestamp());
                        oldTopic.setLastPage(newTopic.getLastPage());
                        oldTopic.lock.writeLock().unlock();
                    } else {
                        // baleeted topic
                        if(!newTopics.contains(oldTopicNum))
                            newTopics.add(oldTopicNum);
View Full Code Here


        int omImages = 0;
        mat = omImagesPattern.matcher(text);
        if(mat.find()) omImages = Integer.parseInt(mat.group(1));

        Post op = this.parsePost(text, 0);
        Topic thread = new Topic(op.getNum(), omPosts, omImages);
        thread.addPost(op);

        return thread;
    }
View Full Code Here

        String[] wgetReply = this.wgetText(this.linkPage(pageNum), lastMod);
        String pageText = wgetReply[0];
        String newLastMod = wgetReply[1];

        Page p = new Page(pageNum);
        Topic t = null;

        Matcher mat = postGetPattern.matcher(pageText);

        while(mat.find()) {
            String text = mat.group(1);
            String type = mat.group(2);

            if(type.equals("opContainer")) {
                t = this.parseThread(text);
                p.addThread(t);
            } else {
                if(t != null) t.addPost(this.parsePost(text, t.getNum()));
            }
        }

        p.setLastMod(newLastMod);
        return p;
View Full Code Here

    public Topic getThread(int threadNum, String lastMod) throws ContentGetException, ContentParseException {
        String[] wgetReply = this.wgetText(this.linkThread(threadNum), lastMod);
        String threadText = wgetReply[0];
        String newLastMod = wgetReply[1];

        Topic t = null;

        Matcher mat = postGetPattern.matcher(threadText);

        while(mat.find()) {
            String text = mat.group(1);
            String type = mat.group(2);
            if(type.equals("opContainer")) {
                if(t == null) {
                    t = this.parseThread(text);
                    t.setLastMod(newLastMod);
                } else {
                    throw new ContentParseException("Two OP posts in thread in " + threadNum);
                }
            } else {
                if(t != null) {
                    t.addPost(this.parsePost(text, t.getNum()));
                } else {
                    throw new ContentParseException("Thread without OP post in " + threadNum);
                }
            }
        }
View Full Code Here

                            continue;
                        }

                        // Otherwise we'll go ahead and try to update the
                        // topic with the posts we have from this index page.
                        Topic fullTopic = topics.get(num);

                        // Perhaps we had extremely bad luck and a TopicFetcher
                        // just saw this thread 404 and got rid of it before we
                        // could grab it? Oh well.
                        if(fullTopic == null) continue;

                        // Try to get the write lock for this topic.
                        fullTopic.lock.writeLock().lock();

                        // Oh, forget it. A ThreadFetcher beat us to this one.
                        // (Or another PageScanner)
                        if(fullTopic.getLastHit() > pageStartTime) {
                            fullTopic.lock.writeLock().unlock();
                            continue;
                        }

                        // Update the last page where we saw this topic
                        fullTopic.setLastPage(pageNo);

                        int oldPosts = 0;
                        int newPosts = 0;
                        boolean mustRefresh = false;

                        // We check for any posts that got deleted
                        // We have the write lock, so TopicFetchers can suck it.
                        if(findDeleted(fullTopic, newTopic, false)) {
                            // Pages cannot be trusted to not have posts missing.
                            // We need to force a refresh, it can't be helped.
                            // See GH-11. Sigh.
                            mustRefresh = true;
                            newPosts++;
                        }

                        for(Iterator<Post> it = newTopic.getPosts().iterator(); it.hasNext();) {
                            Post newPost = it.next();

                            // This post was already in topics map. Next post
                            if(fullTopic.findPost(newPost.getNum())) {
                                if(newPost.isOmitted()) it.remove();
                                oldPosts++;
                                continue;
                            }

                            // Looks like it's new
                            // Add the post's num to the full topic, we'll
                            // update it for real with newTopic.
                            fullTopic.addPost(newPost.getNum()); newPosts++;

                            // Comment too long. Click here to view the full text.
                            // This means we have to refresh the full thread
                            if(newPost.isOmitted()) mustRefresh = true;
                        }

                        // Update the time we last hit this thread
                        fullTopic.setLastHit(pageStartTime);

                        fullTopic.lock.writeLock().unlock();

                        //  No new posts
                        if(oldPosts != 0 && newPosts == 0) continue;
View Full Code Here

    protected class TopicInserter implements Runnable {
        @Override
        @SuppressWarnings("InfiniteLoopStatement")
        public void run() {
            while(true) {
                Topic newTopic;
                try {
                     newTopic = topicUpdates.take();
                } catch(InterruptedException e) { continue; }

                newTopic.lock.writeLock().lock();

                try {
                    topicLocalBoard.insert(newTopic);
                } catch(ContentStoreException e) {
                    debug(ERROR, "Couldn't insert topic " + newTopic.getNum() +
                            ": " + e.getMessage());
                    newTopic.lock.writeLock().unlock();
                    continue;
                } catch(DBConnectionException e) {
                    debug(ERROR, "Database connection error while inserting topic: " + newTopic.getNum()
                            + ". Lost connection to database, can't reconnect. Reason: "
                            + e.getMessage());
                    newTopic.lock.writeLock().unlock();
                    continue;
                }

                List<Post> posts = newTopic.getPosts();
                if(posts == null) {
                    newTopic.lock.writeLock().unlock();
                    return;
                }

                for(Post post : posts) {
                    try {
                        MediaPost mediaPost = new MediaPost(post.getNum(), post.getThreadNum(), post.isOp(),
                                post.getPreviewOrig(), post.getMediaOrig(), post.getMediaHash());

                        if(post.getPreviewOrig() != null && fullThumb) {
                            if(!mediaPreviewUpdates.contains(mediaPost))
                                mediaPreviewUpdates.put(mediaPost);
                        }
                        if(post.getMediaOrig() != null && fullMedia) {
                            if(!mediaUpdates.contains(mediaPost))
                                mediaUpdates.put(mediaPost);
                        }
                    } catch(InterruptedException e) { }

                    if(post.isArchived()) {
                        debug(TALK, newTopic.getNum() + ": archived");
                        topics.remove(newTopic.getNum());
                    }
                }
                newTopic.purgePosts();
                newTopic.lock.writeLock().unlock();
            }
        }
View Full Code Here

                    newTopic = newTopics.take();
               } catch(InterruptedException e) { continue; }

               String lastMod = null;

               Topic oldTopic = topics.get(newTopic);

               // If we already saw this topic before, acquire its lock
               if(oldTopic != null) {
                   oldTopic.lock.readLock().lock();
                   lastMod = oldTopic.getLastMod();
                   oldTopic.lock.readLock().unlock();
               }

               long startTime = DateTime.now().getMillis();

               // Let's go get our updated topic, from the topic page
               Topic topic;
               try {
                   topic = sourceBoard.getThread(newTopic, lastMod);
               } catch(HttpGetException e) {
                   if(e.getHttpStatus() == 304) {
                       // If the old topic exists, update its lastHit timestamp
                       // The old topic should always exist at this point.
                       pingTopic(oldTopic);
                       debug(TALK, newTopic + ": wasn't modified");
                       continue;
                   } else if(e.getHttpStatus() == 404) {
                       if(oldTopic != null) {
                           oldTopic.lock.writeLock().lock();
                           // If we found the topic before the page limbo
                           // threshold, then it was forcefully deleted
                           if(oldTopic.getLastPage() < pageLimbo) {
                               if(oldTopic.getAllPosts().size() > 1) {
                                   int op = oldTopic.getAllPosts().iterator().next();
                                   try {
                                       DeletedPost post = new DeletedPost(op, System.currentTimeMillis() / 1000);
                                       deletedPosts.put(post);
                                   } catch(InterruptedException e1) { }
                               }
                               topicUpdates.add(oldTopic);
                               debug(TALK, newTopic + ": deleted (last seen on page " + oldTopic.getLastPage() + ")");
                           }

                           // Goodbye, old topic.
                           topics.remove(newTopic);
                           oldTopic.lock.writeLock().unlock();
                       }
                       continue;
                   } else {
                       // We got some funky error
                       pingTopic(oldTopic);
                       debug(WARN, newTopic + ": error: " + e.getMessage());
                       continue;
                   }
               } catch(ContentGetException e) {
                   // We got an even funkier, non-HTTP error
                   pingTopic(oldTopic);
                   debug(WARN, newTopic + ": error: " + e.getMessage());
                   continue;
               } catch(ContentParseException e) {
                   pingTopic(oldTopic);
                   debug(ERROR, newTopic + ": " + e.getMessage());
                   continue;
               }

               if(topic == null) {
                   pingTopic(oldTopic);
                   debug(WARN, newTopic + ": topic has no posts");
                   continue;
               }

               topic.setLastHit(startTime);

               // We're about to make our rebuilt topic public
               topic.lock.writeLock().lock();

               if(oldTopic != null) {
                   oldTopic.lock.writeLock().lock();

                   // Beaten to the punch (how?)
                   if(oldTopic.getLastHit() > startTime) {
                       debug(ERROR, "Concurrency issue updating topic " + oldTopic.getNum());
                       oldTopic.lock.writeLock().unlock();

                       // Throw this away now.
                       topic.lock.writeLock().unlock();
                       continue;
                   }

                   // Get the deleted posts from the old topic
                   // Update their status in the DB, too.
                   findDeleted(oldTopic, topic, true);

                   // We don't really know at which page this thread is, so let
                   // us keep the last page a PageScanner (or BoardPoller) saw this thread at.
                   topic.setLastPage(oldTopic.getLastPage());

                   // If the old topic has a lastMod timestamp from a BoardPoller, keep it
                   topic.setLastModTimestamp(oldTopic.getLastModTimestamp());

                   // Goodbye, old topic.
                   topics.put(newTopic, topic);
                   oldTopic.lock.writeLock().unlock();
               } else {
View Full Code Here

        } catch(JsonSyntaxException ex) {
            throw new ContentGetException("API returned invalid JSON", ex);
        }

        Page p = new Page(pageNum);
        Topic t = null;

        for(TopicJson tj : pageJson.getThreads()) {
            for(PostJson pj : tj.getPosts()) {
                if(pj.getResto() == 0) {
                    t = this.makeThreadFromJson(pj);
                    p.addThread(t);
                } else {
                    if(t != null) t.addPost(this.makePostFromJson(pj));
                }
            }
        }

        p.setLastMod(newLastMod);
View Full Code Here

    public Topic getThread(int threadNum, String lastMod) throws ContentGetException, ContentParseException {
        String[] wgetReply = this.wgetText(this.linkThread(threadNum), lastMod);
        String threadText = wgetReply[0];
        String newLastMod = wgetReply[1];

        Topic t = null;

        TopicJson topicJson;
        try {
            topicJson = GSON.fromJson(threadText, TopicJson.class);
        } catch(JsonSyntaxException ex) {
            throw new ContentGetException("API returned invalid JSON", ex);
        }

        if(topicJson == null) {
            throw new ContentParseException("API returned empty JSON in " + threadNum);
        }

        for(PostJson pj : topicJson.getPosts()) {
            if(pj.getResto() == 0) {
                if(t == null) {
                    t = this.makeThreadFromJson(pj);
                    t.setLastMod(newLastMod);
                } else {
                    throw new ContentParseException("Two OP posts in thread in " + threadNum);
                }
            } else {
                if(t != null) {
                    t.addPost(this.makePostFromJson(pj));
                } else {
                    throw new ContentParseException("Thread without OP post in " + threadNum);
                }
            }
        }
View Full Code Here

            throw new ContentParseException("API returned empty JSON in threads.json");
        }

        for(TopicListJson.Page page : topicsJson) {
            for(TopicListJson.Topic topic : page.getThreads()) {
                Topic t = new Topic(topic.getNo(), 0, 0);
                t.setLastModTimestamp(topic.getLastModified());
                t.setLastPage(page.getPage());
                threadList.addThread(t);
            }
        }

        return threadList;
View Full Code Here

TOP

Related Classes of net.easymodo.asagi.model.Topic

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.