If you think the Android project ubihelper listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/**
* Copyright (c) 2012 The University of Nottingham
* //fromwww.java2s.com
* This file is part of ubihelper
*
* ubihelper is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ubihelper is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with ubihelper. If not, see <http://www.gnu.org/licenses/>.
*
* @author Chris Greenhalgh (cmg@cs.nott.ac.uk), The University of Nottingham
*/package uk.ac.horizon.ubihelper.channel;
import java.util.logging.Logger;
import org.json.JSONObject;
/** Subscription to a channel.
*
* @author cmg
*
*/publicabstractclass Subscription {
static Logger logger = Logger.getLogger(Subscription.class.getName());
private String channelName;
private String id;
privatedouble period;
privatelong minInterval;
privatelong expires = 0;
privatelong lastValueTime = 0;
/**
* @param channel
* @param id
* @param period
* @param minInterval
*/public Subscription(String channelName) {
super();
this.channelName = channelName;
}
public Subscription(String channelName, String subscriptionId) {
super();
this.channelName = channelName;
this.id = subscriptionId;
}
/** update metadata */publicsynchronizedvoid updateConfiguration(double period, double minInterval, double timeout) {
this.period = period;
this.minInterval = (long)(1000*minInterval);
if (timeout==0)
expires = Long.MAX_VALUE;
else
expires = System.currentTimeMillis()+(long)(1000*timeout);
}
/** expired? */synchronizedboolean isExpired() {
return System.currentTimeMillis()>expires;
}
/** new value */synchronizedvoid addValue(JSONObject value) {
long now = System.currentTimeMillis();
long elapsed = now-lastValueTime;
if (elapsed<minInterval) {
logger.warning("Discarded value, elapsed="+elapsed+" vs "+minInterval+"ms");
return;
}
lastValueTime = now;
handleAddValue(value);
}
protectedabstractvoid handleAddValue(JSONObject value);
/**
* @return the channel
*/public String getChannelName() {
return channelName;
}
/**
* @return the id
*/public String getId() {
return id;
}
/** from ChannelManager.addSubscription */void init(String id) {
this.id = id;
}
/**
* @return the period
*/publicdouble getPeriod() {
return period;
}
/**
* @return the expires
*/publiclong getExpires() {
return expires;
}
}