static PyObject *mhdoreply(PyObject *self, PyObject *args) { char *input; char *output = NULL; if (!PyArg_ParseTuple(args, "s", &input)) return NULL; output = megahal_do_reply(input, 1); return Py_BuildValue("s", output); }
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; }
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; }
/* * Function: Main * * Purpose: Initialise everything, and then do an infinite loop. In * the loop, we read the user's input and reply to it, and * do some housekeeping task such as responding to special * commands. */ int main(int argc, char **argv) { char *input=NULL; char *output=NULL; char *my_directory = NULL; int directory_set; int c, option_index = 0; directory_set = 0; while(1) { if((c = getopt_long(argc, argv, "hpwbd:", long_options, &option_index)) == -1) break; switch(c) { case 'p': megahal_setnoprompt(); break; case 'w': megahal_setnowrap(); break; case 'd': megahal_setdirectory (optarg); directory_set = 1; break; case 'b': megahal_setnobanner(); break; case 'h': usage(); return 0; } } if (directory_set == 0) { if ((my_directory = getenv("MEGAHAL_DIR"))) { megahal_setdirectory (my_directory); directory_set = 1; } else { struct stat dir_stat; my_directory = getenv ("HOME"); if (my_directory == NULL) { fprintf (stderr, "Cannot find your home directory.\n"); exit (1); } my_directory = malloc (12 + strlen (my_directory)); strcpy (my_directory, getenv ("HOME")); strcat (my_directory, "/.megahal"); if (stat (my_directory, &dir_stat)) { if (errno == ENOENT) { directory_set = mkdir (my_directory, S_IRWXU); if (directory_set != 0) { fprintf (stderr, "Could not create %s.\n", my_directory); exit (1); } megahal_setdirectory (my_directory); directory_set = 1; } } else { if (S_ISDIR(dir_stat.st_mode)) { megahal_setdirectory (my_directory); directory_set = 1; } else { fprintf (stderr, "Could not use megahal directory %s.\n", my_directory); exit (1); } } } } /* * Do some initialisation */ megahal_initialize(); /* * Create a dictionary which will be used to hold the segmented * version of the user's input. */ /* * Load the default MegaHAL personality. */ output = megahal_initial_greeting(); megahal_output(output); /* * Read input, formulate a reply and display it as output */ while(1) { input = megahal_input("> "); /* * If the input was a command, then execute it */ if (megahal_command(input) != 0) continue; output = megahal_do_reply(input, 1); megahal_output(output); } megahal_cleanup(); exit(0); }