using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pluto.Api;
using System.Net;
using Pluto.Registration;
namespace ChoiceMSLConnect
{
internal static class RegistrationWrapper
{
// Allow the Registration wrapper to let the Service enter into a different 'disconnected' state
internal static int HasConnectivityErrorCountInARow = 0;
internal static int HasConnectivityErrorCountMaxBeforeStateChange = 10;
internal static string RegistrationHOST = "";
internal static uint RegistrationPORT = 0;
private const string DEFAULT_REGISTRATION_HOST_N_PORT_URL = "http://ppsmobile.mckesson.com/mobile1/REGURL.htm";
private const string DEFAULT_REGISTRATION_HOST_URL = "ppsmobile.mckesson.com";
private const int DEFAULT_REGISTRATION_CHANNEL_PORT = 443;
#region Connectivity
///
/// Check to make sure we can communicate with the Registration Service.
/// Check the Server's ip & port for connectivity as well as that this
/// computer has connectivity.
/// This way we won't throw any connectivity errors, which is good.
///
/// true, if successfully can connect, false otherwise
private static bool HasConnectivity()
{
try
{
bool bCanConnect = false;
bool bUseConfiguration = true;
// Try the overwritten Registration Host and Port first, if exists
if (RegistrationHOST != "" && RegistrationPORT > 0)
{
bCanConnect = Yaulw.Net.IPHostHelper.HasConnectivity(RegistrationHOST, RegistrationPORT, 0);
if (bCanConnect)
bUseConfiguration = false;
}
// Else, fall-back on the configuration, if we couldn't connect
if (!bCanConnect)
{
bCanConnect = Yaulw.Net.IPHostHelper.HasConnectivity(DEFAULT_REGISTRATION_HOST_URL, (uint)DEFAULT_REGISTRATION_CHANNEL_PORT, 0);
if (bCanConnect)
bUseConfiguration = true;
}
if (bCanConnect)
{
if (bUseConfiguration)
{
IPAddress ip = Yaulw.Net.IPHostHelper.GetIpForHost(DEFAULT_REGISTRATION_HOST_URL);
RegistrationAPI.API.SetNetworkSettings(ip.ToString(), (uint)DEFAULT_REGISTRATION_CHANNEL_PORT);
}
else
{
IPAddress ip = Yaulw.Net.IPHostHelper.GetIpForHost(RegistrationHOST);
RegistrationAPI.API.SetNetworkSettings(ip.ToString(), RegistrationPORT);
}
HasConnectivityErrorCountInARow = 0;
}
else
{
// Try to retrieve the latest Host and Port Setting online
try
{
string HostNPortSetOnline = Yaulw.Net.WCHelper.ScreenScrapeFromURL(DEFAULT_REGISTRATION_HOST_N_PORT_URL);
if (!String.IsNullOrEmpty(HostNPortSetOnline))
{
RegistrationHOST = HostNPortSetOnline.Split(';')[0];
RegistrationPORT = uint.Parse(HostNPortSetOnline.Split(';')[1]);
}
}
catch (Exception)
{
/* ignore */
}
// Try to connection one more time
if (RegistrationHOST != "" && RegistrationPORT > 0)
bCanConnect = Yaulw.Net.IPHostHelper.HasConnectivity(RegistrationHOST, RegistrationPORT, 0);
// if we can connect, we are Golden
if (bCanConnect)
{
IPAddress ip = Yaulw.Net.IPHostHelper.GetIpForHost(RegistrationHOST);
RegistrationAPI.API.SetNetworkSettings(ip.ToString(), RegistrationPORT);
HasConnectivityErrorCountInARow = 0;
}
else
{
// Connectivity Errors too plentiful - Change the state of the service to reflect that
if (HasConnectivityErrorCountInARow >= HasConnectivityErrorCountMaxBeforeStateChange)
{
//PlutoService.state = HostGUIDstate.no_connectivity;
}
else
{
HasConnectivityErrorCountInARow++;
}
}
}
return bCanConnect;
}
catch (Exception)
{
}
return false;
}
#endregion
#region Register Methods
///
/// Retrieve Pin for UserApiKey
///
///
///
public static string RetrievePinForUserApiKey(string UserApiKey)
{
try
{
if (!String.IsNullOrEmpty(UserApiKey) && HasConnectivity())
{
string Pin = RegistrationAPI.API.RetrieveUserApiKeyPin(UserApiKey);
return Pin;
}
}
catch (Exception)
{
/* ignore */
}
return String.Empty;
}
///
/// Get Api Mobile IP/Port and * optionally * check Connectivity
///
/// true if can connect (only bCheckConnectivity is true), else true if it can get IP/port, false otherwise
public static bool GetApiMobile(string UserApiKey, bool bCheckConnectivity, out string ip, out int port)
{
ip = String.Empty;
port = 0;
try
{
if (!String.IsNullOrEmpty(UserApiKey) && HasConnectivity())
{
SystemAccessVerifier verifier = new SystemAccessVerifier(UserApiKey);
Host host = RegistrationAPI.API.GetApiMobile(verifier.SystemApiKey);
ip = host.host;
port = host.port;
if (bCheckConnectivity)
return RegistrationAPI.API.IsApiMobileReachable(verifier.SystemApiKey);
else
return true;
}
}
catch (Exception)
{
/* ignore */
}
return false;
}
///
/// Get Api Mobile IP/Port
///
///
public static bool GetApiMobileSystem(string SystemApiKey, out string ip, out int port)
{
ip = String.Empty;
port = 0;
try
{
if (!String.IsNullOrEmpty(SystemApiKey) && HasConnectivity())
{
Host host = RegistrationAPI.API.GetApiMobile(SystemApiKey);
ip = host.host;
port = host.port;
return RegistrationAPI.API.IsApiMobileReachable(SystemApiKey);
}
}
catch (Exception)
{
/* ignore */
}
return false;
}
#endregion
}
}