📄 cgiclient.cpp
字号:
try { // Time the connection Stopwatch elapsedTime; elapsedTime.start(); CIMClient client; String host; Uint32 portNumber; getHostAndPort (host, portNumber); client.connect (host, portNumber, String::EMPTY, String::EMPTY); // Call enumerate Instances CIM Method Array<CIMObjectPath> instanceNames = client.enumerateInstanceNames( nameSpace, className); elapsedTime.stop(); // Print the CIMObjectPath array PrintObjectNames( "EnumerateInstanceNames Result", nameSpace, instanceNames, elapsedTime.getElapsed(), false); } catch(Exception& e) { ErrorExit(e.getMessage()); }}/*************************************************************************** GetInstance Function***************************************************************************//** GetInstance FunctionThis function is executed for the getInstance OperationIt takes the parameters from the CGIQueryStringto create parameters for the getInstance CIMClientmethod call.The results of the call are printed in an HTML page.*/static void GetInstance(const CGIQueryString& qs){ CIMNamespaceName nameSpace = GetNameSpaceQueryField(qs); // Get InstanceName: const char* tmp; if (!(tmp = qs.findValue("InstanceName"))) ErrorExit("Missing InstanceName field"); // KSREVIEWKS: This must be modified for the toString CIMObjectPath referenceName; try { referenceName = tmp; } catch(Exception& e) { ErrorExit(e.getMessage()); } // if (!instanceName.size()) // ErrorExit("InstanceName parameter is null"); // KSREVIEWKS: Test the following! Boolean localOnly = true; Boolean includeQualifiers = true; Boolean includeClassOrigin = false; if (!(tmp = qs.findValue("LocalOnly"))) localOnly = false; if (!(tmp = qs.findValue("IncludeQualifiers"))) includeQualifiers = false; if ((tmp = qs.findValue("IncludeClassOrigin"))) includeClassOrigin = true; try { CIMClient client; String host; Uint32 portNumber; getHostAndPort (host, portNumber); client.connect (host, portNumber, String::EMPTY, String::EMPTY); CIMInstance cimInstance = client.getInstance(nameSpace, referenceName, localOnly, includeClassOrigin, includeClassOrigin); PrintInstance(nameSpace, cimInstance, localOnly, includeQualifiers, includeClassOrigin); } catch(Exception& e) { ErrorExit(e.getMessage()); }}/** PrintInstancetable - Print a table of enumerated instance information*/void PrintInstanceTableRow(const CIMNamespaceName & nameSpace, CIMInstance CIMInstance){ // KSREVIEWKS: Need to add code here // Loop for each property String className = CIMInstance.getClassName().getString(); for (Uint32 i = 0, n = CIMInstance.getPropertyCount(); i < n; i++) { // KSREVIEW: All of this can become PrintProperty() CIMProperty property = CIMInstance.getProperty(i); const CIMValue& value = property.getValue(); String href = BuildOperationHref("GetPropertyDeclaration", nameSpace); href.append("ClassName="); href.append(className); href.append("&"); href.append("PropertyName="); href.append(property.getName().getString()); href.append("&"); cout << "<tr>\n"; cout << "<td>"; PrintAHref(href, property.getName().getString()); cout << "</td>"; /*cout << "<tr>\n"; cout << "<td>"; cout << property.getName().getString(); cout << "</td>";*/ cout << "<td>" << cimTypeToString (value.getType ()) << "</td>\n"; String valueString = value.toString(); if (valueString.size()) cout << "<td>" << valueString << "</td>\n"; else cout << "<td>null</td>\n"; // Output the ClassOrigin // KSREVIEW: Make this optional cout << "<td>" << property.getClassOrigin() << "</td>\n"; // Output the Propagated field cout << "<td>" << (property.getPropagated() ? "true" : "false"); cout << "</td>\n"; cout << "</tr>\n"; }}/*************************************************************************** EnumerateInstances Function***************************************************************************/static void EnumerateInstances(const CGIQueryString& qs){ // Get NameSpace: CIMNamespaceName nameSpace = GetNameSpaceQueryField(qs); // Get ClassName: CIMName className; const char* tmp; if ((tmp = qs.findValue("ClassName"))) className = CIMName (tmp); Boolean localOnly = true; Boolean includeQualifiers = true; Boolean includeClassOrigin = false; if (!(tmp = qs.findValue("LocalOnly"))) localOnly = false; if (!(tmp = qs.findValue("IncludeQualifiers"))) includeQualifiers = false; if ((tmp = qs.findValue("IncludeClassOrigin"))) includeClassOrigin = true; // Get DeepInheritance: Boolean deepInheritance = false; if (qs.findValue("DeepInheritance")) deepInheritance = true; CIMPropertyList propertyList; //KSREVIEWKS: Fill out the property list // Invoke the method: try { // Time the connection Stopwatch elapsedTime; elapsedTime.start(); CIMClient client; String host; Uint32 portNumber; getHostAndPort (host, portNumber); client.connect (host, portNumber, String::EMPTY, String::EMPTY); /* virtual Array<CIMInstance> enumerateInstances( const CIMNamespaceName& nameSpace, const CIMName& className, Boolean deepIn heritance = true, Boolean localOnly = true, Boolean includeQualifiers = false, Boolean includeClassOrigin = false, const CIMPropertyList& propertyList = CIMPropertyList()); */ Array<CIMInstance> instances = client.enumerateInstances( nameSpace, className, deepInheritance, localOnly, includeQualifiers, includeClassOrigin, propertyList); elapsedTime.stop(); // Generate Table Head //Number of Colums is determined by number of properties // Name row and each property row. ?? from class or instance // step through properties and get name fields. Generate // header entries and a table so that individual entires can // be gotten. PrintHTMLHead("GetInstanceNames", "EnumerateInstances Result"); //ALAGS : commented following block /*for (Uint32 i = 0, n = instances.size(); i < n; i++) { cout << "<th>xxx</th>"; } cout << "</tr>\n";*/ //Generate a Table Row for every instance found for (Uint32 i = 0, n = instances.size(); i < n; i++) { cout << "<table border=1>\n"; cout << "<th colspan=5>Instance " << i+1 << "</th>\n"; cout << "<tr>\n"; cout << "<th>CIMName</th>\n"; cout << "<th>CIMType</th>\n"; cout << "<th>CIMValue</th>\n"; cout << "<th>ClassOrigin</th>\n"; cout << "<th>Propagated</th>\n"; cout << "</tr>\n"; PrintInstanceTableRow(nameSpace, instances[i]); cout << "</table>\n<br><br>"; //ALAGS : MOF disp can be put into a function cout << "<h2>Display MOF for Instance " << i+1 << "</h2>"; cout << "<pre>"; Buffer x; MofWriter::appendInstanceElement(x, instances[i]); x.append('\0'); mofFormat(cout, x.getData(), 4); cout << "</pre><br><br>"; } // Close the HTML Table //cout << "</table>\n"; // Close the Page cout << "<p>Returned " << instances.size() << " Instance(s) "; cout << " in " << elapsedTime.getElapsed() << " Seconds</p>\n"; cout << "</body>\n" << "</html>\n"; } catch(Exception& e) { ErrorExit(e.getMessage()); }}/*************************************************************************** GetProperty Function***************************************************************************/ static void GetProperty(const CGIQueryString& qs){ // Get NameSpace: CIMNamespaceName nameSpace = GetNameSpaceQueryField(qs); // Get ClassName: // String instanceName; CIMName propertyName; const char* inputInstanceName; const char* tmp; if (!(inputInstanceName = qs.findValue("InstanceName"))) ErrorExit("Missing InstanceName field"); // This must be modified for the toString KSREVIEW KS CIMObjectPath referenceName; // Convert instanceName to referenceName try { referenceName = inputInstanceName; } catch(Exception& e) { ErrorExit(e.getMessage()); } if (!(tmp = qs.findValue("PropertyName"))) ErrorExit("Missing propertyName field"); else propertyName = CIMName (tmp); try { CIMClient client; String host; Uint32 portNumber; getHostAndPort (host, portNumber); client.connect (host, portNumber, String::EMPTY, String::EMPTY); //ALAGS : this call does not return the correct "value type" // for a given property. So use the fix implemented below CIMValue value = client.getProperty(nameSpace, referenceName, propertyName); PrintHTMLHead("GetProperty", "GetProperty Result"); cout << "<B>Instance = </B> " << inputInstanceName << "\n<br>"; //ALAGS : use this for getting correct property type // as value.getType() is not returning correct type when "value" // is obtained through a client.getProperty(...) call CIMInstance cimInstance = client.getInstance(nameSpace, referenceName); CIMValue value1; CIMProperty property; for (Uint32 i = 0, n = cimInstance.getPropertyCount(); i < n; i++) { property = cimInstance.getProperty(i); if(String::equalNoCase(property.getName().getString(), propertyName.getString())) { value1 = property.getValue(); break; } } cout << "<B>Property = </B> " << property.getName().getString() << "\n\n<br>"; cout << "<B>Value Type = </B>"; cout << cimTypeToString (value1.getType ()) << "\n\n<br>"; String valueString = value1.toString(); cout << "<B>Value = </B> "; if (valueString.size()) cout << " " << valueString << " \n\n<br>"; else cout << " NULL \n\n<br>"; cout << "</body>\n" << "</html>\n"; } catch(Exception& e) { ErrorExit(e.getMessage()); }}/*************************************************************************** SetProperty Function***************************************************************************/static void SetProperty(const CGIQueryString& qs){ // Get NameSpace: CIMNamespaceName nameSpace = GetNameSpaceQueryField(qs); // Get InstanceName: const char* tmp; if (!(tmp = qs.findValue("InstanceName"))) ErrorExit("Missing InstanceName field"); // KSREVIEWKS: This must be modified for the toString CIMObjectPath referenceName; CIMName propertyName; CIMValue newValue; try { referenceName = tmp; } catch(Exception& e) { ErrorExit(e.getMessage()); } if ((tmp = qs.findValue("PropertyName"))) propertyName = CIMName (tmp); String strValue; if ((tmp = qs.findValue("NewValue"))) strValue = tmp; newValue = CIMValue (strValue); try { CIMClient client; String host; Uint32 portNumber; getHostAndPort (host, portNumber); client.connect (host, portNumber, String::EMPTY, String::EMPTY); client.setProperty(nameSpace, referenceName, propertyName, newValue); String message = "Property \""; message.append(propertyName.getString()); message.append("\" has been set to "); message.append(newValue.toString()); PrintHTMLHead("SetProperty", "Set Property Result"); cout << " <h1>" << message << "</h1>\n"; cout << " </body>\n"; cout << "</html>\n"; } catch(Exception& e) { ErrorExit(e.getMessage()); }}/*************************************************************************** DeleteInstance Function***************************************************************************/static void DeleteInstance(const CGIQueryString& qs){ // Get NameSpace: CIMNamespaceName nameSpace = GetNameSpaceQueryField(qs); // Get InstanceName: const char* tmp; if (!(tmp = qs.findValue("InstanceName"))) ErrorExit("Missing InstanceName field");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -