Ejemplo n.º 1
0
static PyObject *mhlearn(PyObject *self, PyObject *args)
{
    char *input;

    if (!PyArg_ParseTuple(args, "s", &input))
	return NULL;

    megahal_learn_no_reply(input, 1);

    return Py_BuildValue("");
}
Ejemplo n.º 2
0
int main() {
  // std::cout << "Hello, world!" << std::endl;
  if (signal(SIGINT, sig_handler) == SIG_ERR)
    std::cerr << "Couldn't intercept signal." << std::endl;

  unsigned int msg_counter = 0;

  init_megahal();

  zmq::context_t context(1);
  zmq::socket_t socket(context, ZMQ_REP);

  socket.bind("tcp://*:5555");

  while (true) {
    zmq::message_t req;
    socket.recv(&req);
    std::cout << "Received message" << std::endl;

    if (req.size() < 3) {
      // No command, reject
      socket.send("FAIL", 4);
    } else if (memcmp(req.data(), "REP", 3) == 0) {
      // Response requested
      std::string data{static_cast<char*>(req.data()) + 3, req.size() - 3};
      char * resp = megahal_do_reply((char*)data.c_str(), 0);
      socket.send(resp, strlen(resp));
    } else if (memcmp(req.data(), "LRN", 3) == 0) {
      // No response requested, just learn
      std::string data{static_cast<char*>(req.data()) + 3, req.size() - 3};
      megahal_learn_no_reply((char*)data.c_str(), 0);
      socket.send("OK", 2);
    } else {
      // Invalid command, reject
      socket.send("FAIL", 4);
    }

    std::cout << "Reply sent" << std::endl;

    if (++msg_counter % 10 == 0)
      megahal_save();
  }

  megahal_cleanup();

  return 0;
}
Ejemplo n.º 3
0
static int begin_request_handler(struct mg_connection *conn) {

    const struct mg_request_info *request_info = mg_get_request_info(conn);
    char content[1024];
    int content_length = 0;

    if (strstr(request_info->uri, "/ask/") != 0) {
        const char *question = request_info->uri + 5;
        printf("Question: %s\n", question);
        pthread_mutex_lock(&mhlock);
        char *out = megahal_do_reply((char*)question, 0);
        pthread_mutex_unlock(&mhlock);

        printf("Reply: %s\n", out);
        strncpy (content, out, sizeof(content));
        content_length = strlen(content);

    } else if (strcmp(request_info->uri, "/learn/") == 0) {
        const char *question = request_info->uri + 7;
        printf("Learn: %s\n", question);

        pthread_mutex_lock(&mhlock);
        megahal_learn_no_reply((char*)question, 0);
        pthread_mutex_unlock(&mhlock);

        content_length = snprintf(content, sizeof(content), "learnt");
    } else {
        content_length = snprintf(content, sizeof(content), "No comprende");
    }

    mg_printf(conn,
            "HTTP/1.1 200 OK\r\n"
            "Content-Type: text/plain\r\n"
            "Content-Length: %d\r\n"        // Always set Content-Length
            "\r\n"
            "%s",
            content_length, content);

    return 1;
}