Skip to content

Commit

Permalink
The reading from the CLI is now more robust for most calls (data is n…
Browse files Browse the repository at this point in the history
…ot flushed after each write, so the library needs to wait for all the data to be flushed)

Prepared all the returned objects to be compatible with (extension) methods allowing to update them in Speedify as soon as they are updated in C#
  • Loading branch information
sidewinder94 committed Nov 11, 2019
1 parent eac4cb9 commit 8a4cc09
Show file tree
Hide file tree
Showing 18 changed files with 189 additions and 26 deletions.
109 changes: 109 additions & 0 deletions SpeedifyCliWrapper/Common/SpeedifyCollection.cs
@@ -0,0 +1,109 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace SpeedifyCliWrapper.Common
{
internal class SpeedifyCollection<T> : SpeedifyReturnedValue, IList<T> where T : SpeedifyReturnedValue
{
private Speedify _lWrapper;
private List<T> _storage;

internal override Speedify _wrapper
{
get => this._lWrapper;
set
{
this._lWrapper = value;
foreach (T toChange in this._storage.Where(i => i._wrapper == null))
{
toChange._wrapper = this._lWrapper;
}
}
}

public SpeedifyCollection()
{
this._storage = new List<T>();
}

public SpeedifyCollection(Speedify wrapper) : base()
{
this._wrapper = wrapper;
}

private void CheckAndAddWrapper(T item)
{
if (item._wrapper == null)
{
item._wrapper = this._wrapper;
}
}

public T this[int index] { get => ((IList<T>)_storage)[index]; set => ((IList<T>)_storage)[index] = value; }

public int Count => ((IList<T>)_storage).Count;

public bool IsReadOnly => ((IList<T>)_storage).IsReadOnly;


public void Add(T item)
{
this.CheckAndAddWrapper(item);

((IList<T>)_storage).Add(item);
}

public void Clear()
{
((IList<T>)_storage).Clear();
}

public bool Contains(T item)
{
return ((IList<T>)_storage).Contains(item);
}

public void CopyTo(T[] array, int arrayIndex)
{
((IList<T>)_storage).CopyTo(array, arrayIndex);
}

public IEnumerator<T> GetEnumerator()
{
return ((IList<T>)_storage).GetEnumerator();
}

public int IndexOf(T item)
{
return ((IList<T>)_storage).IndexOf(item);
}

public void Insert(int index, T item)
{
this.CheckAndAddWrapper(item);

((IList<T>)_storage).Insert(index, item);
}

public bool Remove(T item)
{
return ((IList<T>)_storage).Remove(item);
}

public void RemoveAt(int index)
{
((IList<T>)_storage).RemoveAt(index);
}

IEnumerator IEnumerable.GetEnumerator()
{
return ((IList<T>)_storage).GetEnumerator();
}

public static implicit operator List<T>(SpeedifyCollection<T> collection)
{
return collection._storage;
}
}
}
11 changes: 11 additions & 0 deletions SpeedifyCliWrapper/Common/SpeedifyReturnedValue.cs
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace SpeedifyCliWrapper.Common
{
public abstract class SpeedifyReturnedValue
{
internal virtual Speedify _wrapper { get; set; }
}
}
5 changes: 3 additions & 2 deletions SpeedifyCliWrapper/Modules/Show.cs
@@ -1,4 +1,5 @@
using SpeedifyCliWrapper.ReturnTypes;
using SpeedifyCliWrapper.Common;
using SpeedifyCliWrapper.ReturnTypes;
using System.Collections.Generic;

namespace SpeedifyCliWrapper.Modules
Expand Down Expand Up @@ -30,7 +31,7 @@ public SpeedifyPrivacy Privacy(int timeout = 60)

public List<SpeedifyAdapter> Adapters(int timeout = 60)
{
return this._wrapper.RunSpeedifyCommand<List<SpeedifyAdapter>>(timeout, args: new[] { _moduleName, "adapters" });
return this._wrapper.RunSpeedifyCommand<SpeedifyCollection<SpeedifyAdapter>>(timeout, args: new[] { _moduleName, "adapters" });
}

public SpeedifyServer CurrentServer(int timeout = 60)
Expand Down
3 changes: 2 additions & 1 deletion SpeedifyCliWrapper/ReturnTypes/SpeedifyAdapter.cs
@@ -1,12 +1,13 @@
using System;
using Newtonsoft.Json;
using SpeedifyCliWrapper.Common;
using SpeedifyCliWrapper.Converters;
using SpeedifyCliWrapper.Enums;
using SpeedifyCliWrapper.Modules;

namespace SpeedifyCliWrapper.ReturnTypes
{
public class SpeedifyAdapter
public class SpeedifyAdapter : SpeedifyReturnedValue
{
public class AdapterDataUsage
{
Expand Down
3 changes: 2 additions & 1 deletion SpeedifyCliWrapper/ReturnTypes/SpeedifyConnectMethod.cs
@@ -1,11 +1,12 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SpeedifyCliWrapper.Common;
using SpeedifyCliWrapper.Converters;
using SpeedifyCliWrapper.Enums;

namespace SpeedifyCliWrapper.ReturnTypes
{
public class SpeedifyConnectMethod
public class SpeedifyConnectMethod : SpeedifyReturnedValue
{
[JsonIgnore]
[JsonConverter(typeof(EnumConverter))]
Expand Down
3 changes: 2 additions & 1 deletion SpeedifyCliWrapper/ReturnTypes/SpeedifyConnectionStats.cs
Expand Up @@ -3,10 +3,11 @@
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using SpeedifyCliWrapper.Common;

namespace SpeedifyCliWrapper.ReturnTypes
{
public class SpeedifyConnectionStats
public class SpeedifyConnectionStats : SpeedifyReturnedValue
{
public class Connection
{
Expand Down
3 changes: 2 additions & 1 deletion SpeedifyCliWrapper/ReturnTypes/SpeedifyDirectory.cs
@@ -1,11 +1,12 @@
using Newtonsoft.Json;
using SpeedifyCliWrapper.Common;
using System;
using System.Collections.Generic;
using System.Text;

namespace SpeedifyCliWrapper.ReturnTypes
{
public class SpeedifyDirectory
public class SpeedifyDirectory : SpeedifyReturnedValue
{
[JsonProperty("domain")]
public string Domain { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion SpeedifyCliWrapper/ReturnTypes/SpeedifyPrivacy.cs
@@ -1,12 +1,13 @@
using JetBrains.Annotations;
using Newtonsoft.Json;
using SpeedifyCliWrapper.Common;
using System;
using System.Collections.Generic;
using System.Text;

namespace SpeedifyCliWrapper.ReturnTypes
{
public class SpeedifyPrivacy
public class SpeedifyPrivacy : SpeedifyReturnedValue
{
[JsonProperty("CrashReports")]
public bool CrashReports { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion SpeedifyCliWrapper/ReturnTypes/SpeedifyServer.cs
@@ -1,11 +1,12 @@
using Newtonsoft.Json;
using SpeedifyCliWrapper.Common;
using System;
using System.Collections.Generic;
using System.Text;

namespace SpeedifyCliWrapper.ReturnTypes
{
public class SpeedifyServer
public class SpeedifyServer : SpeedifyReturnedValue
{
[JsonProperty("tag")]
public string Tag { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion SpeedifyCliWrapper/ReturnTypes/SpeedifyServers.cs
@@ -1,13 +1,14 @@
using JetBrains.Annotations;
using Newtonsoft.Json;
using SpeedifyCliWrapper.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SpeedifyCliWrapper.ReturnTypes
{
public class SpeedifyServers
public class SpeedifyServers : SpeedifyReturnedValue
{
[NotNull]
[JsonProperty("public")]
Expand Down
3 changes: 2 additions & 1 deletion SpeedifyCliWrapper/ReturnTypes/SpeedifySessionStats.cs
@@ -1,8 +1,9 @@
using Newtonsoft.Json;
using SpeedifyCliWrapper.Common;

namespace SpeedifyCliWrapper.ReturnTypes
{
public class SpeedifySessionStats
public class SpeedifySessionStats : SpeedifyReturnedValue
{
[JsonProperty("bytesReceived")]
public long BytesReceived { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion SpeedifyCliWrapper/ReturnTypes/SpeedifySettings.cs
@@ -1,12 +1,13 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using SpeedifyCliWrapper.Common;
using SpeedifyCliWrapper.Converters;
using SpeedifyCliWrapper.Enums;

namespace SpeedifyCliWrapper.ReturnTypes
{
public class SpeedifySettings
public class SpeedifySettings : SpeedifyReturnedValue
{
public class ForwardedPort
{
Expand Down
3 changes: 2 additions & 1 deletion SpeedifyCliWrapper/ReturnTypes/SpeedifyState.cs
@@ -1,10 +1,11 @@
using Newtonsoft.Json;
using SpeedifyCliWrapper.Common;
using SpeedifyCliWrapper.Converters;
using SpeedifyCliWrapper.Enums;

namespace SpeedifyCliWrapper.ReturnTypes
{
public class SpeedifyState
public class SpeedifyState : SpeedifyReturnedValue
{
[JsonProperty("state")]
[JsonConverter(typeof(EnumConverter))]
Expand Down
9 changes: 7 additions & 2 deletions SpeedifyCliWrapper/ReturnTypes/SpeedifyStats.cs
Expand Up @@ -7,10 +7,11 @@
using System.Text;
using JetBrains.Annotations;
using Newtonsoft.Json;
using SpeedifyCliWrapper.Common;

namespace SpeedifyCliWrapper.ReturnTypes
{
public class SpeedifyStats : ICustomJson, INotifyPropertyChanged
public class SpeedifyStats : SpeedifyReturnedValue, ICustomJson, INotifyPropertyChanged
{
[JsonProperty("state")]
public SpeedifyState State
Expand Down Expand Up @@ -66,7 +67,7 @@ private set
private SpeedifyConnectionStats _connectionStats;
private SpeedifySessionStats _sessionStats;

public SpeedifyStats()
internal SpeedifyStats()
{
this._accessorDictionary = this.GetType()
.GetProperties()
Expand All @@ -77,6 +78,10 @@ public SpeedifyStats()
.ToDictionary(kv => kv.Key, v => v.Value);
}

public SpeedifyStats(Speedify wrapper) : this()
{
this._wrapper = wrapper;
}

public MethodInfo this[string part] => this._accessorDictionary[part];

Expand Down
3 changes: 2 additions & 1 deletion SpeedifyCliWrapper/ReturnTypes/SpeedifyUser.cs
@@ -1,11 +1,12 @@
using Newtonsoft.Json;
using SpeedifyCliWrapper.Common;
using System;
using System.Collections.Generic;
using System.Text;

namespace SpeedifyCliWrapper.ReturnTypes
{
public class SpeedifyUser
public class SpeedifyUser : SpeedifyReturnedValue
{
[JsonProperty("email")]
public string Email { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion SpeedifyCliWrapper/ReturnTypes/SpeedifyVersion.cs
@@ -1,8 +1,9 @@
using Newtonsoft.Json;
using SpeedifyCliWrapper.Common;

namespace SpeedifyCliWrapper.ReturnTypes
{
public class SpeedifyVersion
public class SpeedifyVersion : SpeedifyReturnedValue
{
[JsonProperty("maj")]
public int Major { get; set; }
Expand Down

0 comments on commit 8a4cc09

Please sign in to comment.