-
Notifications
You must be signed in to change notification settings - Fork 275
/
Comet.scala
129 lines (109 loc) · 4.6 KB
/
Comet.scala
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
/*
* Copyright 2007-2012 WorldWide Conferencing, LLC
*
* 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.liftweb
package builtin
package snippet
import scala.xml._
import net.liftweb.http._
import net.liftweb.util._
import net.liftweb.common._
import Helpers._
object Comet extends DispatchSnippet with LazyLoggable {
def dispatch : DispatchIt = {
case _ => render _
}
/**
*
* A typical comet tag could look like:
*
* <pre name="code" class="xml">
* <lift:comet type=<Your comet class> name=<Optional, the name of this comet instance>>{xhtml}</lift:comet>
* </pre>
*
* For the name, you have three options
* <ul>
* <li>You can set a fix name using <pre><code>name="MyComet"</code></pre>
* <li>You can use a query parameter, for a url like foo?=id=122, your comet could take the
* name "122" if you use: <pre><code>metaname=id</code></pre>
* <li>You could assign a random name by using <pre><code>randomname=true</code></pre>
* </ul>
*
*
* @param kids The NodeSeq that is enclosed by the comet tags
* @return
*/
def render(kids: NodeSeq) : NodeSeq = {
Props.inGAE match {
case true => Text("Comet Disabled in Google App Engine")
case _ => buildComet(kids)
}
}
private def buildSpan(timeb: Box[Long], xml: NodeSeq, cometActor: LiftCometActor, spanId: String): NodeSeq =
Elem(cometActor.parentTag.prefix, cometActor.parentTag.label, cometActor.parentTag.attributes,
cometActor.parentTag.scope, Group(xml)) %
(new UnprefixedAttribute("id", Text(spanId), Null)) %
(timeb.filter(_ > 0L).map(time => (new PrefixedAttribute("lift", "when", Text(time.toString), Null))) openOr Null)
private def buildComet(kids: NodeSeq) : NodeSeq = {
val theType: Box[String] = S.attr.~("type").map(_.text)
val name: Box[String] = S.currentAttr("name") or
S.currentAttr("metaname").flatMap(S.param) or
S.currentAttr("randomname").map(ignore => Helpers.nextFuncName)
(for {ctx <- S.session} yield {
if (!ctx.stateful_?)
throw new StateInStatelessException(
"Lift does not support Comet for stateless requests")
try {
ctx.findComet(theType, name, kids, S.attrsFlattenToMap).map {
c => {
// Update the view on each page load in dev mode
// this make development easier
if (Props.devMode) {
c ! UpdateDefaultXml(kids)
}
(c.!?(c.cometRenderTimeout, AskRender)) match {
case Full(AnswerRender(response, _, when, _)) if c.hasOuter =>
LiftRules.cometElementTagger.vend(buildSpan(Empty, c.buildSpan(when, response.inSpan) ++ response.outSpan, c, c.uniqueId+"_outer"))
case Full(AnswerRender(response, _, when, _)) =>
LiftRules.cometElementTagger.vend(c.buildSpan(when, response.inSpan))
case e =>
if (c.cometRenderTimeoutHandler().isDefined) {
c.cometRenderTimeoutHandler().open_!
} else {
throw new CometTimeoutException("type: "+theType+" name: "+name)
}
}}} openOr {
throw new CometNotFoundException("type: "+theType+" name: "+name)
}
} catch {
case e: SnippetFailureException => throw e
case e: Exception => logger.error("Failed to find a comet actor", e); kids
}
}) openOr {
throw new CometNotFoundException("Session not found. type: "+theType+" name: "+name)
}
}
}
abstract class CometFailureException(msg: String) extends SnippetFailureException(msg) {
override def buildStackTrace: NodeSeq = <div>{msg}</div> ++ super.buildStackTrace
}
class CometTimeoutException(msg: String) extends CometFailureException(msg) {
def snippetFailure: LiftRules.SnippetFailures.Value =
LiftRules.SnippetFailures.CometTimeout
}
class CometNotFoundException(msg: String) extends CometFailureException(msg) {
def snippetFailure: LiftRules.SnippetFailures.Value =
LiftRules.SnippetFailures.CometNotFound
}