public void DetachClientAndRemoveShares(string clientSystemId) { ISQLDatabase database = ServiceRegistration.Get <ISQLDatabase>(); ITransaction transaction = database.BeginTransaction(); try { using (IDbCommand command = ClientManager_SubSchema.DeleteAttachedClientCommand(transaction, clientSystemId)) command.ExecuteNonQuery(); IMediaLibrary mediaLibrary = ServiceRegistration.Get <IMediaLibrary>(); mediaLibrary.DeleteMediaItemOrPath(clientSystemId, null, true); mediaLibrary.RemoveSharesOfSystem(clientSystemId); transaction.Commit(); } catch (Exception e) { ServiceRegistration.Get <ILogger>().Error("ClientManager: Error detaching client '{0}'", e, clientSystemId); transaction.Rollback(); throw; } ServiceRegistration.Get <ILogger>().Info("ClientManager: Client with system ID '{0}' detached", clientSystemId); // Last action: Remove the client from the collection of attached clients and disconnect the client connection, if connected _attachedClients = ReadAttachedClientsFromDB(); _controlPoint.RemoveAttachedClient(clientSystemId); ClientManagerMessaging.SendClientAttachmentChangeMessage(ClientManagerMessaging.MessageType.ClientDetached, clientSystemId); }
public void AttachClient(string clientSystemId) { ISQLDatabase database = ServiceRegistration.Get <ISQLDatabase>(); ITransaction transaction = database.BeginTransaction(); try { using (IDbCommand command = ClientManager_SubSchema.InsertAttachedClientCommand(transaction, clientSystemId, null, null)) command.ExecuteNonQuery(); transaction.Commit(); } catch (Exception e) { ServiceRegistration.Get <ILogger>().Error("ClientManager: Error attaching client '{0}'", e, clientSystemId); transaction.Rollback(); throw; } ServiceRegistration.Get <ILogger>().Info("ClientManager: Client with system ID '{0}' attached", clientSystemId); // Establish the UPnP connection to the client, if available in the network IDictionary <string, MPClientMetadata> attachedClients = ReadAttachedClientsFromDB(); lock (_syncObj) _attachedClients = attachedClients; _controlPoint.AddAttachedClient(clientSystemId); ClientManagerMessaging.SendClientAttachmentChangeMessage(ClientManagerMessaging.MessageType.ClientAttached, clientSystemId); }
/// <summary> /// Returns a dictionary which maps the system ids of all attached clients to their last hostname. /// </summary> /// <returns>Dictionary with system ids mapped to host names.</returns> protected IDictionary <string, MPClientMetadata> ReadAttachedClientsFromDB() { ISQLDatabase database = ServiceRegistration.Get <ISQLDatabase>(); ITransaction transaction = database.BeginTransaction(); try { int systemIdIndex; int lastHostNameIndex; int lastClientNameIndex; IDictionary <string, MPClientMetadata> result = new Dictionary <string, MPClientMetadata>(); using (IDbCommand command = ClientManager_SubSchema.SelectAttachedClientsCommand(transaction, out systemIdIndex, out lastHostNameIndex, out lastClientNameIndex)) using (IDataReader reader = command.ExecuteReader()) { while (reader.Read()) { string clientSystemId = database.ReadDBValue <string>(reader, systemIdIndex); string lastClientHostName = database.ReadDBValue <string>(reader, lastHostNameIndex); SystemName lastHostName = lastClientHostName == null ? null : new SystemName(lastClientHostName); string lastClientName = database.ReadDBValue <string>(reader, lastClientNameIndex); result.Add(clientSystemId, new MPClientMetadata(clientSystemId, lastHostName, lastClientName)); } } return(result); } finally { transaction.Dispose(); } }
protected void UpdateClientSystem(string clientSystemId, SystemName system, string clientName) { ServiceRegistration.Get <ILogger>().Info("ClientManager: Updating host name of client '{0}' to '{1}'", clientSystemId, system.HostName); ISQLDatabase database = ServiceRegistration.Get <ISQLDatabase>(); ITransaction transaction = database.BeginTransaction(); try { using (IDbCommand command = ClientManager_SubSchema.UpdateAttachedClientDataCommand(transaction, clientSystemId, system, clientName)) command.ExecuteNonQuery(); transaction.Commit(); } catch (Exception e) { ServiceRegistration.Get <ILogger>().Error("ClientManager: Error updating host name '{0}' of client '{1}'", e, system.HostName, clientSystemId); transaction.Rollback(); throw; } IDictionary <string, MPClientMetadata> attachedClients = ReadAttachedClientsFromDB(); lock (_syncObj) _attachedClients = attachedClients; }