ServerManager Example


This example is an active server page (ASP) that uses a ServerManager object stored in the ASP Application collection. Users connect to the page with a browser and select a server and a tag whose snapshot value is then displayed.  The connection is made using a PI trust and the resulting PI user is also displayed.

This example is built by creating two pages in a virtual directory under Internet Information Server (IIS).  

1. First create a file called "global.asa". This  page is global code available to all ASP pages in this directory.  Insert the following code in the page:

Sub Application_OnStart
set Application("srvrmgr") = Server.CreateObject("PISDK.ServerManager")
End Sub

2. Now create the asp page that the user will browse.  Name it something appropriate (e.g. servermgr.asp).  Insert the following code in the page.


<%@ Language=Javascript >
  var serverIndex;
  var pointIndex;
   function DoSubmit()
      var i;
      for(i=0;i< document.form1.select1.length;i++)
			serverIndex = i;
      for(i=0;i< document.form1.select2.length;i++)
			pointIndex = i;
	  // set the selected index's in the form so we 
	  // can restore them after running serverside code
	  document.form1.srvrindx.value = serverIndex;
	  document.form1.tagindx.value = pointIndex;
	  document.form1.firstTime.value = 1;
var snapvalue;
var piuser;
var errorstring1 = "";
var errorstring2 = "";
var tagName,srvrName;
var ServerIndex;
var PointIndex;
  if(Request.Form.Item("firstTime")== 1)
		var srvrmgr;
		srvrmgr = Application("srvrmgr")
		var srvr;
		srvrName = Request.Form("select1").Item;
		srvr = srvrmgr.Item(srvrName);
		var pt;
		tagName = Request.Form("select2");
		pt = srvr.PIPoints(Request.Form("select2"));
		var val="empty";
		val = pt.Data.Snapshot;
		snapvalue = val;
		piuser = srvr.CurrentUser;
		ServerIndex = Request.Form.Item("srvrindx");
		PointIndex = Request.Form.Item("tagindx");
		errorstring1 = "Error encountered. Error number = " + err.number+"<p>";
		if(err.number == -2147220031)
		   errorstring2 = "No trust available for current user on selected server." + "<b>";

<FONT color=royalblue><FONT size=4> <FONT face="Lucida Sans Unicode" ><STRONG>Server Manager Test</STRONG></FONT> </FONT></FONT> <br>
<form action="servermgr.asp" method="post" id=form1 name=form1>
<P><FONT color=purple>Select Server<BR></FONT>
<SELECT id=select1 name=select1 style="HEIGHT: 65px; WIDTH: 142px"> 
   <OPTION value=Server1 selected>Server1</OPTION>
   <OPTION value=Server2>Server2</OPTION>
   <OPTION value=Server3>Server3</OPTION>
   <OPTION value=Server4>Server4</OPTION>
   <OPTION value=Server5>Server5</OPTION>
  <p><FONT color=purple>
  Select Tag:<BR></FONT>
<SELECT id=select2 name=select2 
style="HEIGHT: 22px; LEFT: 152px; TOP: 91px; WIDTH: 173px"> 
 <OPTION value=sinusoid selected>sinusoid</OPTION>
 <OPTION value=cdm158>cdm158</OPTION>
 <OPTION value=cdt158>cdt158</OPTION>
 <OPTION value=sinusoidu>sinusoidu</OPTION>
 <OPTION value=ba:active.1>ba:active.1</OPTION>
<INPUT type=hidden name=srvrindx>
<INPUT type=hidden name=tagindx>
<INPUT type=hidden name=firstTime value=0>
<INPUT id=submit1 name=submit1 type=submit value="Get Snapshot" onclick="return DoSubmit();"></P>
<FONT color=purple>
Server: <span style="COLOR: green"> <%=srvrName%> </span></FONT><FONT color=purple>Tag Name</FONT>
 : <span style="COLOR: green"> <%=tagName%> </span><br><hr>
Current snapshot value is: <span CLASS="foo" STYLE="COLOR: red"> <%= snapvalue %></span><br>
Current logged on user is: <span CLASS="foo" STYLE="COLOR: blue"> <%= piuser %></span><br>
<p><%=errorstring1 %> <br> <% = errorstring2 %> </form>
<INPUT id=button1 name=button1 type=button value="Server Management"onclick="window.location='mngmnt.asp';"></P>
   document.form1.select1.item(<%=ServerIndex%>).selected = true;
   document.form1.select2.item(<%=PointIndex%>).selected = true;


3.  The ASP page has a form that submits to itself.  The PI servers in the select box, shown above as Server1 - Server5 should reflect servers available to your application as entries in the Known Servers Table. In addition these Servers should be configured with PI trust relationships for the users who will be viewing the page.  Trusts are flexible enough to define a variety of needs within an Intranet.  The ASP page will run under the Microsoft surrogate process, DLLHOST.exe, so this can be entered as the application in the trust table. 


4.  A button on this ASP page, labeled "Server Management", links to another page which displays the current count of connections in the global ServerManager object and provides a means to call SlimFast to remove connections that are not currently in use.  Build this page by creating a file called "mngmnt.asp" and copying in the following contents:

<%@ Language=JavaScript %>
var ServerCount;
var srvrmgr;
var doslimfast;
srvrmgr = Application("srvrmgr");
if(Request.Form.Item("doslimfast") == 1)
   doslimfast = 0;
ServerCount = srvrmgr.Count;
function SetSlimFast()
   document.form1.doslimfast.value = 1;
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
Current server/username connection count is: <%=ServerCount%>
<P><FORM action="mngmnt.asp" method="post" id=form1 name=form1>
<INPUT id="submit1" name="slimfast" type="submit" value="Slimfast" onclick="return SetSlimFast();">
<INPUT id="goback" Value="Back" type="button" onclick="window.location='servermgr.asp';">
<INPUT type="hidden" name="doslimfast">
</FORM> </P>



5.  To fully secure access to the application, the virtual directory should be set to use "Integrated Windows Authorization" in IIS.  Using Windows Explorer, the ASP file should be made accessible only to the desired domain users.  When configured in this manner, browsers connecting to this page will be issued a challenge (often silently) and if the authentication is successful the page will run in IIS impersonating the logged on user of the browser.  Behind the scenes, the PI-SDK obtains the identity of the logged in user and passes it to the server.  The server validates this identity against the domain controller.  This means that users outside the domain and associated trusted domains can not be validated and will not be granted a trust connection. 


Enabling Operational Intelligence