xref: /freebsd/lib/libdevdctl/consumer.h (revision 186034134768ffc5968a266be37fc99feb652fad)
17a0c41d5SAlan Somers /*-
27a0c41d5SAlan Somers  * Copyright (c) 2011, 2012, 2013, 2014 Spectra Logic Corporation
37a0c41d5SAlan Somers  * All rights reserved.
47a0c41d5SAlan Somers  *
57a0c41d5SAlan Somers  * Redistribution and use in source and binary forms, with or without
67a0c41d5SAlan Somers  * modification, are permitted provided that the following conditions
77a0c41d5SAlan Somers  * are met:
87a0c41d5SAlan Somers  * 1. Redistributions of source code must retain the above copyright
97a0c41d5SAlan Somers  *    notice, this list of conditions, and the following disclaimer,
107a0c41d5SAlan Somers  *    without modification.
117a0c41d5SAlan Somers  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
127a0c41d5SAlan Somers  *    substantially similar to the "NO WARRANTY" disclaimer below
137a0c41d5SAlan Somers  *    ("Disclaimer") and any redistribution must be conditioned upon
147a0c41d5SAlan Somers  *    including a substantially similar Disclaimer requirement for further
157a0c41d5SAlan Somers  *    binary redistribution.
167a0c41d5SAlan Somers  *
177a0c41d5SAlan Somers  * NO WARRANTY
187a0c41d5SAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
197a0c41d5SAlan Somers  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
207a0c41d5SAlan Somers  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
217a0c41d5SAlan Somers  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
227a0c41d5SAlan Somers  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
237a0c41d5SAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
247a0c41d5SAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
257a0c41d5SAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
267a0c41d5SAlan Somers  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
277a0c41d5SAlan Somers  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
287a0c41d5SAlan Somers  * POSSIBILITY OF SUCH DAMAGES.
297a0c41d5SAlan Somers  *
307a0c41d5SAlan Somers  * Authors: Justin T. Gibbs     (Spectra Logic Corporation)
317a0c41d5SAlan Somers  */
327a0c41d5SAlan Somers 
337a0c41d5SAlan Somers /**
347a0c41d5SAlan Somers  * \file devdctl_consumer.h
357a0c41d5SAlan Somers  */
367a0c41d5SAlan Somers #ifndef	_DEVDCTL_CONSUMER_H_
377a0c41d5SAlan Somers #define	_DEVDCTL_CONSUMER_H_
387a0c41d5SAlan Somers 
397a0c41d5SAlan Somers /*============================ Namespace Control =============================*/
407a0c41d5SAlan Somers namespace DevdCtl
417a0c41d5SAlan Somers {
427a0c41d5SAlan Somers 
437a0c41d5SAlan Somers /*=========================== Forward Declarations ===========================*/
447a0c41d5SAlan Somers class Event;
457a0c41d5SAlan Somers 
467a0c41d5SAlan Somers /*============================ Class Declarations ============================*/
477a0c41d5SAlan Somers /*----------------------------- DevdCtl::Consumer ----------------------------*/
487a0c41d5SAlan Somers 
497a0c41d5SAlan Somers /**
507a0c41d5SAlan Somers  */
517a0c41d5SAlan Somers class Consumer
527a0c41d5SAlan Somers {
537a0c41d5SAlan Somers public:
547a0c41d5SAlan Somers 	Consumer(Event::BuildMethod *defBuilder = NULL,
557a0c41d5SAlan Somers 		 EventFactory::Record *regEntries = NULL,
567a0c41d5SAlan Somers 		 size_t numEntries = 0);
577a0c41d5SAlan Somers 	virtual ~Consumer();
587a0c41d5SAlan Somers 
597a0c41d5SAlan Somers 	bool Connected() const;
607a0c41d5SAlan Somers 
617a0c41d5SAlan Somers 	/**
627a0c41d5SAlan Somers 	 * Return file descriptor useful for client's wishing to poll(2)
637a0c41d5SAlan Somers 	 * for new events.
647a0c41d5SAlan Somers 	 */
657a0c41d5SAlan Somers 	int GetPollFd();
667a0c41d5SAlan Somers 
677a0c41d5SAlan Somers 	/**
687a0c41d5SAlan Somers          * Queue an event for deferred processing or replay.
697a0c41d5SAlan Somers          */
707a0c41d5SAlan Somers 	bool SaveEvent(const Event &event);
717a0c41d5SAlan Somers 
727a0c41d5SAlan Somers 	/**
737a0c41d5SAlan Somers 	 * Reprocess any events saved via the SaveEvent() facility.
747a0c41d5SAlan Somers 	 *
757a0c41d5SAlan Somers 	 * \param discardUnconsumed  If true, events that are not consumed
767a0c41d5SAlan Somers 	 *                           during replay are discarded.
777a0c41d5SAlan Somers 	 */
787a0c41d5SAlan Somers 	void ReplayUnconsumedEvents(bool discardUnconsumed);
797a0c41d5SAlan Somers 
807a0c41d5SAlan Somers 	/** Return an event, if one is available.  */
817a0c41d5SAlan Somers 	Event *NextEvent();
827a0c41d5SAlan Somers 
837a0c41d5SAlan Somers 	/**
847a0c41d5SAlan Somers 	 * Extract events and invoke each event's Process method.
857a0c41d5SAlan Somers 	 */
867a0c41d5SAlan Somers 	void ProcessEvents();
877a0c41d5SAlan Somers 
887a0c41d5SAlan Somers 	/** Discard all data pending in m_devdSockFD. */
897a0c41d5SAlan Somers 	void FlushEvents();
907a0c41d5SAlan Somers 
917a0c41d5SAlan Somers 	/**
927a0c41d5SAlan Somers 	 * Test for data pending in m_devdSockFD
937a0c41d5SAlan Somers 	 *
947a0c41d5SAlan Somers 	 * \return  True if data is pending.  Otherwise false.
957a0c41d5SAlan Somers 	 */
967a0c41d5SAlan Somers 	bool EventsPending();
977a0c41d5SAlan Somers 
987a0c41d5SAlan Somers 	/**
997a0c41d5SAlan Somers 	 * Open a connection to devd's unix domain socket.
1007a0c41d5SAlan Somers 	 *
101*18603413SJose Luis Duran 	 * \return  True if the connection attempt is successful.  Otherwise
1027a0c41d5SAlan Somers 	 *          false.
1037a0c41d5SAlan Somers 	 */
1047a0c41d5SAlan Somers 	bool ConnectToDevd();
1057a0c41d5SAlan Somers 
1067a0c41d5SAlan Somers 	/**
1077a0c41d5SAlan Somers 	 * Close a connection (if any) to devd's unix domain socket.
1087a0c41d5SAlan Somers 	 */
1097a0c41d5SAlan Somers 	void DisconnectFromDevd();
1107a0c41d5SAlan Somers 
1117a0c41d5SAlan Somers 	EventFactory GetFactory();
1127a0c41d5SAlan Somers 
1137a0c41d5SAlan Somers protected:
1147a0c41d5SAlan Somers 	/**
1157a0c41d5SAlan Somers 	 * \brief Reads the most recent record
1167a0c41d5SAlan Somers 	 *
1177a0c41d5SAlan Somers 	 * On error, "" is returned, and errno will be set by the OS
1187a0c41d5SAlan Somers 	 *
1197a0c41d5SAlan Somers 	 * \returns      A string containing the record
1207a0c41d5SAlan Somers 	 */
1217a0c41d5SAlan Somers 	std::string ReadEvent();
1227a0c41d5SAlan Somers 
1237a0c41d5SAlan Somers 	enum {
1247a0c41d5SAlan Somers 		/*
1257a0c41d5SAlan Somers 		 * The maximum event size supported by libdevdctl.
1267a0c41d5SAlan Somers 		 */
1277a0c41d5SAlan Somers 		MAX_EVENT_SIZE = 8192,
1287a0c41d5SAlan Somers 	};
1297a0c41d5SAlan Somers 
1307a0c41d5SAlan Somers 	static const char  s_devdSockPath[];
1317a0c41d5SAlan Somers 
1327a0c41d5SAlan Somers 	/**
1337a0c41d5SAlan Somers 	 * File descriptor representing the unix domain socket
1347a0c41d5SAlan Somers 	 * connection with devd.
1357a0c41d5SAlan Somers 	 */
1367a0c41d5SAlan Somers 	int                m_devdSockFD;
1377a0c41d5SAlan Somers 
1387a0c41d5SAlan Somers 	EventFactory	   m_eventFactory;
1397a0c41d5SAlan Somers 
1407a0c41d5SAlan Somers 	/** Queued events for replay. */
1417a0c41d5SAlan Somers 	EventList	   m_unconsumedEvents;
1427a0c41d5SAlan Somers 
1437a0c41d5SAlan Somers 	/**
1447a0c41d5SAlan Somers 	 * Flag controlling whether events can be queued.  This boolean
1457a0c41d5SAlan Somers 	 * is set during event replay to ensure that previosuly deferred
1467a0c41d5SAlan Somers 	 * events are not requeued and thus retained forever.
1477a0c41d5SAlan Somers 	 */
1487a0c41d5SAlan Somers 	bool		   m_replayingEvents;
1497a0c41d5SAlan Somers };
1507a0c41d5SAlan Somers 
1517a0c41d5SAlan Somers //- Consumer Const Public Inline Methods ---------------------------------------
1527a0c41d5SAlan Somers inline bool
Connected()1537a0c41d5SAlan Somers Consumer::Connected() const
1547a0c41d5SAlan Somers {
1557a0c41d5SAlan Somers 	return (m_devdSockFD != -1);
1567a0c41d5SAlan Somers }
1577a0c41d5SAlan Somers 
1587a0c41d5SAlan Somers //- Consumer Public Inline Methods ---------------------------------------------
1597a0c41d5SAlan Somers inline int
GetPollFd()1607a0c41d5SAlan Somers Consumer::GetPollFd()
1617a0c41d5SAlan Somers {
1627a0c41d5SAlan Somers 	return (m_devdSockFD);
1637a0c41d5SAlan Somers }
1647a0c41d5SAlan Somers 
1657a0c41d5SAlan Somers inline EventFactory
GetFactory()1667a0c41d5SAlan Somers Consumer::GetFactory()
1677a0c41d5SAlan Somers {
1687a0c41d5SAlan Somers 	return (m_eventFactory);
1697a0c41d5SAlan Somers }
1707a0c41d5SAlan Somers 
1717a0c41d5SAlan Somers } // namespace DevdCtl
1727a0c41d5SAlan Somers #endif	/* _DEVDCTL_CONSUMER_H_ */
173