Ejemplo n.º 1
0
 static void onEventConnected(RemoteObject* ro, qi::Future<SignalLink> fut, qi::Promise<SignalLink> prom, SignalLink id) {
   if (fut.hasError()) {
     prom.setError(fut.error());
     return;
   }
   prom.setValue(id);
 }
Ejemplo n.º 2
0
 void ServiceDirectoryClient::onSDEventConnected(qi::Future<SignalLink> future,
   qi::Promise<void> promise, bool isAdd)
 {
   if (promise.future().isFinished()) {
     return;
   }
   if (future.hasError())
   {
     qi::Future<void> fdc = onSocketDisconnected(future.error());
     fdc.connect(&qi::Promise<void>::setError, promise, future.error());
     return;
   }
   bool ready = false;
   {
     boost::mutex::scoped_lock lock(_mutex);
     if (isAdd)
       _addSignalLink = future.value();
     else
       _removeSignalLink = future.value();
     ready = _addSignalLink && _removeSignalLink;
   }
   if (ready)
   {
     promise.setValue(0);
     connected();
   }
 }
Ejemplo n.º 3
0
 void serviceReady(qi::Future<void> fut, qi::Promise<unsigned int> result, unsigned int idx) {
   if (fut.hasError()) {
     result.setError(fut.error());
     return;
   }
   result.setValue(idx);
 }
Ejemplo n.º 4
0
static void call_from_java_cont(qi::Future<qi::AnyReference> ret,
    qi::Promise<qi::AnyValue> promise)
{
  if (ret.hasError())
    promise.setError(ret.error());
  else
    promise.setValue(qi::AnyValue(ret.value(), false, true));
}
Ejemplo n.º 5
0
 void RemoteObject::onMetaObject(qi::Future<qi::MetaObject> fut, qi::Promise<void> prom) {
   if (fut.hasError()) {
     qiLogVerbose() << "MetaObject error: " << fut.error();
     prom.setError(fut.error());
     return;
   }
   qiLogVerbose() << "Fetched metaobject";
   setMetaObject(fut.value());
   prom.setValue(0);
 }
void slowDisconnect(qi::Promise<void> ready, qi::Promise<void> done, qi::Future<void> wait, qi::AnyObject obj, boost::shared_ptr<qi::SignalLink> link)
{
  ready.setValue(0);
  wait.wait();
  try {
    obj.disconnect(*link);
  }
  catch (...) {}
  done.setValue(0);
}
Ejemplo n.º 7
0
  void ObjectRegistrar::onFutureFinished(qi::Future<unsigned int> fut, long id, qi::Promise<unsigned int> result)
  {
    if (fut.hasError()) {
      result.setError(fut.error());
      return;
    }
    qi::ServiceInfo              si;
    RegisterServiceMap::iterator it;

    {
      boost::mutex::scoped_lock sl(_registerServiceRequestMutex);
      it = _registerServiceRequest.find(id);
      if (it != _registerServiceRequest.end())
        si = it->second.second;
      if (fut.hasError()) {
        _registerServiceRequest.erase(id);
        result.setError(fut.error());
        return;
      }
    }
    unsigned int idx = fut.value();
    si.setServiceId(idx);
    {
      boost::mutex::scoped_lock sl(_servicesMutex);
      BoundService bs;
      bs.id          = idx;
      bs.object      = it->second.first;
      bs.serviceInfo = si;
      bs.name        = si.name();
      BoundServiceMap::iterator it;
      it = _services.find(idx);
      if (it != _services.end()) {
        qiLogError() << "A service is already registered with that id:" << idx;
        result.setError("Service already registered.");
        return;
      }
      _services[idx] = bs;
      //todo register the object on the server (find a better way)
      Server::addObject(idx, bs.object);
    }

    {
      boost::mutex::scoped_lock sl(_serviceNameToIndexMutex);
      _serviceNameToIndex[si.name()] = idx;
    }
    {
      boost::mutex::scoped_lock sl(_registerServiceRequestMutex);
      _registerServiceRequest.erase(it);
    }

    // ack the Service directory to tell that we are ready
    qi::Future<void> fut2 = _sdClient->serviceReady(idx);
    fut2.connect(boost::bind(&serviceReady, _1, result, idx));
  }
Ejemplo n.º 8
0
  void SessionPrivate::listenStandaloneCont(qi::Promise<void> p, qi::Future<void> f)
  {
    if (f.hasError())
      p.setError(f.error());
    else
    {
      _sdClient.setServiceDirectory(boost::static_pointer_cast<ServiceBoundObject>(_sd._serviceBoundObject)->object());
      // _sdClient will trigger its connected, which will trigger our connected

      p.setValue(0);
    }
  }
Ejemplo n.º 9
0
 void log(const qi::LogLevel,
          const qi::Clock::time_point,
          const qi::SystemClock::time_point,
          const char*,
          const char*,
          const char*,
          const char*,
          int) {
   start.future().wait();
   if (++count == MAX)
     finished.setValue(0);
 }
Ejemplo n.º 10
0
 void operator()(int newValue)
 {
   if (newValue == targetValue)
   {
     running.setValue(true);
   }
 }
Ejemplo n.º 11
0
void onFire2(const int& pl)
{
  if (pl)
    oclient1.async<void>("onFire1", pl-1);
  else
    payload.setValue(true);
}
Ejemplo n.º 12
0
  void SessionPrivate::onServiceTrackingCancelled(qi::Promise<void> promise,
      boost::shared_ptr<qi::Atomic<int> > link)
  {
    SignalLink link2 = link->swap(0);

    if (link2 == 0)
      return;

    _sdClient.serviceAdded.disconnect(link2);
    promise.setCanceled();
  }
Ejemplo n.º 13
0
 int f(int end, qi::Promise<void> finished)
 {
   int startval = prop.get();
   EXPECT_FALSE(calling);
   calling = true;
   qi::os::msleep(5);
   EXPECT_TRUE(calling);
   calling = false;
   EXPECT_EQ(startval, prop.get());
   if (++callcount == end + 1)
     finished.setValue(0);
   return 42;
 }
Ejemplo n.º 14
0
  void SessionPrivate::addSdSocketToCache(Future<void> f, const qi::Url& url,
                                          qi::Promise<void> p)
  {
    qiLogDebug() << "addSocketToCache processing";
    if (f.hasError())
    {
      qiLogDebug() << "addSdSocketToCache: connect reported failure";
      _serviceHandler.removeService("ServiceDirectory");
      p.setError(f.error());
      return;
    }

    // Allow the SD process to use the existing socket to talk to our services
    _serverObject.registerSocket(_sdClient.socket());

    /* Allow reusing the SD socket for communicating with services.
     * To do this, we must add it to our socket cache, and for this we need
     * to know the sd machineId
     */
     std::string mid;
     try
     {
       mid = _sdClient.machineId();
     }
     catch (const std::exception& e)
     { // Provide a nice message for backward compatibility
       qiLogVerbose() << e.what();
       qiLogWarning() << "Failed to obtain machineId, connection to service directory will not be reused for other services.";
       p.setValue(0);
       return;
     }
     TransportSocketPtr s = _sdClient.socket();
     qiLogVerbose() << "Inserting sd to cache for " << mid <<" " << url.str() << std::endl;
     _socketsCache.insert(mid, s->remoteEndpoint(), s);
     p.setValue(0);
  }
Ejemplo n.º 15
0
TEST(QiApplicationSession, defaultConnect)
{
  ASSERT_FALSE(_app->session()->isConnected());
  _app->start();
  ASSERT_TRUE(_app->session()->isConnected());

  ASSERT_EQ(_url, _app->session()->url());

  ASSERT_FALSE(_stopped);
  _sd.close();
  _sync.future().wait();
  ASSERT_TRUE(_stopped);

  EXPECT_THROW(_app->session()->connect("ftp://invalidurl:42"),
               qi::FutureUserException);
  ASSERT_FALSE(_app->session()->isConnected());
}
Ejemplo n.º 16
0
  void SessionPrivate::onTrackedServiceAdded(const std::string& actual,
      const std::string& expected,
      qi::Promise<void> promise,
      boost::shared_ptr<qi::Atomic<int> > link)
  {
    if (actual != expected)
      return;

    // only do it once in case of multiple calls
    SignalLink link2 = link->swap(0);

    if (link2 == 0)
      return;

    _sdClient.serviceAdded.disconnect(link2);

    promise.setValue(0);
  }
Ejemplo n.º 17
0
TEST(Test, Recurse)
{
  payload = qi::Promise<bool>();
  TestSessionPair       p1;
  TestSessionPair       p2(p1);
  qi::DynamicObjectBuilder     ob1, ob2;
  ob1.advertiseMethod("onFire1", &onFire1);
  ob2.advertiseMethod("onFire2", &onFire2);
  qi::AnyObject    oserver1(ob1.object()), oserver2(ob2.object());
  unsigned int           nbServices = TestMode::getTestMode() == TestMode::Mode_Nightmare ? 2 : 1;

  // Two objects with a fire event and a onFire method.
  ASSERT_NO_THROW(p1.server()->registerService("coin1", oserver1).hasValue(2000));
  ASSERT_NO_THROW(p2.server()->registerService("coin2", oserver2).hasValue(2000));
  EXPECT_EQ(nbServices, p1.server()->services(qi::Session::ServiceLocality_Local).value().size());
  EXPECT_EQ(nbServices, p2.server()->services(qi::Session::ServiceLocality_Local).value().size());
  oclient1 = p2.client()->service("coin1");
  oclient2 = p1.client()->service("coin2");
#ifdef WIN32
  int niter = 1000;
#else
  int niter = 1000;
#endif
  if (TestMode::getTestMode() == TestMode::Mode_SSL)
  {
    niter /= 100;
  }
  if (getenv("VALGRIND"))
  {
    std::cerr << "Valgrind detected, reducing iteration count" << std::endl;
    niter = 50;
  }
  oclient1.call<void>("onFire1", niter);
  ASSERT_NO_THROW(payload.future().hasValue(2000));
  oclient1.reset();
  oclient2.reset();
}
Ejemplo n.º 18
0
void onStop()
{
  _stopped = true;
  _sync.setValue(0);
}
Ejemplo n.º 19
0
void producer(qi::Promise<int> pro) {
  qi::os::msleep(100);
  pro.setValue(42);
}
Ejemplo n.º 20
0
void unlock(qi::Promise<int> prom, bool* tag)
{
  *tag = true;
  prom.setValue(1);
}
Ejemplo n.º 21
0
 void operator()(const T& value)
 {
   result.setValue(value);
 }
Ejemplo n.º 22
0
void testDelete(bool afirst, bool disconnectFirst, qi::Promise<void> end)
{
  qi::Promise<void> p0;
  qi::Promise<void> p1;
  qi::SignalLink fireId, onFireId, onFireId2;
  qi::AnyObject *a;
  qi::AnyObject *b;
  {
    qi::DynamicObjectBuilder oba, obb;
    fireId = oba.advertiseSignal<int>("fire");
    onFireId = obb.advertiseMethod("onFire", boost::function<void(int)>(boost::bind<void>(&onFire, _1, boost::ref(p0))));
    onFireId2 = obb.advertiseMethod("onFire2", boost::function<void(int)>(boost::bind<void>(&onFire2, _1, boost::ref(p1))));
    a = new qi::AnyObject(oba.object());
    b = new qi::AnyObject(obb.object());
  }
  qi::SignalLink linkId = (*a).connect(fireId, *b, onFireId);
  (*a).connect(fireId, *b, onFireId2);
  //std::vector<qi::SignalSubscriber> subs = (*a)->subscribers(fireId);
  //EXPECT_EQ(static_cast<unsigned int>(2), subs.size());
  // Subs ordering is unspecified
  //EXPECT_EQ(subs[0].method + subs[1].method, onFireId + onFireId2);
  lastPayload = 11;
  lastPayload2 = 11;
  (*a).post("fire", 12);
  EXPECT_TRUE(p0.future().hasValue(1000));
  EXPECT_TRUE(p1.future().hasValue(1000));
  EXPECT_EQ(12, *lastPayload);
  EXPECT_EQ(12, *lastPayload2);
  p0 = qi::Promise<void>();
  p1 = qi::Promise<void>();
  if (disconnectFirst)
  {
    (*a).disconnect(linkId);
    (*a).post("fire", 13);
    EXPECT_TRUE(p1.future().hasValue(1000));
    EXPECT_EQ(12, *lastPayload);
    EXPECT_EQ(13, *lastPayload2);
    p0 = qi::Promise<void>();
    p1 = qi::Promise<void>();
  }
  if (afirst)
  {
    delete a;
    delete b;
  }
  else
  {
    int e1 = *lastPayload, e2 = *lastPayload2;
    delete b;
    // wait for the object to be deleted (signal callbacks may still be
    // running)
    qi::os::msleep(10);
    (*a).post("fire", 12);
    EXPECT_EQ(e1, *lastPayload);
    EXPECT_EQ(e2, *lastPayload2);
    delete a;
  }
  ++completed;
  int next = *completed;
  if (next == 4)
  {
    end.setValue(0);
    return;
  }
  qi::getEventLoop()->post(
    boost::bind(&testDelete, !!((int)next/2), !!((int)next%2), end));
}
Ejemplo n.º 23
0
void onFire2(const int& pl, qi::Promise<void>& p)
{
  ++lastPayload2;
  p.setValue(0);
}