/*-1* Copyright (c) 2011, 2012, 2013, 2014 Spectra Logic Corporation2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions, and the following disclaimer,9* without modification.10* 2. Redistributions in binary form must reproduce at minimum a disclaimer11* substantially similar to the "NO WARRANTY" disclaimer below12* ("Disclaimer") and any redistribution must be conditioned upon13* including a substantially similar Disclaimer requirement for further14* binary redistribution.15*16* NO WARRANTY17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR20* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21* HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,25* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING26* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE27* POSSIBILITY OF SUCH DAMAGES.28*29* Authors: Justin T. Gibbs (Spectra Logic Corporation)30*/3132/**33* \file devdctl_consumer.h34*/35#ifndef _DEVDCTL_CONSUMER_H_36#define _DEVDCTL_CONSUMER_H_3738/*============================ Namespace Control =============================*/39namespace DevdCtl40{4142/*=========================== Forward Declarations ===========================*/43class Event;4445/*============================ Class Declarations ============================*/46/*----------------------------- DevdCtl::Consumer ----------------------------*/4748/**49*/50class Consumer51{52public:53Consumer(Event::BuildMethod *defBuilder = NULL,54EventFactory::Record *regEntries = NULL,55size_t numEntries = 0);56virtual ~Consumer();5758bool Connected() const;5960/**61* Return file descriptor useful for client's wishing to poll(2)62* for new events.63*/64int GetPollFd();6566/**67* Queue an event for deferred processing or replay.68*/69bool SaveEvent(const Event &event);7071/**72* Reprocess any events saved via the SaveEvent() facility.73*74* \param discardUnconsumed If true, events that are not consumed75* during replay are discarded.76*/77void ReplayUnconsumedEvents(bool discardUnconsumed);7879/** Return an event, if one is available. */80Event *NextEvent();8182/**83* Extract events and invoke each event's Process method.84*/85void ProcessEvents();8687/** Discard all data pending in m_devdSockFD. */88void FlushEvents();8990/**91* Test for data pending in m_devdSockFD92*93* \return True if data is pending. Otherwise false.94*/95bool EventsPending();9697/**98* Open a connection to devd's unix domain socket.99*100* \return True if the connection attempt is successful. Otherwise101* false.102*/103bool ConnectToDevd();104105/**106* Close a connection (if any) to devd's unix domain socket.107*/108void DisconnectFromDevd();109110EventFactory GetFactory();111112protected:113/**114* \brief Reads the most recent record115*116* On error, "" is returned, and errno will be set by the OS117*118* \returns A string containing the record119*/120std::string ReadEvent();121122enum {123/*124* The maximum event size supported by libdevdctl.125*/126MAX_EVENT_SIZE = 8192,127};128129static const char s_devdSockPath[];130131/**132* File descriptor representing the unix domain socket133* connection with devd.134*/135int m_devdSockFD;136137EventFactory m_eventFactory;138139/** Queued events for replay. */140EventList m_unconsumedEvents;141142/**143* Flag controlling whether events can be queued. This boolean144* is set during event replay to ensure that previosuly deferred145* events are not requeued and thus retained forever.146*/147bool m_replayingEvents;148};149150//- Consumer Const Public Inline Methods ---------------------------------------151inline bool152Consumer::Connected() const153{154return (m_devdSockFD != -1);155}156157//- Consumer Public Inline Methods ---------------------------------------------158inline int159Consumer::GetPollFd()160{161return (m_devdSockFD);162}163164inline EventFactory165Consumer::GetFactory()166{167return (m_eventFactory);168}169170} // namespace DevdCtl171#endif /* _DEVDCTL_CONSUMER_H_ */172173174