xref: /freebsd/lib/libdevdctl/consumer.h (revision 4e462178745853ecc014c13f82f89cfe39b83e9c)
1 /*-
2  * Copyright (c) 2011, 2012, 2013, 2014 Spectra Logic Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    substantially similar to the "NO WARRANTY" disclaimer below
13  *    ("Disclaimer") and any redistribution must be conditioned upon
14  *    including a substantially similar Disclaimer requirement for further
15  *    binary redistribution.
16  *
17  * NO WARRANTY
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGES.
29  *
30  * Authors: Justin T. Gibbs     (Spectra Logic Corporation)
31  *
32  * $FreeBSD$
33  */
34 
35 /**
36  * \file devdctl_consumer.h
37  */
38 #ifndef	_DEVDCTL_CONSUMER_H_
39 #define	_DEVDCTL_CONSUMER_H_
40 
41 /*============================ Namespace Control =============================*/
42 namespace DevdCtl
43 {
44 
45 /*=========================== Forward Declarations ===========================*/
46 class Event;
47 
48 /*============================ Class Declarations ============================*/
49 /*----------------------------- DevdCtl::Consumer ----------------------------*/
50 
51 /**
52  */
53 class Consumer
54 {
55 public:
56 	Consumer(Event::BuildMethod *defBuilder = NULL,
57 		 EventFactory::Record *regEntries = NULL,
58 		 size_t numEntries = 0);
59 	virtual ~Consumer();
60 
61 	bool Connected() const;
62 
63 	/**
64 	 * Return file descriptor useful for client's wishing to poll(2)
65 	 * for new events.
66 	 */
67 	int GetPollFd();
68 
69 	/**
70          * Queue an event for deferred processing or replay.
71          */
72 	bool SaveEvent(const Event &event);
73 
74 	/**
75 	 * Reprocess any events saved via the SaveEvent() facility.
76 	 *
77 	 * \param discardUnconsumed  If true, events that are not consumed
78 	 *                           during replay are discarded.
79 	 */
80 	void ReplayUnconsumedEvents(bool discardUnconsumed);
81 
82 	/** Return an event, if one is available.  */
83 	Event *NextEvent();
84 
85 	/**
86 	 * Extract events and invoke each event's Process method.
87 	 */
88 	void ProcessEvents();
89 
90 	/** Discard all data pending in m_devdSockFD. */
91 	void FlushEvents();
92 
93 	/**
94 	 * Test for data pending in m_devdSockFD
95 	 *
96 	 * \return  True if data is pending.  Otherwise false.
97 	 */
98 	bool EventsPending();
99 
100 	/**
101 	 * Open a connection to devd's unix domain socket.
102 	 *
103 	 * \return  True if the connection attempt is successsful.  Otherwise
104 	 *          false.
105 	 */
106 	bool ConnectToDevd();
107 
108 	/**
109 	 * Close a connection (if any) to devd's unix domain socket.
110 	 */
111 	void DisconnectFromDevd();
112 
113 	EventFactory GetFactory();
114 
115 protected:
116 	/**
117 	 * \brief Reads the most recent record
118 	 *
119 	 * On error, "" is returned, and errno will be set by the OS
120 	 *
121 	 * \returns      A string containing the record
122 	 */
123 	std::string ReadEvent();
124 
125 	enum {
126 		/*
127 		 * The maximum event size supported by libdevdctl.
128 		 */
129 		MAX_EVENT_SIZE = 8192,
130 	};
131 
132 	static const char  s_devdSockPath[];
133 
134 	/**
135 	 * File descriptor representing the unix domain socket
136 	 * connection with devd.
137 	 */
138 	int                m_devdSockFD;
139 
140 	EventFactory	   m_eventFactory;
141 
142 	/** Queued events for replay. */
143 	EventList	   m_unconsumedEvents;
144 
145 	/**
146 	 * Flag controlling whether events can be queued.  This boolean
147 	 * is set during event replay to ensure that previosuly deferred
148 	 * events are not requeued and thus retained forever.
149 	 */
150 	bool		   m_replayingEvents;
151 };
152 
153 //- Consumer Const Public Inline Methods ---------------------------------------
154 inline bool
155 Consumer::Connected() const
156 {
157 	return (m_devdSockFD != -1);
158 }
159 
160 //- Consumer Public Inline Methods ---------------------------------------------
161 inline int
162 Consumer::GetPollFd()
163 {
164 	return (m_devdSockFD);
165 }
166 
167 inline EventFactory
168 Consumer::GetFactory()
169 {
170 	return (m_eventFactory);
171 }
172 
173 } // namespace DevdCtl
174 #endif	/* _DEVDCTL_CONSUMER_H_ */
175