import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.util.HashMap;
import java.util.Map;
import com.lightstreamer.interfaces.data.DataProvider;
import com.lightstreamer.interfaces.data.DataProviderExce ption;
import com.lightstreamer.interfaces.data.FailureException ;
import com.lightstreamer.interfaces.data.ItemEventListene r;
import com.lightstreamer.interfaces.data.SubscriptionExce ption;
public class Simplest implements DataProvider {
private ItemEventListener listener;
private Map<String, ItemThread> threads = new HashMap<String, ItemThread>();
public void init(Map params, File configDir) throws DataProviderException {
}
public boolean isSnapshotAvailable(String itemName)
throws SubscriptionException {
return false;
}
public void setListener(ItemEventListener listener) {
this.listener = listener;
}
public synchronized void subscribe(String itemName, boolean needsIterator)
throws SubscriptionException, FailureException {
if (itemName.equals("stream")) {
ItemThread it = new ItemThread(itemName);
it.start();
/*ItemThread it = threads.get(itemName);
if (it == null) {
it = new ItemThread(itemName);
it.start();
threads.put(itemName, it);
*/}
}
public synchronized void unsubscribe(String itemName) throws SubscriptionException,
FailureException {
ItemThread it = threads.get(itemName);
if (it != null) {
it.close();
threads.remove(itemName);
}
}
class ItemThread extends Thread {
private final String item;
private volatile boolean go = true;
public ItemThread(String item) {
this.item = item;
}
public void run() {
int i=0;
while(go) {
Map<String,String> data = new HashMap<String,String>();
try {Reader r = new BufferedReader(new FileReader("primjer.txt"));
StreamTokenizer stok = new StreamTokenizer(r);
stok.parseNumbers();
// double sum = 0;
stok.nextToken();
while (stok.ttype != StreamTokenizer.TT_EOF) {
if (stok.ttype == StreamTokenizer.TT_NUMBER)
i++;
if(i%3==2){
data.put("Sljedeca vrijednost:", Double.toString(stok.nval));
listener.update(item, data, false);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
stok.nextToken();
}
catch(Exception e){e.printStackTrace();}
}}
public void close() {
go = false;
}
}
}
Bookmarks