-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestServer.cs
109 lines (86 loc) · 4.23 KB
/
TestServer.cs
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
using System;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using SCION;
//using com.inficon.scion;
//using java.util;
class TestServer{
public static string getResponseJson(int session,IList<string> configuration){
string result = "{\"sessionToken\" : " + session + ", \"nextConfiguration\" : [";
for(int i=0; i < configuration.Count; i++){
string stateId = configuration[i];
result += "\"" + stateId + "\"";
if(i != configuration.Count - 1){
result += ",";
}
}
result += "]}";
return result;
}
public static void Main(string[] args)
{
if (!HttpListener.IsSupported)
{
Console.WriteLine ("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
return;
}
// Create a listener.
HttpListener listener = new HttpListener();
// Add the prefixes.
listener.Prefixes.Add("http://+:42000/");
int sessionCounter = 0;
Dictionary<int,SCXML> sessions = new Dictionary<int,SCXML>();
listener.Start();
while(true){
// Note: The GetContext method blocks while waiting for a request.
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
// Obtain a response object.
HttpListenerResponse response = context.Response;
System.IO.Stream output = response.OutputStream;
String jsonBody = new StreamReader(request.InputStream).ReadToEnd();
//Dictionary<string, Dictionary<string,string>> values = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string,string>>>(jsonBody);
JObject reqJson = JObject.Parse(jsonBody);
JToken scxmlToken,eventToken,sessionJsonToken;
int sessionToken;
try{
if(reqJson.TryGetValue("load",out scxmlToken)){
Console.WriteLine("Loading new statechart");
string scxmlStr = scxmlToken.ToString();
SCXML scxml = new SCXML(new System.Uri(scxmlStr));
IList<string> initialConfiguration = scxml.Start();
sessionToken = sessionCounter++;
sessions[sessionToken] = scxml;
// Construct a response.
string responseString = TestServer.getResponseJson(sessionToken,initialConfiguration);
System.Console.WriteLine(responseString);
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
output.Write(buffer,0,buffer.Length);
}else if(reqJson.TryGetValue("event",out eventToken) && reqJson.TryGetValue("sessionToken",out sessionJsonToken)){
string eventName = (string) reqJson["event"]["name"];
sessionToken = (int) reqJson["sessionToken"];
IList<string> nextConfiguration = sessions[sessionToken].Gen(eventName,null);
string responseString = TestServer.getResponseJson(sessionToken,nextConfiguration);
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
output.Write(buffer,0,buffer.Length);
}else{
string responseString = "Unrecognized request.";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
output.Write(buffer,0,buffer.Length);
}
}catch(Exception e){
string responseString = "An error occured: " + e.ToString();
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
output.Write(buffer,0,buffer.Length);
}
output.Close();
}
}
}