Permalink
Browse files

Started query agent, mainly just groundwork thus far

Use -Xagent.port=<number> to tell the agent to startup and listen on
that TCP port. It will speak BERT.
  • Loading branch information...
1 parent 2c5730b commit ff881b87ca0ee520de2927259c9426d55b7c95ac Evan Phoenix committed Oct 29, 2009
Showing with 111 additions and 2 deletions.
  1. +58 −0 vm/agent.cpp
  2. +26 −0 vm/agent.hpp
  3. +12 −0 vm/configuration.hpp
  4. +12 −2 vm/environment.cpp
  5. +3 −0 vm/environment.hpp
View
@@ -0,0 +1,58 @@
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+
+#include "util/bert.hpp"
+
+#include "agent.hpp"
+#include "exception.hpp"
+
+namespace rubinius {
+ void QueryAgent::bind() {
+ std::cout << "binding to " << port_ << "\n";
+
+ server_fd_ = ::socket(AF_INET, SOCK_STREAM, 0);
+ struct sockaddr_in sin = {0};
+ sin.sin_family = AF_INET;
+ sin.sin_addr.s_addr = INADDR_ANY;
+ sin.sin_port = htons(port_);
+
+ ::bind(server_fd_, (struct sockaddr*)&sin, sizeof(sin));
+ if(verbose_) {
+ std::cerr << "[QA: Bound to port " << ntohs(sin.sin_port) << "]\n";
+ }
+ }
+
+ void QueryAgent::perform() {
+ bind();
+
+ while(1) {
+ listen(server_fd_, cBackLog);
+
+ // now accept an incoming connection
+ struct sockaddr_storage their_addr;
+ socklen_t addr_size = sizeof(their_addr);
+ int client = accept(server_fd_, (struct sockaddr *)&their_addr, &addr_size);
+
+ struct sockaddr_in* sin = (struct sockaddr_in*)&their_addr;
+
+ if(verbose_) {
+ std::cerr << "[QA: Connection from " << inet_ntoa(sin->sin_addr)
+ << ":" << sin->sin_port << "]\n";
+ }
+
+ bert::IOWriter writer(client);
+ bert::Encoder<bert::IOWriter> encoder(writer);
+ encoder.write_version();
+
+ encoder.write_tuple(2);
+ encoder.write_atom("hello_query_agent");
+ encoder.write_binary(RBX_HOST);
+
+ close(client);
+ }
+ }
+}
View
@@ -0,0 +1,26 @@
+#include "util/thread.hpp"
+
+namespace rubinius {
+ class QueryAgent : public thread::Thread {
+ int port_;
+ int server_fd_;
+ bool verbose_;
+
+ const static int cBackLog = 10;
+
+ public:
+ QueryAgent(int port)
+ : Thread()
+ , port_(port)
+ , server_fd_(0)
+ , verbose_(false)
+ {}
+
+ void set_verbose() {
+ verbose_ = true;
+ }
+
+ void bind();
+ virtual void perform();
+ };
+}
View
@@ -27,6 +27,10 @@ namespace rubinius {
config::BoolSet jit_defaults;
+ // Query Agent
+ config::Integer qa_port;
+ config::Bool qa_verbose;
+
// Debug
config::Bool gil_debug;
config::Integer print_config;
@@ -60,6 +64,8 @@ namespace rubinius {
, jit_inline_blocks(this, "jit.inline.blocks")
, jit_force_off(this, "int")
, jit_defaults(this, "J")
+ , qa_port(this, "agent.port")
+ , qa_verbose(this, "agent.verbose")
, gil_debug(this, "vm.gil.debug")
, print_config(this, "config.print")
, ic_stats(this, "ic.stats")
@@ -120,6 +126,12 @@ namespace rubinius {
ic_stats.set_description(
"Print out stats about the InlineCaches before exiting");
+
+ qa_port.set_description(
+ "The TCP port for the query agent to listen on");
+
+ qa_verbose.set_description(
+ "Whether or not the query agent should print out status to stderr");
}
};
}
View
@@ -26,20 +26,23 @@
#include "native_thread.hpp"
#include "inline_cache.hpp"
+#include "agent.hpp"
+
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
namespace rubinius {
- Environment::Environment() {
+ Environment::Environment()
+ : agent(0)
+ {
#ifdef ENABLE_LLVM
assert(llvm::llvm_start_multithreaded() && "llvm doesn't support threading!");
#endif
shared = new SharedState(config, config_parser);
-
state = shared->new_vm();
}
@@ -147,6 +150,8 @@ namespace rubinius {
}
void Environment::boot_vm() {
+ if(config.qa_port > 0) start_agent(config.qa_port);
+
// Respect -Xint
if(config.jit_force_off) {
config.jit_enabled.set("no");
@@ -220,4 +225,9 @@ namespace rubinius {
return 0;
}
+ void Environment::start_agent(int port) {
+ agent = new QueryAgent(port);
+ if(config.qa_verbose) agent->set_verbose();
+ agent->run();
+ }
}
View
@@ -11,11 +11,13 @@
namespace rubinius {
class ConfigParser;
+ class QueryAgent;
class Environment {
public:
SharedState* shared;
VM* state;
+ QueryAgent* agent;
ConfigParser config_parser;
Configuration config;
@@ -35,6 +37,7 @@ namespace rubinius {
void halt();
int exit_code();
void start_signals();
+ void start_agent(int port);
};
}

0 comments on commit ff881b8

Please sign in to comment.