package org.saf.smppagent;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.saf.business.BaseMTInfo;
import org.saf.pdu.AbstractPDUCache;
public class TransmitterQueue {
private static final Logger logger =
Logger.getLogger(TransmitterQueue.class);
private static final long WRITE_TIMEOUT = 1;
private ArrayBlockingQueue<BaseMTInfo> queue = null;
private boolean safeStop = false;
private String channel = null;
private AbstractPDUCache pduCache = null;
public TransmitterQueue(int capacity, String channel) {
init(capacity, channel, null);
}
public TransmitterQueue(int capacity, String channel,
AbstractPDUCache pduCache) {
init(capacity, channel, pduCache);
}
public boolean add(BaseMTInfo mtInfo) {
boolean ret = false;
try {
int retries = 1;
while (ret == false ) {
if(this.safeStop == true) {
logger.fatal("adding to queue safe exiting...");
ret = true;
if(pduCache != null) {
pduCache.addSubmitSMToTransmit(mtInfo, this.channel);
}
} else {
ret = this.queue.offer(
mtInfo, WRITE_TIMEOUT, TimeUnit.SECONDS);
if(ret == false) {
logger.error("retries " + retries +
" no good try new one");
retries ++;
}
}
}
} catch (InterruptedException e) {
logger.error(e.getMessage());
}
if(ret == true) {
logger.debug("adding a submit sm true");
} else {
logger.debug("adding a submit sm false");
}
return ret;
}
BaseMTInfo get(long timeout) throws InterruptedException {
BaseMTInfo info = null;
if(timeout > 0) {
info = this.queue.poll(timeout, TimeUnit.SECONDS);
} else {
info = this.queue.poll();
}
if(info != null) {
logger.debug("getting a submit sm");
}
return info;
}
synchronized void safeStop(){
this.safeStop = true;
}
private void init(int capacity, String channel, AbstractPDUCache pduCache) {
this.queue = new ArrayBlockingQueue<BaseMTInfo>(capacity);
this.safeStop = false;
this.channel = channel;
this.pduCache = pduCache;
}
}