001: package vicazh.hyperpool.stream;
002:
003: import java.util.*;
004:
005: /**
006: * The speed service
007: *
008: * @author Victor Zhigunov
009: * @version 0.4.0
010: */
011: public class SpeedService extends Service implements SpeedServiceMBean {
012: public Connection getConnection() {
013: return new SpeedConnection(this );
014: }
015:
016: private long clientindexsave;
017:
018: private long serverindexsave;
019:
020: private long tsave;
021:
022: private long clientindex;
023:
024: private long serverindex;
025:
026: private long t;
027:
028: private double getClientSpeed() {
029: return (double) (clientindexsave + clientindex) * 1000
030: / (new Date().getTime() - tsave) / 1024;
031: }
032:
033: private double getServerSpeed() {
034: return (double) (serverindexsave + serverindex) * 1000
035: / (new Date().getTime() - tsave) / 1024;
036: }
037:
038: private double client;
039:
040: public void setClient(double client) {
041: this .client = client;
042: }
043:
044: public double getClient() {
045: return client;
046: }
047:
048: void clientwrite() throws InterruptedException {
049: if (getClientSpeed() > client)
050: Thread.sleep(10);
051: clientindex++;
052: }
053:
054: private double server;
055:
056: public void setServer(double server) {
057: this .server = server;
058: }
059:
060: public double getServer() {
061: return server;
062: }
063:
064: void serverwrite() throws InterruptedException {
065: if (getServerSpeed() > server)
066: Thread.sleep(10);
067: serverindex++;
068: }
069:
070: public void stop() throws Exception {
071: timer.cancel();
072: super .stop();
073: }
074:
075: private Timer timer;
076:
077: public void start() throws Exception {
078: super .start();
079: timer = new Timer();
080: timer.schedule(new TimerTask() {
081: public void run() {
082: sendAttribute(SpeedServiceMBean.CLIENTACTION,
083: getClientSpeed());
084: sendAttribute(SpeedServiceMBean.SERVERACTION,
085: getServerSpeed());
086: clientindexsave = clientindex;
087: serverindexsave = serverindex;
088: tsave = t;
089: clientindex = 0;
090: serverindex = 0;
091: t = new Date().getTime();
092: }
093: }, 1000, 1000);
094: }
095:
096: public void setAttribute(String name, Object value)
097: throws Exception {
098: if (name.equals(SpeedServiceMBean.CLIENT))
099: setClient((Double) value);
100: else if (name.equals(SpeedServiceMBean.SERVER))
101: setServer((Double) value);
102: super.setAttribute(name, value);
103: }
104:
105: }
|