Skip to content

Instantly share code, notes, and snippets.

@mvacha
Created July 17, 2016 16:41
Show Gist options
  • Save mvacha/582292d444ede640526fe3119a727ec2 to your computer and use it in GitHub Desktop.
Save mvacha/582292d444ede640526fe3119a727ec2 to your computer and use it in GitHub Desktop.
public bool Start()
{
Gateway = new GatewayConnection(IPAddress, Port, UserName, Password, AppId);
var connectedEvent = new ManualResetEvent(false);
var cancel = new CancellationTokenSource();
var connectedSuccessfully = false;
var rootNode = default(SiteNode);
var connectedTCS = new TaskCompletionSource<Result>(cancel);
var userStatusTCS = new TaskCompletionSource<Result>(cancel);
var profileTCS = new TaskCompletionSource<Result<SiteNode>>(cancel);
EventHandler<ConnectionEventArgs> connectedHandler = (s, e) =>
{
var connection = (GatewayConnection)s;
Log.Info($"Start - ConnectionStatus: {connection.Status}");
connectedTCS.TrySetResult(Result.Create(connection.Status == ConnectionStatus.Connected));
};
EventHandler<AuthenticationEventArgs> userStatusHandler = (s, e) =>
{
Log.Info($"Start - UserStatus: {e.AuthenticationStatus }");
userStatusTCS.TrySetResult(Result.Create(e.AuthenticationStatus == AuthenticationStatus.LoggedIn));
};
EventHandler<ProfileEventArgs> profileEventHandler = (s, e) =>
{
Log.Info($"Start - profileEvent: {e.EventType}");
if (Gateway == null)
{
profileTCS.TrySetResult(Result.Fail<SiteNode>("Gateway is null"));
return;
}
if (e.EventType == ProfileEventType.ProfileNamesReceived)
{
if (!Gateway.SessionContext.ProfileService.ProfileNames.Contains(Profile))
{
Log.Error("Wrong profile name");
profileTCS.TrySetResult(Result.Fail<SiteNode>("Wrong profile name"));
}
else
{
Thread.Sleep(250);
Gateway.SessionContext.ProfileService.GetProfile(Profile);
}
}
else if (e.EventType == ProfileEventType.ProfileReceived)
{
profileTCS.TrySetResult(Result.Ok(Gateway.SessionContext.ProfileService.CurrentProfile));
}
};
Task.Factory.StartNew(async () =>
{
var result = await connectedTCS.Task.ToResult()
.OnSuccess(async () => await userStatusTCS.Task.ToResult())
.OnSuccess(async () => await profileTCS.Task.ToResult());
result.OnSuccess(() =>
{
connectedSuccessfully = true;
rootNode = profileTCS.Task.Result.Value;
connectedEvent.Set();
})
.OnFailure(() =>
{
connectedEvent.Set();
});
});
try
{
Gateway.ConnectionEvent += connectedHandler;
Gateway.SessionContext.ProfileService.UserStatusChanged += userStatusHandler;
Gateway.SessionContext.ProfileService.ProfileEventReceived += profileEventHandler;
Gateway.Connect();
connectedEvent.WaitStoppable();
cancel.Cancel();
if (connectedSuccessfully)
{
Gateway.ConnectionEvent += OnConnectionEvent;
Gateway.SessionContext.ProfileService.UserStatusChanged += OnUserStatusChanged;
Gateway.SessionContext.ProfileService.ProfileEventReceived += OnProfileEventReceived;
}
else
{
Gateway.Disconnect();
Gateway.Dispose();
Gateway = null;
}
return connectedSuccessfully;
}
finally
{
Gateway.ConnectionEvent -= connectedHandler;
Gateway.SessionContext.ProfileService.UserStatusChanged -= userStatusHandler;
Gateway.SessionContext.ProfileService.ProfileEventReceived -= profileEventHandler;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment