/* * Copyright 2015 Institut Laue-Langevin * * Licensed under the EUPL, Version 1.1 only (the "License"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * * http://joinup.ec.europa.eu/software/page/eupl * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. */ #ifndef CAMEO_SERVER_H_ #define CAMEO_SERVER_H_ #include #include #include #include "Application.h" #include "ConnectionChecker.h" #include "ConnectionTimeout.h" #include "Response.h" #include "Services.h" #include "SubscriberCreationException.h" namespace cameo { namespace application { class This; } class EventListener; class EventThread; class Server : private Services { friend class SubscriberImpl; friend class RequestImpl; friend class application::Instance; friend class application::This; friend class application::Subscriber; friend std::ostream& operator<<(std::ostream&, const Server&); public: typedef std::function ConnectionCheckerType; Server(const std::string& endpoint, int timeoutMs = 0); ~Server(); void setTimeout(int timeoutMs); int getTimeout() const; const std::string& getEndpoint() const; const std::string& getUrl() const; std::array getVersion() const; int getPort() const; bool isAvailable(int timeoutMs) const; /** * Returns true if is available. Uses the timeout if set or 10000ms. */ bool isAvailable() const; std::unique_ptr start(const std::string& name, const std::vector &args, Option options = NONE); std::unique_ptr start(const std::string& name, Option options = NONE); application::InstanceArray connectAll(const std::string& name, Option options = NONE); std::unique_ptr connect(const std::string& name, Option options = NONE); std::unique_ptr connect(int id, Option options = NONE); /** * throws ConnectionTimeout */ void killAllAndWaitFor(const std::string& name); /** * throws ConnectionTimeout */ std::vector getApplicationConfigurations() const; /** * throws ConnectionTimeout */ std::vector getApplicationInfos() const; /** * throws ConnectionTimeout */ std::vector getApplicationInfos(const std::string& name) const; /** * throws ConnectionTimeout */ application::State getActualState(int id) const; /** * throws ConnectionTimeout */ std::set getPastStates(int id) const; /** * throws ConnectionTimeout */ std::unique_ptr openEventStream(); /** * Creates a connection handler with polling time. */ std::unique_ptr createConnectionChecker(ConnectionCheckerType handler, int pollingTimeMs = 10000); /** * Gets the event listeners. Copies the list. */ std::vector getEventListeners(); /** * Registers an event listener. */ void registerEventListener(EventListener * listener); /** * Unregisters an event listener. */ void unregisterEventListener(EventListener * listener); private: std::unique_ptr makeInstance(); bool isAlive(int id) const; Response stopApplicationAsynchronously(int id, bool immediately) const; std::unique_ptr createSubscriber(int id, const std::string& publisherName, const std::string& instanceName); int getAvailableTimeout() const; void storeKeyValue(int id, const std::string& key, const std::string& value); std::string getKeyValue(int id, const std::string& key); void removeKey(int id, const std::string& key); int requestPort(int id); void setPortUnavailable(int id, int port); void releasePort(int id, int port); std::mutex m_eventListenersMutex; std::vector m_eventListeners; std::unique_ptr m_eventThread; }; std::ostream& operator<<(std::ostream&, const Server&); } #endif