/// <summary> /// Get the client. /// </summary> /// <param name="uniqueIdentifier">The unique client identifier (key).</param> /// <returns>The client model; else null.</returns> public virtual ChatModel GetClient(string uniqueIdentifier) { // Attempt to find the client. Nequeo.Client.Chat.IChat client = null; // Atempt to get the messages. Tuple <string, string, MessageType, byte[]>[] messages = null; // Attempt to get the token. string token = null; // Invoke the finds in parallel Parallel.Invoke ( () => client = Find(uniqueIdentifier), () => messages = FindMessages(uniqueIdentifier), () => token = FindToken(uniqueIdentifier) ); // The client; else null. return(new ChatModel() { Client = client, Messages = messages, Token = token }); }
/// <summary> /// Create a new client. /// </summary> /// <param name="uniqueIdentifier">The unique client identifier (key).</param> /// <param name="client">The client connected to the machine.</param> /// <param name="token">The client token to add.</param> /// <returns>The communication token; else null.</returns> private void CreateEx(string uniqueIdentifier, Nequeo.Client.Chat.IChat client, string token) { // Create the connection. client.OnServerMessage += client_OnServerMessage; client.OnValidCommand += client_OnValidCommand; client.OnDisconnected += client_OnDisconnected; client.OnError += client_OnError; client.UniqueIdentifier = uniqueIdentifier; // Add the new client. Add(uniqueIdentifier, client); AddToken(uniqueIdentifier, token); }
/// <summary> /// Add the client item. /// </summary> /// <param name="uniqueIdentifier">The unique client identifier (key).</param> /// <param name="client">The client connected to the machine.</param> /// <exception cref="System.Exception"></exception> /// <exception cref="System.ArgumentNullException"></exception> public virtual void Add(string uniqueIdentifier, Nequeo.Client.Chat.IChat client) { if (String.IsNullOrEmpty(uniqueIdentifier)) { throw new ArgumentNullException("uniqueIdentifier"); } if (client == null) { throw new ArgumentNullException("client"); } // Add the item. base.AddClient(uniqueIdentifier, client); }
/// <summary> /// Create and authorise a new client and connect to the server.. /// </summary> /// <param name="uniqueIdentifier">The unique client identifier (key).</param> /// <param name="createAction">If not null then the action is executed to create a client.</param> /// <param name="tokenAction">If not null then the action is executed to create a token.</param> /// <param name="credentials">The credentials.</param> /// <param name="token">The token.</param> /// <returns>True if the client has been authorised; else false.</returns> public virtual bool CreateClient(string uniqueIdentifier, Func <Nequeo.Client.Chat.IChat> createAction, Func <string> tokenAction, Nequeo.Model.Credentials credentials, out string token) { // Attempt to find the client. Nequeo.Client.Chat.IChat client = null; string tokenInternal = null; // Invoke the finds in parallel Parallel.Invoke ( () => client = Find(uniqueIdentifier), () => tokenInternal = FindToken(uniqueIdentifier) ); // If not found. if (client == null) { // Create the client if does not exist. if (createAction != null) { // Create the client. client = createAction(); } // If created. if (client != null) { // Initialisation, connect and authorise. client.Initialisation(); client.Connect(); // Only add the client if authorised. if (client.IsAuthorised) { // If token action exists. if (tokenAction != null) { // Execute the token action. tokenInternal = tokenAction(); } else { // Create a defalt token. tokenInternal = Nequeo.Invention.TokenGenerator.Instance.Random(15); } // Add the client to the list. CreateEx(uniqueIdentifier, client, tokenInternal); } } } else { // If not connected then connect. if (!client.Connected) { client.Connect(); } // If not authorised then authorise. if (!client.IsAuthorised) { client.Authorise(credentials.Username, credentials.Password, credentials.Domain); } } // The token. token = tokenInternal; // Has the client been authorised. return(client.IsAuthorised); }