aboutsummaryrefslogtreecommitdiffstats
path: root/trunk/infrastructure/net.appjet.ajstdlib/streaming.scala
blob: fbff13769c82a3423f6c35b60258ccca4dc55235 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
/**
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.appjet.ajstdlib;

import scala.collection.mutable.{Queue, HashMap, SynchronizedMap, ArrayBuffer};
import javax.servlet.http.{HttpServletRequest, HttpServletResponse, HttpServlet};
import org.mortbay.jetty.servlet.{ServletHolder, Context};
import org.mortbay.jetty.{HttpConnection, Handler, RetryRequest};
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.io.nio.SelectChannelEndPoint;
import org.mortbay.util.ajax.{ContinuationSupport, Continuation};

import java.util.{Timer, TimerTask};
import java.lang.ref.WeakReference;

import org.mozilla.javascript.{Context => JSContext, Scriptable};

import net.appjet.oui._;
import net.appjet.oui.Util.enumerationToRichEnumeration;
import net.appjet.common.util.HttpServletRequestFactory;

trait SocketConnectionHandler {
  def message(sender: StreamingSocket, data: String, req: HttpServletRequest);
  def connect(socket: StreamingSocket, req: HttpServletRequest);
  def disconnect(socket: StreamingSocket, req: HttpServletRequest);
}

object SocketManager {
  val sockets = new HashMap[String, StreamingSocket] with SynchronizedMap[String, StreamingSocket];
  val handler = new SocketConnectionHandler {
    val cometLib = new FixedDiskLibrary(new SpecialJarOrNotFile(config.ajstdlibHome, "oncomet.js"));
    def cometExecutable = cometLib.executable;
    
    def message(socket: StreamingSocket, data: String, req: HttpServletRequest) {
      val t1 = profiler.time;
//      println("Message from: "+socket.id+": "+data);
      val runner = ScopeReuseManager.getRunner;
      val ec = ExecutionContext(new RequestWrapper(req), new ResponseWrapper(null), runner);
      ec.attributes("cometOperation") = "message";
      ec.attributes("cometId") = socket.id;
      ec.attributes("cometData") = data;
      ec.attributes("cometSocket") = socket;
      net.appjet.oui.execution.execute(
        ec, 
        (sc: Int, msg: String) => 
          throw new HandlerException(sc, msg, null), 
        () => {}, 
        () => { ScopeReuseManager.freeRunner(runner); }, 
				Some(cometExecutable));
      cometlatencies.register(((profiler.time-t1)/1000).toInt);
    }
    def connect(socket: StreamingSocket, req: HttpServletRequest) {
//      println("Connect on: "+socket);
      val runner = ScopeReuseManager.getRunner;
      val ec = ExecutionContext(new RequestWrapper(req), new ResponseWrapper(null), runner);
      ec.attributes("cometOperation") = "connect";
      ec.attributes("cometId") = socket.id;
      ec.attributes("cometSocket") = socket;
      net.appjet.oui.execution.execute(
        ec, 
        (sc: Int, msg: String) => 
          throw new HandlerException(sc, msg, null), 
        () => {}, 
        () => { ScopeReuseManager.freeRunner(runner); }, 
				Some(cometExecutable));      
    }
    def disconnect(socket: StreamingSocket, req: HttpServletRequest) {
      val toRun = new Runnable {
        def run() {
      	  val runner = ScopeReuseManager.getRunner;
      	  val ec = ExecutionContext(new RequestWrapper(req), new ResponseWrapper(null), runner);
      	  ec.attributes("cometOperation") = "disconnect";
      	  ec.attributes("cometId") = socket.id;
      	  ec.attributes("cometSocket") = socket;
          net.appjet.oui.execution.execute(
            ec, 
            (sc: Int, msg: String) => 
              throw new HandlerException(sc, msg, null), 
            () => {}, 
            () => { ScopeReuseManager.freeRunner(runner); }, 
    				Some(cometExecutable));    
      	}
      }
      main.server.getThreadPool().dispatch(toRun);
    }
  }
  def apply(id: String, create: Boolean) = {
    if (create) {
      Some(sockets.getOrElseUpdate(id, new StreamingSocket(id, handler)));
    } else {
      if (id == null)
        error("bad id: "+id);
      sockets.get(id);
    }
  }
  class HandlerException(val sc: Int, val msg: String, val cause: Exception) 
    extends RuntimeException("An error occurred while handling a request: "+sc+" - "+msg, cause);
}

// And this would be the javascript interface. Whee.
object Comet extends CometSupport.CometHandler {
  def init() {
    CometSupport.cometHandler = this;
    context.start();
  }
  
  val acceptableTransports = {
    val t = new ArrayBuffer[String];
    if (! config.disableShortPolling) {
      t += "shortpolling";
    }
    if (config.transportUseWildcardSubdomains) {
      t += "longpolling";
    }
    t += "streaming";
    t.mkString("['", "', '", "']");
  }
  
  
  val servlet = new StreamingSocketServlet();
  val holder = new ServletHolder(servlet);
  val context = new Context(null, "/", Context.NO_SESSIONS | Context.NO_SECURITY);
  context.addServlet(holder, "/*");
  context.setMaxFormContentSize(1024*1024);
  
  def handleCometRequest(req: HttpServletRequest, res: HttpServletResponse) {
    context.handle(req.getRequestURI().substring(config.transportPrefix.length), req, res, Handler.FORWARD);
  }
  
  lazy val ccLib = new FixedDiskResource(new JarOrNotFile(config.ajstdlibHome, "streaming-client.js") {
    override val classBase = "/net/appjet/ajstdlib/";
    override val fileSep = "/../../net.appjet.ajstdlib/";
  });
  def clientCode(contextPath: String, acceptableChannelTypes: String) = {
    ccLib.contents.replaceAll("%contextPath%", contextPath).replaceAll("\"%acceptableChannelTypes%\"", acceptableChannelTypes).replaceAll("\"%canUseSubdomains%\"", if (config.transportUseWildcardSubdomains) "true" else "false");
  }
  def clientMTime = ccLib.fileLastModified;
  
  lazy val ccFrame = new FixedDiskResource(new JarOrNotFile(config.ajstdlibHome, "streaming-iframe.html") {
    override val classBase = "/net/appjet/ajstdlib/";
    override val fileSep = "/../../net.appjet.ajstdlib/";
  });
  def frameCode = {    
    if (! config.devMode) 
      ccFrame.contents.replace("<head>\n<script>", """<head>
  <script>
  window.onerror = function() { /* silently drop errors */ }
  </script>
  <script>""");
    else 
      ccFrame.contents;
  }
  
  
  // public
  def connections(ec: ExecutionContext): Scriptable = {
    JSContext.getCurrentContext().newArray(ec.runner.globalScope, SocketManager.sockets.keys.toList.toArray[Object]);
  }
  
  // public
  def connectionStatus = {
    val m = new HashMap[String, Int];
    for (socket <- SocketManager.sockets.values) {
      val key = socket.currentChannel.map(_.kind.toString()).getOrElse("(unconnected)");
      m(key) = m.getOrElse(key, 0) + 1;
    }
    m;
  }

  // public
  def getNumCurrentConnections = SocketManager.sockets.size;

  // public 
  def write(id: String, msg: String) {
    SocketManager.sockets.get(id).foreach(_.sendMessage(false, msg));
  }

  // public
  def isConnected(id: String): java.lang.Boolean = {
    SocketManager.sockets.contains(id);
  }

  // public
  def getTransportType(id: String): String = {
    SocketManager.sockets.get(id).map(_.currentChannel.map(_.kind.toString()).getOrElse("none")).getOrElse("none");
  }

  // public
  def disconnect(id: String) {
    SocketManager.sockets.get(id).foreach(x => x.close());
  }

  // public
  def setAttribute(ec: ExecutionContext, id: String, key: String, value: String) {
    ec.attributes.get("cometSocket").map(x => Some(x.asInstanceOf[StreamingSocket])).getOrElse(SocketManager.sockets.get(id))
      .foreach(_.attributes(key) = value);
  }
  // public
  def getAttribute(ec: ExecutionContext, id: String, key: String): String = {
    ec.attributes.get("cometSocket").map(x => Some(x.asInstanceOf[StreamingSocket])).getOrElse(SocketManager.sockets.get(id))
      .map(_.attributes.getOrElse(key, null)).getOrElse(null);
  }

  // public
  def getClientCode(ec: ExecutionContext) = {
    clientCode(config.transportPrefix, acceptableTransports);
  }
  def getClientMTime(ec: ExecutionContext) = clientMTime;
}

class StreamingSocket(val id: String, handler: SocketConnectionHandler) {
  var hasConnected = false;
  var shutdown = false;
  var killed = false;
  var currentChannel: Option[Channel] = None;
  val activeChannels = new HashMap[ChannelType.Value, Channel] 
                         with SynchronizedMap[ChannelType.Value, Channel];
  
  lazy val attributes = new HashMap[String, String] with SynchronizedMap[String, String];
  
  def channel(typ: String, create: Boolean, subType: String): Option[Channel] = {
    val channelType = ChannelType.valueOf(typ);
    if (channelType.isEmpty) {
      streaminglog(Map(
        "type" -> "error",
        "error" -> "unknown channel type",
        "channelType" -> channelType));
      None;
    } else if (create) {
      Some(activeChannels.getOrElseUpdate(channelType.get, Channels.createNew(channelType.get, this, subType)));
    } else {
      activeChannels.get(channelType.get);
    }
  }

  val outgoingMessageQueue = new Queue[SocketMessage];
  val unconfirmedMessages = new HashMap[Int, SocketMessage];

  var lastSentSeqNumber = 0;
  var lastConfirmedSeqNumber = 0;
  
  // external API
  def sendMessage(isControl: boolean, body: String) {
    if (hasConnected && ! shutdown) {
      synchronized {
        lastSentSeqNumber += 1;
        val msg = new SocketMessage(lastSentSeqNumber, isControl, body);
        outgoingMessageQueue += msg;
        unconfirmedMessages(msg.seq) = msg;
      }
      currentChannel.foreach(_.messageWaiting());
    }
  }
  def close() {
    synchronized {
      sendMessage(true, "kill");
      shutdown = true;
      Channels.timer.schedule(new TimerTask {
        def run() {
          kill("server request, timeout");
        }
      }, 15000);
    }
  }
  
  var creatingRequest: Option[HttpServletRequest] = None;
  // internal API
  def kill(reason: String) {
    synchronized {
      if (! killed) {
        streaminglog(Map(
          "type" -> "event",
          "event" -> "connection-killed",
          "connection" -> id,
          "reason" -> reason));
        killed = true;
        SocketManager.sockets -= id;
        activeChannels.foreach(_._2.close());
        currentChannel = None;
        if (hasConnected) {
          handler.disconnect(this, creatingRequest.getOrElse(null));
        }
      }
    }
  }
  def receiveMessage(body: String, req: HttpServletRequest) {
//    println("Message received on "+id+": "+body);
    handler.message(this, body, req);
  }
  def getWaitingMessage(channel: Channel): Option[SocketMessage] = {
    synchronized {
      if (currentChannel.isDefined && currentChannel.get == channel && 
          ! outgoingMessageQueue.isEmpty) {
        Some(outgoingMessageQueue.dequeue);
      } else {
        None;
      }
    }
  }
  def getUnconfirmedMessages(channel: Channel): Collection[SocketMessage] = {
    synchronized {
      if (currentChannel.isDefined && currentChannel.get == channel) {
        for (i <- lastConfirmedSeqNumber+1 until lastSentSeqNumber+1) 
          yield unconfirmedMessages(i);
      } else {
        List[SocketMessage]();
      }
    }
  }
  def updateConfirmedSeqNumber(channel: Channel, received: Int) {
    synchronized {
      if (received > lastConfirmedSeqNumber && (channel == null || (currentChannel.isDefined && channel == currentChannel.get))) {
        val oldConfirmed = lastConfirmedSeqNumber;
        lastConfirmedSeqNumber = received;
        for (i <- oldConfirmed+1 until lastConfirmedSeqNumber+1) { // inclusive!
          unconfirmedMessages -= i;
        }
      }
    }
  }
  
  var lastChannelUpdate = 0;
  def useChannel(seqNo: Int, channelType0: String, req: HttpServletRequest) = synchronized {
    if (seqNo <= lastChannelUpdate) false else {
      lastChannelUpdate = seqNo;
      val channelType = ChannelType.valueOf(channelType0);
      if (channelType.isDefined) {
        val channel = activeChannels.get(channelType.get);
        if (channel.isDefined) {
          if (! hasConnected) {
            hasConnected = true;
            creatingRequest = Some(HttpServletRequestFactory.createRequest(req));
            handler.connect(this, req);
          } 
          currentChannel = channel;
//          println("switching "+id+" to channel: "+channelType0);
          if (currentChannel.get.isConnected) {
            revive(channel.get);
          } else {
            hiccup(channel.get);
          }
          currentChannel.get.messageWaiting();
          true;
        } else 
          false;
      } else 
        false;
    }
  }
//   def handleReceivedMessage(seq: Int, data: String) {
//     synchronized {
//       handler.message(this, data)
// //  TODO(jd): add client->server sequence numbers.
// //      if (seq == lastReceivedSeqNumber+1){
// //        lastReceivedSeqNumber = seq;
// //        handler.message(this, data);
// //      } else {
// //        // handle error.
// //      }
//     }
//   }
  def hiccup(channel: Channel) = synchronized {
    if (currentChannel.isDefined && channel == currentChannel.get) {
//      println("hiccuping: "+id);
      scheduleTimeout();
    }
  }
  def revive(channel: Channel) = synchronized {
    if (currentChannel.isDefined && channel == currentChannel.get) {
//      println("reviving: "+id);
      cancelTimeout();
    }
  }
  def prepareForReconnect() = synchronized {
//    println("client-side hiccup: "+id);
    activeChannels.foreach(_._2.close());
    activeChannels.clear();
    currentChannel = None;
    scheduleTimeout();
  }
  
  // helpers
  var timeoutTask: TimerTask = null;
  
  def scheduleTimeout() {
    if (timeoutTask != null) return;
    val p = new WeakReference(this);
    timeoutTask = new TimerTask {
  	  def run() {
    	  val socket = p.get();
    	  if (socket != null) {
    	    socket.kill("timeout");
	      }
  	  }
  	}
  	Channels.timer.schedule(timeoutTask, 15*1000);
  }
  def cancelTimeout() {
    if (timeoutTask != null)
      timeoutTask.cancel();
    timeoutTask = null;
  }
  scheduleTimeout();
  
  streaminglog(Map(
    "type" -> "event",
    "event" -> "connection-created",
    "connection" -> id));
}

object ChannelType extends Enumeration("shortpolling", "longpolling", "streaming") {
  val ShortPolling, LongPolling, Streaming = Value;
}

object Channels {
  def createNew(typ: ChannelType.Value, socket: StreamingSocket, subType: String): Channel = {
    typ match {
      case ChannelType.ShortPolling => new ShortPollingChannel(socket);
      case ChannelType.LongPolling => new LongPollingChannel(socket);
      case ChannelType.Streaming => {
        subType match {
          case "iframe" => new StreamingChannel(socket) with IFrameChannel;
          case "opera" => new StreamingChannel(socket) with OperaChannel;
          case _ => new StreamingChannel(socket);
        }
      } 
    }
  }
  
  val timer = new Timer(true);
}

class SocketMessage(val seq: Int, val isControl: Boolean, val body: String) {
  def payload = seq+":"+(if (isControl) "1" else "0")+":"+body;
}

trait Channel {
  def messageWaiting();
  def close();
  def handle(req: HttpServletRequest, res: HttpServletResponse);
  def isConnected: Boolean;
  
  def kind: ChannelType.Value;
  def sendRestartFailure(ec: ExecutionContext);
}

trait XhrChannel extends Channel {
  def wrapBody(msg: String) = msg.length+":"+msg;

  // wire format: msgLength:seq:[01]:msg
  def wireFormat(msg: SocketMessage) = wrapBody(msg.payload);
  def controlMessage(data: String) = wrapBody("oob:"+data);
  
  def sendRestartFailure(ec: ExecutionContext) {
    ec.response.write(controlMessage("restart-fail"));
  }
}

// trait IFrameChannel extends Channel {
//   def wireFormat(msg: SocketMessage)
// }

class ShortPollingChannel(val socket: StreamingSocket) extends Channel with XhrChannel {
  def kind = ChannelType.ShortPolling;
  
  def messageWaiting() {
    // do nothing.
  }
  def close() {
    // do nothing
  }
  def isConnected = false;
    
  def handle(req: HttpServletRequest, res: HttpServletResponse) {
    val ec = req.getAttribute("executionContext").asInstanceOf[ExecutionContext];
    val out = new StringBuilder();
    socket.synchronized {
      socket.revive(this);
      if (req.getParameter("new") == "yes") {
        out.append(controlMessage("ok"));
      } else {
        val lastReceivedSeq = java.lang.Integer.parseInt(req.getParameter("seq"));
        socket.updateConfirmedSeqNumber(this, lastReceivedSeq);
        for (msg <- socket.getUnconfirmedMessages(this)) {
          out.append(wireFormat(msg));
        }
        // ALL MESSAGES ARE UNCONFIRMED AT THIS POINT! JUST CLEAR QUEUE.
        var msg = socket.getWaitingMessage(this);
        while (msg.isDefined) {
          msg = socket.getWaitingMessage(this);
        }        
      }
    }
//    println("Writing to "+socket.id+": "+out.toString);
    ec.response.write(out.toString);
    socket.synchronized {
      socket.hiccup(this);
    }
  }
}

trait IFrameChannel extends StreamingChannel {
  override def wrapBody(msgBody: String) = {
    val txt = "<script type=\"text/javascript\">p('"+
        msgBody.replace("\\","\\\\").replace("'", "\\'")+"');</script>";
    if (txt.length < 256)
      String.format("%256s", txt);
    else
      txt;
  }
  
  def header(req: HttpServletRequest) = {
    val document_domain = 
        "\""+req.getHeader("Host").split("\\.").slice(2).mkString(".").split(":")(0)+"\"";
    """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><head><title>f</title></head><body id="thebody" onload="(!parent.closed)&&d()"><script type="text/javascript">document.domain = """+document_domain+""";
var p = function(data) { try { parent.comet.pass_data } catch (err) { /* failed to pass data. no recourse. */ } };
var d = parent.comet.disconnect;"""+(if(!config.devMode)"\nwindow.onerror = function() { /* silently drop errors */ }\n" else "")+"</script>"; // " - damn textmate mode!
  }
  
  override def sendRestartFailure(ec: ExecutionContext) {
    ec.response.write(header(ec.request.req));
    ec.response.write(controlMessage("restart-fail"));
  }
  
  override def handleNewConnection(req: HttpServletRequest, res: HttpServletResponse, out: StringBuilder) {
    super.handleNewConnection(req, res, out);
    res.setContentType("text/html");
    out.append(header(req));
  }
}

trait OperaChannel extends StreamingChannel {
  override def wrapBody(msgBody: String) = {
    "Event: message\ndata: "+msgBody+"\n\n";
  }
  override def handleNewConnection(req: HttpServletRequest, res: HttpServletResponse, out: StringBuilder) {
    super.handleNewConnection(req, res, out);
    res.setContentType("application/x-dom-event-stream");
  }
}

class StreamingChannel(val socket: StreamingSocket) extends Channel with XhrChannel {
  def kind = ChannelType.Streaming;
  
  var c: Option[SelectChannelConnector.RetryContinuation] = None;
  var doClose = false;
  
  def messageWaiting() {
    main.server.getThreadPool().dispatch(new Runnable() {
      def run() {
        socket.synchronized {
          c.filter(_.isPending()).foreach(_.resume());
        }        
      }
    });
  }
  
  def setSequenceNumberIfAppropriate(req: HttpServletRequest) {
    if (c.get.isNew) {
      val lastReceivedSeq = java.lang.Integer.parseInt(req.getParameter("seq"));
      socket.updateConfirmedSeqNumber(this, lastReceivedSeq);    
    }
  }
  
  def sendHandshake(req: HttpServletRequest, res: HttpServletResponse, out: StringBuilder) {
    out.append(controlMessage("ok"));
  }
  
  def sendUnconfirmedMessages(req: HttpServletRequest, res: HttpServletResponse, out: StringBuilder) {
    for (msg <- socket.getUnconfirmedMessages(this)) {
      out.append(wireFormat(msg));
    }    
  }
  
  def sendWaitingMessages(req: HttpServletRequest, res: HttpServletResponse, out: StringBuilder) {
    var msg = socket.getWaitingMessage(this);
    while (msg.isDefined) {
      out.append(wireFormat(msg.get));
      msg = socket.getWaitingMessage(this);
    }    
  }
  
  def handleUnexpectedDisconnect(req: HttpServletRequest, res: HttpServletResponse, ep: KnowsAboutDispatch) {
    socket.synchronized {
      socket.hiccup(this);
    }
    ep.close();    
  }
  
  def writeAndFlush(req: HttpServletRequest, res: HttpServletResponse, out: StringBuilder, ep: KnowsAboutDispatch) {
//    println("Writing to "+socket.id+": "+out.toString);
    res.getWriter.print(out.toString);
    res.getWriter.flush();
  }
  
  def suspendIfNecessary(req: HttpServletRequest, res: HttpServletResponse,
                         out: StringBuilder, ep: KnowsAboutDispatch) {
    scheduleKeepalive(50*1000);
    ep.undispatch();
    c.get.suspend(0);    
  }

  def sendKeepaliveIfNecessary(out: StringBuilder, sendKeepalive: Boolean) {
    if (out.length == 0 && sendKeepalive) {
      out.append(controlMessage("keepalive"));
    }
  }
  
  def shouldHandshake(req: HttpServletRequest, res: HttpServletResponse) = c.get.isNew;
  
  var sendKeepalive = false;
  var keepaliveTask: TimerTask = null;
  def scheduleKeepalive(timeout: Int) {
    if (keepaliveTask != null) {
      keepaliveTask.cancel();
    }
    val p = new WeakReference(this);
    keepaliveTask = new TimerTask {
  	  def run() {
    	  val channel = p.get();
    	  if (channel != null) {
    	    channel.synchronized {
      	    channel.sendKeepalive = true;
      	    channel.messageWaiting();
          }
	      }
  	  }
  	}
    Channels.timer.schedule(keepaliveTask, timeout);
  }
  
  def handleNewConnection(req: HttpServletRequest, res: HttpServletResponse, out: StringBuilder) {
    req.setAttribute("StreamingSocketServlet_channel", this);
    res.setHeader("Connection", "close");
    for ((k, v) <- Util.noCacheHeaders) { res.setHeader(k, v); } // maybe this will help with proxies?
    res.setContentType("text/messages; charset=utf-8");
  }
  
  def handle(req: HttpServletRequest, res: HttpServletResponse) {
    val ec = req.getAttribute("executionContext").asInstanceOf[ExecutionContext];
    val ep = HttpConnection.getCurrentConnection.getEndPoint.asInstanceOf[KnowsAboutDispatch];
    val out = new StringBuilder;
    try {
      socket.synchronized {
        val sendKeepaliveNow = sendKeepalive;
        sendKeepalive = false;
        if (keepaliveTask != null) { 
          keepaliveTask.cancel();
          keepaliveTask = null;
        }
        c = Some(ContinuationSupport.getContinuation(req, socket).asInstanceOf[SelectChannelConnector.RetryContinuation]);
        setSequenceNumberIfAppropriate(req);
        if (doClose) {
          ep.close();
          return;
        }
        if (c.get.isNew) {
          handleNewConnection(req, res, out); 
        } else {
          c.get.suspend(-1);
          if (ep.isDispatched) {
            handleUnexpectedDisconnect(req, res, ep);
            return;
          }
        }
        if (shouldHandshake(req, res)) {
//        println("new stream request: "+socket.id);
          sendHandshake(req, res, out);
          sendUnconfirmedMessages(req, res, out);
        }
        sendWaitingMessages(req, res, out);
        sendKeepaliveIfNecessary(out, sendKeepaliveNow);
        suspendIfNecessary(req, res, out, ep);
      }
    } finally {
      writeAndFlush(req, res, out, ep);
    }
  }
  
  def close() {
    doClose = true;
    messageWaiting();
  }
  
  def isConnected = ! doClose;
}

class LongPollingChannel(socket: StreamingSocket) extends StreamingChannel(socket) {
//  println("creating longpoll!");
  override def kind = ChannelType.LongPolling;
  
  override def shouldHandshake(req: HttpServletRequest, res: HttpServletResponse) = 
    req.getParameter("new") == "yes";
  
  override def sendHandshake(req: HttpServletRequest, res: HttpServletResponse, out: StringBuilder) {
//    println("sending handshake");
    out.append(controlMessage("ok"));
  }
  
  override def suspendIfNecessary(req: HttpServletRequest, res: HttpServletResponse, 
                                  out: StringBuilder, ep: KnowsAboutDispatch) {
    if (out.length == 0) {
//      println("suspending longpoll: "+socket.id);
      val to = java.lang.Integer.parseInt(req.getParameter("timeout"));
//      println("LongPoll scheduling keepalive for: "+to);
      scheduleKeepalive(to);
      ep.undispatch();
      c.get.suspend(0);
    }
  }
  
  override def writeAndFlush(req: HttpServletRequest, res: HttpServletResponse, 
                             out: StringBuilder, ep: KnowsAboutDispatch) {
    if (out.length > 0) {
//      println("Writing to "+socket.id+": "+out.toString);
//      println("writing and flushing longpoll")
      val ec = req.getAttribute("executionContext").asInstanceOf[ExecutionContext];
      for ((k, v) <- Util.noCacheHeaders) { ec.response.setHeader(k, v); } // maybe this will help with proxies?
//      println("writing: "+out);
      ec.response.write(out.toString);
      socket.synchronized {
        socket.hiccup(this);
        c = None;
      }
    }
  }
  
  override def handleNewConnection(req: HttpServletRequest, res: HttpServletResponse, out: StringBuilder) {
    socket.revive(this);
    req.setAttribute("StreamingSocketServlet_channel", this);
  }
  
  override def isConnected = socket.synchronized {
    c.isDefined;
  }
}

class StreamingSocketServlet extends HttpServlet {
  val version = 2;
  
  override def doGet(req: HttpServletRequest, res: HttpServletResponse) {
//    describeRequest(req);
    val ec = req.getAttribute("executionContext").asInstanceOf[ExecutionContext];
    try {
      if (req.getPathInfo() == "/js/client.js") {
        val contextPath = config.transportPrefix;
        val acceptableTransports = Comet.acceptableTransports;
        ec.response.setContentType("application/x-javascript");
        ec.response.write(Comet.clientCode(contextPath, acceptableTransports));
      } else if (req.getPathInfo() == "/xhrXdFrame") {
        ec.response.setContentType("text/html; charset=utf-8");
        ec.response.write(Comet.frameCode);
      } else {
        val v = req.getParameter("v");
        if (v == null || java.lang.Integer.parseInt(v) != version) {
          res.sendError(HttpServletResponse.SC_BAD_REQUEST, "bad version number!");
          return;
        }
        val existingChannel = req.getAttribute("StreamingSocketServlet_channel");
        if (existingChannel != null) {
          existingChannel.asInstanceOf[Channel].handle(req, res);
        } else {
          val socketId = req.getParameter("id");
          val channelType = req.getParameter("channel");
          val isNew = req.getParameter("new") == "yes";
          val shouldCreateSocket = req.getParameter("create") == "yes";
          val subType = req.getParameter("type");
          val channel = SocketManager(socketId, shouldCreateSocket).map(_.channel(channelType, isNew, subType)).getOrElse(None);
          if (channel.isDefined) {
            channel.get.handle(req, res);        
          } else {
            streaminglog(Map(
              "type" -> "event",
              "event" -> "restart-failure",
              "connection" -> socketId));
            val failureChannel = ChannelType.valueOf(channelType).map(Channels.createNew(_, null, subType));
            if (failureChannel.isDefined) {
              failureChannel.get.sendRestartFailure(ec);
            } else {
              ec.response.setStatusCode(HttpServletResponse.SC_NOT_FOUND);
              ec.response.write("So such socket, and/or unknown channel type: "+channelType);
            }
          }
        }
      }
    } catch {
      case e: RetryRequest => throw e;
      case t: Throwable => {
        exceptionlog("A comet error occurred: ");
        exceptionlog(t);
        ec.response.setStatusCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        ec.response.write(t.getMessage());
      }
    }
  }
  
  def describeRequest(req: HttpServletRequest) {
    println(req.getMethod+" on "+req.getRequestURI()+"?"+req.getQueryString());
    for (pname <- 
        req.getParameterNames.asInstanceOf[java.util.Enumeration[String]]) {
      println("  "+pname+" -> "+req.getParameterValues(pname).mkString("[", ",", "]"));
    }
  }
  
  override def doPost(req: HttpServletRequest, res: HttpServletResponse) {
    val v = req.getParameter("v");
    if (v == null || java.lang.Integer.parseInt(v) != version) {
      res.sendError(HttpServletResponse.SC_BAD_REQUEST, "bad version number!");
      return;
    }
    val ec = req.getAttribute("executionContext").asInstanceOf[ExecutionContext];
    val socketId = req.getParameter("id");
    val socket = SocketManager(socketId, false);
    
//    describeRequest(req);
    
    if (socket.isEmpty) {
      ec.response.write("restart-fail");
      streaminglog(Map(
        "type" -> "event",
        "event" -> "restart-failure",
        "connection" -> socketId));
//      println("socket restart-fail: "+socketId);
    } else {
      val seq = java.lang.Integer.parseInt(req.getParameter("seq"));
      socket.get.updateConfirmedSeqNumber(null, seq);
      val messages = req.getParameterValues("m");
      val controlMessages = req.getParameterValues("oob");
      try {
        if (messages != null)
          for (msg <- messages) socket.get.receiveMessage(msg, req);
        if (controlMessages != null)
          for (msg <- controlMessages) {
//            println("Control message from "+socket.get.id+": "+msg);
            msg match {
              case "hiccup" => {
                streaminglog(Map(
                  "type" -> "event",
                  "event" -> "hiccup",
                  "connection" -> socketId));
                socket.get.prepareForReconnect();
              }
              case _ => {
                if (msg.startsWith("useChannel")) {
                  val msgParts = msg.split(":");
                  socket.get.useChannel(java.lang.Integer.parseInt(msgParts(1)), msgParts(2), req);
                } else if (msg.startsWith("kill")) {
                  socket.get.kill("client request: "+msg.substring(Math.min(msg.length, "kill:".length)));
                } else {
                  streaminglog(Map(
                    "type" -> "error",
                    "error" -> "unknown control message",
                    "connection" -> socketId,
                    "message" -> msg));
                }
              }
            }
          }
        ec.response.write("ok");
      } catch {
        case e: SocketManager.HandlerException => {
          exceptionlog(e);
          ec.response.setStatusCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
          ec.response.write(e.getMessage());
          // log these?
        }
        case t: Throwable => {
          // shouldn't happen...
          exceptionlog(t);
          ec.response.setStatusCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
          ec.response.write(t.getMessage());
        }
      }      
    }
  }
}