/
SCSCPHandler.scala
94 lines (81 loc) · 3.35 KB
/
SCSCPHandler.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
package info.kwarc.mmt.odk.SCSCP.Server
import info.kwarc.mmt.odk.OpenMath._
import info.kwarc.mmt.odk.SCSCP.CD.SymbolSet
import info.kwarc.mmt.odk.SCSCP.Protocol.SCSCPCallArguments
/** A handler for SCSCP based functions */
trait SCSCPHandler {
/** Minimal number of arguments to be passed to this handler. */
val min : Int
/** Maximal number of arguments to be passed to this handler. If < 0 assume Infinity */
val max : Int
/** The signature of this function, if any */
val signature : Option[OMApplication]
def handle(client: SCSCPServerClient, arguments: SCSCPCallArguments, parameters: OMExpression*): OMExpression
/** gets an argument as integer. Should only be used if signature != None */
def getArgAsInt(args: List[OMExpression], no: Int): OMInteger = {
if(signature.isEmpty) throw new AssertionError("signature must not be none")
val arg = args(no)
arg match {
case arg: OMInteger =>
arg
case _ =>
throw new SignatureMismatchException(signature.get.arguments, args)
}
}
/** gets an argument as string. Should only be used if signature != None */
def getArgAsString(args: List[OMExpression], no: Int): OMString = {
if(signature.isEmpty) throw new AssertionError("signature must not be none")
val arg = args(no)
arg match {
case arg: OMString =>
arg
case _ =>
throw new SignatureMismatchException(signature.get.arguments, args)
}
}
/** gets an argument as symbol. Should only be used if signature != None */
def getArgAsSymbol(args: List[OMExpression], no: Int): OMSymbol = {
if(signature.isEmpty) throw new AssertionError("signature must not be none")
val arg = args(no)
arg match {
case arg: OMSymbol =>
arg
case _ =>
throw new SignatureMismatchException(signature.get.arguments, args)
}
}
}
/**
* A handler that consists of a single function
* @param h
*/
class LambdaHandler(h : (SCSCPCallArguments, Seq[OMExpression]) => OMExpression, val min : Int = 0, val max : Int = -1, val signature : Option[OMApplication]) extends SCSCPHandler {
def handle(client: SCSCPServerClient, arguments : SCSCPCallArguments, parameters: OMExpression* ) : OMExpression = h(arguments, parameters)
}
/**
* Implements the getAllowedHeads method
* @param server
*/
class GetAllowedHeads(server : SCSCPServer) extends SCSCPHandler {
val min = 0
val max = 0
val signature = Some(SymbolSet(Nil))
def handle(client: SCSCPServerClient, arguments : SCSCPCallArguments, parameters: OMExpression* ) : OMExpression =
SymbolSet(server.getHandlerNames)
}
/**
* Implements the get_signature standard procedure specified by the SCSCP spec.
* @param server the server of the procedure.
*/
class GetSignature(server : SCSCPServer) extends SCSCPHandler {
override val min: Int = 1
override val max: Int = 1
override val signature = Some(SymbolSet(server.getHandlerNames))
override def handle(client: SCSCPServerClient, arguments: SCSCPCallArguments, parameters: OMExpression*): OMExpression =
server.getHandler(getArgAsSymbol(parameters.toList, 0)).signature.getOrElse(OMSymbol("symbol_set_all", "scscp2", None, None))
}
class SignatureMismatchException(expected: List[OMExpression], actual: List[OMExpression]) extends Exception {
def getExpected = expected
def getActual = actual
}
class RemoteServerException() extends Exception