final boolean session = "true".equals(request.getParameter("session"));
final int count = Integer.parseInt(request.getParameter("count"));
final int batch = Integer.parseInt(request.getParameter("batch"));
final int delay = Integer.parseInt(request.getParameter("delay"));
final boolean string = "string".equals(request.getParameter("mode"));
final CometSession cometSession = cometResponse.getSession(false);
final boolean order = request.getRequestURI().endsWith("order");
if (session && cometSession == null) {
cometResponse.terminate();
return;
}
if (session && connectionCount != 1) {
return;
}
new Thread() {
public void run() {
try {
if (cometSession == null) {
for (int i = 0; i < count; i++) {
if (!cometResponse.isTerminated()) {
if (batch > 1) {
List<Serializable> messages = new ArrayList<Serializable>(batch);
for (int b = 0; b < batch; b++) {
messages.add(getMessage(string, order, i * batch + b));
}
synchronized (cometResponse) {
if (!cometResponse.isTerminated()) {
cometResponse.write(messages);
}
}
}
else {
synchronized (cometResponse) {
if (!cometResponse.isTerminated()) {
cometResponse.write(getMessage(string, order, i));
}
}
}
if (delay > 0) {
try {
sleep(delay);
}
catch (InterruptedException e) {
throw new InterruptedIOException();
}
}
}
}
cometResponse.terminate();
}
else {
for (int i = 0; i < count; i++) {
for (int b = 0; b < batch; b++) {
cometSession.enqueue(getMessage(string, order, i * batch + b));
}
if (delay > 0) {
try {
sleep(delay);
}
catch (InterruptedException e) {
throw new InterruptedIOException();
}
}
}
// there is no proper way to wait for the queue to drain :-(
while (cometSession.isValid() && !cometSession.getQueue().isEmpty()) {
try {
sleep(1);
}
catch (InterruptedException e) {
throw new InterruptedIOException();
}
}
cometSession.invalidate();
}
}
catch (IOException e) {
log("Error writing data", e);
}