void ResetHandler::handleRequest(HttpRequest *request, const std::vector<std::string>& tokens) { map<Dashel::Stream *, HttpDashelTarget *>& targets = getInterface()->getTargets(); map<Dashel::Stream *, HttpDashelTarget *>::iterator end = targets.begin(); for(std::map<Dashel::Stream *, HttpDashelTarget *>::iterator iter = targets.begin(); iter != end; ++iter) { Dashel::Stream *stream = iter->first; HttpDashelTarget *target = iter->second; const map<unsigned, HttpDashelTarget::Node>& nodes = target->getNodes(); map<unsigned, HttpDashelTarget::Node>::const_iterator nodesEnd = nodes.end(); for(map<unsigned, HttpDashelTarget::Node>::const_iterator nodesIter = nodes.begin(); nodesIter != nodesEnd; ++nodesIter) { const HttpDashelTarget::Node& node = nodesIter->second; try { Reset(node.localId).serialize(stream); // reset node stream->flush(); Run(node.localId).serialize(stream); // re-run node stream->flush(); } catch(Dashel::DashelException e) { if(getInterface()->isVerbose()) { cerr << request << " Failed to reset and re-run node: " << e.what() << endl; } return; } if(node.name.find("thymio-II") == 0) { // Special case for Thymio-II. Should we instead just check whether motor.*.target exists? vector<string> tokens; tokens.push_back("motor.left.target"); tokens.push_back("0"); if(!target->sendSetVariable(node.globalId, tokens)) { request->respond().setStatus(HttpResponse::HTTP_STATUS_INTERNAL_SERVER_ERROR); request->respond().setContent("Failed to set Thymio left motor target"); return; } tokens[0] = "motor.right.target"; if(!target->sendSetVariable(node.globalId, tokens)) { request->respond().setStatus(HttpResponse::HTTP_STATUS_INTERNAL_SERVER_ERROR); request->respond().setContent("Failed to set Thymio left motor target"); return; } } size_t eventPos; if(getInterface()->getProgram().getCommonDefinitions().events.contains(UTF8ToWString("reset"), &eventPos)) { vector<string> data; data.push_back("reset"); if(!getInterface()->sendEvent(data)) { request->respond().setStatus(HttpResponse::HTTP_STATUS_INTERNAL_SERVER_ERROR); request->respond().setContent("Failed to send reset event"); return; } } } } request->respond(); }
int main(int argc, char *argv[]) { Dashel::initPlugins(); const char *target = ASEBA_DEFAULT_TARGET; int argCounter = 1; if (argc == 1) { Aseba::dumpHelp(std::cout, argv[0]); return 0; } while (argCounter < argc) { const char *arg = argv[argCounter]; if (strcmp(arg, "-t") == 0) { if (++argCounter < argc) target = argv[argCounter]; else Aseba::errorMissingArgument(argv[0]); } else if ((strcmp(arg, "-h") == 0) || (strcmp(arg, "--help") == 0)) { Aseba::dumpHelp(std::cout, argv[0]); return 0; } else if ((strcmp(arg, "-V") == 0) || (strcmp(arg, "--version") == 0)) { Aseba::dumpVersion(std::cout); return 0; } else { Dashel::Hub client; Dashel::Stream* stream = client.connect(target); assert(stream); // process command try { argCounter += Aseba::processCommand(stream, argc - argCounter, &argv[argCounter]); stream->flush(); } catch (Dashel::DashelException e) { Aseba::errorServerDisconnected(); } catch (Aseba::BootloaderInterface::Error e) { Aseba::errorBootloader(e.what()); } } argCounter++; } return 0; }
virtual void incomingData(Dashel::Stream *stream) { uint16 temp; uint16 len; stream->read(&temp, 2); len = bswap16(temp); stream->read(&temp, 2); lastMessageSource = bswap16(temp); lastMessageData.resize(len+2); stream->read(&lastMessageData[0], lastMessageData.size()); AsebaProcessIncomingEvents(&vm); }
extern "C" void AsebaSendBuffer(AsebaVMState *vm, const uint8* data, uint16 length) { Enki::AsebaMarxbot& marxBot = *asebaSocketMaps[vm]; Dashel::Stream* stream = marxBot.stream; if (!stream) return; // send to stream try { uint16 temp; temp = bswap16(length - 2); stream->write(&temp, 2); temp = bswap16(vm->nodeId); stream->write(&temp, 2); stream->write(data, length); stream->flush(); // push to other nodes for (size_t i = 0; i < marxBot.modules.size(); ++i) { Enki::AsebaMarxbot::Module& module = *(marxBot.modules[i]); if (&(module.vm) != vm) { module.events.push_back(Enki::AsebaMarxbot::Event(vm->nodeId, data, length)); AsebaProcessIncomingEvents(&(module.vm)); } } } catch (Dashel::DashelException e) { std::cerr << "Cannot write to socket: " << stream->getFailReason() << std::endl; } }
virtual void connectionCreated(Dashel::Stream *stream) { std::string targetName = stream->getTargetName(); if (targetName.substr(0, targetName.find_first_of(':')) == "tcp") { std::cerr << this << " : New client connected." << std::endl; if (this->stream) { closeStream(this->stream); std::cerr << this << " : Disconnected old client." << std::endl; } this->stream = stream; } }
extern "C" void AsebaSendBuffer(AsebaVMState *vm, const uint8* data, uint16 length) { Dashel::Stream* stream = node.stream; if (stream) { try { uint16 temp; temp = bswap16(length - 2); stream->write(&temp, 2); temp = bswap16(vm->nodeId); stream->write(&temp, 2); stream->write(data, length); stream->flush(); } catch (Dashel::DashelException e) { std::cerr << "Cannot write to socket: " << stream->getFailReason() << std::endl; } } }