Skip to content

Commit

Permalink
#8, #9 implémentation terminaison, sauvegarde de script et de session.
Browse files Browse the repository at this point in the history
  • Loading branch information
mayanje committed Oct 23, 2017
1 parent b0bf2eb commit e308b57
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 13 deletions.
204 changes: 200 additions & 4 deletions Solution/LanguageServerRobot/Controller/RecordingModeController.cs
Expand Up @@ -81,6 +81,24 @@ protected string InitializeRequest
set;
}

/// <summary>
/// The original "shutdown" request
/// </summary>
protected string ShutdownRequest
{
get;
set;
}

/// <summary>
/// The original JSON Object of "shutdown" request
/// </summary>
protected JObject JShutdownObject
{
get;
set;
}

public override bool IsModeInitialized
{
get
Expand Down Expand Up @@ -237,6 +255,12 @@ public override void FromClient(string message)
{
consumed = RecordScriptMessage(Script.MessageCategory.Client, Protocol.Message_Kind.Request, message, uri, jsonObject);
}
else if (Protocol.IsShutdownRequest(jsonObject))
{
JShutdownObject = jsonObject;
ShutdownRequest = message;
consumed = true;
}
}
else if (Protocol.IsErrorResponse(jsonObject))
{//Hum...A response receive from the Client this cannot happend ==> Log it.
Expand Down Expand Up @@ -302,6 +326,19 @@ public override void FromServer(string message)
consumed = RecordScriptMessage(Script.MessageCategory.Server, Protocol.Message_Kind.Notification, message, uri, jsonObject);
}
}
else if (Protocol.IsResponseAndNotError(jsonObject))
{
string id = Protocol.GetRequestId(jsonObject);
if (JShutdownObject != null)
{
string id_shutdown = Protocol.GetRequestId(JShutdownObject);
if (id.Equals(id_shutdown))
{//Shutdown response
StopSession(message, jsonObject);
consumed = true;
}
}
}
else if (Protocol.IsErrorResponse(jsonObject))
{
string id = Protocol.GetRequestId(jsonObject);
Expand Down Expand Up @@ -348,6 +385,24 @@ private void InitializeSession(string message, JObject jsonObject)
State &= ~RecordingState.NotInitialized;
State |= RecordingState.Initialized;
SessionModel = new Session();
string sessionDirectoryPath = null;
if (!Util.CreateSessionDirectory(out sessionDirectoryPath, this.ScriptRepositoryPath))
{
string defaultDirectoryPath;
if (Util.CreateSessionDirectory(out defaultDirectoryPath))
{
LogWriter?.WriteLine(string.Format(Resource.FailCreateSessionDirectoryUseDefaultPath, sessionDirectoryPath, defaultDirectoryPath));
SessionModel.directory = defaultDirectoryPath;
}
else
{
LogWriter?.WriteLine(string.Format(Resource.FailtoCreateSessionDirectory, sessionDirectoryPath));
}
}
else
{
SessionModel.directory = sessionDirectoryPath;
}
SessionModel.initialize = InitializeRequest;
SessionModel.initialize_result = message;
}
Expand Down Expand Up @@ -413,7 +468,7 @@ private bool RecordScriptMessage(Script.MessageCategory category, Protocol.Messa
}

/// <summary>
/// Stop a script
/// Stop a script and save it
/// </summary>
/// <param name="message">The original "textDocument/didClose" notification</param>
/// <param name="jsonObject">The Json object corresponding to the "textDocument/didClose" notification</param>
Expand All @@ -439,6 +494,7 @@ private void StopScript(string message, JObject jsonObject)
Scripts.Add(script);
//Remove the uri in the entry Map, because one can reopen the document, thus creates a new script.
ScriptMap.Remove(uri);
SaveScript(script);
#if DEBUG
script.DebugDump();
#endif
Expand All @@ -447,14 +503,154 @@ private void StopScript(string message, JObject jsonObject)
}

/// <summary>
/// Save a script in a file
/// Save a script in a file using UTF-8 encoding.
/// </summary>
/// <param name="script">The script to be saved</param>
private void SaveScript(Script script)
/// <param name="scriptFile">The script firl path</param>
private bool SaveScript(Script script)
{
System.Diagnostics.Contracts.Contract.Assume(script.IsValid);
System.Diagnostics.Contracts.Contract.Requires(script.uri != null);
string filename = Util.UriToIdentifierName(script.uri + '_' + script.date);
string scriptFile = Util.UriToIdentifierName(script.uri + '_' + script.date);
scriptFile += Util.SCRIPT_FILE_EXTENSION;
if (SessionModel.directory == null)
{
LogWriter?.WriteLine(string.Format(Resource.FailToSaveScriptNoSessionDirectory, scriptFile));
return false;
}
scriptFile = System.IO.Path.Combine(SessionModel.directory, scriptFile);
bool bResult = false;
script.session = SessionModel.GetSessionFileName();
SessionModel.scripts.Add(scriptFile);
try
{
using (FileStream stream = System.IO.File.Create(scriptFile))
{
script.Write(stream);
bResult = true;
}
}
catch (System.UnauthorizedAccessException uae)
{
// The caller does not have the required permission.-or- path specified a file that
// is read-only.
LogWriter?.WriteLine(string.Format(Resource.FailToSaveScript, scriptFile, uae.Message));
}
catch (System.ArgumentNullException ane)
{
// path is null.
LogWriter?.WriteLine(string.Format(Resource.FailToSaveScript, scriptFile, ane.Message));
}
catch (System.ArgumentException ae)
{
// path is a zero-length string, contains only white space, or contains one or more
// invalid characters as defined by System.IO.Path.InvalidPathChars.
LogWriter?.WriteLine(string.Format(Resource.FailToSaveScript, scriptFile, ae.Message));
}
catch (System.IO.PathTooLongException ptle)
{
// The specified path, file name, or both exceed the system-defined maximum length.
// For example, on Windows-based platforms, paths must be less than 248 characters,
// and file names must be less than 260 characters.
LogWriter?.WriteLine(string.Format(Resource.FailToSaveScript, scriptFile, ptle.Message));
}
catch (System.IO.DirectoryNotFoundException dnfe)
{
// The specified path is invalid (for example, it is on an unmapped drive).
LogWriter?.WriteLine(string.Format(Resource.FailToSaveScript, scriptFile, dnfe.Message));
}
catch (System.IO.IOException ioe)
{
// An I/O error occurred while creating the file.
LogWriter?.WriteLine(string.Format(Resource.FailToSaveScript, scriptFile, ioe.Message));
}
catch (System.NotSupportedException nse)
{
// path is in an invalid format.
LogWriter?.WriteLine(string.Format(Resource.FailToSaveScript, scriptFile, nse.Message));
}
return bResult;
}

/// <summary>
/// Stops the current session and save it.
/// </summary>
/// <param name="message">The "shutdown" message</param>
/// <param name="jsonObject">The JSON object corresponding to the shutdown message.</param>
private void StopSession(string message, JObject jsonObject)
{
System.Diagnostics.Contracts.Contract.Assert(SessionModel != null);
SessionModel.shutdown = ShutdownRequest;
SaveSession();
#if DEBUG
SessionModel.DebugDump();
#endif
//No session anymore
SessionModel = null;
}

/// <summary>
/// Save the curreent session
/// </summary>
/// <returns>true if the session has been saved false otherwise</returns>
private bool SaveSession()
{
System.Diagnostics.Contracts.Contract.Assume(SessionModel != null);
if (SessionModel.directory == null)
{
LogWriter?.WriteLine(Resource.FailToSaveSessionNoDirectory);
return false;
}
bool bResult = false;
string sessionFile = SessionModel.GetSessionFileName();
try
{
using (FileStream stream = System.IO.File.Create(sessionFile))
{
SessionModel.Write(stream);
bResult = true;
}
}
catch (System.UnauthorizedAccessException uae)
{
// The caller does not have the required permission.-or- path specified a file that
// is read-only.
LogWriter?.WriteLine(string.Format(Resource.FailToSaveSession, sessionFile, uae.Message));
}
catch (System.ArgumentNullException ane)
{
// path is null.
LogWriter?.WriteLine(string.Format(Resource.FailToSaveSession, sessionFile, ane.Message));
}
catch (System.ArgumentException ae)
{
// path is a zero-length string, contains only white space, or contains one or more
// invalid characters as defined by System.IO.Path.InvalidPathChars.
LogWriter?.WriteLine(string.Format(Resource.FailToSaveSession, sessionFile, ae.Message));
}
catch (System.IO.PathTooLongException ptle)
{
// The specified path, file name, or both exceed the system-defined maximum length.
// For example, on Windows-based platforms, paths must be less than 248 characters,
// and file names must be less than 260 characters.
LogWriter?.WriteLine(string.Format(Resource.FailToSaveSession, sessionFile, ptle.Message));
}
catch (System.IO.DirectoryNotFoundException dnfe)
{
// The specified path is invalid (for example, it is on an unmapped drive).
LogWriter?.WriteLine(string.Format(Resource.FailToSaveSession, sessionFile, dnfe.Message));
}
catch (System.IO.IOException ioe)
{
// An I/O error occurred while creating the file.
LogWriter?.WriteLine(string.Format(Resource.FailToSaveSession, sessionFile, ioe.Message));
}
catch (System.NotSupportedException nse)
{
// path is in an invalid format.
LogWriter?.WriteLine(string.Format(Resource.FailToSaveSession, sessionFile, nse.Message));
}
return true;
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion Solution/LanguageServerRobot/Model/Script.cs
Expand Up @@ -180,6 +180,19 @@ public void DebugDump()
System.Diagnostics.Debug.WriteLine(text);
}

/// <summary>
/// Write the script in the given FileStream using UTF8 encoding.
/// </summary>
public void Write(System.IO.FileStream stream)
{
System.Diagnostics.Contracts.Contract.Assert(stream != null);
System.Diagnostics.Contracts.Contract.Requires(stream.CanWrite);
JObject jobject = JObject.FromObject(this);
string text = jobject.ToString();
byte[] bytes = Encoding.UTF8.GetBytes(text);
stream.Write(bytes, 0, bytes.Length);
}

/// <summary>
/// Empty constructor
/// </summary>
Expand All @@ -198,6 +211,5 @@ public Script(string uri)
date = System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss fff");
messages = new List<Message>();
}

}
}
34 changes: 32 additions & 2 deletions Solution/LanguageServerRobot/Model/Session.cs
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Threading.Tasks;
using LanguageServer.Protocol;
using LanguageServerRobot.Utilities;
using Newtonsoft.Json.Linq;

namespace LanguageServerRobot.Model
Expand All @@ -30,6 +31,10 @@ public class Session
/// </summary>
public string description { get; set; }
/// <summary>
/// The session directory.
/// </summary>
public string directory;
/// <summary>
/// Parameters of the Initialization message request request
/// </summary>
public string initialize { get; set; }
Expand Down Expand Up @@ -78,14 +83,39 @@ public void DebugDump()
System.Diagnostics.Debug.WriteLine(text);
}

/// <summary>
/// Write the session in the given FileStream using UTF8 encoding.
/// </summary>
public void Write(System.IO.FileStream stream)
{
System.Diagnostics.Contracts.Contract.Assert(stream != null);
System.Diagnostics.Contracts.Contract.Requires(stream.CanWrite);
JObject jobject = JObject.FromObject(this);
string text = jobject.ToString();
byte[] bytes = Encoding.UTF8.GetBytes(text);
stream.Write(bytes, 0, bytes.Length);
}

/// <summary>
/// Get the session file name
/// </summary>
/// <returns></returns>
public string GetSessionFileName()
{
string filename = Util.UriToIdentifierName("TestSuite" + '_' + date);
filename += Util.SESSION_FILE_EXTENSION;
return directory != null ? System.IO.Path.Combine(directory, filename): filename;
}


/// <summary>
/// Empty constructor
/// </summary>
public Session()
{
scripts = new List<string>();
date = System.DateTime.Now.ToString();
user = Environment.UserName;
date = System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss fff");
user = Environment.UserName;

client_in_initialize_messages = new List<string>();
client_in_start_messages = new List<string>();
Expand Down

0 comments on commit e308b57

Please sign in to comment.