//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.SqlTools.Extensibility;
using Microsoft.SqlTools.Hosting;
using Microsoft.SqlTools.Hosting.Contracts;
using Microsoft.SqlTools.Hosting.Protocol;
using Microsoft.SqlTools.Hosting.Protocol.Channel;
using Microsoft.SqlTools.Utility;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Admin;
using Microsoft.SqlTools.ServiceLayer.Utility;
using System.Diagnostics;
namespace Microsoft.SqlTools.ServiceLayer.Hosting
{
///
/// SQL Tools VS Code Language Server request handler. Provides the entire JSON RPC
/// implementation for sending/receiving JSON requests and dispatching the requests to
/// handlers that are registered prior to startup.
///
public sealed class ServiceHost : ServiceHostBase
{
public const string ProviderName = "MSSQL";
private const string ProviderDescription = "Microsoft SQL Server";
private const string ProviderProtocolVersion = "1.0";
///
/// This timeout limits the amount of time that shutdown tasks can take to complete
/// prior to the process shutting down.
///
private const int ShutdownTimeoutInSeconds = 120;
public static readonly string[] CompletionTriggerCharacters = new string[] { ".", "-", ":", "\\", "[", "\"" };
private IMultiServiceProvider serviceProvider;
#region Singleton Instance Code
///
/// Singleton instance of the service host for internal storage
///
private static readonly Lazy instance = new Lazy(() => new ServiceHost());
///
/// Current instance of the ServiceHost
///
public static ServiceHost Instance
{
get { return instance.Value; }
}
///
/// Constructs new instance of ServiceHost using the host and profile details provided.
/// Access is private to ensure only one instance exists at a time.
///
private ServiceHost() : base(new StdioServerChannel())
{
// Initialize the shutdown activities
shutdownCallbacks = new List();
initializeCallbacks = new List();
}
public IMultiServiceProvider ServiceProvider
{
get
{
return serviceProvider;
}
internal set
{
serviceProvider = value;
}
}
///
/// Provide initialization that must occur after the service host is started
///
public void InitializeRequestHandlers()
{
// Register the requests that this service host will handle
this.SetRequestHandler(InitializeRequest.Type, HandleInitializeRequest);
this.SetRequestHandler(CapabilitiesRequest.Type, HandleCapabilitiesRequest);
this.SetRequestHandler(ShutdownRequest.Type, HandleShutdownRequest);
this.SetRequestHandler(VersionRequest.Type, HandleVersionRequest);
}
#endregion
#region Member Variables
///
/// Delegate definition for the host shutdown event
///
///
///
public delegate Task ShutdownCallback(object shutdownParams, RequestContext