xref: /freebsd/lib/libdevdctl/event_factory.cc (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1*7a0c41d5SAlan Somers /*-
2*7a0c41d5SAlan Somers  * Copyright (c) 2013 Spectra Logic Corporation
3*7a0c41d5SAlan Somers  * All rights reserved.
4*7a0c41d5SAlan Somers  *
5*7a0c41d5SAlan Somers  * Redistribution and use in source and binary forms, with or without
6*7a0c41d5SAlan Somers  * modification, are permitted provided that the following conditions
7*7a0c41d5SAlan Somers  * are met:
8*7a0c41d5SAlan Somers  * 1. Redistributions of source code must retain the above copyright
9*7a0c41d5SAlan Somers  *    notice, this list of conditions, and the following disclaimer,
10*7a0c41d5SAlan Somers  *    without modification.
11*7a0c41d5SAlan Somers  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12*7a0c41d5SAlan Somers  *    substantially similar to the "NO WARRANTY" disclaimer below
13*7a0c41d5SAlan Somers  *    ("Disclaimer") and any redistribution must be conditioned upon
14*7a0c41d5SAlan Somers  *    including a substantially similar Disclaimer requirement for further
15*7a0c41d5SAlan Somers  *    binary redistribution.
16*7a0c41d5SAlan Somers  *
17*7a0c41d5SAlan Somers  * NO WARRANTY
18*7a0c41d5SAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*7a0c41d5SAlan Somers  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*7a0c41d5SAlan Somers  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21*7a0c41d5SAlan Somers  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*7a0c41d5SAlan Somers  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*7a0c41d5SAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*7a0c41d5SAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*7a0c41d5SAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26*7a0c41d5SAlan Somers  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27*7a0c41d5SAlan Somers  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*7a0c41d5SAlan Somers  * POSSIBILITY OF SUCH DAMAGES.
29*7a0c41d5SAlan Somers  *
30*7a0c41d5SAlan Somers  * Authors: Justin T. Gibbs     (Spectra Logic Corporation)
31*7a0c41d5SAlan Somers  */
32*7a0c41d5SAlan Somers 
33*7a0c41d5SAlan Somers /**
34*7a0c41d5SAlan Somers  * \file event_factory.cc
35*7a0c41d5SAlan Somers  */
36*7a0c41d5SAlan Somers #include <sys/cdefs.h>
37*7a0c41d5SAlan Somers #include <sys/time.h>
38*7a0c41d5SAlan Somers 
39*7a0c41d5SAlan Somers #include <list>
40*7a0c41d5SAlan Somers #include <map>
41*7a0c41d5SAlan Somers #include <string>
42*7a0c41d5SAlan Somers 
43*7a0c41d5SAlan Somers #include "guid.h"
44*7a0c41d5SAlan Somers #include "event.h"
45*7a0c41d5SAlan Somers #include "event_factory.h"
46*7a0c41d5SAlan Somers /*================================== Macros ==================================*/
47*7a0c41d5SAlan Somers #define NUM_ELEMENTS(x) (sizeof(x) / sizeof(*x))
48*7a0c41d5SAlan Somers 
49*7a0c41d5SAlan Somers /*============================ Namespace Control =============================*/
50*7a0c41d5SAlan Somers namespace DevdCtl
51*7a0c41d5SAlan Somers {
52*7a0c41d5SAlan Somers 
53*7a0c41d5SAlan Somers /*=========================== Class Implementations ==========================*/
54*7a0c41d5SAlan Somers /*------------------------------- EventFactory -------------------------------*/
55*7a0c41d5SAlan Somers //- Event Public Methods -------------------------------------------------------
EventFactory(Event::BuildMethod * defaultBuildMethod)56*7a0c41d5SAlan Somers EventFactory::EventFactory(Event::BuildMethod *defaultBuildMethod)
57*7a0c41d5SAlan Somers  : m_defaultBuildMethod(defaultBuildMethod)
58*7a0c41d5SAlan Somers {
59*7a0c41d5SAlan Somers }
60*7a0c41d5SAlan Somers 
61*7a0c41d5SAlan Somers void
UpdateRegistry(Record regEntries[],size_t numEntries)62*7a0c41d5SAlan Somers EventFactory::UpdateRegistry(Record regEntries[], size_t numEntries)
63*7a0c41d5SAlan Somers {
64*7a0c41d5SAlan Somers 	EventFactory::Record *rec(regEntries);
65*7a0c41d5SAlan Somers 	EventFactory::Record *lastRec(rec + numEntries - 1);
66*7a0c41d5SAlan Somers 
67*7a0c41d5SAlan Somers 	for (; rec <= lastRec; rec++) {
68*7a0c41d5SAlan Somers 		Key key(rec->m_type, rec->m_subsystem);
69*7a0c41d5SAlan Somers 
70*7a0c41d5SAlan Somers 		if (rec->m_buildMethod == NULL)
71*7a0c41d5SAlan Somers 			m_registry.erase(key);
72*7a0c41d5SAlan Somers 		else
73*7a0c41d5SAlan Somers 			m_registry[key] = rec->m_buildMethod;
74*7a0c41d5SAlan Somers 	}
75*7a0c41d5SAlan Somers }
76*7a0c41d5SAlan Somers 
77*7a0c41d5SAlan Somers Event *
Build(Event::Type type,NVPairMap & nvpairs,const std::string eventString) const78*7a0c41d5SAlan Somers EventFactory::Build(Event::Type type, NVPairMap &nvpairs,
79*7a0c41d5SAlan Somers 		    const std::string eventString) const
80*7a0c41d5SAlan Somers {
81*7a0c41d5SAlan Somers 	Key key(type, nvpairs["system"]);
82*7a0c41d5SAlan Somers 	Event::BuildMethod *buildMethod(m_defaultBuildMethod);
83*7a0c41d5SAlan Somers 
84*7a0c41d5SAlan Somers 	Registry::const_iterator foundMethod(m_registry.find(key));
85*7a0c41d5SAlan Somers 	if (foundMethod != m_registry.end())
86*7a0c41d5SAlan Somers 		buildMethod = foundMethod->second;
87*7a0c41d5SAlan Somers 
88*7a0c41d5SAlan Somers 	if (buildMethod == NULL) {
89*7a0c41d5SAlan Somers 		delete &nvpairs;
90*7a0c41d5SAlan Somers 		return (NULL);
91*7a0c41d5SAlan Somers 	}
92*7a0c41d5SAlan Somers 
93*7a0c41d5SAlan Somers 	return (buildMethod(type, nvpairs, eventString));
94*7a0c41d5SAlan Somers }
95*7a0c41d5SAlan Somers 
96*7a0c41d5SAlan Somers } // namespace DevdCtl
97