using System;
using System.IO;
using System.Threading;
using System.Xml;
using GrassValley.Mseries.AppServer;
using GrassValley.Mseries.AppServerLog;
using GrassValley.Mseries.ChanStatus;
using GrassValley.Mseries.ConfigMgr;
using GrassValley.Mseries.Control;
using GrassValley.Mseries.DVCapture;
using GrassValley.Mseries.Editor;
using GrassValley.Mseries.MediaMgr;
using GrassValley.Mseries.Security;
using GrassValley.Mseries.Status;
using GrassValley.Mseries.TransferQueue;
// Leave the following "css_reference" lines in when running with CS-Script. They're
// more than just comments - they tell CS-Script which DLLs to load if the DLL
// name does not match the namespace that it implements.
//css_reference Credentials.dll;
//css_reference ServerUtil.dll;
namespace K2Script
{
class Test
{
static int Main(string[] args)
{
try
{
// get the application's name & create a unique suite name
string appName = Environment.GetCommandLineArgs()[0];
string suiteName = appName + "_" + System.Guid.NewGuid().ToString("N");
// fill in these values for your connection (i.e. hardcode them, pass in cmdline arguments, read from file, read from console, etc.)
string host = "";
string username = "";
string password = "";
string domain = "";
// Connect to host & pass in credentials. The "using" block disposes connection when done.
using ( Connection connection = new Connection(appName, suiteName,
host, username, password, domain) )
{
// do work only if we have a valid connection
if ( connection.IsConnected )
{
try
{
string filename = @"C:\temp\savedConfig.xml";
// create ConfigMgr
string xmlConfig = "";
IConfigMgr configMgr = connection.AppServer.CreateConfigMgr(appName);
// load the xml string into an XmlDocument
XmlDocument xmlDoc = new XmlDocument();
/////////////////////////////////////////////////////////////////
// Example 1: get the K2's xml configuration file and save it to a local file
/////////////////////////////////////////////////////////////////
// get config xml document
configMgr.GetConfigXml(out xmlConfig);
// note that this function also parses & validates the XML data. If the data is invalid
// this function will throw an XmlException
xmlDoc.LoadXml(xmlConfig);
// uncomment this line to print out xml
// Console.WriteLine(xmlDoc.DocumentElement.OuterXml);
// if file already exists, delete it
if ( File.Exists(filename) )
File.Delete(filename);
// save the XmlDocument to a file
xmlDoc.Save(filename);
/*
//////////////////////////////////////////////////////////////////////
// Example 2: load a configuration from a local file and set it in the K2 Client
//////////////////////////////////////////////////////////////////////
// if saved config file exists, load it
if ( File.Exists(filename) )
{
// load an XML file from disk into an XmlDocument. Note that this function also
// parses and validates the XML in the file. If the data in the file is not valid XML,
// this function will throw an exception (XmlException, ArgumentException, etc.)
xmlDoc.Load(filename);
// convert the XmlDocument contents to a normal string
xmlConfig = xmlDoc.DocumentElement.OuterXml;
// make sure that xmlConfig data looks valid to ConfigMgr
bool validConfigData = configMgr.ValidateConfigXml(xmlConfig);
// if data looks good, set the config xml data
if ( validConfigData )
configMgr.SetConfigXml(xmlConfig);
}
// if saved config file does not exist, print error.
else
Console.WriteLine("ERROR: Cannot load '{0}'. File does not exist.", filename);
*/
//cleanup
configMgr.Dispose();
}
catch (Exception e)
{
// add code to handle exception
// for now, print exception
Console.WriteLine(e);
}
}
}
}
catch (SystemException se)
{
Console.WriteLine(se);
}
return 0;
}
}
// Connection helper class: sets up credentials & hostname, connects to K2
public class Connection: IDisposable
{
private bool _connected = false;
private AppServerMgrProxy _appServerMgrProxy = null;
private IAppServer _appServer = null;
///
/// Object that connects to the specified K2 credentials
///
/// The application's name.
/// The suite's name.
/// The K2's host name.
/// The username.
/// The password.
/// The domain.
public Connection(string appname, string suitename, string host, string username, string password, string domain)
{
try
{
//////////////////////////
// SETUP
//////////////////////////
if ( null == host || host.Length == 0)
{
Console.WriteLine("Empty host name passed to Connection object.");
return;
}
//////////////////////////
// CONNECT TO K2
//////////////////////////
// create an AppServerMgr proxy object
_appServerMgrProxy = new AppServerMgrProxy();
// tell it which K2 we're connecting to
_appServerMgrProxy.SetHost(host);
// give it the user credentials we want to use
if ( username.Length > 0 )
_appServerMgrProxy.SetUserCredentials(username, password, domain, false);
// connect to the K2's AppServerMgr
if ( !_appServerMgrProxy.Connect() )
{
// if the connection failed, report an error and exit
Console.WriteLine("ERROR: Could not connect to AppService on host '" + host + "'. Check the host name.");
_appServerMgrProxy = null;
return;
}
// if we got here we're connected to K2 AppService
//////////////////////////
// CREATE AN APPSERVER
//////////////////////////
// now create an AppServer
bool newConnection = false;
_appServer = _appServerMgrProxy.CreateAppServer(suitename,
appname, out newConnection);
_connected = true;
}
catch (SystemException se)
{
Console.WriteLine(se);
}
}
///
/// Gets a value indicating whether this instance is connected.
///
///
/// true if this instance is connected; otherwise, false.
///
public bool IsConnected
{
get
{
return _connected;
}
}
///
/// Gets the AppServerMgr.
///
/// The AppServerMgr
public AppServerMgrProxy AppServerMgr
{
get
{
if ( _appServerMgrProxy == null )
Console.WriteLine("WARNING: AppServerMgrProxy object == null!");
return _appServerMgrProxy;
}
}
///
/// Gets the AppServer
///
/// The AppServer
public IAppServer AppServer
{
get
{
if ( _appServerMgrProxy == null )
Console.WriteLine("WARNING: AppServerProxy object == null!");
return _appServer;
}
}
///
/// Disposes this instance.
///
public void Dispose()
{
if ( _appServer != null )
_appServer.CloseConnection();
}
}
}