<commit_before>#include "blinkyBlocksVM.h"
#include "blinkyBlocksBlock.h"
#include "blinkyBlocksBlockCode.h"
#include <boost/asio/io_service.hpp>
#include <sys/wait.h>
#include <stdio.h>
#include <boost/bind.hpp>
#include "trace.h"
#include <stdexcept>
#include <string.h>
#include "events.h"
#include "blinkyBlocksEvents.h"
#include "openglViewer.h"

#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

using namespace boost;
using asio::ip::tcp;

namespace BlinkyBlocks {

boost::asio::io_service *BlinkyBlocksVM::ios = NULL;
boost::interprocess::interprocess_mutex BlinkyBlocksVM::mutex_ios;
tcp::acceptor *BlinkyBlocksVM::acceptor = NULL;
string BlinkyBlocksVM::vmPath;
string BlinkyBlocksVM::programPath;
bool BlinkyBlocksVM::debugging = false;

BlinkyBlocksVM::BlinkyBlocksVM(BlinkyBlocksBlock* bb){
   int ret;
   boost::system::error_code error;
   stringstream vmLogFile;

	assert(ios != NULL && acceptor != NULL);
	hostBlock = bb;
   vmLogFile << "VM" << hostBlock->blockId << ".log";
   
	OUTPUT << "VM "<< hostBlock->blockId << " constructor" << endl;
	// Start the VM
	pid = 0;
   mutex_ios.lock();
	ios->notify_fork(boost::asio::io_service::fork_prepare);
	pid = fork();
	if(pid < 0) {ERRPUT << "Error when starting the VM" << endl;}
    if(pid == 0) {
		ios->notify_fork(boost::asio::io_service::fork_child);
      mutex_ios.unlock();
      acceptor->close();
      getWorld()->closeAllSockets();
#ifdef LOGFILE
      log_file.close();
#endif
		int fd = open(vmLogFile.str().c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
		dup2(fd, 1);
		dup2(fd, 2);
		close(fd);
		if (debugging) {
			//./meld -f  /home/ubuntu/Bureau/CMU/meld/examples/ends.m -c sl -D SIM
			char* cmd[] = {(char*)vmPath.c_str(), (char*)"-f", (char*)programPath.c_str(), (char*)"-c", (char*) "sl", (char*) "-D", (char*) "SIM", NULL };
			ret = execv(vmPath.c_str(), const_cast<char**>(cmd));
		} else {
			//./meld -f  /home/ubuntu/Bureau/CMU/meld/examples/ends.m -c sl
			char* cmd[] = {(char*)vmPath.c_str(), (char*)"-f", (char*)programPath.c_str(), (char*)"-c", (char*) "sl", NULL };
			ret = execv(vmPath.c_str(), const_cast<char**>(cmd));
		}
      if(ret == -1) {
         cerr << "Error: VM executable, " << vmPath.c_str()  << ", does not exist or is not executable" << endl;
         exit(EXIT_FAILURE);
      }
	}
	ios->notify_fork(boost::asio::io_service::fork_parent);
   mutex_ios.unlock();
	socket = boost::shared_ptr<tcp::socket>(new tcp::socket(*ios));
   if (hostBlock->blockId == 1) {
      bool connected = false;
      
      acceptor->async_accept(*(socket.get()), boost::bind(&BlinkyBlocksVM::asyncAcceptHandler, this, error , &connected));
      while(!connected &&  (pid != waitpid(pid, NULL, WNOHANG)));
      if(!connected) {
         ifstream file (vmLogFile.str().c_str());
         string line;
         cerr << "VisibleSim error: unable to connect to the VM" << endl;<commit_msg>Gets error messages working for non-debugging mode<commit_after>#include "blinkyBlocksVM.h"
#include "blinkyBlocksBlock.h"
#include "blinkyBlocksBlockCode.h"
#include <boost/asio/io_service.hpp>
#include <sys/wait.h>
#include <stdio.h>
#include <boost/bind.hpp>
#include "trace.h"
#include <stdexcept>
#include <string.h>
#include "events.h"
#include "blinkyBlocksEvents.h"
#include "openglViewer.h"

#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

using namespace boost;
using asio::ip::tcp;

namespace BlinkyBlocks {

boost::asio::io_service *BlinkyBlocksVM::ios = NULL;
boost::interprocess::interprocess_mutex BlinkyBlocksVM::mutex_ios;
tcp::acceptor *BlinkyBlocksVM::acceptor = NULL;
string BlinkyBlocksVM::vmPath;
string BlinkyBlocksVM::programPath;
bool BlinkyBlocksVM::debugging = false;

BlinkyBlocksVM::BlinkyBlocksVM(BlinkyBlocksBlock* bb){
   int ret;
   boost::system::error_code error;
   stringstream vmLogFile;

	assert(ios != NULL && acceptor != NULL);
	hostBlock = bb;
   vmLogFile << "VM" << hostBlock->blockId << ".log";
   
	OUTPUT << "VM "<< hostBlock->blockId << " constructor" << endl;
	// Start the VM
	pid = 0;
   mutex_ios.lock();
	ios->notify_fork(boost::asio::io_service::fork_prepare);
	pid = fork();
	if(pid < 0) {ERRPUT << "Error when starting the VM" << endl;}
    if(pid == 0) {
		ios->notify_fork(boost::asio::io_service::fork_child);
      mutex_ios.unlock();
      acceptor->close();
      getWorld()->closeAllSockets();
#ifdef LOGFILE
      log_file.close();
#endif
		int fd = open(vmLogFile.str().c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
		dup2(fd, 1);
		dup2(fd, 2);
		close(fd);
		if (debugging) {
			//./meld -f  /home/ubuntu/Bureau/CMU/meld/examples/ends.m -c sl -D SIM
			char* cmd[] = {(char*)vmPath.c_str(), (char*)"-f", (char*)programPath.c_str(), (char*)"-c", (char*) "sl", (char*) "-D", (char*) "SIM", NULL };
			ret = execv(vmPath.c_str(), const_cast<char**>(cmd));
		} else {
			//./meld -f  /home/ubuntu/Bureau/CMU/meld/examples/ends.m -c sl
			char* cmd[] = {(char*)vmPath.c_str(), (char*)"-f", (char*)programPath.c_str(), (char*)"-c", (char*) "sl", NULL };
			ret = execv(vmPath.c_str(), const_cast<char**>(cmd));
		}
      if(ret == -1) {
         cerr << "Error: VM executable, " << vmPath.c_str()  << ", does not exist or is not executable" << endl;
         exit(EXIT_FAILURE);
      }
	}
	ios->notify_fork(boost::asio::io_service::fork_parent);
   mutex_ios.unlock();
	socket = boost::shared_ptr<tcp::socket>(new tcp::socket(*ios));
   if (hostBlock->blockId == 1) {
      bool connected = false;
      
      acceptor->async_accept(*(socket.get()), boost::bind(&BlinkyBlocksVM::asyncAcceptHandler, this, error , &connected));
      while(!connected &&  (pid != waitpid(pid, NULL, WNOHANG))) {
         	if (!BlinkyBlocksVM::isInDebuggingMode()) { // In debugging mode the scheduler thread is looking for connections
               checkForReceivedVMCommands(); // it is actually check for connection
               usleep(10000);
            }
      }
      if(!connected) {
         ifstream file (vmLogFile.str().c_str());
         string line;
         cerr << "VisibleSim error: unable to connect to the VM" << endl;
{'fraction_non_alphanumeric': 0.18098307816277195, 'fraction_numerical': 0.0037066881547139405, 'mean_word_length': 4.171666666666667, 'pattern_counts': {'":': 0, '<': 89, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6716341', 'n_tokens_mistral': 2351, 'n_tokens_neox': 2209, 'n_words': 453}