summaryrefslogtreecommitdiffstats
path: root/test/src/scxml-test-framework-client.cpp
blob: 1eeab9cd7efcd1f95945ca8a7289de1d79a01d42 (plain)
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
#include "uscxml/Interpreter.h"
#include "uscxml/server/HTTPServer.h"
#include <sstream>

extern "C" {
#include "jsmn.h" // minimal json parser
}

#include <event2/keyvalq_struct.h>
#include <event2/buffer.h>

/**
 POST /foo HTTP/1.1
 host: localhost:9000
 accept: application/json
 content-type: application/json
 content-length: 92
 Connection: keep-alive
 
 {"load":"http://localhost:9999/scxml-test-framework/test/targetless-transition/test3.scxml"}
*/

class TestIOProcessor : public uscxml::HTTPServlet, public uscxml::InterpreterMonitor {
public:

  static int lastToken;
  static bool alreadyAnswered; // we need this for delayed events
  static std::map<std::string, std::pair<uscxml::Interpreter*, uscxml::HTTPServer::Request> > _interpreters;

  TestIOProcessor() {}
  
  virtual void beforeCompletion(uscxml::Interpreter* interpreter) {
    _interpreters[interpreter->getName()].second.curlReq = NULL;
  }

  virtual void afterCompletion(uscxml::Interpreter* interpreter) {}
  virtual void beforeMicroStep(uscxml::Interpreter* interpreter) {}
  virtual void beforeTakingTransitions(uscxml::Interpreter* interpreter, const Arabica::XPath::NodeSet<std::string>& transitions) {}

  virtual void beforeEnteringStates(uscxml::Interpreter* interpreter, const Arabica::XPath::NodeSet<std::string>& statesToEnter) {
    std::cout << "Entering states: ";
    for (int i = 0; i < statesToEnter.size(); i++) {
      std::cout << ATTR(statesToEnter[i], "id") << ", ";
    }
    std::cout << std::endl;
  }
  
  virtual void afterEnteringStates(uscxml::Interpreter* interpreter) {
    std::cout << "After entering states: ";
    for (int i = 0; i < interpreter->getConfiguration().size(); i++) {
      std::cout << ATTR(interpreter->getConfiguration()[i], "id") << ", ";
    }
    std::cout << std::endl;
  }
  
  virtual void beforeExitingStates(uscxml::Interpreter* interpreter, const Arabica::XPath::NodeSet<std::string>& statesToExit) {
    std::cout << "Configuration: ";
    for (int i = 0; i < interpreter->getConfiguration().size(); i++) {
      std::cout << ATTR(interpreter->getConfiguration()[i], "id") << ", ";
    }
    std::cout << std::endl;
    std::cout << "Exiting states: ";
    for (int i = 0; i < statesToExit.size(); i++) {
      std::cout << ATTR(statesToExit[i], "id") << ", ";
    }
    std::cout << std::endl;
  }
  
  virtual void afterExitingStates(uscxml::Interpreter* interpreter) {
    std::cout << "After exiting states: ";
    for (int i = 0; i < interpreter->getConfiguration().size(); i++) {
      std::cout << ATTR(interpreter->getConfiguration()[i], "id") << ", ";
    }
    std::cout << std::endl;
  }
  
  virtual void onStableConfiguration(uscxml::Interpreter* interpreter) {
    if (alreadyAnswered)
      return;
    
    Arabica::XPath::NodeSet<std::string> configuration = interpreter->getConfiguration();
    
    uscxml::Data reply;
    reply.compound["sessionToken"] = uscxml::Data(interpreter->getName());
    std::string seperator;
    for (size_t i = 0; i < configuration.size(); i++) {
      if (uscxml::Interpreter::isAtomic(configuration[i]))
        reply.compound["nextConfiguration"].array.push_back(uscxml::Data(ATTR(configuration[i], "id"), uscxml::Data::VERBATIM));
    }
    
    std::cout << "---- reply:" << std::endl;
    std::cout << reply << std::endl;
    
    std::stringstream replyString;
    replyString << reply;
    
    alreadyAnswered = true;
    
    uscxml::HTTPServer::Request httpRequest = _interpreters[interpreter->getName()].second;
    uscxml::HTTPServer::Reply httpReply(httpRequest);
    httpReply.content = replyString.str();
    uscxml::HTTPServer::reply(httpReply);
    
  }

  void httpRecvRequest(const uscxml::HTTPServer::Request& request) {
    
//    uscxml::HTTPServer::Reply httpReply(request);
//    uscxml::HTTPServer::reply(httpReply);
//    return;
    
    std::cout << "---- received:" << std::endl;
    evhttp_request_own(request.curlReq);
    
    std::cout << request.content << std::endl;
    uscxml::Data jsonReq = uscxml::Data::fromJSON(request.content);
    std::cout << jsonReq << std::endl;
    
    
    // is this a load request?
    if (jsonReq.compound.find("load") != jsonReq.compound.end()) {
      std::string filename = jsonReq.compound["load"].atom;
      std::cout << "Starting Interpreter with " << filename << std::endl;
      alreadyAnswered = false;
      
      std::map<std::string, std::pair<uscxml::Interpreter*, uscxml::HTTPServer::Request> >::iterator interpreterIter = _interpreters.begin();
      while(interpreterIter != _interpreters.end()) {
//        if (interpreterIter->second.second.curlReq == NULL) {
          delete interpreterIter->second.first;
          _interpreters.erase(interpreterIter++);
//        } else {
//          interpreterIter++;
//        }
      }

      
      uscxml::Interpreter* interpreter = uscxml::Interpreter::fromURI(filename);
      if (interpreter) {
        std::string token = uscxml::toStr(lastToken++);
        assert(_interpreters.find(token) == _interpreters.end());
        interpreter->setName(token);
        interpreter->addMonitor(this);
        interpreter->start();
        _interpreters[token] = std::make_pair(interpreter, request);
      }
      return;
    }

    if(jsonReq.compound.find("event") != jsonReq.compound.end()) {
      assert(jsonReq.compound["event"].compound.find("sessionToken") != jsonReq.compound["event"].compound.end());
      std::string token = jsonReq.compound["event"].compound["sessionToken"].atom;
      assert(_interpreters.find(token) != _interpreters.end());
      uscxml::Event event;
      event.type = uscxml::Event::INTERNAL;
      event.name = jsonReq.compound["event"].compound["name"].atom;
      std::cout << "Sending event " << event << std::endl;
//      evhttp_request_free(_interpreters[token].second);
      alreadyAnswered = false;
      _interpreters[token].second = request;
      _interpreters[token].first->receive(event);
    }
   
  }
    
  void setURL(const std::string& url) {
    std::cout << "Listening at " << url << std::endl;
  }
};

int TestIOProcessor::lastToken;
bool TestIOProcessor::alreadyAnswered;
std::map<std::string, std::pair<uscxml::Interpreter*, uscxml::HTTPServer::Request> > TestIOProcessor::_interpreters;

int main(int argc, char** argv) {
  TestIOProcessor* testServer = new TestIOProcessor();
  uscxml::HTTPServer::registerServlet("test", testServer);

  while(true)
    tthread::this_thread::sleep_for(tthread::chrono::milliseconds(20));
  
}