[Karmalib-commits] csharp/src/KarmaLib KarmaClient.cs,1.2,1.3
Brought to you by:
justinkwaugh
From: <jus...@us...> - 2003-12-24 05:53:23
|
Update of /cvsroot/karmalib/csharp/src/KarmaLib In directory sc8-pr-cvs1:/tmp/cvs-serv5384 Modified Files: KarmaClient.cs Log Message: now implements both IKarmaAdvancedClient and IKarmaBasicClient. The TCP stuff was moved out as well. Use KarmaClientFactory to get an instance. Index: KarmaClient.cs =================================================================== RCS file: /cvsroot/karmalib/csharp/src/KarmaLib/KarmaClient.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** KarmaClient.cs 19 Dec 2003 05:13:12 -0000 1.2 --- KarmaClient.cs 24 Dec 2003 05:53:18 -0000 1.3 *************** *** 1,4 **** --- 1,5 ---- using System; + using System.Collections; using System.IO; using System.Net; *************** *** 14,28 **** /// Summary description for KarmaClient. /// </summary> ! public class KarmaClient { ! public static readonly string version = "0.1.0 Alpha"; ! public static readonly int PORT = 8302; ! private KarmaConnection conn = null; private KarmaProtocol protocol = null; ! public KarmaClient(string ipAddress) { ! conn = new KarmaConnection(ipAddress,PORT); ! protocol = new KarmaProtocol(conn); } --- 15,52 ---- /// Summary description for KarmaClient. /// </summary> ! public class KarmaClient: IBasicKarmaClient, IAdvancedKarmaClient { ! private bool isBasic = true; ! private static readonly string version = "0.1.0 Alpha"; ! ! enum IOLock { Read, Write, None }; ! ! private string password = ""; ! ! private IKarmaConnection conn = null; private KarmaProtocol protocol = null; ! public KarmaClient( IKarmaConnection connection, bool isBasic ) ! { ! conn = connection; ! this.isBasic = isBasic; ! protocol = new KarmaProtocol( connection ); ! } ! ! public IKarmaConnection Connection ! { ! get ! { ! return conn; ! } ! } ! ! public string Version ! { ! get ! { ! return version; ! } } *************** *** 51,56 **** } ! public KarmaLib.Data.Version GetDeviceProtocolVersion() ! { KarmaLib.Data.Version rv = null; if (Connect()) --- 75,122 ---- } ! private void BasicConnect( IOLock locktype ) { ! if (isBasic) ! { ! if (!IsValidDeviceProtocolVersion()) ! { ! throw new KarmaException("Unsupported Protocol Version"); ! } ! ! switch (locktype) ! { ! case IOLock.Read: ! { ! Login(password); ! GetReadLock(); ! break; ! } ! case IOLock.Write: ! { ! Login(password); ! GetWriteLock(); ! break; ! } ! case IOLock.None: ! { ! goto default; ! } ! default: ! { ! break; ! } ! } ! } ! } ! ! private void BasicDisconnect() { ! if (isBasic) ! { ! ReleaseIOLock(); ! Hangup(); ! Disconnect(); ! } ! } ! ! public KarmaLib.Data.Version GetDeviceProtocolVersion() { KarmaLib.Data.Version rv = null; if (Connect()) *************** *** 61,64 **** --- 127,191 ---- } + public bool IsValidDeviceProtocolVersion() { + bool rv = false; + if (Connect()) + { + KarmaLib.Data.Version deviceVersion = protocol.GetVersion(); + if (KarmaProtocol.supportedVersion.Equals(deviceVersion)) + { + rv = true; + } + } + return rv; + } + + public AccessRights Login(string password) { + AccessRights rv = null; + if (Connect()) + { + byte[] salt = protocol.GetAuthenticationSalt(); + byte[] newPassword = new byte[salt.Length + password.Length]; + byte[] encodedPassword = null; + MD5CryptoServiceProvider crypto = new MD5CryptoServiceProvider(); + + System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding(); + encodedPassword = encoder.GetBytes(password); + + salt.CopyTo(newPassword,0); + encodedPassword.CopyTo(newPassword,salt.Length); + + byte[] hash = null; + + try + { + hash = crypto.ComputeHash(newPassword); + } + catch + { + throw new KarmaException("Could not find MD5 algorithm"); + } + + rv = protocol.Authenticate(hash); + this.password = password; + } + return rv; + } + + public DeviceDetails GetDeviceDetails() { + + DeviceDetails rv = null; + if (Connect()) + { + BasicConnect( IOLock.None ); + rv = protocol.GetDeviceDetails(); + BasicDisconnect(); + } + return rv; + } + + public StorageDetails GetStorageDetails( int storageId ){ + throw new KarmaException("Not yet implemented"); + } + public DeviceSettings GetDeviceSettings() { *************** *** 66,74 **** --- 193,207 ---- if (Connect()) { + BasicConnect( IOLock.Read ); rv = protocol.GetDeviceSettings(); + BasicDisconnect(); } return rv; } + public void UpdateDeviceSettings( DeviceSettings deviceSettings ){ + throw new KarmaException("Not yet implemented"); + } + public void GetReadLock() { if (Connect()) { *************** *** 89,135 **** } ! public AccessRights Login(string password) { ! AccessRights rv = null; ! if (Connect()) { ! byte[] salt = protocol.GetAuthenticationSalt(); ! byte[] newPassword = new byte[salt.Length + password.Length]; ! byte[] encodedPassword = null; ! MD5CryptoServiceProvider crypto = new MD5CryptoServiceProvider(); ! System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding(); ! encodedPassword = encoder.GetBytes(password); ! salt.CopyTo(newPassword,0); ! encodedPassword.CopyTo(newPassword,salt.Length); ! byte[] hash = null; ! try { ! hash = crypto.ComputeHash(newPassword); ! } ! catch { ! throw new KarmaException("Could not find MD5 algorithm"); ! } ! rv = protocol.Authenticate(hash); ! } ! return rv; } public void DeleteFile( int fileId ) { if (Connect()) { protocol.DeleteFile( fileId ); } } ! ! public DeviceDetails GetDeviceDetails() { ! ! DeviceDetails rv = null; ! if (Connect()) { ! rv = protocol.GetDeviceDetails(); } - return rv; } - } } --- 222,285 ---- } ! public void WriteDataFile( string filename, long size, int fileId, int storage, Stream input ) { ! throw new KarmaException("Not yet implemented"); ! } ! public void WriteDataFile( string pathname, int fileId, int storage ) { ! throw new KarmaException("Not yet implemented"); ! } ! public void WriteFile( FileDetails fd, int storage, Stream input ) { ! throw new KarmaException("Not yet implemented"); ! } ! public void WriteFileChunk( long offset, long size, int fileId, int storage, Stream input ) { ! throw new KarmaException("Not yet implemented"); ! } ! public FileDetails[] GetAllFileDetails() { ! throw new KarmaException("Not yet implemented"); ! } ! public IEnumerator GetFileDetailsIterator() { ! throw new KarmaException("Not yet implemented"); ! } ! ! public FileDetails GetFileDetails( int fileId ) { ! throw new KarmaException("Not yet implemented"); ! } ! ! public void UpdateFileDetails( FileDetails fd ) { ! throw new KarmaException("Not yet implemented"); ! } ! ! public void ReadFileChunk( long offset, long size, int fileId, Stream output) { ! throw new KarmaException("Not yet implemented"); ! } ! ! public byte[] ReadFileChunk(long offset, long size, int fileId) { ! throw new KarmaException("Not yet implemented"); ! } ! ! public void ReadFile( int fileId, Stream output ) { ! throw new KarmaException("Not yet implemented"); } public void DeleteFile( int fileId ) { if (Connect()) { + BasicConnect( IOLock.Write ); protocol.DeleteFile( fileId ); + BasicDisconnect(); } } ! ! public void Hangup() { ! if (Connect()) ! { ! //todo.. implement ! // protocol.Hangup(); ! Disconnect(); } } } } |