2012-07-19 23:47:54 +00:00
using System ;
using System.Collections.Generic ;
using System.Text ;
using System.Text.RegularExpressions ;
using CookComputing.XmlRpc ;
using log4net ;
namespace uds
{
public class rpc
{
private static ILog logger = LogManager . GetLogger ( typeof ( rpc ) ) ;
private static rpc _manager = null ;
private static bool _unlocked = false ;
private string _id = null ;
private IUDS service = null ;
private const string LOGON_MSG = "logon" ;
private const string LOGOFF_MSG = "logoff" ;
2012-11-26 09:08:10 +00:00
private const string INFO_MSG = "information" ;
2012-07-19 23:47:54 +00:00
private const string READY_MSG = "ready" ;
private const string IP_MSG = "ip" ;
2012-12-12 12:46:45 +00:00
private const string LOG_MSG = "log" ;
2012-07-19 23:47:54 +00:00
private rpc ( string url )
{
System . Net . ServicePointManager . ServerCertificateValidationCallback = delegate { return true ; } ;
logger . Debug ( "Initializing rpc at \"" + url + "\" with timeout of " + config . timeOut . ToString ( ) ) ;
_id = null ;
service = XmlRpcProxyGen . Create < IUDS > ( ) ;
service . Timeout = config . timeOut * 1000 ;
service . Url = url ;
}
public static void Initialize ( string broker , bool useSSl )
{
if ( ! _unlocked )
throw new Exception ( "access denied" ) ;
_manager = null ; // Release previous manager
string protocol = useSSl ? "https" : "http" ;
_manager = new rpc ( protocol + "://" + broker + "/xmlrpc" ) ;
}
public static void Unlock ( string pass )
{
if ( pass = = string . Join ( "" , new string [ ] { "m" , "a" , "m" , "0" } ) )
_unlocked = true ;
}
public static rpc Manager
{
get { return _manager ; }
}
private static string Unscramble ( string str )
{
if ( str = = "" | | str = = null )
return str ;
StringBuilder sb = new StringBuilder ( ) ;
int val = 0x32 ;
for ( int i = 0 ; i < str . Length ; i + = 2 )
{
int c = Int32 . Parse ( str . Substring ( i , 2 ) , System . Globalization . NumberStyles . HexNumber ) ^ val ;
val = ( val + c ) & 0xFF ;
sb . Insert ( 0 , Convert . ToString ( Convert . ToChar ( c ) ) ) ;
}
return sb . ToString ( ) ;
}
public bool Test ( )
{
try
{
logger . Debug ( "Invoking service test" ) ;
service . Test ( ) ;
}
catch ( Exception e )
{
logger . Debug ( "Exception" , e ) ;
return false ;
}
return true ;
}
public string Message ( string message , string data )
{
if ( _id = = null )
{
logger . Debug ( "The id is null, recreating id" ) ;
List < Info . Computer . InterfaceInfo > interfaces = Info . Computer . GetInterfacesInfo ( ) ;
_id = string . Join ( "," , interfaces . ConvertAll < String > ( i = > i . mac ) . ToArray ( ) ) ;
logger . Debug ( "The id is now " + _id ) ;
}
logger . Debug ( "Sending message to broker: " + _id + ", " + message + ", " + data ) ;
string ret = Unscramble ( service . Message ( _id , message , data ) ) ;
logger . Debug ( "Returned value: " + ret ) ;
return ret ;
}
2012-12-12 12:46:45 +00:00
public static string [ ] Logon ( string username )
2012-07-19 23:47:54 +00:00
{
if ( rpc . Manager ! = null )
{
2012-12-12 12:46:45 +00:00
logger . Info ( "Invoking remote logon of user " + username ) ;
2012-07-19 23:47:54 +00:00
try
{
2012-12-12 12:46:45 +00:00
return rpc . Manager . Message ( LOGON_MSG , username ) . Split ( '\t' ) ;
2012-07-19 23:47:54 +00:00
}
catch ( Exception )
{
2012-12-12 12:46:45 +00:00
logger . Fatal ( "Could cont contact broker at " + rpc . Manager . service . Url ) ;
return new string [ 0 ] ;
2012-07-19 23:47:54 +00:00
}
}
2012-12-12 12:46:45 +00:00
logger . Debug ( "Remote logon not invoked. RPC Disabled" ) ;
return new string [ 0 ] ;
2012-07-19 23:47:54 +00:00
}
public static void Logoff ( string username )
{
if ( rpc . Manager ! = null )
{
logger . Debug ( "Invoking remote logoff of user " + username ) ;
try
{
rpc . Manager . Message ( LOGOFF_MSG , username ) ;
}
catch ( Exception )
{
logger . Info ( "Could cont contact broker at " + rpc . Manager . service . Url ) ;
}
}
else
{
logger . Debug ( "Remote logoff not invoked. RPC Disabled" ) ;
}
}
public static string GetInfo ( )
{
string res = null ;
if ( rpc . Manager ! = null )
{
logger . Debug ( "Invoking remote GetInfo" ) ;
try
{
res = rpc . Manager . Message ( INFO_MSG , "" ) ;
}
catch ( Exception )
{
res = null ;
}
}
return res ;
}
public static bool SetReady ( )
{
bool ok = false ;
if ( rpc . Manager ! = null )
{
2013-03-12 00:15:27 +00:00
logger . Info ( "Machine is Ready" ) ;
2012-07-19 23:47:54 +00:00
try
{
List < Info . Computer . InterfaceInfo > interfaces = Info . Computer . GetInterfacesInfo ( ) ;
string info = string . Join ( "," , interfaces . ConvertAll < String > ( i = > i . mac + "=" + i . ip ) . ToArray ( ) ) ;
rpc . Manager . Message ( READY_MSG , info ) ;
ok = true ;
}
catch ( Exception )
{
}
}
return ok ;
}
public static bool NotifyIPChange ( )
{
bool ok = false ;
if ( rpc . Manager ! = null )
{
logger . Debug ( "Informing broker of ip change" ) ;
try
{
List < Info . Computer . InterfaceInfo > interfaces = Info . Computer . GetInterfacesInfo ( ) ;
string info = string . Join ( "," , interfaces . ConvertAll < String > ( i = > i . mac + "=" + i . ip ) . ToArray ( ) ) ;
rpc . Manager . Message ( IP_MSG , info ) ;
ok = true ;
}
catch ( Exception )
{
}
}
return ok ;
}
2012-12-12 12:46:45 +00:00
public static void Log ( string msg , string Level )
{
if ( rpc . Manager ! = null )
{
logger . Debug ( "Sending message " + msg + " of level " + Level ) ;
rpc . Manager . Message ( LOG_MSG , string . Join ( "\t" , new string [ ] { msg , Level } ) ) ;
}
}
2013-03-12 00:15:27 +00:00
public static void FlushLoggers ( )
{
log4net . Repository . ILoggerRepository rep = LogManager . GetRepository ( ) ;
foreach ( log4net . Appender . IAppender appender in rep . GetAppenders ( ) )
{
var buffered = appender as log4net . Appender . BufferingAppenderSkeleton ;
if ( buffered ! = null )
buffered . Flush ( ) ;
}
}
2012-07-19 23:47:54 +00:00
public static void ResetId ( )
{
logger . Debug ( "Reseting ID of rpc" ) ;
2013-03-12 00:15:27 +00:00
FlushLoggers ( ) ;
2012-07-19 23:47:54 +00:00
if ( rpc . Manager ! = null )
rpc . Manager . _id = null ;
}
public static void ResetManager ( )
{
logger . Debug ( "Disabling rpc" ) ;
2013-03-12 00:15:27 +00:00
FlushLoggers ( ) ;
2012-07-19 23:47:54 +00:00
rpc . _manager = null ;
}
}
}