示例#1
0
void DummyBoltInstance::HandleNewInstanceAssignmentMsg(
    heron::proto::stmgr::NewInstanceAssignmentMessage* _msg) {
  DummyInstance::HandleNewInstanceAssignmentMsg(_msg);
  if (expected_msgs_to_recv_ == 0) {
    getEventLoop()->loopExit();
  }
}
示例#2
0
 void PeriodicTaskPrivate::_reschedule(qi::int64_t delay)
 {
   qiLogDebug() << _name <<" rescheduling in " << delay;
   _task = getEventLoop()->async(boost::bind(&PeriodicTaskPrivate::_wrap, shared_from_this()), delay);
   if (!_state.setIfEquals(Task_Rescheduling, Task_Scheduled))
     qiLogError() << "PeriodicTask forbidden state change while rescheduling " << *_state;
 }
示例#3
0
 void PeriodicTaskPrivate::_reschedule(qi::Duration delay)
 {
   qiLogDebug() << "rescheduling in " << qi::to_string(delay);
   if (_scheduleCallback)
     _task = _scheduleCallback(boost::bind(&PeriodicTaskPrivate::_wrap, shared_from_this()), delay);
   else
     _task = getEventLoop()->asyncDelay(boost::bind(&PeriodicTaskPrivate::_wrap, shared_from_this()), delay);
   _state = TaskState::Scheduled;
   _task.connect(boost::bind(
         &PeriodicTaskPrivate::_onTaskFinished, shared_from_this(), _1));
 }
void JSInterIsolateCallManager::notify(const Request& request, void* userData, JSPersistentFunctionHandle* cb)
{
  bool delegateToSelf = getIsolate()->isThreadSelf();
  JSInterIsolateCall::Ptr pCall(new JSInterIsolateCall(request, 0, userData, cb));
  enqueue(pCall);
  if (delegateToSelf)
  {
    doOneWork();
  }
  else
  {
    getEventLoop()->wakeup();
  }
}
bool JSInterIsolateCallManager::execute(const Request& request, Result& result, uint32_t timeout, void* userData)
{
  bool delegateToSelf = getIsolate()->isThreadSelf();
  JSInterIsolateCall::Ptr pCall(new JSInterIsolateCall(request, timeout, userData));
  enqueue(pCall);

  if (delegateToSelf)
  {
    doOneWork();
  }
  else
  {
    getEventLoop()->wakeup();
  }
  
  if (!pCall->waitForResult())
  {
    return false;
  }
  result = pCall->getResult();
  return true;
}
示例#6
0
文件: signal.cpp 项目: cgestes/libqi
  void SignalSubscriber::call(const GenericFunctionParameters& args, MetaCallType callType)
  {
    // this is held alive by caller
    if (handler)
    {
      bool async = true;
      if (threadingModel != MetaCallType_Auto)
        async = (threadingModel == MetaCallType_Queued);
      else if (callType != MetaCallType_Auto)
        async = (callType == MetaCallType_Queued);

      qiLogDebug() << "subscriber call async=" << async <<" ct " << callType <<" tm " << threadingModel;
      if (async)
      {
        GenericFunctionParameters* copy = new GenericFunctionParameters(args.copy());
        // We will check enabled when we will be scheduled in the target
        // thread, and we hold this SignalSubscriber alive, so no need to
        // explicitly track the asynccall

        // courtesy-check of el, but it should be kept alive longuer than us
        qi::EventLoop* el = getEventLoop();
        if (!el) // this is an assert basicaly, no sense trying to do something clever.
          throw std::runtime_error("Event loop was destroyed");
        el->post(FunctorCall(copy, new SignalSubscriberPtr(shared_from_this())));
      }
      else
      {
        // verify-enabled-then-register-active op must be locked
        {
          boost::mutex::scoped_lock sl(mutex);
          if (!enabled)
            return;
          addActive(false);
        }
        //do not throw
        bool mustDisconnect = false;
        try
        {
          handler(args);
        }
        catch(const qi::PointerLockException&)
        {
          qiLogDebug() << "PointerLockFailure excepton, will disconnect";
          mustDisconnect = true;
        }
        catch(const std::exception& e)
        {
          qiLogWarning() << "Exception caught from signal subscriber: " << e.what();
        }
        catch (...)
        {
          qiLogWarning() << "Unknown exception caught from signal subscriber";
        }
        removeActive(true);
        if (mustDisconnect)
          source->disconnect(linkId);
      }
    }
    else if (target)
    {
      AnyObject lockedTarget = target->lock();
      if (!lockedTarget)
      {
        source->disconnect(linkId);
      }
      else // no need to keep anything locked, whatever happens this is not used
        lockedTarget.metaPost(method, args);
    }
  }
示例#7
0
void OrderServer::Terminate() {
  Stop();
  getEventLoop()->loopExit();
}
示例#8
0
 virtual void HandleClose(NetworkErrorCode) {
   Stop();
   getEventLoop()->loopExit();
 }
示例#9
0
namespace qi
{
  inline boost::asio::io_service* asIoServicePtr(EventLoop* e)
  {
    return static_cast<boost::asio::io_service*>(e->nativeHandle());
  }

  class Session;

  class MessageSocket : private boost::noncopyable, public StreamContext
  {
  public:
    virtual ~MessageSocket();

    // Do not change the values because TcpMessageSocket::status() relies on it.
    enum class Status {
      Disconnected  = 0,
      Connecting    = 1,
      Connected     = 2,
      Disconnecting = 3,
    };
    enum Event {
      Event_Error = 0,
      Event_Message = 1,
    };

    explicit MessageSocket(qi::EventLoop* eventLoop = qi::getEventLoop())
      : _eventLoop(eventLoop)
      , _status(Status::Disconnected)
    {
      connected.setCallType(MetaCallType_Direct);
      disconnected.setCallType(MetaCallType_Direct);
      messageReady.setCallType(MetaCallType_Direct);
      socketEvent.setCallType(MetaCallType_Direct);
    }

    virtual qi::FutureSync<void> connect(const qi::Url &url) = 0;
    virtual qi::FutureSync<void> disconnect()                = 0;

    virtual bool send(const qi::Message &msg)                = 0;

    /// Start reading if is not already reading.
    /// Must be called once if the socket is obtained through TransportServer::newConnection()
    virtual bool  ensureReading() = 0;

    virtual Status status() const = 0;
    virtual boost::optional<qi::Url> remoteEndpoint() const = 0;

    virtual qi::Url url() const = 0;

    virtual bool isConnected() const;

    static const unsigned int ALL_OBJECTS = (unsigned int)-1;

    qi::SignalLink messagePendingConnect(unsigned int serviceId, unsigned int objectId, boost::function<void (const qi::Message&)> fun) {
      return _dispatcher.messagePendingConnect(serviceId, objectId, fun);
    }

    bool messagePendingDisconnect(unsigned int serviceId, unsigned int objectId, qi::SignalLink linkId) {
      return _dispatcher.messagePendingDisconnect(serviceId, objectId, linkId);
    }

  protected:
    qi::EventLoop* _eventLoop;
    qi::MessageDispatcher _dispatcher;

    std::atomic<MessageSocket::Status> _status;
  public:
    // C4251
    qi::Signal<>                   connected;
    // C4251
    qi::Signal<std::string>        disconnected;
    // C4251
    qi::Signal<const qi::Message&> messageReady;
    using SocketEventData = boost::variant<std::string, qi::Message>;
    // C4251
    qi::Signal<SocketEventData>  socketEvent;
  };

  using MessageSocketPtr = boost::shared_ptr<MessageSocket>;
  MessageSocketPtr makeMessageSocket(const std::string &protocol, qi::EventLoop *eventLoop = getEventLoop());
}
示例#10
0
void DummyBoltInstance::HandleTupleMessage(heron::proto::system::HeronTupleSet2* msg) {
  if (msg->has_data()) msgs_recvd_ += msg->mutable_data()->tuples_size();
  if (msgs_recvd_ >= expected_msgs_to_recv_) getEventLoop()->loopExit();
}
示例#11
0
namespace qi
{
  class Session;

  class TransportSocket : private boost::noncopyable, public StreamContext
  {
  public:
    virtual ~TransportSocket();
    enum Status {
      Status_Disconnected  = 0,
      Status_Connecting    = 1,
      Status_Connected     = 2,
      Status_Disconnecting = 3,
    };

    explicit TransportSocket(qi::EventLoop* eventLoop = qi::getEventLoop())
      : _eventLoop(NULL)
      , _err(0)
      , _status(Status_Disconnected)
    {
      connected.setCallType(MetaCallType_Queued);
      disconnected.setCallType(MetaCallType_Queued);
      // Set messageReady signal to async mode to protect our network thread
      messageReady.setCallType(MetaCallType_Queued);
    }

    virtual qi::FutureSync<void> connect(const qi::Url &url) = 0;
    virtual qi::FutureSync<void> disconnect()                = 0;

    virtual bool send(const qi::Message &msg)                = 0;
    /// Must be called once if the socket is obtained through TransportServer::newConnection()
    virtual void  startReading() = 0;

    virtual qi::Url remoteEndpoint() const = 0;

    qi::Url url() const {
      return _url;
    }

    Status status() const {
      return _status;
    }

    int error() const
    {
      return _err;
    }

    bool isConnected() const
    {
      return _status == qi::TransportSocket::Status_Connected;
    }

    static const unsigned int ALL_OBJECTS = (unsigned int)-1;

    qi::SignalLink messagePendingConnect(unsigned int serviceId, unsigned int objectId, boost::function<void (const qi::Message&)> fun) {
      return _dispatcher.messagePendingConnect(serviceId, objectId, fun);
    }

    bool                 messagePendingDisconnect(unsigned int serviceId, unsigned int objectId, qi::SignalLink linkId) {
      return _dispatcher.messagePendingDisconnect(serviceId, objectId, linkId);
    }

  protected:
    qi::EventLoop*          _eventLoop;
    qi::MessageDispatcher   _dispatcher;

    int                     _err;
    TransportSocket::Status _status;
    qi::Url                 _url;

  public:
    // C4251
    qi::Signal<>                   connected;
    // C4251
    qi::Signal<std::string>        disconnected;
    // C4251
    qi::Signal<const qi::Message&> messageReady;
  };

  typedef boost::shared_ptr<TransportSocket> TransportSocketPtr;

  TransportSocketPtr makeTransportSocket(const std::string &protocol, qi::EventLoop *eventLoop = getEventLoop());

}
		virtual void runBlocking() {
			getEventLoop()->postEvent(boost::bind(boost::ref(onResult), getNATTraversalInterface()->addPortForward(boost::numeric_cast<int>(localIP), boost::numeric_cast<int>(publicIP))));
		}
		virtual void runBlocking() {
			getEventLoop()->postEvent(boost::bind(boost::ref(onResult), getNATTraversalInterface()->getPublicIP()));
		}
		virtual void runBlocking() {
			getEventLoop()->postEvent(boost::bind(boost::ref(onResult), getNATTraversalInterface()->removePortForward(mapping)));
		}