From 7ed1ad840e57ebe84772a60cdde29cadc7506db2 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Wed, 30 Apr 2014 13:57:39 +0200 Subject: Stream interface --- src/main/StreamCommand.java | 45 ------------------------------------- src/mining/OAuthStream.java | 26 --------------------- src/mining/Stream.java | 29 +++++++++++++++++------- src/provider/ExceptionListener.java | 14 ++++++++++++ 4 files changed, 35 insertions(+), 79 deletions(-) delete mode 100644 src/main/StreamCommand.java delete mode 100644 src/mining/OAuthStream.java create mode 100644 src/provider/ExceptionListener.java diff --git a/src/main/StreamCommand.java b/src/main/StreamCommand.java deleted file mode 100644 index f0f38fe..0000000 --- a/src/main/StreamCommand.java +++ /dev/null @@ -1,45 +0,0 @@ -package main; - -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import mining.OAuthStream; -import mining.Stream; -import mining.StreamListener; -import mining.TwitterApi; -import org.json.JSONObject; - -/** - * A command that reads a stream from a stream listener and writes the data to a - * file. - * - * @author Maurice Laveaux - */ -public class StreamCommand implements StreamListener { - - /** - * A private reference for the stream. - */ - private final Stream m_stream; - - public StreamCommand(final TwitterApi api) { - - m_stream = new OAuthStream(); - } - - @Override - public void notify(JSONObject data) { - try { - // TODO: add the results to the main database. - // For now write to a file - File file = new File("database.txt"); - - FileWriter writer = new FileWriter(file, true); - - writer.write("\n\r" + data.toString()); - writer.flush(); - } catch (IOException ex) { - throw new RuntimeException(ex); - } - } -} diff --git a/src/mining/OAuthStream.java b/src/mining/OAuthStream.java deleted file mode 100644 index 3cddb57..0000000 --- a/src/mining/OAuthStream.java +++ /dev/null @@ -1,26 +0,0 @@ - -package mining; - -/** - * An implementation of a Stream using the OAuthentication method. - * @author Maurice Laveaux - */ -public class OAuthStream implements Stream { - - @Override - public void open() { - - } - - @Override - public void setListener(StreamListener listener) { - - } - - @Override - public boolean isValid() { - - return false; - } - -} diff --git a/src/mining/Stream.java b/src/mining/Stream.java index 283b4ea..456a405 100644 --- a/src/mining/Stream.java +++ b/src/mining/Stream.java @@ -1,23 +1,36 @@ package mining; +import java.io.IOException; + /** - * interface for all streaming classes. - * - * @author Maurice Laveaux + * Provides access to a stream. The implementor is supposed to provide means + * that allows a client to be notified of new tweets. */ public interface Stream { /** - * Open the connection to the server. + * Add the keyword (or phrase) to the list of keywords to be watched in a + * stream. + * + * @param keyword + */ + public void watchKeyword(String keyword); + + /** + * Removes the keyword (or phrase) from the list of keywords to watch for in + * a stream. + * + * @param keyword */ - public void open(); + public void unwatchKeyword(String keyword); /** - * Set a single listener for this stream. + * Starts streaming tweets. If a connection is already open, then it may be + * re-opened to use new keywords. * - * @param listener The object that is listening. + * @throws IOException on failure to open a streaming connection. */ - public void setListener(StreamListener listener); + public void commit() throws IOException; /** * Test whether the stream is ready for streaming diff --git a/src/provider/ExceptionListener.java b/src/provider/ExceptionListener.java new file mode 100644 index 0000000..bc4860e --- /dev/null +++ b/src/provider/ExceptionListener.java @@ -0,0 +1,14 @@ +package provider; + +/** + * Notifies whenever an unexpected, fatal exception occurred. + */ +public interface ExceptionListener { + + /** + * Callback for fatal errors. + * + * @param ex Exception that occurred. + */ + public void exceptionGenerated(Exception ex); +} -- cgit v1.2.1