static Resolver* create(ScriptState* scriptState, CryptoResultImpl* result)
 {
     ASSERT(scriptState->contextIsValid());
     Resolver* resolver = new Resolver(scriptState, result);
     resolver->suspendIfNeeded();
     resolver->keepAliveWhilePending();
     return resolver;
 }
예제 #2
0
int main(int, char** )
{
  Deferred<int> d;
  Promise<int> p;
  Resolver<int> r;

  p = d.promise();
  r = d.resolver();

  p.done([] (int a) {
      std::cout << "A = " << a << std::endl;
    });
  r.resolve(42);
  return 0;
}
예제 #3
0
    static void resolve(uv_loop_t* loop, const std::string& host, int port,
                        void* data, Callback cb, struct addrinfo* hints = NULL) {
        Resolver* resolver = new Resolver(host, port, data, cb);

        std::ostringstream ss;
        ss << port;

        int rc = uv_getaddrinfo(loop, &resolver->req_, on_resolve, host.c_str(),
                                ss.str().c_str(), hints);

        if (rc != 0) {
            resolver->status_ = FAILED_BAD_PARAM;
            resolver->cb_(resolver);
            delete resolver;
        }
    }
예제 #4
0
void DependencyTree::resolve()
{
  resolvedList.clear();
  resolvingList.clear();
  ThreadPool threadPool;
  for (;;)
  {
    Resolvers resolvers;
    for (auto &&target : tree)
      resolve(target.first, resolvers);

    if (resolvers.empty() && resolvingList.empty() && threadPool.empty())
      return;

    std::string target;
    Resolver *resolver;
    time_t newestModificationTime;

    for (auto &&r : resolvers)
    {
      std::tie(target, resolver, newestModificationTime) = r;
      if (!isFileExist(target))
      {
        resolvingList.insert(target);
        threadPool.addJob([resolver]() { resolver->exec(); },
                          [this, target]() {
                            resolvingList.erase(target);
                            resolvedList.insert(target);
                          });
        continue;
      }
      auto time = getFileModification(target);
      if (time < newestModificationTime)
      {
        resolvingList.insert(target);
        threadPool.addJob([resolver]() { resolver->exec(); },
                          [this, target]() {
                            resolvingList.erase(target);
                            resolvedList.insert(target);
                          });
      }
      else
        resolvedList.insert(target);
    }
    threadPool.waitForOne();
  }
}
예제 #5
0
    static void on_resolve(uv_getaddrinfo_t* req, int status,
                           struct addrinfo* res) {
        Resolver* resolver = static_cast<Resolver*>(req->data);

        if (status != 0) {
            resolver->status_ = FAILED_UNABLE_TO_RESOLVE;
        } else if (!resolver->address_.init(res->ai_addr)) {
            resolver->status_ = FAILED_UNSUPPORTED_ADDRESS_FAMILY;
        } else {
            resolver->status_ = SUCCESS;
        }

        resolver->cb_(resolver);

        delete resolver;
        uv_freeaddrinfo(res);
    }
예제 #6
0
/**
 * egg_controller_resolvev:
 * @path: A #GStrv containing the path portions
 *
 * Resolves a controller for the path provided.
 *
 * Return value: An #EggController
 */
EggController*
egg_controller_resolvev (gchar **path)
{
	EggController *controller = NULL;
	GList         *list;
	Resolver      *resolver;

	g_return_val_if_fail (path != NULL, NULL);

	for (list = resolvers; list; list = list->next) {
		if (NULL != (resolver = list->data)) {
			controller = resolver->callback (path, resolver->data);
			if (controller)
				break;
		}
	}

	return controller;
}
예제 #7
0
void Resolver::ares_host_callback(void *arg, int status, int timeouts, struct hostent* host)
{
    Resolver* ch = reinterpret_cast<Resolver*>(arg);
    if(!host || status != ARES_SUCCESS)
    {
        TORNADO_LOG_WARN("Failed to lookup %s\n", ares_strerror(status));
        ch->run_callback(-1, NULL);
        return;
    }
    TORNADO_LOG_DEBUG("Found address name %s", host->h_name);
    Hosts hosts;
    char ip[INET6_ADDRSTRLEN];
    for (int i = 0; host->h_addr_list[i]; ++i) 
    {
        inet_ntop(host->h_addrtype, host->h_addr_list[i], ip, sizeof(ip));
        std::string strIP(ip);
        hosts.push_back( std::move(strIP) );
    }
    ch->run_callback(0, &hosts);
}
예제 #8
0
Handle<Value> Resolver::New(const Arguments& args) {
	HandleScope scope;

	if (!args.IsConstructCall()) {
		return ThrowException(Exception::TypeError(
				String::New("Use the new operator to create instances of this object."))
		);
	}

	if (args.Length() < 2) {
		return ThrowException(Exception::TypeError(
				String::New("First argument must be a string, second a boolean")));
	}

	Resolver* obj = new Resolver();
	String::Utf8Value name(args[0]->ToString());
	obj->name_ = std::string(*name);
	obj->resolver_ = new O3SResolver(obj->name_, false);

	if (obj->resolver_) {
		while (!obj->resolver_->getDico()) {
			std::cout<<"Waiting for dico..."<<std::endl;
			Poco::Thread::sleep(1000);
		}
		obj->Wrap(args.This());

		Local<Function> cb = Local<Function>::Cast(args[2]);
		const unsigned argc = 1;
		Local<Value> argv[argc] = { args.This() };
		cb->Call(Context::GetCurrent()->Global(), argc, argv);

		return args.This();
	}

	return Undefined();
}
예제 #9
0
bool Query::xchg_(Resolver& res, uint16_t id)
{
  auto tries = 3;

  while (tries) {

    a_ = res.xchg(q_);

    if (!size(a_)) {
      bogus_or_indeterminate_ = true;
      LOG(WARNING) << "no reply from nameserver";
      return false;
    }

    if (size(a_) < min_udp_sz()) {
      bogus_or_indeterminate_ = true;
      LOG(WARNING) << "packet too small";
      return false;
    }

    if (a_.id() == id)
      break;

    LOG(WARNING) << "packet out of order; ids don't match, got " << a_.id()
                 << " expecting " << id;
    --tries;
  }

  if (tries)
    return true;

  bogus_or_indeterminate_ = true;
  LOG(WARNING) << "no tries left, giving up";

  return false;
}
예제 #10
0
int
main(int argc, char** argv)
{
  std::string dbName;
  std::string dbUser;
  std::string dbPassword;
  std::chrono::seconds queryInterval(60);
  int opt;
  while ((opt = getopt(argc, argv, "hD:u:p:i:")) != -1) {
    switch (opt) {
    case 'h':
      usage();
      return 0;
    case 'D':
      dbName = optarg;
      break;
    case 'u':
      dbUser = optarg;
      break;
    case 'p':
      dbPassword = optarg;
      break;
    case 'i':
      queryInterval = std::chrono::seconds(atoi(optarg));
      break;
    default:
      usage();
      return 1;
    }
  }
  if (dbName.empty() || dbUser.empty() || dbPassword.empty() ||
      queryInterval < std::chrono::seconds(5)) {
    usage();
    return 1;
  }

  mysqlpp::Connection conn(dbName.c_str(), nullptr, dbUser.c_str(), dbPassword.c_str());
  if (!conn) {
    std::cerr << "cannot open database connection" << std::endl;
    return 2;
  }
  MySqlDb db(conn);

  Resolver resolver;

  executeAtInterval(queryInterval, [&db, &resolver](){
    std::vector<Domain> domains = db.listDomains();
    std::for_each(domains.begin(), domains.end(), [&db, &resolver](const Domain& domain){
      if (!domain.enabled)
        return;
      std::string hostname = makeRandomHost(domain.name);
      ProbeLog pl;
      pl.timestamp = std::chrono::system_clock::now();
      pl.domainId = domain.id;
      pl.queryTime = std::chrono::duration_cast<std::chrono::milliseconds>(
                     resolver.benchDomain(hostname));
      db.insertProbeLog(pl);
      std::cerr << std::chrono::system_clock::to_time_t(pl.timestamp)
                << " " << hostname
                << " " << pl.queryTime.count() << std::endl;
    });
  });
  return 0;
}
예제 #11
0
void Resolver :: run(void* arg)
{
    Resolver* ptr = (Resolver*) arg;
    ptr->DNS_manager();
}
예제 #12
0
파일: data.cpp 프로젝트: zeil/Synca
StrPair loadContent(const StrPair& url)
{
    const int MAX_BUF_SIZE = 1024*1000;
    const int READ_BUF_SIZE = 1024*16;

    auto&& host = url.first;
    auto&& path = url.second;
    JLOG("loading url: " << host << ", " << path);
    Resolver r;
    Resolver::EndPoints ends = r.resolve(host, 80);
    VERIFY(ends != Resolver::EndPoints(), "Cannot resolve hostname: " + host);
    Socket s;
    s.connect(*ends);
    Str req =
        "GET " + path + " HTTP/1.1" EOL
        "Host: " + host + EOL EOL;
    s.write(req);
    Str resp;
    Str buf;
    size_t header = std::string::npos;
    while (header == std::string::npos)
    {
        VERIFY(resp.size() < MAX_BUF_SIZE, "Header is too large");
        buf.resize(READ_BUF_SIZE);
        s.partialRead(buf);
        VERIFY(!buf.empty(), "Empty buffer on partial read");
        resp += buf;
        header = resp.find(EOL EOL);
    }
    Str head = resp.substr(0, header);
    header += 4; // add 2 EOLs
    static const regex eStatus("^http/1\\.[01] ([\\d]{3})", regex::icase);
    smatch whatStatus;
    bool result = regex_search(head, whatStatus, eStatus);
    VERIFY(result, "Invalid http status header");
    Str status = whatStatus[1];
    static const std::unordered_set<Str> allowedStatuses = {"200", "301", "302", "303"};
    VERIFY(allowedStatuses.count(status), "Unexpected status: " + status);
    static const regex e("content-length: *(\\d+)", regex::icase);
    smatch what;
    result = regex_search(head, what, e);
    if (result)
    {
        int len = std::atoi(Str(what[1]).c_str());
        VERIFY(len > 0 && len < MAX_BUF_SIZE, "Content length: invalid value");
        buf.resize(len - (resp.size() - header));
        s.read(buf);
        resp += buf;
    }
    else
    {
        static const regex et("transfer-encoding: *chunked", regex::icase);
        // read chunks
        result = regex_search(head, et);
        VERIFY(result, "Either content-length or transfer-encoding must be present in header");
        while (resp.find(EOL EOL, resp.size() - 4) == std::string::npos)
        {
            VERIFY(resp.size() < MAX_BUF_SIZE, "Response is too large");
            buf.resize(READ_BUF_SIZE);
            s.partialRead(buf);
            VERIFY(!buf.empty(), "Empty buffer on partial read");
            resp += buf;
        }
    }
    Str body = resp.substr(header);
    return {host, body};
}
예제 #13
0
void Resolver::sock_state_callback(void *data, int socket, int read, int write)
{
    TORNADO_LOG_DEBUG("Change state fd %d read:%d write:%d", socket, read, write);
    Resolver* ch = reinterpret_cast<Resolver*>(data);
    ch->sock_state_callback(socket, read, write);
}
//==========================
// StrSymPool
StrSymPool::StrSymPool(const Resolver& pResolver, StrSymPool::size_type pSize)
  : m_pResolver(pResolver.clone()), m_Table(pSize) {
}