public ConSolUser CreateUserConSol(IWebSocketConnection context, string name, Func <string, IWebSocketConnection, int> sendMessageFunc) { if (OnlineUsersByName.ContainsKey(name)) { return(OnlineUsersByName[name]); } else { ConSolUser user = new ConSolUser(context, name, Logs, sendMessageFunc); OnlineUsersByName.TryAdd(name, user); OnlineUsersByContext.TryAdd(context, user); return(user); } }
public ConSolUser RemoveUserConSol(IWebSocketConnection context) { try { if (OnlineUsersByContext.ContainsKey(context)) { ConSolUser user = OnlineUsersByContext[context]; OnlineUsersByContext.TryRemove(context, out user); OnlineUsersByName.TryRemove(user.Name, out user); return(user); } } catch (Exception ex) { WriteLine(ex.Message); WriteLine(ex.StackTrace); } return(null); }
/// <summary> /// Launches, on a different thread, the method stored in "command". This method must be from the MenuFunctions class object /// </summary> /// <param name="command"> /// Name of the method to execute (must be part of MenuFunctions) /// </param> /// <param name="context"> /// User launching the method /// TODO Console outputs generated by commands of this user should be available to this user only, and flaged appropriatly in the Logs /// </param> public static void ShowSpectrum(string source, string scan, string sequence, ConSolUser user) { try { Task task = Task.Factory.StartNew(() => { try { object result = SpectrumView.Viewer.CreateView(source, scan, user); SendDataToClient(result, user.Context, "AnnotatedSpectrum"); } catch (Exception) { SendError("Could not run command", user.Context); } }); } catch (Exception) { SendError("Could not run command", user.Context); } }
/// <summary> /// Launches, on a different thread, the method stored in "command". This method must be from the MenuFunctions class object /// </summary> /// <param name="command"> /// Name of the method to execute (must be part of MenuFunctions) /// </param> /// <param name="context"> /// User launching the method /// TODO Console outputs generated by commands of this user should be available to this user only, and flaged appropriatly in the Logs /// </param> public static void ListSources(ConSolUser user) { try { Task task = Task.Factory.StartNew(() => { try { List <string> list = new List <string>(Numerics.SourceStore.GetDictionary().Keys); object result = list.ToArray(); SendDataToClient(result, user.Context, "ListSources"); } catch (Exception) { SendError("Could not run command", user.Context); } }); } catch (Exception) { SendError("Could not run command", user.Context); } }
/// <summary> /// Launches, on a different thread, the method stored in "command". This method must be from the MenuFunctions class object /// </summary> /// <param name="command"> /// Name of the method to execute (must be part of MenuFunctions) /// </param> /// <param name="context"> /// User launching the method /// TODO Console outputs generated by commands of this user should be available to this user only, and flaged appropriatly in the Logs /// </param> public static void Execute(string command, ConSolUser user) { try { Task task = Task.Factory.StartNew(() => { try { Type refType = typeof(PeptidAce.Structures.Spectrum); string[] splits = command.Split(' '); //List all possible classes, filter by keyword (second param) if ("ls".CompareTo(splits[0]) == 0) { string strCommands = ""; foreach (Type tttype in refType.Assembly.GetTypes()) { if (tttype.IsClass && tttype.IsPublic && (splits.Length == 1 || tttype.FullName.Contains(splits[1]))) { strCommands += "\n" + tttype.Name; } } if (!string.IsNullOrEmpty(strCommands)) { SendToTerminal(strCommands.Substring(1), user.Context); } } else { string[] splitsDot = command.Split('.'); int iterIndexSplit = 0; string className = splitsDot[iterIndexSplit]; string methodName = "Launch"; for (iterIndexSplit = 1; iterIndexSplit < splitsDot.Length - 1; iterIndexSplit++) { className += "." + splitsDot[iterIndexSplit]; } Type theType = GetClassType(className, refType); if (theType == null)//No specified method, use default "Launch()" method { className += "." + splitsDot[iterIndexSplit]; iterIndexSplit++; theType = GetClassType(className, refType); } if (theType != null) { System.Reflection.MethodInfo theMethod = null; if (iterIndexSplit < splitsDot.Length) { methodName = splitsDot[iterIndexSplit]; } theMethod = theType.GetMethod(methodName); object result = null; if (theMethod != null) { result = theMethod.Invoke(Instance, new[] { user }); } else { SendError("Could not find method " + className + "." + methodName + "(ConSolUser user)", user.Context); } if (result != null) { SendDataToClient(result, user.Context); } } else { SendError("Could not find class " + command, user.Context); } } } catch (Exception) { SendError("Could not run command", user.Context); } }); } catch (Exception) { SendError("Could not run command", user.Context); } }
/// <summary> /// Event fired when a data is received from the Alchemy Websockets server instance. /// Parses data as JSON and calls the appropriate message or sends an error message. /// </summary> /// <param name="context">The user's connection context</param> public static void OnReceive(IWebSocketConnection socket, string message) { // Sol.OutputLine("Received Data From :" + context.ClientAddress); try { ConSolUser user = Sol.GetConSolUser(socket); if (user == null) { if (!message.Contains("Register")) { SendRegisterInfo(socket); } else { dynamic obj = JsonConvert.DeserializeObject(message); if (((string)obj.Type).CompareTo("Register") == 0) { Register(obj.Name.Value, socket); } } } else { // <3 dynamics dynamic obj = JsonConvert.DeserializeObject(message); switch ((string)obj.Type) { case "Message": break; case "Menu": break; case "Command": if (user == null || string.IsNullOrEmpty(user.Name)) { SendError("You must log in if you want to send commands to the PeptidAce Server", socket); } else { Execute(obj.Command.Value, user); } //Sol.Execute(obj.Command.Value); break; case "DisplaySpectrum": if (obj.Data.length > 2) { ShowSpectrum(obj.Data[0], obj.Data[1], obj.Data[2], user); } else { ShowSpectrum(obj.Data[0], obj.Data[1], "", user); } break; case "ListSources": ListSources(user); break; case "NameChange": //NameChange(obj.Name.Value, context); break; }//*/ } } catch (Exception e) // Bad JSON! For shame. { Console.WriteLine("Received uninterpretable data from " + socket.ConnectionInfo); Console.WriteLine(e.StackTrace); if (!string.IsNullOrEmpty(message)) { Console.WriteLine(" => " + message); } //var r = new Response { Type = "Error", Message = e.Message }; //SendError(JsonConvert.SerializeObject(r), context); //context.Send(JsonConvert.SerializeObject(r)); } }