?? testeventpushsources3.java
字號:
// Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
// Distributable under LGPL license. See terms of license at gnu.org.
package nl.justobjects.pushlet.test;
import nl.justobjects.pushlet.core.Dispatcher;
import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.core.EventSource;
import nl.justobjects.pushlet.util.Rand;
import hfut.wispy.ListBuffer;
import hfut.wispy.RFIDReader;
import hfut.wispy.TagDataPacket;
import hfut.wispy.webservice.GlobalSettings;
import hfut.wispy.webservice.RFIDReaderSettings;
import hfut.wispy.webservice.RFIDReaderSettings.ReaderModel;
import java.util.Date;
/**
* Event sources that push events (for testing).
*
* @author Just van den Broecke - Just Objects ©
* @version $Id: TestEventPushSources.java,v 1.10 2007/11/09 13:16:57 justb Exp $
*/
public class TestEventPushSources3 {
static public class AEXStocksEventPushSourceABN {
String pageURL = "http://ri2.rois.com/E36msPtnZC0e15CVb4KT97JAGfGSfCcrvv6*FcyZIoNyh/CTIB/RI2APISNAP?RIC=0%23.AEX&FORMAT=XML";
// This could be further expanded: getting the Reuters AEX stocks
// as XML from ABN with this URL, but we may get into legal problems...
}
/**
* Produces events from REAL stocks from the AEX.
*/
static public class AEXStocksEventPushSource1 implements EventSource,
Runnable {
Thread thread = null;
boolean active = false;
public static Date lastUpdateDate = new Date();
private int restarts = 1;
static RFIDReader reader = new RFIDReader();
static ListBuffer listBuffer = ListBuffer.getInstance();
static RFIDReaderSettings readerSettings = new RFIDReaderSettings(
ReaderModel.YWG804_6c_cycle);
static GlobalSettings globalSettings = GlobalSettings.getInstance();
public AEXStocksEventPushSource1() {
}
/**
* Activate the event source.
*/
synchronized public void activate() {
// e("activating...");
// Stop a possibly running thread
stopThread();
// Start new thread and
thread = new Thread(this, "RFIDReaderPublisher-" + (restarts++));
active = true;
thread.start();
p("[---Thread---] : RFIDReaderPublisher-" + restarts);
e("activated");
}
synchronized public Thread getThread() {
if (thread != null) {
return thread;
}
return null;
}
/**
* Deactivate the event source.
*/
synchronized public void passivate() {
e("passivating...");
active = false;
stopThread();
// Mark the cache modified so we'll send the contents
// on the next activation.
for (int i = 0; i < listBuffer.getBufferLength(); i++) {
listBuffer.getTagPacket(i).modified = true;
}
e("passivated");
}
/**
* Deactivate the event source.
*/
synchronized public void stop() {
}
public void run() {
p("RFIDReaderPublisher thread is created. now run() called");
// Publish cache content (if any) first.
// publishStocks();
int count = 2; // enforce update first
initCache();
updateCache();
while (active) {
this.thread.setPriority(Thread.MIN_PRIORITY);
while (globalSettings.IsStartReader) {
this.thread.setPriority(Thread.NORM_PRIORITY);
if (count % 2 == 0) {
updateCache();
Date now = new Date();
p("lastUpdateDate : "
+ (now.getTime() - lastUpdateDate.getTime() + "毫秒"));
lastUpdateDate = now;
}
count++;
// Do updates for changed stock rates
publishStocks();
// If we were interrupted just return.
if (thread == null || thread.isInterrupted()) {
break;
}
try {
Thread.sleep(Rand.randomLong(500, 8000));
} catch (InterruptedException ie) {
break;
}
}
}
// Loop terminated: reset vars
thread = null;
active = false;
}
private void publishStocks() {
// p("sending updates");
// Publish only modified stocks from the cache
for (int i = 0; i < listBuffer.getBufferLength(); i++) {
TagDataPacket nextStock = listBuffer.getTagPacket(i);
// Publish modified stocks
if (nextStock.modified) {
publishStock(i, nextStock.TagData, nextStock.HaveReadTimes,
nextStock.ReadTime);
nextStock.modified = false;
// wispy no sleep test
try {
Thread.sleep(200);
} catch (InterruptedException ie) {
return;
}
}
// test---不管modified了,全都push一把
// publishStock(i, nextStock.TagData, nextStock.HaveReadTimes,
// nextStock.ReadTime);
}
}
public void publishStock(int index, byte[] tagData, int HaveReadTimes,
Date ReadTime) {
Event event = Event.createDataEvent("/stocks/aexonlineRFIDReader");
event.setField("number", index + "");
event.setField("name", RFIDReader.Bytes2HexString(tagData));
event.setField("rate", HaveReadTimes + "");
event.setField("time", ReadTime.toString());
Dispatcher.getInstance().multicast(event);
}
public void publishStock(int index, String name, String rate,
String time) {
Event event = Event.createDataEvent("/stocks/aexonlineRFIDReader1");
event.setField("number", index + "");
event.setField("name", name);
event.setField("rate", rate);
event.setField("time", time);
// p("publish: nr=" + index + " name=" + name + " rate=" + rate);
// p("[sending updates] --rate-- : " + rate);
// p("[sending updates] --time-- : " + time);
Dispatcher.getInstance().multicast(event);
}
private void stopThread() {
if (thread != null) {
thread.interrupt();
thread = null;
}
}
public static void initCache() {
if (!reader.GetOpenState())
reader.Open();
}
public static void updateCache() {
reader.Identify(readerSettings.Antenna, readerSettings.IsCycleRead);
for (int i = 0; i < readerSettings.CycleReadTime; i++) {
TagDataPacket tag = new TagDataPacket(reader.Report());
p(tag.getFullStr());
listBuffer.MyAdd(tag);
p("now listBuffer length = " + listBuffer.getBufferLength());
}
reader.PowerOff();
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
}
/**
* Util: stderr print method.
*/
public static void e(String s) {
System.out.println("RFIDReaderEventPush: " + s);
}
/**
* Util: stdout print method.
*/
public static void p(String s) {
System.out.println(s);
}
public static void main(String[] args) {
// new TestEventPushSources$AEXStocksEventPushSource();
}
}
/*
* $Log: TestEventPushSources.java,v $ Revision 1.10 2007/11/09 13:16:57 justb
* use Rand from util package (and and Rand.java to pushlet client jar
*
* Revision 1.9 2005/02/28 09:14:56 justb sessmgr/dispatcher factory/singleton
* support
*
* Revision 1.8 2005/02/21 16:59:17 justb SessionManager and session lease
* introduced
*
* Revision 1.7 2005/02/18 10:07:23 justb many renamings of classes (make names
* compact)
*
* Revision 1.6 2005/02/18 09:54:15 justb refactor: rename Publisher Dispatcher
* and single Subscriber class
*
* Revision 1.5 2004/09/03 22:35:37 justb Almost complete rewrite, just checking
* in now
*
* Revision 1.4 2004/03/10 15:45:55 justb many cosmetic changes
*
* Revision 1.3 2003/08/15 08:37:41 justb fix/add Copyright+LGPL file headers
* and footers
*
* Revision 1.2 2003/05/18 16:15:08 justb support for XML encoded Events
*
* Revision 1.1.1.1 2002/09/24 21:02:33 justb import to sourceforge
*
* Revision 1.1.1.1 2002/09/20 22:48:20 justb import to SF
*
* Revision 1.1.1.1 2002/09/20 14:19:02 justb first import into SF
*
* Revision 1.6 2001/02/18 23:45:13 just fixes for AEX
*
* Revision 1.5 2000/10/30 14:16:09 just no message
*
* Revision 1.4 2000/09/24 21:02:43 just chnages due to changed webpage in
* debeurs.nl
*
* Revision 1.3 2000/08/31 12:49:50 just added CVS comment tags for log and
* copyright
*
*
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -