diff --git a/Async/Async.cs b/Async/Async.cs
index df2676d..7946f62 100644
--- a/Async/Async.cs
+++ b/Async/Async.cs
@@ -1,80 +1,81 @@
-using System;
+using System;
using System.Collections.Generic;
-
-///
-/// Async is a utility class which provides straight-forward, powerful functions for working with asynchronous C#.
-///
-/// Async provides around 20 functions that include the usual 'functional' suspects (map, reduce, filter, each…) as well as
-/// some common patterns for asynchronous control flow (parallel, series, waterfall…). All these functions assume you follow
-/// the convention of providing a single callback as the last argument of your async function.
-///
-public class Async {
- public static void each (List items, Action> fn, Action cb) {
- if (items == null || items.Count == 0) {
- cb(null);
- return;
- }
-
- int currentItemI = 0;
- Action iterate = null;
- iterate = () => {
- if (currentItemI >= items.Count) {
+namespace Wizcorp.MageSDK.Utils {
+ ///
+ /// Async is a utility class which provides straight-forward, powerful functions for working with asynchronous C#.
+ ///
+ /// Async provides around 20 functions that include the usual 'functional' suspects (map, reduce, filter, each…) as well as
+ /// some common patterns for asynchronous control flow (parallel, series, waterfall…). All these functions assume you follow
+ /// the convention of providing a single callback as the last argument of your async function.
+ ///
+ public class Async {
+ public static void each(List items, Action> fn, Action cb) {
+ if (items == null || items.Count == 0) {
cb(null);
return;
}
- // Execute the given function on this item
- fn(items[currentItemI], (Exception error) => {
- // Stop iteration if there was an error
- if (error != null) {
- cb(error);
+ int currentItemI = 0;
+ Action iterate = null;
+ iterate = () => {
+ if (currentItemI >= items.Count) {
+ cb(null);
return;
}
- // Continue to next item
- currentItemI++;
- iterate();
- });
- };
+ // Execute the given function on this item
+ fn(items[currentItemI], (Exception error) => {
+ // Stop iteration if there was an error
+ if (error != null) {
+ cb(error);
+ return;
+ }
- // Begin the iteration
- iterate ();
- }
+ // Continue to next item
+ currentItemI++;
+ iterate();
+ });
+ };
- public static void series (List>> actionItems, Action cb) {
- bool isEmpty = actionItems == null || actionItems.Count == 0;
- if (isEmpty) {
- cb(null);
- return;
+ // Begin the iteration
+ iterate();
}
- int currentItemI = 0;
- Action iterate = null;
- iterate = () => {
- if (currentItemI >= actionItems.Count) {
+ public static void series(List>> actionItems, Action cb) {
+ bool isEmpty = actionItems == null || actionItems.Count == 0;
+ if (isEmpty) {
cb(null);
return;
}
-
- // Shift an element from the list
- Action> actionItem = actionItems[currentItemI];
-
- // Execute the given function on this item
- actionItem((Exception error) => {
- // Stop iteration if there was an error
- if (error != null) {
- cb(error);
+
+ int currentItemI = 0;
+ Action iterate = null;
+ iterate = () => {
+ if (currentItemI >= actionItems.Count) {
+ cb(null);
return;
}
-
- // Continue to next item
- currentItemI++;
- iterate();
- });
- };
- // Begin the iteration
- iterate ();
+ // Shift an element from the list
+ Action> actionItem = actionItems[currentItemI];
+
+ // Execute the given function on this item
+ actionItem((Exception error) => {
+ // Stop iteration if there was an error
+ if (error != null) {
+ cb(error);
+ return;
+ }
+
+ // Continue to next item
+ currentItemI++;
+ iterate();
+ });
+ };
+
+ // Begin the iteration
+ iterate();
+ }
}
}
diff --git a/EventEmitter/EventEmitter.cs b/EventEmitter/EventEmitter.cs
index 45b36fb..dacbc57 100644
--- a/EventEmitter/EventEmitter.cs
+++ b/EventEmitter/EventEmitter.cs
@@ -1,64 +1,60 @@
-using System;
+using System;
using System.Collections.Generic;
using System.ComponentModel;
-public class EventEmitter {
- //
- private Dictionary eventTags = new Dictionary();
- private EventHandlerList eventsList = new EventHandlerList();
+namespace Wizcorp.MageSDK.Event {
+ public class EventEmitter {
+ //
+ private Dictionary eventTags = new Dictionary();
+ private EventHandlerList eventsList = new EventHandlerList();
- //
- public void on(string eventTag, Action handler)
- {
- if (!eventTags.ContainsKey(eventTag)) {
- eventTags.Add(eventTag, new object());
+ //
+ public void on(string eventTag, Action handler) {
+ if (!eventTags.ContainsKey(eventTag)) {
+ eventTags.Add(eventTag, new object());
+ }
+
+ eventsList.AddHandler(eventTags[eventTag], handler);
}
- eventsList.AddHandler(eventTags[eventTag], handler);
- }
+ //
+ public void once(string eventTag, Action handler) {
+ Action handlerWrapper = null;
+ handlerWrapper = (object obj, T arguments) => {
+ eventsList.RemoveHandler(eventTags[eventTag], handlerWrapper);
+ handler(obj, arguments);
+ };
- //
- public void once(string eventTag, Action handler)
- {
- Action handlerWrapper = null;
- handlerWrapper = (object obj, T arguments) => {
- eventsList.RemoveHandler(eventTags[eventTag], handlerWrapper);
- handler(obj, arguments);
- };
+ on(eventTag, handlerWrapper);
+ }
- on(eventTag, handlerWrapper);
- }
+ //
+ public void emit(string eventTag, object sender, T arguments) {
+ if (!eventTags.ContainsKey(eventTag)) {
+ return;
+ }
- //
- public void emit(string eventTag, object sender, T arguments)
- {
- if (!eventTags.ContainsKey(eventTag)) {
- return;
+ Action execEventList = (Action)eventsList[eventTags[eventTag]];
+ execEventList(sender, arguments);
}
- Action execEventList = (Action)eventsList[eventTags[eventTag]];
- execEventList(sender, arguments);
- }
-
- public void emit(string eventTag, T arguments)
- {
- emit(eventTag, null, arguments);
- }
+ public void emit(string eventTag, T arguments) {
+ emit(eventTag, null, arguments);
+ }
- //
- public void off(string eventTag, Action handler)
- {
- eventsList.RemoveHandler(eventTags[eventTag], handler);
- }
+ //
+ public void off(string eventTag, Action handler) {
+ eventsList.RemoveHandler(eventTags[eventTag], handler);
+ }
- //
- public void removeAllListeners()
- {
- // Destroy all event handlers
- eventsList.Dispose();
- eventsList = new EventHandlerList();
+ //
+ public void removeAllListeners() {
+ // Destroy all event handlers
+ eventsList.Dispose();
+ eventsList = new EventHandlerList();
- // Destroy all event tags
- eventTags = new Dictionary();
+ // Destroy all event tags
+ eventTags = new Dictionary();
+ }
}
-}
\ No newline at end of file
+}
diff --git a/HTTPRequest/HTTPRequest.DotNet.cs b/HTTPRequest/HTTPRequest.DotNet.cs
index c243d90..085f3a2 100644
--- a/HTTPRequest/HTTPRequest.DotNet.cs
+++ b/HTTPRequest/HTTPRequest.DotNet.cs
@@ -10,183 +10,159 @@
using System.Text;
using System.Threading;
-
-public class HTTPRequest {
- private HttpWebRequest request;
- private CookieContainer cookies;
- private Action cb;
- private Timer timeoutTimer;
-
- // Timeout setting for request
- private long _timeout = 100 * 1000;
- public long timeout
- {
- get
- {
- return _timeout;
+namespace Wizcorp.MageSDK.Network.Http {
+ public class HTTPRequest {
+ private HttpWebRequest request;
+ private CookieContainer cookies;
+ private Action cb;
+ private Timer timeoutTimer;
+
+ // Timeout setting for request
+ private long _timeout = 100 * 1000;
+ public long timeout {
+ get {
+ return _timeout;
+ }
+ set {
+ _timeout = value;
+
+ timeoutTimer.Dispose();
+ timeoutTimer = new Timer((object state) => {
+ this.Abort();
+ cb(new Exception("Request timed out"), null);
+ }, null, timeout, Timeout.Infinite);
+ }
}
- set
- {
- _timeout = value;
- timeoutTimer.Dispose();
+
+ // Constructor
+ public HTTPRequest(string url, string contentType, byte[] postData, Dictionary headers, CookieContainer cookies, Action cb) {
+ // Start timeout timer
timeoutTimer = new Timer((object state) => {
this.Abort();
cb(new Exception("Request timed out"), null);
}, null, timeout, Timeout.Infinite);
- }
- }
+ // Initialize request instance
+ HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
+ httpRequest.Method = (postData != null) ? WebRequestMethods.Http.Post : WebRequestMethods.Http.Get;
- // Constructor
- public HTTPRequest(string url, string contentType, byte[] postData, Dictionary headers, CookieContainer cookies, Action cb)
- {
- // Start timeout timer
- timeoutTimer = new Timer((object state) => {
- this.Abort();
- cb(new Exception("Request timed out"), null);
- }, null, timeout, Timeout.Infinite);
-
- // Initialize request instance
- HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
- httpRequest.Method = (postData != null) ? WebRequestMethods.Http.Post : WebRequestMethods.Http.Get;
-
- // Set request headers
- if (headers != null)
- {
- foreach (KeyValuePair entry in headers)
- {
- httpRequest.Headers.Add(entry.Key, entry.Value);
+ // Set request headers
+ if (headers != null) {
+ foreach (KeyValuePair entry in headers) {
+ httpRequest.Headers.Add(entry.Key, entry.Value);
+ }
}
- }
- // Set content type if provided
- if (contentType != null)
- {
- httpRequest.ContentType = contentType;
- }
+ // Set content type if provided
+ if (contentType != null) {
+ httpRequest.ContentType = contentType;
+ }
- // Set cookies if provided
- if (cookies != null)
- {
- httpRequest.CookieContainer = cookies;
- }
+ // Set cookies if provided
+ if (cookies != null) {
+ httpRequest.CookieContainer = cookies;
+ }
- // Setup private properties and fire off the request
- this.cb = cb;
- this.cookies = cookies;
- this.request = httpRequest;
-
- // Initiate response
- if (postData == null)
- {
- ReadResponseData(cb);
- return;
- } else
- {
- WritePostData(postData, (Exception requestError) => {
- if (requestError != null)
- {
- cb(requestError, null);
- return;
- }
+ // Setup private properties and fire off the request
+ this.cb = cb;
+ this.cookies = cookies;
+ this.request = httpRequest;
- // Process the response
+ // Initiate response
+ if (postData == null) {
ReadResponseData(cb);
- });
- return;
- }
- }
-
-
- // Write post data to request buffer
- private void WritePostData(byte[] postData, Action cb)
- {
- request.BeginGetRequestStream((IAsyncResult callbackResult) => {
- try
- {
- Stream postStream = request.EndGetRequestStream(callbackResult);
- postStream.Write(postData, 0, postData.Length);
- postStream.Close();
- }
- catch (Exception error)
- {
- cb(error);
+ return;
+ } else {
+ WritePostData(postData, (Exception requestError) => {
+ if (requestError != null) {
+ cb(requestError, null);
+ return;
+ }
+
+ // Process the response
+ ReadResponseData(cb);
+ });
return;
}
- cb(null);
- }, null);
- }
+ }
- // Read response data from request buffer
- private void ReadResponseData(Action cb)
- {
- // Begin waiting for a response
- request.BeginGetResponse(new AsyncCallback((IAsyncResult callbackResult) => {
- // Cleanup timeout
- timeoutTimer.Dispose();
- timeoutTimer = null;
- // Process response
- string responseString = null;
- try
- {
- HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
+ // Write post data to request buffer
+ private void WritePostData(byte[] postData, Action cb) {
+ request.BeginGetRequestStream((IAsyncResult callbackResult) => {
+ try {
+ Stream postStream = request.EndGetRequestStream(callbackResult);
+ postStream.Write(postData, 0, postData.Length);
+ postStream.Close();
+ } catch (Exception error) {
+ cb(error);
+ return;
+ }
+ cb(null);
+ }, null);
+ }
- using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
- {
- responseString = httpWebStreamReader.ReadToEnd();
+ // Read response data from request buffer
+ private void ReadResponseData(Action cb) {
+ // Begin waiting for a response
+ request.BeginGetResponse(new AsyncCallback((IAsyncResult callbackResult) => {
+ // Cleanup timeout
+ timeoutTimer.Dispose();
+ timeoutTimer = null;
+
+ // Process response
+ string responseString = null;
+ try {
+ HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
+
+ using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream())) {
+ responseString = httpWebStreamReader.ReadToEnd();
+ }
+
+ response.Close();
+ } catch (Exception error) {
+ cb(error, null);
+ return;
}
- response.Close();
- }
- catch (Exception error)
- {
- cb(error, null);
+ cb(null, responseString);
+ }), null);
+ }
+
+
+ // Abort request
+ public void Abort() {
+ if (request == null) {
return;
}
- cb(null, responseString);
- }), null);
- }
-
+ HttpWebRequest _request = request;
+ request = null;
- // Abort request
- public void Abort()
- {
- if (request == null)
- {
- return;
+ _request.Abort();
+ timeoutTimer.Dispose();
+ timeoutTimer = null;
}
- HttpWebRequest _request = request;
- request = null;
-
- _request.Abort();
- timeoutTimer.Dispose();
- timeoutTimer = null;
- }
-
- // Create GET request and return it
- public static HTTPRequest Get(string url, Dictionary headers, CookieContainer cookies, Action cb)
- {
- // Create request and return it
- // The callback will be called when the request is complete
- return new HTTPRequest(url, null, null, headers, cookies, cb);
- }
+ // Create GET request and return it
+ public static HTTPRequest Get(string url, Dictionary headers, CookieContainer cookies, Action cb) {
+ // Create request and return it
+ // The callback will be called when the request is complete
+ return new HTTPRequest(url, null, null, headers, cookies, cb);
+ }
- // Create POST request and return it
- public static HTTPRequest Post(string url, string contentType, string postData, Dictionary headers, CookieContainer cookies, Action cb)
- {
- byte[] binaryPostData = Encoding.UTF8.GetBytes(postData);
- return Post(url, contentType, binaryPostData, headers, cookies, cb);
- }
+ // Create POST request and return it
+ public static HTTPRequest Post(string url, string contentType, string postData, Dictionary headers, CookieContainer cookies, Action cb) {
+ byte[] binaryPostData = Encoding.UTF8.GetBytes(postData);
+ return Post(url, contentType, binaryPostData, headers, cookies, cb);
+ }
- // Create POST request and return it
- public static HTTPRequest Post(string url, string contentType, byte[] postData, Dictionary headers, CookieContainer cookies, Action cb)
- {
- return new HTTPRequest(url, contentType, postData, headers, cookies, cb);
+ // Create POST request and return it
+ public static HTTPRequest Post(string url, string contentType, byte[] postData, Dictionary headers, CookieContainer cookies, Action cb) {
+ return new HTTPRequest(url, contentType, postData, headers, cookies, cb);
+ }
}
}
#endif
diff --git a/HTTPRequest/HTTPRequest.Unity.cs b/HTTPRequest/HTTPRequest.Unity.cs
index d0c25ea..b9293f7 100644
--- a/HTTPRequest/HTTPRequest.Unity.cs
+++ b/HTTPRequest/HTTPRequest.Unity.cs
@@ -1,4 +1,4 @@
-// IF WE ARE USING UNITY AND WE HAVE NOT DEFINED THE
+// IF WE ARE USING UNITY AND WE HAVE NOT DEFINED THE
// MAGE_USE_WEBREQUEST MACROS, USE THIS VERSION
#if UNITY_5 && !MAGE_USE_WEBREQUEST
@@ -12,164 +12,160 @@
using UnityEngine;
-
-public class HTTPRequest {
- private WWW request;
- private CookieContainer cookies;
- private Action cb;
- private Stopwatch timeoutTimer;
-
- // Timeout setting for request
- private long _timeout = 100 * 1000;
- public long timeout
- {
- get
- {
- return _timeout;
- }
- set
- {
- _timeout = value;
+namespace Wizcorp.MageSDK.Network.Http {
+ public class HTTPRequest {
+ private WWW request;
+ private CookieContainer cookies;
+ private Action cb;
+ private Stopwatch timeoutTimer;
+
+ // Timeout setting for request
+ private long _timeout = 100 * 1000;
+ public long timeout {
+ get {
+ return _timeout;
+ }
+ set {
+ _timeout = value;
+ }
}
- }
- // Constructor
- public HTTPRequest(string url, string contentType, byte[] postData, Dictionary headers, CookieContainer cookies, Action cb) {
- // Start timeout timer
- timeoutTimer = new Stopwatch();
- timeoutTimer.Start();
+ // Constructor
+ public HTTPRequest(string url, string contentType, byte[] postData, Dictionary headers, CookieContainer cookies, Action cb) {
+ // Start timeout timer
+ timeoutTimer = new Stopwatch();
+ timeoutTimer.Start();
- // Queue constructor for main thread execution
- HTTPRequestManager.Queue(Constructor(url, contentType, postData, headers, cookies, cb));
- }
+ // Queue constructor for main thread execution
+ HTTPRequestManager.Queue(Constructor(url, contentType, postData, headers, cookies, cb));
+ }
- // Main thread constructor
- private IEnumerator Constructor(string url, string contentType, byte[] postData, Dictionary headers, CookieContainer cookies, Action cb) {
- Dictionary headersCopy = new Dictionary(headers);
+ // Main thread constructor
+ private IEnumerator Constructor(string url, string contentType, byte[] postData, Dictionary headers, CookieContainer cookies, Action cb) {
+ Dictionary headersCopy = new Dictionary(headers);
- // Set content type if provided
- if (contentType != null) {
- headersCopy.Add("ContentType", contentType);
- }
+ // Set content type if provided
+ if (contentType != null) {
+ headersCopy.Add("ContentType", contentType);
+ }
- // Set cookies if provided
- if (cookies != null) {
- Uri requestUri = new Uri(url);
- string cookieString = cookies.GetCookieHeader(requestUri);
- if (!string.IsNullOrEmpty(cookieString)) {
- headersCopy.Add("Cookie", cookieString);
+ // Set cookies if provided
+ if (cookies != null) {
+ Uri requestUri = new Uri(url);
+ string cookieString = cookies.GetCookieHeader(requestUri);
+ if (!string.IsNullOrEmpty(cookieString)) {
+ headersCopy.Add("Cookie", cookieString);
+ }
}
+
+ // Setup private properties and fire off the request
+ this.cb = cb;
+ this.cookies = cookies;
+ this.request = new WWW(url, postData, headersCopy);
+
+ // Initiate response
+ HTTPRequestManager.Queue(WaitLoop());
+ yield break;
}
- // Setup private properties and fire off the request
- this.cb = cb;
- this.cookies = cookies;
- this.request = new WWW(url, postData, headersCopy);
- // Initiate response
- HTTPRequestManager.Queue(WaitLoop());
- yield break;
- }
+ // Wait for www request to complete with timeout checks
+ private IEnumerator WaitLoop() {
+ while (request != null && !request.isDone) {
+ if (timeoutTimer.ElapsedMilliseconds >= timeout) {
+ // Timed out abort the request with timeout error
+ this.Abort();
+ cb(new Exception("Request timed out"), null);
+ yield break;
+ }
+ // Otherwise continue to wait
+ yield return null;
+ }
- // Wait for www request to complete with timeout checks
- private IEnumerator WaitLoop() {
- while (request != null && !request.isDone) {
- if (timeoutTimer.ElapsedMilliseconds >= timeout) {
- // Timed out abort the request with timeout error
- this.Abort();
- cb(new Exception("Request timed out"), null);
+ // Check if we destroyed the request due to an abort
+ if (request == null) {
yield break;
}
- // Otherwise continue to wait
- yield return null;
- }
-
- // Check if we destroyed the request due to an abort
- if (request == null) {
- yield break;
- }
+ // Cleanup timeout
+ timeoutTimer.Stop();
+ timeoutTimer = null;
- // Cleanup timeout
- timeoutTimer.Stop();
- timeoutTimer = null;
+ // Check if there is a callback
+ if (cb == null) {
+ yield break;
+ }
- // Check if there is a callback
- if (cb == null) {
- yield break;
- }
+ // Check if there was an error with the request
+ if (request.error != null) {
+ int statusCode = 0;
+ if (request.responseHeaders.ContainsKey("STATUS")) {
+ statusCode = int.Parse(request.responseHeaders["STATUS"].Split(' ')[1]);
+ }
- // Check if there was an error with the request
- if (request.error != null) {
- int statusCode = 0;
- if (request.responseHeaders.ContainsKey("STATUS")) {
- statusCode = int.Parse(request.responseHeaders["STATUS"].Split(' ')[1]);
+ cb(new HTTPRequestException(request.error, statusCode), null);
+ yield break;
}
- cb(new HTTPRequestException(request.error, statusCode), null);
- yield break;
- }
-
- // Otherwise check for cookies and return the response
- // HACK: we need to use reflection as cookieContainer
- // junks same key cookies as it uses a dictionary. To
- // work around this we use reflection to get the original
- // cookie response text, which isn't exposed, and parse
- // it ourselves
- Uri requestUri = new Uri(request.url);
- PropertyInfo pinfoHeadersString = typeof(WWW).GetProperty("responseHeadersString", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
-
- if (pinfoHeadersString != null) {
- string headersString = pinfoHeadersString.GetValue(request, null) as string;
- string[] headerLines = headersString.Split('\n');
-
- foreach (string headerStr in headerLines) {
- if (headerStr.StartsWith("set-cookie:", true, null)) {
- cookies.SetCookies(requestUri, headerStr.Remove(0, 11));
+ // Otherwise check for cookies and return the response
+ // HACK: we need to use reflection as cookieContainer
+ // junks same key cookies as it uses a dictionary. To
+ // work around this we use reflection to get the original
+ // cookie response text, which isn't exposed, and parse
+ // it ourselves
+ Uri requestUri = new Uri(request.url);
+ PropertyInfo pinfoHeadersString = typeof(WWW).GetProperty("responseHeadersString", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
+
+ if (pinfoHeadersString != null) {
+ string headersString = pinfoHeadersString.GetValue(request, null) as string;
+ string[] headerLines = headersString.Split('\n');
+
+ foreach (string headerStr in headerLines) {
+ if (headerStr.StartsWith("set-cookie:", true, null)) {
+ cookies.SetCookies(requestUri, headerStr.Remove(0, 11));
+ }
}
}
+
+ cb(null, request.text);
}
- cb(null, request.text);
- }
+ // Abort request
+ public void Abort() {
+ if (this.request == null) {
+ return;
+ }
- // Abort request
- public void Abort()
- {
- if (this.request == null)
- {
- return;
+ WWW request = this.request;
+ this.request = null;
+
+ request.Dispose();
+ timeoutTimer.Stop();
+ timeoutTimer = null;
}
- WWW request = this.request;
- this.request = null;
- request.Dispose();
- timeoutTimer.Stop();
- timeoutTimer = null;
- }
+ // Create GET request and return it
+ public static HTTPRequest Get(string url, Dictionary headers, CookieContainer cookies, Action cb) {
+ // Create request and return it
+ // The callback will be called when the request is complete
+ return new HTTPRequest(url, null, null, headers, cookies, cb);
+ }
+ // Create POST request and return it
+ public static HTTPRequest Post(string url, string contentType, string postData, Dictionary headers, CookieContainer cookies, Action cb) {
+ byte[] binaryPostData = Encoding.UTF8.GetBytes(postData);
+ return Post(url, contentType, binaryPostData, headers, cookies, cb);
+ }
- // Create GET request and return it
- public static HTTPRequest Get(string url, Dictionary headers, CookieContainer cookies, Action cb) {
- // Create request and return it
- // The callback will be called when the request is complete
- return new HTTPRequest(url, null, null, headers, cookies, cb);
- }
-
- // Create POST request and return it
- public static HTTPRequest Post(string url, string contentType, string postData, Dictionary headers, CookieContainer cookies, Action cb) {
- byte[] binaryPostData = Encoding.UTF8.GetBytes(postData);
- return Post(url, contentType, binaryPostData, headers, cookies, cb);
- }
-
- // Create POST request and return it
- public static HTTPRequest Post(string url, string contentType, byte[] postData, Dictionary headers, CookieContainer cookies, Action cb) {
- return new HTTPRequest(url, contentType, postData, headers, cookies, cb);
+ // Create POST request and return it
+ public static HTTPRequest Post(string url, string contentType, byte[] postData, Dictionary headers, CookieContainer cookies, Action cb) {
+ return new HTTPRequest(url, contentType, postData, headers, cookies, cb);
+ }
}
}
-#endif
\ No newline at end of file
+#endif
diff --git a/HTTPRequest/HTTPRequestException.cs b/HTTPRequest/HTTPRequestException.cs
index 6864e74..dda517e 100644
--- a/HTTPRequest/HTTPRequestException.cs
+++ b/HTTPRequest/HTTPRequestException.cs
@@ -1,9 +1,11 @@
-using System;
+using System;
-public class HTTPRequestException : Exception {
- public int Status = 0;
+namespace Wizcorp.MageSDK.Network.Http {
+ public class HTTPRequestException : Exception {
+ public int Status = 0;
- public HTTPRequestException(string Message, int Status) : base(Message) {
- this.Status = Status;
+ public HTTPRequestException(string Message, int Status) : base(Message) {
+ this.Status = Status;
+ }
}
-}
\ No newline at end of file
+}
diff --git a/HTTPRequest/HTTPRequestManager.cs b/HTTPRequest/HTTPRequestManager.cs
index eee9c65..4ad2c4d 100644
--- a/HTTPRequest/HTTPRequestManager.cs
+++ b/HTTPRequest/HTTPRequestManager.cs
@@ -1,27 +1,29 @@
-using System.Collections;
+using System.Collections;
using System.Collections.Generic;
-using UnityEngine;
+using Wizcorp.MageSDK.Utils;
-public class HTTPRequestManager : MonoSingleton {
- //
- private static List queued = new List();
+namespace Wizcorp.MageSDK.Network.Http {
+ public class HTTPRequestManager : MonoSingleton {
+ //
+ private static List queued = new List();
- //
- public static void Queue(IEnumerator coroutine) {
- lock ((object)queued) {
- queued.Add(coroutine);
+ //
+ public static void Queue(IEnumerator coroutine) {
+ lock ((object)queued) {
+ queued.Add(coroutine);
+ }
}
- }
- //
- void Update () {
- lock ((object)queued) {
- for (int i = 0; i < queued.Count; i += 1) {
- StartCoroutine(queued[i]);
- }
+ //
+ void Update() {
+ lock ((object)queued) {
+ for (int i = 0; i < queued.Count; i += 1) {
+ StartCoroutine(queued[i]);
+ }
- queued.Clear();
+ queued.Clear();
+ }
}
}
-}
\ No newline at end of file
+}
diff --git a/JSONRPC/JSONRPC.cs b/JSONRPC/JSONRPC.cs
index dd0eff9..f6497a4 100644
--- a/JSONRPC/JSONRPC.cs
+++ b/JSONRPC/JSONRPC.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
@@ -6,114 +6,118 @@
using Newtonsoft.Json.Linq;
-public class JSONRPC {
- // Endpoint and Basic Authentication
- private string endpoint;
- Dictionary headers;
+using Wizcorp.MageSDK.Network.Http;
- public void SetEndpoint(string endpoint, Dictionary headers = null) {
- this.endpoint = endpoint;
- this.headers = new Dictionary(headers);
- }
+namespace Wizcorp.MageSDK.Network.JsonRpc {
+ public class JSONRPC {
+ // Endpoint and Basic Authentication
+ private string endpoint;
+ Dictionary headers;
- public void Call(string methodName, JObject parameters, Dictionary headers, CookieContainer cookies, Action cb) {
- Call(JValue.CreateNull(), methodName, parameters, headers, cookies, cb);
- }
+ public void SetEndpoint(string endpoint, Dictionary headers = null) {
+ this.endpoint = endpoint;
+ this.headers = new Dictionary(headers);
+ }
- public void Call(string id, string methodName, JObject parameters, Dictionary headers, CookieContainer cookies, Action cb) {
- Call(new JValue(id), methodName, parameters, headers, cookies, cb);
- }
+ public void Call(string methodName, JObject parameters, Dictionary headers, CookieContainer cookies, Action cb) {
+ Call(JValue.CreateNull(), methodName, parameters, headers, cookies, cb);
+ }
- public void Call(int id, string methodName, JObject parameters, Dictionary headers, CookieContainer cookies, Action cb) {
- Call(new JValue(id), methodName, parameters, headers, cookies, cb);
- }
+ public void Call(string id, string methodName, JObject parameters, Dictionary headers, CookieContainer cookies, Action cb) {
+ Call(new JValue(id), methodName, parameters, headers, cookies, cb);
+ }
- public void Call(JValue id, string methodName, JObject parameters, Dictionary headers, CookieContainer cookies, Action cb) {
- // Setup JSON request object
- JObject requestObject = new JObject ();
- requestObject.Add("jsonrpc", new JValue("2.0"));
-
- requestObject.Add("id", id);
- requestObject.Add("method", new JValue(methodName));
- requestObject.Add("params", parameters);
-
- // Serialize JSON request object into string
- string postData;
- try {
- postData = requestObject.ToString ();
- } catch (Exception serializeError) {
- cb(serializeError, null);
- return;
+ public void Call(int id, string methodName, JObject parameters, Dictionary headers, CookieContainer cookies, Action cb) {
+ Call(new JValue(id), methodName, parameters, headers, cookies, cb);
}
- // Send request
- SendRequest(postData, headers, cookies, (Exception requestError, string responseString) => {
- if (requestError != null) {
- cb(requestError, null);
- return;
- }
-
- // Deserialize the JSON response
- JObject responseObject;
+ public void Call(JValue id, string methodName, JObject parameters, Dictionary headers, CookieContainer cookies, Action cb) {
+ // Setup JSON request object
+ JObject requestObject = new JObject();
+ requestObject.Add("jsonrpc", new JValue("2.0"));
+
+ requestObject.Add("id", id);
+ requestObject.Add("method", new JValue(methodName));
+ requestObject.Add("params", parameters);
+
+ // Serialize JSON request object into string
+ string postData;
try {
- responseObject = JObject.Parse(responseString);
- } catch (Exception parseError) {
- cb(parseError, null);
+ postData = requestObject.ToString();
+ } catch (Exception serializeError) {
+ cb(serializeError, null);
return;
}
-
- cb(null, responseObject);
- });
- }
- public void CallBatch(JSONRPCBatch rpcBatch, Dictionary headers, CookieContainer cookies, Action cb) {
- // Serialize JSON request object into string
- string postData;
- try {
- postData = rpcBatch.batch.ToString();
- } catch (Exception serializeError) {
- cb(serializeError, null);
- return;
+ // Send request
+ SendRequest(postData, headers, cookies, (Exception requestError, string responseString) => {
+ if (requestError != null) {
+ cb(requestError, null);
+ return;
+ }
+
+ // Deserialize the JSON response
+ JObject responseObject;
+ try {
+ responseObject = JObject.Parse(responseString);
+ } catch (Exception parseError) {
+ cb(parseError, null);
+ return;
+ }
+
+ cb(null, responseObject);
+ });
}
- // Send request
- SendRequest(postData, headers, cookies, (Exception requestError, string responseString) => {
- if (requestError != null) {
- cb(requestError, null);
- return;
- }
-
- // Deserialize the JSON response
- JArray responseArray;
+ public void CallBatch(JSONRPCBatch rpcBatch, Dictionary headers, CookieContainer cookies, Action cb) {
+ // Serialize JSON request object into string
+ string postData;
try {
- responseArray = JArray.Parse(responseString);
- } catch (Exception parseError) {
- cb(parseError, null);
+ postData = rpcBatch.batch.ToString();
+ } catch (Exception serializeError) {
+ cb(serializeError, null);
return;
}
-
- cb(null, responseArray);
- });
- }
- private void SendRequest(string postData, Dictionary headers, CookieContainer cookies, Action cb) {
- // Make sure the endpoint is set
- if (string.IsNullOrEmpty(this.endpoint)) {
- cb(new Exception("Endpoint has not been set"), null);
- return;
+ // Send request
+ SendRequest(postData, headers, cookies, (Exception requestError, string responseString) => {
+ if (requestError != null) {
+ cb(requestError, null);
+ return;
+ }
+
+ // Deserialize the JSON response
+ JArray responseArray;
+ try {
+ responseArray = JArray.Parse(responseString);
+ } catch (Exception parseError) {
+ cb(parseError, null);
+ return;
+ }
+
+ cb(null, responseArray);
+ });
}
- // Make a copy of the provided headers and add additional required headers
- Dictionary finalHeaders = new Dictionary(this.headers);
- foreach (var header in headers) {
- if (finalHeaders.ContainsKey(header.Key)) {
- continue;
+ private void SendRequest(string postData, Dictionary headers, CookieContainer cookies, Action cb) {
+ // Make sure the endpoint is set
+ if (string.IsNullOrEmpty(this.endpoint)) {
+ cb(new Exception("Endpoint has not been set"), null);
+ return;
}
- finalHeaders.Add(header.Key, header.Value);
- }
+ // Make a copy of the provided headers and add additional required headers
+ Dictionary finalHeaders = new Dictionary(this.headers);
+ foreach (var header in headers) {
+ if (finalHeaders.ContainsKey(header.Key)) {
+ continue;
+ }
- // Send HTTP post to JSON rpc endpoint
- HTTPRequest.Post(this.endpoint, "application/json", postData, finalHeaders, cookies, cb);
+ finalHeaders.Add(header.Key, header.Value);
+ }
+
+ // Send HTTP post to JSON rpc endpoint
+ HTTPRequest.Post(this.endpoint, "application/json", postData, finalHeaders, cookies, cb);
+ }
}
}
diff --git a/JSONRPC/JSONRPCBatch.cs b/JSONRPC/JSONRPCBatch.cs
index 25d8007..1b74c17 100644
--- a/JSONRPC/JSONRPCBatch.cs
+++ b/JSONRPC/JSONRPCBatch.cs
@@ -1,28 +1,30 @@
-using Newtonsoft.Json.Linq;
+using Newtonsoft.Json.Linq;
-public class JSONRPCBatch {
- public JArray batch = new JArray();
-
- public void Add(string methodName, JObject parameters) {
- Add(JValue.CreateNull(), methodName, parameters);
- }
+namespace Wizcorp.MageSDK.Network.JsonRpc {
+ public class JSONRPCBatch {
+ public JArray batch = new JArray();
- public void Add(string id, string methodName, JObject parameters) {
- Add(new JValue(id), methodName, parameters);
- }
+ public void Add(string methodName, JObject parameters) {
+ Add(JValue.CreateNull(), methodName, parameters);
+ }
- public void Add(int id, string methodName, JObject parameters) {
- Add(new JValue(id), methodName, parameters);
- }
+ public void Add(string id, string methodName, JObject parameters) {
+ Add(new JValue(id), methodName, parameters);
+ }
+
+ public void Add(int id, string methodName, JObject parameters) {
+ Add(new JValue(id), methodName, parameters);
+ }
+
+ public void Add(JValue id, string methodName, JObject parameters) {
+ JObject requestObject = new JObject();
+ requestObject.Add("jsonrpc", new JValue("2.0"));
- public void Add(JValue id, string methodName, JObject parameters) {
- JObject requestObject = new JObject();
- requestObject.Add("jsonrpc", new JValue("2.0"));
-
- requestObject.Add("id", id);
- requestObject.Add("method", new JValue(methodName));
- requestObject.Add("params", parameters);
+ requestObject.Add("id", id);
+ requestObject.Add("method", new JValue(methodName));
+ requestObject.Add("params", parameters);
- batch.Add(requestObject);
+ batch.Add(requestObject);
+ }
}
-}
\ No newline at end of file
+}
diff --git a/Mage/Archivist/Archivist.cs b/Mage/Archivist/Archivist.cs
index 526c067..8f13ba1 100644
--- a/Mage/Archivist/Archivist.cs
+++ b/Mage/Archivist/Archivist.cs
@@ -1,473 +1,477 @@
-using System;
-using System.Collections;
+using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
-public class Archivist : EventEmitter {
- private Mage mage { get { return Mage.Instance; } }
- private Logger logger { get { return mage.logger("archivist"); } }
+using Wizcorp.MageSDK.Event;
+using Wizcorp.MageSDK.Log;
+
+namespace Wizcorp.MageSDK.MageClient {
+ public class Archivist : EventEmitter {
+ private Mage mage { get { return Mage.Instance; } }
+ private Logger logger { get { return mage.logger("archivist"); } }
+
+ // Local cache of all retrieved vault values
+ private Dictionary _cache = new Dictionary();
+
+
+ // Constructor
+ public Archivist() {
+ // Set data to vault value when set event received
+ mage.eventManager.on("archivist:set", (object sender, JToken info) => {
+ string topic = (string)info["key"]["topic"];
+ JObject index = (JObject)info["key"]["index"];
+ JToken data = info["value"]["data"];
+ string mediaType = (string)info["value"]["mediaType"];
+ int? expirationTime = (int?)info["expirationTime"];
+ ValueSet(topic, index, data, mediaType, expirationTime);
+ });
+
+ // Del data inside vault value when del event received
+ mage.eventManager.on("archivist:del", (object sender, JToken info) => {
+ string topic = (string)info["key"]["topic"];
+ JObject index = (JObject)info["key"]["index"];
+ ValueDel(topic, index);
+ });
+
+ // Touch vault value expiry when touch event received
+ mage.eventManager.on("archivist:touch", (object sender, JToken info) => {
+ string topic = (string)info["key"]["topic"];
+ JObject index = (JObject)info["key"]["index"];
+ int? expirationTime = (int?)info["expirationTime"];
+ ValueTouch(topic, index, expirationTime);
+ });
+
+ // Apply changes to vault value when applyDiff event is received
+ mage.eventManager.on("archivist:applyDiff", (object sender, JToken info) => {
+ string topic = (string)info["key"]["topic"];
+ JObject index = (JObject)info["key"]["index"];
+ JArray diff = (JArray)info["diff"];
+ int? expirationTime = (int?)info["expirationTime"];
+ ValueApplyDiff(topic, index, diff, expirationTime);
+ });
+ }
- // Local cache of all retrieved vault values
- private Dictionary _cache = new Dictionary();
+ ////////////////////////////////////////////
+ // Cache Manipulation //
+ ////////////////////////////////////////////
- // Constructor
- public Archivist () {
- // Set data to vault value when set event received
- mage.eventManager.on("archivist:set", (object sender, JToken info) => {
- string topic = (string)info["key"]["topic"];
- JObject index = (JObject)info["key"]["index"];
- JToken data = info["value"]["data"];
- string mediaType = (string)info["value"]["mediaType"];
- int? expirationTime = (int?)info["expirationTime"];
- ValueSet(topic, index, data, mediaType, expirationTime);
- });
-
- // Del data inside vault value when del event received
- mage.eventManager.on("archivist:del", (object sender, JToken info) => {
- string topic = (string)info["key"]["topic"];
- JObject index = (JObject)info["key"]["index"];
- ValueDel(topic, index);
- });
+ // Returns string id of a vault value for given topic and index
+ private static string CreateCacheKey(string topic, JObject index) {
+ // Sort the keys so order of index is always the same
+ List indexKeys = new List();
+ foreach (var property in index) {
+ indexKeys.Add(property.Key);
+ }
+ indexKeys.Sort();
- // Touch vault value expiry when touch event received
- mage.eventManager.on("archivist:touch", (object sender, JToken info) => {
- string topic = (string)info["key"]["topic"];
- JObject index = (JObject)info["key"]["index"];
- int? expirationTime = (int?)info["expirationTime"];
- ValueTouch(topic, index, expirationTime);
- });
+ // Construct cache key list with correct ordering
+ List cacheKeys = new List();
+ cacheKeys.Add(topic);
- // Apply changes to vault value when applyDiff event is received
- mage.eventManager.on("archivist:applyDiff", (object sender, JToken info) => {
- string topic = (string)info["key"]["topic"];
- JObject index = (JObject)info["key"]["index"];
- JArray diff = (JArray)info["diff"];
- int? expirationTime = (int?)info["expirationTime"];
- ValueApplyDiff(topic, index, diff, expirationTime);
- });
- }
+ foreach (string indexKey in indexKeys) {
+ cacheKeys.Add(indexKey + "=" + index[indexKey].ToString());
+ }
+ // Join the cache key list into final key string
+ return string.Join(":", cacheKeys.ToArray());
+ }
- ////////////////////////////////////////////
- // Cache Manipulation //
- ////////////////////////////////////////////
- // Returns string id of a vault value for given topic and index
- private static string CreateCacheKey (string topic, JObject index) {
- // Sort the keys so order of index is always the same
- List indexKeys = new List ();
- foreach (var property in index) {
- indexKeys.Add(property.Key);
- }
- indexKeys.Sort ();
+ // Returns cache value if it exists and has not passed max allowed age
+ private VaultValue GetCacheValue(string cacheKeyName, int? maxAge = null) {
+ lock ((object)_cache) {
+ if (!_cache.ContainsKey(cacheKeyName)) {
+ return null;
+ }
- // Construct cache key list with correct ordering
- List cacheKeys = new List ();
- cacheKeys.Add (topic);
+ VaultValue value = _cache[cacheKeyName];
+ double timespan = (DateTime.UtcNow - value.writtenAt).TotalMilliseconds;
+ if (maxAge != null && timespan > maxAge * 1000) {
+ return null;
+ }
- foreach (string indexKey in indexKeys) {
- cacheKeys.Add (indexKey + "=" + index[indexKey].ToString());
+ return value;
+ }
}
- // Join the cache key list into final key string
- return string.Join(":", cacheKeys.ToArray());
- }
+ // Return cache dictionary
+ public Dictionary GetCache() {
+ return _cache;
+ }
- // Returns cache value if it exists and has not passed max allowed age
- private VaultValue GetCacheValue(string cacheKeyName, int? maxAge = null) {
- lock ((object)_cache) {
- if (!_cache.ContainsKey(cacheKeyName)) {
- return null;
- }
- VaultValue value = _cache[cacheKeyName];
- double timespan = (DateTime.UtcNow - value.writtenAt).TotalMilliseconds;
- if (maxAge != null && timespan > maxAge * 1000) {
- return null;
+ // Clear out the cache entirely
+ public void ClearCache() {
+ lock ((object)_cache) {
+ _cache.Clear();
}
-
- return value;
}
- }
- // Return cache dictionary
- public Dictionary GetCache() {
- return _cache;
- }
+ // Remove a vault value from the cache by it's topic and index
+ public void DeleteCacheItem(string topic, JObject index) {
+ DeleteCacheItem(CreateCacheKey(topic, index));
+ }
- // Clear out the cache entirely
- public void ClearCache() {
- lock ((object)_cache) {
- _cache.Clear();
- }
- }
+ // Remove a vault value from the cache by it's cache key name
+ public void DeleteCacheItem(string cacheKeyName) {
+ lock ((object)_cache) {
+ logger.debug("Deleting cache item: " + cacheKeyName);
+ if (!_cache.ContainsKey(cacheKeyName)) {
+ return;
+ }
+ _cache.Remove(cacheKeyName);
+ }
+ }
- // Remove a vault value from the cache by it's topic and index
- public void DeleteCacheItem(string topic, JObject index) {
- DeleteCacheItem(CreateCacheKey(topic, index));
- }
+ ////////////////////////////////////////////
+ // Vault Value Manipulation //
+ ////////////////////////////////////////////
+ private void ValueSetOrDelete(JObject info) {
+ string topic = (string)info["key"]["topic"];
+ JObject index = (JObject)info["key"]["index"];
+ JObject rawValue = (JObject)info["value"];
- // Remove a vault value from the cache by it's cache key name
- public void DeleteCacheItem(string cacheKeyName) {
- lock ((object)_cache) {
- logger.debug("Deleting cache item: " + cacheKeyName);
- if (!_cache.ContainsKey(cacheKeyName)) {
- return;
+ if (rawValue != null) {
+ ValueSet(topic, index, rawValue["data"], (string)rawValue["mediaType"], (int?)info["expirationTime"]);
+ } else {
+ ValueDel(topic, index);
}
-
- _cache.Remove(cacheKeyName);
}
- }
+ private void ValueSet(string topic, JObject index, JToken data, string mediaType, int? expirationTime) {
+ string cacheKeyName = CreateCacheKey(topic, index);
+ VaultValue cacheValue = null;
+
+ // NOTE: even though some of these operations lock already, we put them inside this
+ // lock to ensure there is no time inconsistencies if things happen too fast.
+ lock ((object)_cache) {
+ cacheValue = GetCacheValue(cacheKeyName);
+ if (cacheValue == null) {
+ // If it doesn't exist, create a new vault value
+ cacheValue = new VaultValue(topic, index);
+ _cache.Add(cacheKeyName, cacheValue);
+ } else {
+ // If it exists delete existing value in preparation for set
+ cacheValue.Del();
+ }
- ////////////////////////////////////////////
- // Vault Value Manipulation //
- ////////////////////////////////////////////
- private void ValueSetOrDelete(JObject info) {
- string topic = (string)info["key"]["topic"];
- JObject index = (JObject)info["key"]["index"];
- JObject rawValue = (JObject)info["value"];
+ // Set data to vault value
+ cacheValue.SetData(mediaType, data);
+ cacheValue.Touch(expirationTime);
+ }
- if (rawValue != null) {
- ValueSet(topic, index, rawValue["data"], (string)rawValue["mediaType"], (int?)info["expirationTime"]);
- } else {
- ValueDel(topic, index);
+ // Emit set event
+ this.emit(topic + ":set", cacheValue);
}
- }
- private void ValueSet(string topic, JObject index, JToken data, string mediaType, int? expirationTime) {
- string cacheKeyName = CreateCacheKey(topic, index);
- VaultValue cacheValue = null;
+ private void ValueAdd(string topic, JObject index, JToken data, string mediaType, int? expirationTime) {
+ string cacheKeyName = CreateCacheKey(topic, index);
+ VaultValue cacheValue = null;
+
+ // NOTE: even though some of these operations lock already, we put them inside this
+ // lock to ensure there is no time inconsistencies if things happen too fast.
+ lock ((object)_cache) {
+ // Check if value already exists
+ cacheValue = GetCacheValue(cacheKeyName);
+ if (cacheValue != null) {
+ logger.error("Could not add value (already exists): " + cacheKeyName);
+ return;
+ }
- // NOTE: even though some of these operations lock already, we put them inside this
- // lock to ensure there is no time inconsistencies if things happen too fast.
- lock ((object)_cache) {
- cacheValue = GetCacheValue(cacheKeyName);
- if (cacheValue == null) {
- // If it doesn't exist, create a new vault value
+ // Create new vault value
cacheValue = new VaultValue(topic, index);
_cache.Add(cacheKeyName, cacheValue);
- } else {
- // If it exists delete existing value in preparation for set
- cacheValue.Del();
+
+ // Set data to vault value
+ cacheValue.SetData(mediaType, data);
+ cacheValue.Touch(expirationTime);
}
- // Set data to vault value
- cacheValue.SetData(mediaType, data);
- cacheValue.Touch(expirationTime);
+ // Emit add event
+ this.emit(topic + ":add", cacheValue);
}
- // Emit set event
- this.emit(topic + ":set", cacheValue);
- }
-
- private void ValueAdd(string topic, JObject index, JToken data, string mediaType, int? expirationTime) {
- string cacheKeyName = CreateCacheKey(topic, index);
- VaultValue cacheValue = null;
-
- // NOTE: even though some of these operations lock already, we put them inside this
- // lock to ensure there is no time inconsistencies if things happen too fast.
- lock ((object)_cache) {
+ private void ValueDel(string topic, JObject index) {
// Check if value already exists
- cacheValue = GetCacheValue(cacheKeyName);
- if (cacheValue != null) {
- logger.error("Could not add value (already exists): " + cacheKeyName);
+ string cacheKeyName = CreateCacheKey(topic, index);
+ VaultValue cacheValue = GetCacheValue(cacheKeyName);
+ if (cacheValue == null) {
+ logger.warning("Could not delete value (doesn't exist): " + cacheKeyName);
return;
}
- // Create new vault value
- cacheValue = new VaultValue(topic, index);
- _cache.Add(cacheKeyName, cacheValue);
+ // Do delete
+ cacheValue.Del();
- // Set data to vault value
- cacheValue.SetData(mediaType, data);
- cacheValue.Touch(expirationTime);
+ // Emit touch event
+ this.emit(topic + ":del", cacheValue);
}
- // Emit add event
- this.emit(topic + ":add", cacheValue);
- }
+ private void ValueTouch(string topic, JObject index, int? expirationTime) {
+ // Check if value already exists
+ string cacheKeyName = CreateCacheKey(topic, index);
+ VaultValue cacheValue = GetCacheValue(cacheKeyName);
+ if (cacheValue == null) {
+ logger.warning("Could not touch value (doesn't exist): " + cacheKeyName);
+ return;
+ }
- private void ValueDel(string topic, JObject index) {
- // Check if value already exists
- string cacheKeyName = CreateCacheKey(topic, index);
- VaultValue cacheValue = GetCacheValue(cacheKeyName);
- if (cacheValue == null) {
- logger.warning("Could not delete value (doesn't exist): " + cacheKeyName);
- return;
- }
-
- // Do delete
- cacheValue.Del();
-
- // Emit touch event
- this.emit(topic + ":del", cacheValue);
- }
+ // Do touch
+ cacheValue.Touch(expirationTime);
- private void ValueTouch(string topic, JObject index, int? expirationTime) {
- // Check if value already exists
- string cacheKeyName = CreateCacheKey(topic, index);
- VaultValue cacheValue = GetCacheValue(cacheKeyName);
- if (cacheValue == null) {
- logger.warning("Could not touch value (doesn't exist): " + cacheKeyName);
- return;
+ // Emit touch event
+ this.emit(topic + ":touch", cacheValue);
}
- // Do touch
- cacheValue.Touch(expirationTime);
-
- // Emit touch event
- this.emit(topic + ":touch", cacheValue);
- }
+ private void ValueApplyDiff(string topic, JObject index, JArray diff, int? expirationTime) {
+ // Make sure value exists
+ string cacheKeyName = CreateCacheKey(topic, index);
+ VaultValue cacheValue = GetCacheValue(cacheKeyName);
+ if (cacheValue == null) {
+ logger.warning("Got a diff for a non-existent value:" + cacheKeyName);
+ return;
+ }
- private void ValueApplyDiff(string topic, JObject index, JArray diff, int? expirationTime) {
- // Make sure value exists
- string cacheKeyName = CreateCacheKey(topic, index);
- VaultValue cacheValue = GetCacheValue(cacheKeyName);
- if (cacheValue == null) {
- logger.warning("Got a diff for a non-existent value:" + cacheKeyName);
- return;
+ // Apply diff
+ cacheValue.ApplyDiff(diff);
+ cacheValue.Touch(expirationTime);
+
+ // Emit applyDiff event
+ this.emit(topic + ":applyDiff", cacheValue);
}
-
- // Apply diff
- cacheValue.ApplyDiff(diff);
- cacheValue.Touch(expirationTime);
-
- // Emit applyDiff event
- this.emit(topic + ":applyDiff", cacheValue);
- }
- ////////////////////////////////////////////
- // Raw Communication //
- ////////////////////////////////////////////
- private void rawGet(string topic, JObject index, Action cb) {
- JObject parameters = new JObject();
- parameters.Add("topic", topic);
- parameters.Add("index", index);
-
- mage.commandCenter.SendCommand("archivist.rawGet", parameters, cb);
- }
-
- private void rawMGet(JToken queries, JObject options, Action cb) {
- JObject parameters = new JObject();
- parameters.Add(new JProperty ("queries", queries));
- parameters.Add("options", options);
+ ////////////////////////////////////////////
+ // Raw Communication //
+ ////////////////////////////////////////////
+ private void rawGet(string topic, JObject index, Action cb) {
+ JObject parameters = new JObject();
+ parameters.Add("topic", topic);
+ parameters.Add("index", index);
- mage.commandCenter.SendCommand("archivist.rawMGet", parameters, cb);
- }
-
- private void rawList(string topic, JObject partialIndex, JObject options, Action cb) {
- JObject parameters = new JObject();
- parameters.Add("topic", new JValue(topic));
- parameters.Add("partialIndex", partialIndex);
- parameters.Add("options", options);
-
- mage.commandCenter.SendCommand("archivist.rawList", parameters, cb);
- }
-
- private void rawSet(string topic, JObject index, JToken data, string mediaType, string encoding, string expirationTime, Action cb) {
- JObject parameters = new JObject();
- parameters.Add ("topic", new JValue(topic));
- parameters.Add ("index", index);
- parameters.Add(new JProperty ("data", data));
- parameters.Add ("mediaType", new JValue(mediaType));
- parameters.Add ("encoding", new JValue(encoding));
- parameters.Add ("expirationTime", new JValue(expirationTime));
-
- mage.commandCenter.SendCommand("archivist.rawSet", parameters, cb);
- }
-
- private void rawDel(string topic, JObject index, Action cb) {
- JObject parameters = new JObject();
- parameters.Add ("topic", new JValue(topic));
- parameters.Add ("index", index);
+ mage.commandCenter.SendCommand("archivist.rawGet", parameters, cb);
+ }
- mage.commandCenter.SendCommand("archivist.rawDel", parameters, cb);
- }
-
-
- ////////////////////////////////////////////
- // Exposed Operations //
- ////////////////////////////////////////////
- public void get(string topic, JObject index, JObject options, Action cb) {
- // Default options
- options = (options != null) ? options : new JObject();
- if (options["optional"] == null) {
- options.Add("optional", new JValue(false));
+ private void rawMGet(JToken queries, JObject options, Action cb) {
+ JObject parameters = new JObject();
+ parameters.Add(new JProperty("queries", queries));
+ parameters.Add("options", options);
+
+ mage.commandCenter.SendCommand("archivist.rawMGet", parameters, cb);
}
+ private void rawList(string topic, JObject partialIndex, JObject options, Action cb) {
+ JObject parameters = new JObject();
+ parameters.Add("topic", new JValue(topic));
+ parameters.Add("partialIndex", partialIndex);
+ parameters.Add("options", options);
- // Check cache
- string cacheKeyName = CreateCacheKey(topic, index);
- VaultValue cacheValue = GetCacheValue(cacheKeyName, (int?)options["maxAge"]);
- if (cacheValue != null) {
- cb(null, cacheValue.data);
- return;
+ mage.commandCenter.SendCommand("archivist.rawList", parameters, cb);
}
-
- // Get data from server
- rawGet (topic, index, (Exception error, JToken result) => {
- if (error != null) {
- cb (error, null);
- return;
- }
+ private void rawSet(string topic, JObject index, JToken data, string mediaType, string encoding, string expirationTime, Action cb) {
+ JObject parameters = new JObject();
+ parameters.Add("topic", new JValue(topic));
+ parameters.Add("index", index);
+ parameters.Add(new JProperty("data", data));
+ parameters.Add("mediaType", new JValue(mediaType));
+ parameters.Add("encoding", new JValue(encoding));
+ parameters.Add("expirationTime", new JValue(expirationTime));
- // Parse value
- try {
- ValueSetOrDelete((JObject)result);
- } catch (Exception cacheError) {
- cb(cacheError, null);
- return;
- }
+ mage.commandCenter.SendCommand("archivist.rawSet", parameters, cb);
+ }
- // Return result
- cb (null, GetCacheValue(cacheKeyName).data);
- });
- }
+ private void rawDel(string topic, JObject index, Action cb) {
+ JObject parameters = new JObject();
+ parameters.Add("topic", new JValue(topic));
+ parameters.Add("index", index);
- public void mget(JArray queries, JObject options, Action cb) {
- // Default options
- options = (options != null) ? options : new JObject();
- if (options["optional"] == null) {
- options.Add("optional", new JValue(false));
+ mage.commandCenter.SendCommand("archivist.rawDel", parameters, cb);
}
- // Keep track of actual data we need from server
- JArray realQueries = new JArray();
- Dictionary realQueryKeys = new Dictionary();
- JArray responseArray = new JArray();
+ ////////////////////////////////////////////
+ // Exposed Operations //
+ ////////////////////////////////////////////
+ public void get(string topic, JObject index, JObject options, Action cb) {
+ // Default options
+ options = (options != null) ? options : new JObject();
+ if (options["optional"] == null) {
+ options.Add("optional", new JValue(false));
+ }
- // Check cache
- foreach (JObject query in queries) {
- string topic = (string)query["topic"];
- JObject index = (JObject)query["index"];
+ // Check cache
string cacheKeyName = CreateCacheKey(topic, index);
VaultValue cacheValue = GetCacheValue(cacheKeyName, (int?)options["maxAge"]);
if (cacheValue != null) {
- responseArray.Add(cacheValue.data);
- } else {
- realQueryKeys.Add(cacheKeyName, responseArray.Count);
- responseArray.Add(null);
- realQueries.Add(query);
+ cb(null, cacheValue.data);
+ return;
}
- }
- // Check if any real queries exist
- if (realQueries.Count == 0) {
- cb (null, responseArray);
- return;
- }
+ // Get data from server
+ rawGet(topic, index, (Exception error, JToken result) => {
+ if (error != null) {
+ cb(error, null);
+ return;
+ }
+
+ // Parse value
+ try {
+ ValueSetOrDelete((JObject)result);
+ } catch (Exception cacheError) {
+ cb(cacheError, null);
+ return;
+ }
+ // Return result
+ cb(null, GetCacheValue(cacheKeyName).data);
+ });
+ }
- // Get data from server
- rawMGet (realQueries, options, (Exception error, JToken results) => {
- if (error != null) {
- cb (error, null);
- return;
+ public void mget(JArray queries, JObject options, Action cb) {
+ // Default options
+ options = (options != null) ? options : new JObject();
+ if (options["optional"] == null) {
+ options.Add("optional", new JValue(false));
}
- try {
- foreach (JObject topicValue in results as JArray) {
- // Determine value cacheKeyName
- string topic = (string)topicValue["key"]["topic"];
- JObject index = (JObject)topicValue["key"]["index"];
- string cacheKeyName = CreateCacheKey(topic, index);
-
- // Set value to cache
- ValueSetOrDelete(topicValue);
- // Add value to response
- int responseKey = realQueryKeys[cacheKeyName];
- responseArray[responseKey].Replace(GetCacheValue(cacheKeyName).data);
+ // Keep track of actual data we need from server
+ JArray realQueries = new JArray();
+ Dictionary realQueryKeys = new Dictionary();
+ JArray responseArray = new JArray();
+
+
+ // Check cache
+ foreach (JObject query in queries) {
+ string topic = (string)query["topic"];
+ JObject index = (JObject)query["index"];
+ string cacheKeyName = CreateCacheKey(topic, index);
+ VaultValue cacheValue = GetCacheValue(cacheKeyName, (int?)options["maxAge"]);
+ if (cacheValue != null) {
+ responseArray.Add(cacheValue.data);
+ } else {
+ realQueryKeys.Add(cacheKeyName, responseArray.Count);
+ responseArray.Add(null);
+ realQueries.Add(query);
}
- } catch (Exception cacheError) {
- cb(cacheError, null);
+ }
+
+
+ // Check if any real queries exist
+ if (realQueries.Count == 0) {
+ cb(null, responseArray);
return;
}
-
- // Return result
- cb (null, responseArray);
- });
- }
-
- public void mget(JObject queries, JObject options, Action cb) {
- // Default options
- options = (options != null) ? options : new JObject();
- if (options["optional"] == null) {
- options.Add("optional", new JValue(false));
- }
- // Keep track of actual data we need from server
- JObject realQueries = new JObject();
- Dictionary realQueryKeys = new Dictionary();
- JObject responseObject = new JObject();
+ // Get data from server
+ rawMGet(realQueries, options, (Exception error, JToken results) => {
+ if (error != null) {
+ cb(error, null);
+ return;
+ }
+ try {
+ foreach (JObject topicValue in results as JArray) {
+ // Determine value cacheKeyName
+ string topic = (string)topicValue["key"]["topic"];
+ JObject index = (JObject)topicValue["key"]["index"];
+ string cacheKeyName = CreateCacheKey(topic, index);
+
+ // Set value to cache
+ ValueSetOrDelete(topicValue);
+
+ // Add value to response
+ int responseKey = realQueryKeys[cacheKeyName];
+ responseArray[responseKey].Replace(GetCacheValue(cacheKeyName).data);
+ }
+ } catch (Exception cacheError) {
+ cb(cacheError, null);
+ return;
+ }
- // Check cache
- foreach (var query in queries) {
- string cacheKeyName = CreateCacheKey(query.Value["topic"].ToString(), query.Value["index"] as JObject);
- VaultValue cacheValue = GetCacheValue(cacheKeyName, (int?)options["maxAge"]);
- if (cacheValue != null) {
- responseObject.Add(query.Key, cacheValue.data);
- } else {
- realQueryKeys.Add(cacheKeyName, query.Key);
- realQueries.Add(query.Key, query.Value);
- }
+ // Return result
+ cb(null, responseArray);
+ });
}
+ public void mget(JObject queries, JObject options, Action cb) {
+ // Default options
+ options = (options != null) ? options : new JObject();
+ if (options["optional"] == null) {
+ options.Add("optional", new JValue(false));
+ }
+
+
+ // Keep track of actual data we need from server
+ JObject realQueries = new JObject();
+ Dictionary realQueryKeys = new Dictionary();
+ JObject responseObject = new JObject();
- // Check if any real queries exist
- if (realQueries.Count == 0) {
- cb (null, responseObject);
- return;
- }
+ // Check cache
+ foreach (var query in queries) {
+ string cacheKeyName = CreateCacheKey(query.Value["topic"].ToString(), query.Value["index"] as JObject);
+ VaultValue cacheValue = GetCacheValue(cacheKeyName, (int?)options["maxAge"]);
+ if (cacheValue != null) {
+ responseObject.Add(query.Key, cacheValue.data);
+ } else {
+ realQueryKeys.Add(cacheKeyName, query.Key);
+ realQueries.Add(query.Key, query.Value);
+ }
+ }
- // Get data from server
- rawMGet (realQueries, options, (Exception error, JToken results) => {
- if (error != null) {
- cb (error, null);
+
+ // Check if any real queries exist
+ if (realQueries.Count == 0) {
+ cb(null, responseObject);
return;
}
- try {
- foreach (JObject topicValue in results as JArray) {
- // Determine value cacheKeyName
- string valueTopic = topicValue["key"]["topic"].ToString();
- JObject valueIndex = (JObject)topicValue["key"]["index"];
- string cacheKeyName = CreateCacheKey(valueTopic, valueIndex);
- // Set value to cache
- ValueSetOrDelete(topicValue);
+ // Get data from server
+ rawMGet(realQueries, options, (Exception error, JToken results) => {
+ if (error != null) {
+ cb(error, null);
+ return;
+ }
- // Add value to response
- string responseKey = realQueryKeys[cacheKeyName];
- responseObject.Add(responseKey, GetCacheValue(cacheKeyName).data);
+ try {
+ foreach (JObject topicValue in results as JArray) {
+ // Determine value cacheKeyName
+ string valueTopic = topicValue["key"]["topic"].ToString();
+ JObject valueIndex = (JObject)topicValue["key"]["index"];
+ string cacheKeyName = CreateCacheKey(valueTopic, valueIndex);
+
+ // Set value to cache
+ ValueSetOrDelete(topicValue);
+
+ // Add value to response
+ string responseKey = realQueryKeys[cacheKeyName];
+ responseObject.Add(responseKey, GetCacheValue(cacheKeyName).data);
+ }
+ } catch (Exception cacheError) {
+ cb(cacheError, null);
+ return;
}
- } catch (Exception cacheError) {
- cb(cacheError, null);
- return;
- }
-
- // Return result
- cb (null, responseObject);
- });
- }
-
- public void list(string topic, JObject partialIndex, JObject options, Action cb) {
- rawList (topic, partialIndex, options, cb);
+
+ // Return result
+ cb(null, responseObject);
+ });
+ }
+
+ public void list(string topic, JObject partialIndex, JObject options, Action cb) {
+ rawList(topic, partialIndex, options, cb);
+ }
}
}
diff --git a/Mage/Archivist/VaultValue.cs b/Mage/Archivist/VaultValue.cs
index 2214459..aca67c5 100644
--- a/Mage/Archivist/VaultValue.cs
+++ b/Mage/Archivist/VaultValue.cs
@@ -1,93 +1,96 @@
-using System;
+using System;
using Newtonsoft.Json.Linq;
-public class VaultValue {
- //
- private string _topic;
- public string topic { get { return _topic; } }
+using Wizcorp.MageSDK.Tomes;
- private JObject _index;
- public JObject index { get { return _index; } }
-
- private JToken _data;
- public JToken data { get { return _data; } }
+namespace Wizcorp.MageSDK.MageClient {
+ public class VaultValue {
+ //
+ private string _topic;
+ public string topic { get { return _topic; } }
- private string _mediaType;
- public string mediaType { get { return _mediaType; } }
+ private JObject _index;
+ public JObject index { get { return _index; } }
- private int? _expirationTime;
- public int? expirationTime { get { return _expirationTime; } }
+ private JToken _data;
+ public JToken data { get { return _data; } }
- //
- private DateTime _writtenAt;
- public DateTime writtenAt { get { return _writtenAt; }}
+ private string _mediaType;
+ public string mediaType { get { return _mediaType; } }
+ private int? _expirationTime;
+ public int? expirationTime { get { return _expirationTime; } }
- //
- public VaultValue(string topic, JObject index) {
- _topic = topic;
- _index = index;
- }
+ //
+ private DateTime _writtenAt;
+ public DateTime writtenAt { get { return _writtenAt; } }
- // TODO: implement multiple media-types and encoding
- public void SetData(string mediaType, JToken data) {
- lock ((object)this) {
- // Detect media type
- _mediaType = mediaType;
+ //
+ public VaultValue(string topic, JObject index) {
+ _topic = topic;
+ _index = index;
+ }
- // Set data based on media type
- _data = Tome.Conjure(JToken.Parse ((string)data));
- // Bump the last written time
- _writtenAt = DateTime.UtcNow;
- }
- }
+ // TODO: implement multiple media-types and encoding
+ public void SetData(string mediaType, JToken data) {
+ lock ((object)this) {
+ // Detect media type
+ _mediaType = mediaType;
+ // Set data based on media type
+ _data = Tome.Conjure(JToken.Parse((string)data));
- //
- public void Del() {
- lock ((object)this) {
- // Bump the last written time and check if we have data to destroy
- _writtenAt = DateTime.UtcNow;
- if (_data == null) {
- return;
+ // Bump the last written time
+ _writtenAt = DateTime.UtcNow;
}
+ }
- // Cleanup data
- Tome.Destroy(_data);
- _data = null;
- _mediaType = null;
- // Clear expiration time
- Touch (null);
- }
- }
+ //
+ public void Del() {
+ lock ((object)this) {
+ // Bump the last written time and check if we have data to destroy
+ _writtenAt = DateTime.UtcNow;
+ if (_data == null) {
+ return;
+ }
+ // Cleanup data
+ Tome.Destroy(_data);
+ _data = null;
+ _mediaType = null;
- // TODO: the actual implementation of this requires the MAGE time module,
- // also we have a timer to clear the value once expired.
- public void Touch(int? expirationTime) {
- lock ((object)this) {
- _expirationTime = expirationTime;
+ // Clear expiration time
+ Touch(null);
+ }
}
- }
- //
- public void ApplyDiff(JArray diff) {
- lock ((object)this) {
- if (diff == null || _data == null) {
- return;
+ // TODO: the actual implementation of this requires the MAGE time module,
+ // also we have a timer to clear the value once expired.
+ public void Touch(int? expirationTime) {
+ lock ((object)this) {
+ _expirationTime = expirationTime;
}
+ }
- // Apply diff to data
- Tome.ApplyDiff(_data, diff);
- // Bump the last written time
- _writtenAt = DateTime.UtcNow;
+ //
+ public void ApplyDiff(JArray diff) {
+ lock ((object)this) {
+ if (diff == null || _data == null) {
+ return;
+ }
+
+ // Apply diff to data
+ Tome.ApplyDiff(_data, diff);
+
+ // Bump the last written time
+ _writtenAt = DateTime.UtcNow;
+ }
}
}
}
-
\ No newline at end of file
diff --git a/Mage/CommandCenter/CommandBatch.cs b/Mage/CommandCenter/CommandBatch.cs
index 9abbdf4..716a691 100644
--- a/Mage/CommandCenter/CommandBatch.cs
+++ b/Mage/CommandCenter/CommandBatch.cs
@@ -3,19 +3,20 @@
using Newtonsoft.Json.Linq;
+namespace Wizcorp.MageSDK.MageClient.Command {
+ public class CommandBatch {
+ public int queryId;
+ public List> batchHeaders = new List>();
+ public List batchItems = new List();
-public class CommandBatch {
- public int queryId;
- public List> batchHeaders = new List>();
- public List batchItems = new List();
+ public object serialisedCache;
- public object serialisedCache;
+ public CommandBatch(int queryId) {
+ this.queryId = queryId;
+ }
- public CommandBatch(int queryId) {
- this.queryId = queryId;
- }
-
- public void Queue(string commandName, JObject parameters, Action cb) {
- batchItems.Add(new CommandBatchItem(commandName, parameters, cb));
+ public void Queue(string commandName, JObject parameters, Action cb) {
+ batchItems.Add(new CommandBatchItem(commandName, parameters, cb));
+ }
}
}
diff --git a/Mage/CommandCenter/CommandBatchItem.cs b/Mage/CommandCenter/CommandBatchItem.cs
index 2e12c17..97ce042 100644
--- a/Mage/CommandCenter/CommandBatchItem.cs
+++ b/Mage/CommandCenter/CommandBatchItem.cs
@@ -1,16 +1,17 @@
-using System;
+using System;
using Newtonsoft.Json.Linq;
+namespace Wizcorp.MageSDK.MageClient.Command {
+ public class CommandBatchItem {
+ public string commandName;
+ public JObject parameters;
+ public Action cb;
-public class CommandBatchItem {
- public string commandName;
- public JObject parameters;
- public Action cb;
-
- public CommandBatchItem(string commandName, JObject parameters, Action cb) {
- this.commandName = commandName;
- this.parameters = parameters;
- this.cb = cb;
+ public CommandBatchItem(string commandName, JObject parameters, Action cb) {
+ this.commandName = commandName;
+ this.parameters = parameters;
+ this.cb = cb;
+ }
}
-}
\ No newline at end of file
+}
diff --git a/Mage/CommandCenter/CommandCenter.cs b/Mage/CommandCenter/CommandCenter.cs
index c68cea1..609f143 100644
--- a/Mage/CommandCenter/CommandCenter.cs
+++ b/Mage/CommandCenter/CommandCenter.cs
@@ -3,146 +3,151 @@
using Newtonsoft.Json.Linq;
-
-public class CommandCenter {
- private Mage mage { get { return Mage.Instance; }}
- private Logger logger { get { return mage.logger("CommandCenter"); }}
-
- // Endpoint and credentials
- private string baseUrl;
- private string appName;
- Dictionary headers = new Dictionary();
-
- // Message Hooks
- public delegate void MessageHook(CommandBatch commandBatch);
- public MessageHook preSerialiseHook;
- public MessageHook preNetworkHook;
-
- // Current transport client
- private CommandTransportClient transportClient;
-
- // Command Batches
- private int nextQueryId = 1;
- private CommandBatch currentBatch;
- private CommandBatch sendingBatch;
-
- //
- public CommandCenter(CommandTransportType transportType = CommandTransportType.HTTP) {
- currentBatch = new CommandBatch(nextQueryId++);
- SetTransport(transportType);
- }
-
- //
- public void SetTransport(CommandTransportType transportType) {
- // Cleanup existing transport client
- if (transportClient != null) {
- transportClient = null;
+using Wizcorp.MageSDK.Command.Client;
+using Wizcorp.MageSDK.CommandCenter.Client;
+using Wizcorp.MageSDK.Log;
+
+namespace Wizcorp.MageSDK.MageClient.Command {
+ public class CommandCenter {
+ private Mage mage { get { return Mage.Instance; } }
+ private Logger logger { get { return mage.logger("CommandCenter"); } }
+
+ // Endpoint and credentials
+ private string baseUrl;
+ private string appName;
+ Dictionary headers = new Dictionary();
+
+ // Message Hooks
+ public delegate void MessageHook(CommandBatch commandBatch);
+ public MessageHook preSerialiseHook;
+ public MessageHook preNetworkHook;
+
+ // Current transport client
+ private CommandTransportClient transportClient;
+
+ // Command Batches
+ private int nextQueryId = 1;
+ private CommandBatch currentBatch;
+ private CommandBatch sendingBatch;
+
+ //
+ public CommandCenter(CommandTransportType transportType = CommandTransportType.HTTP) {
+ currentBatch = new CommandBatch(nextQueryId++);
+ SetTransport(transportType);
}
- // Create new transport client instance
- if (transportType == CommandTransportType.HTTP) {
- transportClient = new CommandHTTPClient() as CommandTransportClient;
- transportClient.SetEndpoint(baseUrl, appName, headers);
- } else if (transportType == CommandTransportType.JSONRPC) {
- transportClient = new CommandJSONRPCClient() as CommandTransportClient;
- transportClient.SetEndpoint(baseUrl, appName, headers);
- } else {
- throw new Exception("Invalid transport type: " + transportType);
- }
+ //
+ public void SetTransport(CommandTransportType transportType) {
+ // Cleanup existing transport client
+ if (transportClient != null) {
+ transportClient = null;
+ }
- // Setup event handlers
- transportClient.OnSendComplete += BatchComplete;
- transportClient.OnTransportError += TransportError;
- }
+ // Create new transport client instance
+ if (transportType == CommandTransportType.HTTP) {
+ transportClient = new CommandHTTPClient() as CommandTransportClient;
+ transportClient.SetEndpoint(baseUrl, appName, headers);
+ } else if (transportType == CommandTransportType.JSONRPC) {
+ transportClient = new CommandJSONRPCClient() as CommandTransportClient;
+ transportClient.SetEndpoint(baseUrl, appName, headers);
+ } else {
+ throw new Exception("Invalid transport type: " + transportType);
+ }
- //
- public void SetEndpoint(string baseUrl, string appName, Dictionary headers = null) {
- this.baseUrl = baseUrl;
- this.appName = appName;
- this.headers = new Dictionary(headers);
+ // Setup event handlers
+ transportClient.OnSendComplete += BatchComplete;
+ transportClient.OnTransportError += TransportError;
+ }
+
+ //
+ public void SetEndpoint(string baseUrl, string appName, Dictionary headers = null) {
+ this.baseUrl = baseUrl;
+ this.appName = appName;
+ this.headers = new Dictionary(headers);
- if (transportClient != null) {
- transportClient.SetEndpoint(this.baseUrl, this.appName, this.headers);
+ if (transportClient != null) {
+ transportClient.SetEndpoint(this.baseUrl, this.appName, this.headers);
+ }
}
- }
- //
- private void SendBatch() {
- mage.eventManager.emit("io.send", null);
+ //
+ private void SendBatch() {
+ mage.eventManager.emit("io.send", null);
- lock ((object)this) {
- // Swap batches around locking the queue
- sendingBatch = currentBatch;
- currentBatch = new CommandBatch(nextQueryId++);
+ lock ((object)this) {
+ // Swap batches around locking the queue
+ sendingBatch = currentBatch;
+ currentBatch = new CommandBatch(nextQueryId++);
- // Execute pre-serialisation message hooks
- if (preSerialiseHook != null) {
- preSerialiseHook.Invoke(sendingBatch);
- }
+ // Execute pre-serialisation message hooks
+ if (preSerialiseHook != null) {
+ preSerialiseHook.Invoke(sendingBatch);
+ }
+
+ // Serialise the batch
+ logger.debug("Serialising batch: " + sendingBatch.queryId);
+ transportClient.SerialiseBatch(sendingBatch);
- // Serialise the batch
- logger.debug("Serialising batch: " + sendingBatch.queryId);
- transportClient.SerialiseBatch(sendingBatch);
+ // Execute pre-network message hooks
+ if (preNetworkHook != null) {
+ preNetworkHook.Invoke(sendingBatch);
+ }
- // Execute pre-network message hooks
- if (preNetworkHook != null) {
- preNetworkHook.Invoke(sendingBatch);
+ // Send the batch
+ logger.debug("Sending batch: " + sendingBatch.queryId);
+ transportClient.SendBatch(sendingBatch);
}
+ }
+
+ // Resend batch
+ public void Resend() {
+ mage.eventManager.emit("io.resend", null);
- // Send the batch
- logger.debug("Sending batch: " + sendingBatch.queryId);
+ logger.debug("Re-sending batch: " + sendingBatch.queryId);
transportClient.SendBatch(sendingBatch);
}
- }
-
- // Resend batch
- public void Resend() {
- mage.eventManager.emit("io.resend", null);
- logger.debug("Re-sending batch: " + sendingBatch.queryId);
- transportClient.SendBatch(sendingBatch);
- }
+ //
+ private void BatchComplete() {
+ mage.eventManager.emit("io.response", null);
- //
- private void BatchComplete() {
- mage.eventManager.emit("io.response", null);
+ lock ((object)this) {
+ sendingBatch = null;
- lock ((object)this) {
- sendingBatch = null;
-
- // Check if next queued batch should be sent as well
- if (currentBatch.batchItems.Count > 0) {
- SendBatch();
+ // Check if next queued batch should be sent as well
+ if (currentBatch.batchItems.Count > 0) {
+ SendBatch();
+ }
}
}
- }
-
- //
- private void TransportError(string errorType, Exception error) {
- logger.data(error).error("Error when sending command batch request '" + errorType + "'");
- mage.eventManager.emit("io.error." + errorType, null);
- }
- // Try and send a command right away if there is nothing being sent.
- public void SendCommand(string commandName, JObject parameters, Action cb) {
- lock ((object)this) {
- // Add command to queue
- currentBatch.Queue(commandName, parameters, cb);
+ //
+ private void TransportError(string errorType, Exception error) {
+ logger.data(error).error("Error when sending command batch request '" + errorType + "'");
+ mage.eventManager.emit("io.error." + errorType, null);
+ }
- // Check if we are busy and should only queue
- if (sendingBatch != null) {
- return;
- }
+ // Try and send a command right away if there is nothing being sent.
+ public void SendCommand(string commandName, JObject parameters, Action cb) {
+ lock ((object)this) {
+ // Add command to queue
+ currentBatch.Queue(commandName, parameters, cb);
- // Otherwise send the batch
- SendBatch();
+ // Check if we are busy and should only queue
+ if (sendingBatch != null) {
+ return;
+ }
+
+ // Otherwise send the batch
+ SendBatch();
+ }
}
- }
- // Queue command to current batch and wait for it to get processed by another command
- public void PiggyBackCommand(string commandName, JObject parameters, Action cb) {
- lock ((object)this) {
- currentBatch.Queue(commandName, parameters, cb);
+ // Queue command to current batch and wait for it to get processed by another command
+ public void PiggyBackCommand(string commandName, JObject parameters, Action cb) {
+ lock ((object)this) {
+ currentBatch.Queue(commandName, parameters, cb);
+ }
}
}
}
diff --git a/Mage/CommandCenter/TransportClient/CommandHTTPClient.cs b/Mage/CommandCenter/TransportClient/CommandHTTPClient.cs
index bebb5a0..86ff736 100644
--- a/Mage/CommandCenter/TransportClient/CommandHTTPClient.cs
+++ b/Mage/CommandCenter/TransportClient/CommandHTTPClient.cs
@@ -5,138 +5,145 @@
using Newtonsoft.Json.Linq;
-public class CommandHttpClientCache {
- public string batchUrl;
- public string postData;
- public Dictionary headers;
-
- public CommandHttpClientCache(string batchUrl, string postData, Dictionary headers) {
- this.batchUrl = batchUrl;
- this.postData = postData;
- this.headers = headers;
+using Wizcorp.MageSDK.CommandCenter.Client;
+using Wizcorp.MageSDK.Log;
+using Wizcorp.MageSDK.MageClient;
+using Wizcorp.MageSDK.MageClient.Command;
+using Wizcorp.MageSDK.Network.Http;
+
+namespace Wizcorp.MageSDK.Command.Client {
+ public class CommandHttpClientCache {
+ public string batchUrl;
+ public string postData;
+ public Dictionary headers;
+
+ public CommandHttpClientCache(string batchUrl, string postData, Dictionary headers) {
+ this.batchUrl = batchUrl;
+ this.postData = postData;
+ this.headers = headers;
+ }
}
-}
-public class CommandHTTPClient : CommandTransportClient {
- private Mage mage { get { return Mage.Instance; } }
- private Logger logger { get { return mage.logger("CommandHTTPClient"); } }
-
- //
- private string endpoint;
- private Dictionary headers;
-
- //
- public override void SetEndpoint(string baseUrl, string appName, Dictionary headers = null) {
- this.endpoint = baseUrl + "/" + appName;
- this.headers = new Dictionary(headers);
- }
+ public class CommandHTTPClient : CommandTransportClient {
+ private Mage mage { get { return Mage.Instance; } }
+ private Logger logger { get { return mage.logger("CommandHTTPClient"); } }
- //
- public override void SerialiseBatch(CommandBatch commandBatch) {
- List commands = new List();
- List data = new List();
+ //
+ private string endpoint;
+ private Dictionary headers;
- // Attach batch headers to post data
- JArray batchHeaders = new JArray();
- for (int batchHeaderI = 0; batchHeaderI < commandBatch.batchHeaders.Count; batchHeaderI += 1) {
- Dictionary batchHeader = commandBatch.batchHeaders[batchHeaderI];
- batchHeaders.Add(JObject.FromObject(batchHeader));
- }
- data.Add(batchHeaders.ToString(Newtonsoft.Json.Formatting.None));
-
- // Attach command names to url and parameters to post data
- for (int batchItemI = 0; batchItemI < commandBatch.batchItems.Count; batchItemI += 1) {
- CommandBatchItem commandItem = commandBatch.batchItems[batchItemI];
- commands.Add(commandItem.commandName);
- data.Add(commandItem.parameters.ToString(Newtonsoft.Json.Formatting.None));
- logger.data(commandItem.parameters).verbose("sending command: " + commandItem.commandName);
+ //
+ public override void SetEndpoint(string baseUrl, string appName, Dictionary headers = null) {
+ this.endpoint = baseUrl + "/" + appName;
+ this.headers = new Dictionary(headers);
}
- string batchUrl = endpoint + "/" + String.Join(",", commands.ToArray()) + "?queryId=" + commandBatch.queryId.ToString();
- string postData = string.Join("\n", data.ToArray());
+ //
+ public override void SerialiseBatch(CommandBatch commandBatch) {
+ List commands = new List();
+ List data = new List();
- // Cached the serialisation
- commandBatch.serialisedCache = (object)new CommandHttpClientCache(
- batchUrl,
- postData,
- new Dictionary(this.headers)
- );
- }
-
- //
- public override void SendBatch(CommandBatch commandBatch) {
- // Extract serialisation from cache
- CommandHttpClientCache serialisedCache = (CommandHttpClientCache)commandBatch.serialisedCache;
- string batchUrl = serialisedCache.batchUrl;
- string postData = serialisedCache.postData;
- Dictionary headers = serialisedCache.headers;
-
- // Send HTTP request
- SendRequest(batchUrl, postData, headers, (JArray responseArray) => {
- // Process each command response
- try {
- for (int batchId = 0; batchId < responseArray.Count; batchId += 1) {
- JArray commandResponse = responseArray[batchId] as JArray;
- CommandBatchItem commandItem = commandBatch.batchItems[batchId];
- string commandName = commandItem.commandName;
- Action commandCb = commandItem.cb;
-
- // Check if there are any events attached to this request
- if (commandResponse.Count >= 3) {
- logger.verbose("[" + commandName + "] processing events");
- mage.eventManager.emitEventList((JArray)commandResponse[2]);
- }
-
- // Check if the response was an error
- if (commandResponse[0].Type != JTokenType.Null) {
- logger.verbose("[" + commandName + "] server error");
- commandCb(new Exception(commandResponse[0].ToString()), null);
- return;
- }
-
- // Pull off call result object, if it doesn't exist
- logger.verbose("[" + commandName + "] call response");
- commandCb(null, commandResponse[1]);
- }
- } catch (Exception error) {
- logger.data(error).error("Error when processing command batch responses");
+ // Attach batch headers to post data
+ JArray batchHeaders = new JArray();
+ for (int batchHeaderI = 0; batchHeaderI < commandBatch.batchHeaders.Count; batchHeaderI += 1) {
+ Dictionary batchHeader = commandBatch.batchHeaders[batchHeaderI];
+ batchHeaders.Add(JObject.FromObject(batchHeader));
+ }
+ data.Add(batchHeaders.ToString(Newtonsoft.Json.Formatting.None));
+
+ // Attach command names to url and parameters to post data
+ for (int batchItemI = 0; batchItemI < commandBatch.batchItems.Count; batchItemI += 1) {
+ CommandBatchItem commandItem = commandBatch.batchItems[batchItemI];
+ commands.Add(commandItem.commandName);
+ data.Add(commandItem.parameters.ToString(Newtonsoft.Json.Formatting.None));
+ logger.data(commandItem.parameters).verbose("sending command: " + commandItem.commandName);
}
- });
- }
- private void SendRequest(string batchUrl, string postData, Dictionary headers, Action cb) {
- HTTPRequest.Post(batchUrl, "", postData, headers, mage.cookies, (Exception requestError, string responseString) => {
- logger.verbose("Recieved response: " + responseString);
+ string batchUrl = endpoint + "/" + String.Join(",", commands.ToArray()) + "?queryId=" + commandBatch.queryId.ToString();
+ string postData = string.Join("\n", data.ToArray());
- // Check if there was a transport error
- if (requestError != null) {
- string error = "network";
+ // Cached the serialisation
+ commandBatch.serialisedCache = (object)new CommandHttpClientCache(
+ batchUrl,
+ postData,
+ new Dictionary(this.headers)
+ );
+ }
- // On error
- var httpError = requestError as HTTPRequestException;
- if (httpError != null && httpError.Status == 503)
- {
- error = "maintenance";
+ //
+ public override void SendBatch(CommandBatch commandBatch) {
+ // Extract serialisation from cache
+ CommandHttpClientCache serialisedCache = (CommandHttpClientCache)commandBatch.serialisedCache;
+ string batchUrl = serialisedCache.batchUrl;
+ string postData = serialisedCache.postData;
+ Dictionary headers = serialisedCache.headers;
+
+ // Send HTTP request
+ SendRequest(batchUrl, postData, headers, (JArray responseArray) => {
+ // Process each command response
+ try {
+ for (int batchId = 0; batchId < responseArray.Count; batchId += 1) {
+ JArray commandResponse = responseArray[batchId] as JArray;
+ CommandBatchItem commandItem = commandBatch.batchItems[batchId];
+ string commandName = commandItem.commandName;
+ Action commandCb = commandItem.cb;
+
+ // Check if there are any events attached to this request
+ if (commandResponse.Count >= 3) {
+ logger.verbose("[" + commandName + "] processing events");
+ mage.eventManager.emitEventList((JArray)commandResponse[2]);
+ }
+
+ // Check if the response was an error
+ if (commandResponse[0].Type != JTokenType.Null) {
+ logger.verbose("[" + commandName + "] server error");
+ commandCb(new Exception(commandResponse[0].ToString()), null);
+ return;
+ }
+
+ // Pull off call result object, if it doesn't exist
+ logger.verbose("[" + commandName + "] call response");
+ commandCb(null, commandResponse[1]);
+ }
+ } catch (Exception error) {
+ logger.data(error).error("Error when processing command batch responses");
}
+ });
+ }
- OnTransportError.Invoke(error, requestError);
- return;
- }
+ private void SendRequest(string batchUrl, string postData, Dictionary headers, Action cb) {
+ HTTPRequest.Post(batchUrl, "", postData, headers, mage.cookies, (Exception requestError, string responseString) => {
+ logger.verbose("Recieved response: " + responseString);
- // Parse reponse array
- JArray responseArray;
- try {
- responseArray = JArray.Parse(responseString);
- } catch (Exception parseError) {
- OnTransportError.Invoke("parse", parseError);
- return;
- }
+ // Check if there was a transport error
+ if (requestError != null) {
+ string error = "network";
- // Let CommandCenter know this batch was successful
- OnSendComplete.Invoke();
+ // On error
+ var httpError = requestError as HTTPRequestException;
+ if (httpError != null && httpError.Status == 503) {
+ error = "maintenance";
+ }
- // Return array for processing
- cb(responseArray);
- });
+ OnTransportError.Invoke(error, requestError);
+ return;
+ }
+
+ // Parse reponse array
+ JArray responseArray;
+ try {
+ responseArray = JArray.Parse(responseString);
+ } catch (Exception parseError) {
+ OnTransportError.Invoke("parse", parseError);
+ return;
+ }
+
+ // Let CommandCenter know this batch was successful
+ OnSendComplete.Invoke();
+
+ // Return array for processing
+ cb(responseArray);
+ });
+ }
}
}
diff --git a/Mage/CommandCenter/TransportClient/CommandJSONRPCClient.cs b/Mage/CommandCenter/TransportClient/CommandJSONRPCClient.cs
index ed9cd3f..727d60b 100644
--- a/Mage/CommandCenter/TransportClient/CommandJSONRPCClient.cs
+++ b/Mage/CommandCenter/TransportClient/CommandJSONRPCClient.cs
@@ -1,83 +1,89 @@
using System;
using System.Collections.Generic;
-using Newtonsoft.Json.Linq;
+using Wizcorp.MageSDK.CommandCenter.Client;
+using Wizcorp.MageSDK.Log;
+using Wizcorp.MageSDK.MageClient;
+using Wizcorp.MageSDK.MageClient.Command;
+using Wizcorp.MageSDK.Network.JsonRpc;
-public class CommandJSONRPCClient : CommandTransportClient {
- private Mage mage { get { return Mage.Instance; } }
- private Logger logger { get { return mage.logger("CommandJSONRPCClient"); } }
-
- private JSONRPC rpcClient = new JSONRPC();
+namespace Wizcorp.MageSDK.Command.Client {
+ public class CommandJSONRPCClient : CommandTransportClient {
+ private Mage mage { get { return Mage.Instance; } }
+ private Logger logger { get { return mage.logger("CommandJSONRPCClient"); } }
- //
- public override void SetEndpoint(string baseUrl, string appName, Dictionary headers = null) {
- rpcClient.SetEndpoint(baseUrl + "/" + appName + "/jsonrpc", headers);
- }
-
- //
- public override void SerialiseBatch(CommandBatch commandBatch) {
- logger.verbose("THIS TRANSPORT CLIENT IS NOT IMPLEMENTED");
- throw new Exception("THIS TRANSPORT CLIENT IS NOT IMPLEMENTED");
- }
-
- //
- public override void SendBatch(CommandBatch commandBatch) {
- // NOTE: This transport client cannot be implemented yet as JSON RPC support is
- // terminally broken in MAGE (does not support queryId and response caching).
- // Until this is fixed, this transport client cannot be used or completed.
- logger.verbose("THIS TRANSPORT CLIENT IS NOT IMPLEMENTED");
- throw new Exception("THIS TRANSPORT CLIENT IS NOT IMPLEMENTED");
+ private JSONRPC rpcClient = new JSONRPC();
- /*
- JSONRPCBatch rpcBatch = new JSONRPCBatch();
- for (int batchId = 0; batchId < commandBatch.batchItems.Count; batchId += 1) {
- CommandBatchItem commandItem = commandBatch.batchItems[batchId];
- rpcBatch.Add(batchId, commandItem.commandName, commandItem.parameters);
- logger.data(commandItem.parameters).verbose("[" + commandItem.commandName + "] executing command");
+ //
+ public override void SetEndpoint(string baseUrl, string appName, Dictionary headers = null) {
+ rpcClient.SetEndpoint(baseUrl + "/" + appName + "/jsonrpc", headers);
}
- // Attach any required mage headers
- Dictionary headers = new Dictionary();
-
- string sessionKey = mage.session.GetSessionKey();
- if (!string.IsNullOrEmpty(sessionKey)) {
- headers.Add("X-MAGE-SESSION", sessionKey);
+ //
+ public override void SerialiseBatch(CommandBatch commandBatch) {
+ logger.verbose("THIS TRANSPORT CLIENT IS NOT IMPLEMENTED");
+ throw new Exception("THIS TRANSPORT CLIENT IS NOT IMPLEMENTED");
}
- // Send rpc batch
- rpcClient.CallBatch(rpcBatch, headers, mage.cookies, (Exception error, JArray responseArray) => {
- logger.data(responseArray).verbose("Recieved response: ");
+ //
+ public override void SendBatch(CommandBatch commandBatch) {
+ // NOTE: This transport client cannot be implemented yet as JSON RPC support is
+ // terminally broken in MAGE (does not support queryId and response caching).
+ // Until this is fixed, this transport client cannot be used or completed.
+ logger.verbose("THIS TRANSPORT CLIENT IS NOT IMPLEMENTED");
+ throw new Exception("THIS TRANSPORT CLIENT IS NOT IMPLEMENTED");
- if (error != null) {
- //TODO: OnTransportError.Invoke("", error);
- return;
+ /*
+ JSONRPCBatch rpcBatch = new JSONRPCBatch();
+ for (int batchId = 0; batchId < commandBatch.batchItems.Count; batchId += 1) {
+ CommandBatchItem commandItem = commandBatch.batchItems[batchId];
+ rpcBatch.Add(batchId, commandItem.commandName, commandItem.parameters);
+ logger.data(commandItem.parameters).verbose("[" + commandItem.commandName + "] executing command");
}
- // Process each command response
- foreach (JObject responseObject in responseArray) {
- int batchId = (int)responseObject["id"];
- CommandBatchItem commandItem = commandBatch.batchItems[batchId];
- string commandName = commandItem.commandName;
- Action commandCb = commandItem.cb;
+ // Attach any required mage headers
+ Dictionary headers = new Dictionary();
- // Check if there are any events attached to this request
- if (responseObject["result"]["myEvents"] != null) {
- logger.verbose("[" + commandName + "] processing events");
- mage.eventManager.emitEventList((JArray)responseObject["result"]["myEvents"]);
- }
+ string sessionKey = mage.session.GetSessionKey();
+ if (!string.IsNullOrEmpty(sessionKey)) {
+ headers.Add("X-MAGE-SESSION", sessionKey);
+ }
+
+ // Send rpc batch
+ rpcClient.CallBatch(rpcBatch, headers, mage.cookies, (Exception error, JArray responseArray) => {
+ logger.data(responseArray).verbose("Recieved response: ");
- // Check if the response was an error
- if (responseObject["result"]["errorCode"] != null) {
- logger.verbose("[" + commandName + "] server error");
- commandCb(new Exception(responseObject["result"]["errorCode"].ToString()), null);
+ if (error != null) {
+ //TODO: OnTransportError.Invoke("", error);
return;
}
- // Pull off call result object, if it doesn't exist
- logger.verbose("[" + commandName + "] call response");
- commandCb(null, responseObject["result"]["response"]);
- }
- });
- */
+ // Process each command response
+ foreach (JObject responseObject in responseArray) {
+ int batchId = (int)responseObject["id"];
+ CommandBatchItem commandItem = commandBatch.batchItems[batchId];
+ string commandName = commandItem.commandName;
+ Action commandCb = commandItem.cb;
+
+ // Check if there are any events attached to this request
+ if (responseObject["result"]["myEvents"] != null) {
+ logger.verbose("[" + commandName + "] processing events");
+ mage.eventManager.emitEventList((JArray)responseObject["result"]["myEvents"]);
+ }
+
+ // Check if the response was an error
+ if (responseObject["result"]["errorCode"] != null) {
+ logger.verbose("[" + commandName + "] server error");
+ commandCb(new Exception(responseObject["result"]["errorCode"].ToString()), null);
+ return;
+ }
+
+ // Pull off call result object, if it doesn't exist
+ logger.verbose("[" + commandName + "] call response");
+ commandCb(null, responseObject["result"]["response"]);
+ }
+ });
+ */
+ }
}
}
diff --git a/Mage/CommandCenter/TransportClient/CommandTransportClient.cs b/Mage/CommandCenter/TransportClient/CommandTransportClient.cs
index 959d7b4..8363c1e 100644
--- a/Mage/CommandCenter/TransportClient/CommandTransportClient.cs
+++ b/Mage/CommandCenter/TransportClient/CommandTransportClient.cs
@@ -1,16 +1,20 @@
using System;
using System.Collections.Generic;
-public enum CommandTransportType {
- HTTP,
- JSONRPC
-}
+using Wizcorp.MageSDK.MageClient.Command;
+
+namespace Wizcorp.MageSDK.CommandCenter.Client {
+ public enum CommandTransportType {
+ HTTP,
+ JSONRPC
+ }
-public abstract class CommandTransportClient {
- public Action OnSendComplete;
- public Action OnTransportError;
+ public abstract class CommandTransportClient {
+ public Action OnSendComplete;
+ public Action OnTransportError;
- public abstract void SetEndpoint(string baseUrl, string appName, Dictionary headers = null);
- public abstract void SerialiseBatch(CommandBatch commandBatch);
- public abstract void SendBatch(CommandBatch batch);
+ public abstract void SetEndpoint(string baseUrl, string appName, Dictionary headers = null);
+ public abstract void SerialiseBatch(CommandBatch commandBatch);
+ public abstract void SendBatch(CommandBatch batch);
+ }
}
diff --git a/Mage/EventManager.cs b/Mage/EventManager.cs
index b526d12..6c200e0 100644
--- a/Mage/EventManager.cs
+++ b/Mage/EventManager.cs
@@ -1,36 +1,41 @@
-using Newtonsoft.Json.Linq;
+using Newtonsoft.Json.Linq;
-public class EventManager : EventEmitter {
- private Mage mage { get { return Mage.Instance; } }
- private Logger logger { get { return mage.logger("eventManager"); } }
+using Wizcorp.MageSDK.Event;
+using Wizcorp.MageSDK.Log;
- public void emitEventList(JArray events) {
- foreach (JToken responseEvent in events) {
- string eventTag = null;
- JToken eventData = null;
+namespace Wizcorp.MageSDK.MageClient {
+ public class EventManager : EventEmitter {
+ private Mage mage { get { return Mage.Instance; } }
+ private Logger logger { get { return mage.logger("eventManager"); } }
- // Copy the eventItem for processing
- JArray eventItem = JArray.Parse(responseEvent.ToString());
+ public void emitEventList(JArray events) {
+ foreach (JToken responseEvent in events) {
+ string eventTag = null;
+ JToken eventData = null;
- // Check that event name is present
- if (eventItem.Count >= 1) {
- eventTag = eventItem[0].ToString();
- }
+ // Copy the eventItem for processing
+ JArray eventItem = JArray.Parse(responseEvent.ToString());
- // Check if any event data is present
- if (eventItem.Count == 2) {
- eventData = eventItem[1];
- }
+ // Check that event name is present
+ if (eventItem.Count >= 1) {
+ eventTag = eventItem[0].ToString();
+ }
- // Check if there were any errors, log and skip them
- if (eventTag == null || eventItem.Count > 2) {
- logger.data(eventItem).error("Invalid event format:");
- continue;
- }
+ // Check if any event data is present
+ if (eventItem.Count == 2) {
+ eventData = eventItem[1];
+ }
- // Emit the event
- logger.debug("Emitting '" + eventTag + "'");
- mage.eventManager.emit (eventTag, eventData);
+ // Check if there were any errors, log and skip them
+ if (eventTag == null || eventItem.Count > 2) {
+ logger.data(eventItem).error("Invalid event format:");
+ continue;
+ }
+
+ // Emit the event
+ logger.debug("Emitting '" + eventTag + "'");
+ mage.eventManager.emit(eventTag, eventData);
+ }
}
}
}
diff --git a/Mage/Logger/LogEntry.cs b/Mage/Logger/LogEntry.cs
index 85981a3..234b3fe 100644
--- a/Mage/Logger/LogEntry.cs
+++ b/Mage/Logger/LogEntry.cs
@@ -1,76 +1,77 @@
+namespace Wizcorp.MageSDK.Log {
+ public class LogEntry {
+ //
+ public string channel;
+ public string context;
+ public object data;
+ public string message;
-public class LogEntry {
- //
- public string channel;
- public string context;
- public object data;
- public string message;
+ public LogEntry(string context, object data = null) {
+ this.context = context;
+ this.data = data;
+ }
- public LogEntry(string context, object data = null) {
- this.context = context;
- this.data = data;
- }
+ //
+ private void emitLog() {
+ Logger.logEmitter.emit("log", this);
+ Logger.logEmitter.emit("log:" + this.channel, this);
+ }
- //
- private void emitLog() {
- Logger.logEmitter.emit("log", this);
- Logger.logEmitter.emit("log:" + this.channel, this);
- }
-
-
- //
- public void verbose (string message) {
- this.channel = "verbose";
- this.message = message;
- emitLog();
- }
-
- public void debug (string message) {
- this.channel = "debug";
- this.message = message;
- emitLog();
- }
-
- public void info (string message) {
- this.channel = "info";
- this.message = message;
- emitLog();
- }
-
- public void notice (string message) {
- this.channel = "notice";
- this.message = message;
- emitLog();
- }
-
- public void warning (string message) {
- this.channel = "warning";
- this.message = message;
- emitLog();
- }
-
- public void error (string message) {
- this.channel = "error";
- this.message = message;
- emitLog();
- }
-
- public void critical (string message) {
- this.channel = "critical";
- this.message = message;
- emitLog();
- }
-
- public void alert (string message) {
- this.channel = "alert";
- this.message = message;
- emitLog();
- }
-
- public void emergency (string message) {
- this.channel = "emergency";
- this.message = message;
- emitLog();
+
+ //
+ public void verbose(string message) {
+ this.channel = "verbose";
+ this.message = message;
+ emitLog();
+ }
+
+ public void debug(string message) {
+ this.channel = "debug";
+ this.message = message;
+ emitLog();
+ }
+
+ public void info(string message) {
+ this.channel = "info";
+ this.message = message;
+ emitLog();
+ }
+
+ public void notice(string message) {
+ this.channel = "notice";
+ this.message = message;
+ emitLog();
+ }
+
+ public void warning(string message) {
+ this.channel = "warning";
+ this.message = message;
+ emitLog();
+ }
+
+ public void error(string message) {
+ this.channel = "error";
+ this.message = message;
+ emitLog();
+ }
+
+ public void critical(string message) {
+ this.channel = "critical";
+ this.message = message;
+ emitLog();
+ }
+
+ public void alert(string message) {
+ this.channel = "alert";
+ this.message = message;
+ emitLog();
+ }
+
+ public void emergency(string message) {
+ this.channel = "emergency";
+ this.message = message;
+ emitLog();
+ }
}
}
diff --git a/Mage/Logger/Logger.cs b/Mage/Logger/Logger.cs
index db54597..760b30c 100644
--- a/Mage/Logger/Logger.cs
+++ b/Mage/Logger/Logger.cs
@@ -1,95 +1,99 @@
-using System;
+using System;
using System.Collections.Generic;
+using Wizcorp.MageSDK.Event;
+using Wizcorp.MageSDK.Log.Writers;
-public class Logger {
- //
- public static EventEmitter logEmitter = new EventEmitter();
+namespace Wizcorp.MageSDK.Log {
+ public class Logger {
+ //
+ public static EventEmitter logEmitter = new EventEmitter();
- //
- public static Dictionary logWriters;
- public static void SetConfig(Dictionary> config) {
- // Destroy existing log writers
- if (logWriters != null) {
- foreach (var writer in logWriters.Values) {
- writer.Dispose();
+ //
+ public static Dictionary logWriters;
+ public static void SetConfig(Dictionary> config) {
+ // Destroy existing log writers
+ if (logWriters != null) {
+ foreach (var writer in logWriters.Values) {
+ writer.Dispose();
+ }
+ logWriters = null;
+ }
+
+ // Make sure we have configured something
+ if (config == null) {
+ return;
+ }
+
+ // Create each writer with log levels
+ logWriters = new Dictionary();
+ foreach (var property in config) {
+ string writer = property.Key;
+ List writerConfig = property.Value;
+
+ switch (writer) {
+ case "console":
+ logWriters.Add(writer, new ConsoleWriter(writerConfig) as LogWriter);
+ break;
+ case "server":
+ logWriters.Add(writer, new ServerWriter(writerConfig) as LogWriter);
+ break;
+ default:
+ throw new Exception("Unknown Log Writer: " + writer);
+ }
}
- logWriters = null;
}
- // Make sure we have configured something
- if (config == null) {
- return;
+
+ //
+ private string _context;
+
+ public Logger(string context) {
+ _context = context;
}
- // Create each writer with log levels
- logWriters = new Dictionary();
- foreach (var property in config) {
- string writer = property.Key;
- List writerConfig = property.Value;
-
- switch (writer) {
- case "console":
- logWriters.Add(writer, new ConsoleWriter(writerConfig) as LogWriter);
- break;
- case "server":
- logWriters.Add(writer, new ServerWriter(writerConfig) as LogWriter);
- break;
- default:
- throw new Exception("Unknown Log Writer: " + writer);
- }
+
+ //
+ public LogEntry data(object data) {
+ return new LogEntry(_context, data);
}
- }
- //
- private string _context;
+ //
+ public void verbose(string message) {
+ (new LogEntry(_context)).verbose(message);
+ }
- public Logger(string context) {
- _context = context;
- }
-
-
- //
- public LogEntry data (object data) {
- return new LogEntry (_context, data);
- }
+ public void debug(string message) {
+ (new LogEntry(_context)).debug(message);
+ }
+ public void info(string message) {
+ (new LogEntry(_context)).info(message);
+ }
- //
- public void verbose (string message) {
- (new LogEntry (_context)).verbose (message);
- }
-
- public void debug (string message) {
- (new LogEntry (_context)).debug (message);
- }
+ public void notice(string message) {
+ (new LogEntry(_context)).notice(message);
+ }
- public void info (string message) {
- (new LogEntry (_context)).info (message);
- }
-
- public void notice (string message) {
- (new LogEntry (_context)).notice (message);
- }
-
- public void warning (string message) {
- (new LogEntry (_context)).warning (message);
- }
-
- public void error (string message) {
- (new LogEntry (_context)).error (message);
- }
-
- public void critical (string message) {
- (new LogEntry (_context)).critical (message);
- }
-
- public void alert (string message) {
- (new LogEntry (_context)).alert (message);
- }
-
- public void emergency (string message) {
- (new LogEntry (_context)).emergency (message);
+ public void warning(string message) {
+ (new LogEntry(_context)).warning(message);
+ }
+
+ public void error(string message) {
+ (new LogEntry(_context)).error(message);
+ }
+
+ public void critical(string message) {
+ (new LogEntry(_context)).critical(message);
+ }
+
+ public void alert(string message) {
+ (new LogEntry(_context)).alert(message);
+ }
+
+ public void emergency(string message) {
+ (new LogEntry(_context)).emergency(message);
+ }
}
}
diff --git a/Mage/Logger/Writers/ConsoleWriter.cs b/Mage/Logger/Writers/ConsoleWriter.cs
index 7407e08..be96b84 100644
--- a/Mage/Logger/Writers/ConsoleWriter.cs
+++ b/Mage/Logger/Writers/ConsoleWriter.cs
@@ -1,157 +1,158 @@
-using System;
+using System;
using System.Collections.Generic;
+namespace Wizcorp.MageSDK.Log.Writers {
+ public class ConsoleWriter : LogWriter {
+ private List config;
-public class ConsoleWriter : LogWriter {
- private List config;
+ public ConsoleWriter(List logLevels) {
+ config = logLevels;
- public ConsoleWriter(List logLevels) {
- config = logLevels;
+ if (config.Contains("verbose")) {
+ Logger.logEmitter.on("log:verbose", Verbose);
+ }
- if (config.Contains("verbose")) {
- Logger.logEmitter.on("log:verbose", Verbose);
- }
+ if (config.Contains("debug")) {
+ Logger.logEmitter.on("log:debug", Debug);
+ }
- if (config.Contains("debug")) {
- Logger.logEmitter.on("log:debug", Debug);
- }
+ if (config.Contains("info")) {
+ Logger.logEmitter.on("log:info", Info);
+ }
- if (config.Contains("info")) {
- Logger.logEmitter.on("log:info", Info);
- }
+ if (config.Contains("notice")) {
+ Logger.logEmitter.on("log:notice", Notice);
+ }
- if (config.Contains("notice")) {
- Logger.logEmitter.on("log:notice", Notice);
- }
+ if (config.Contains("warning")) {
+ Logger.logEmitter.on("log:warning", Warning);
+ }
- if (config.Contains("warning")) {
- Logger.logEmitter.on("log:warning", Warning);
- }
+ if (config.Contains("error")) {
+ Logger.logEmitter.on("log:error", Error);
+ }
- if (config.Contains("error")) {
- Logger.logEmitter.on("log:error", Error);
- }
+ if (config.Contains("critical")) {
+ Logger.logEmitter.on("log:critical", Critical);
+ }
- if (config.Contains("critical")) {
- Logger.logEmitter.on("log:critical", Critical);
- }
+ if (config.Contains("alert")) {
+ Logger.logEmitter.on("log:alert", Alert);
+ }
- if (config.Contains("alert")) {
- Logger.logEmitter.on("log:alert", Alert);
- }
-
- if (config.Contains("emergency")) {
- Logger.logEmitter.on("log:emergency", Emergency);
+ if (config.Contains("emergency")) {
+ Logger.logEmitter.on("log:emergency", Emergency);
+ }
}
- }
- public override void Dispose() {
- if (config.Contains("verbose")) {
- Logger.logEmitter.off("log:verbose", Verbose);
- }
-
- if (config.Contains("debug")) {
- Logger.logEmitter.off("log:debug", Debug);
- }
-
- if (config.Contains("info")) {
- Logger.logEmitter.off("log:info", Info);
- }
-
- if (config.Contains("notice")) {
- Logger.logEmitter.off("log:notice", Notice);
- }
-
- if (config.Contains("warning")) {
- Logger.logEmitter.off("log:warning", Warning);
- }
-
- if (config.Contains("error")) {
- Logger.logEmitter.off("log:error", Error);
- }
-
- if (config.Contains("critical")) {
- Logger.logEmitter.off("log:critical", Critical);
+ public override void Dispose() {
+ if (config.Contains("verbose")) {
+ Logger.logEmitter.off("log:verbose", Verbose);
+ }
+
+ if (config.Contains("debug")) {
+ Logger.logEmitter.off("log:debug", Debug);
+ }
+
+ if (config.Contains("info")) {
+ Logger.logEmitter.off("log:info", Info);
+ }
+
+ if (config.Contains("notice")) {
+ Logger.logEmitter.off("log:notice", Notice);
+ }
+
+ if (config.Contains("warning")) {
+ Logger.logEmitter.off("log:warning", Warning);
+ }
+
+ if (config.Contains("error")) {
+ Logger.logEmitter.off("log:error", Error);
+ }
+
+ if (config.Contains("critical")) {
+ Logger.logEmitter.off("log:critical", Critical);
+ }
+
+ if (config.Contains("alert")) {
+ Logger.logEmitter.off("log:alert", Alert);
+ }
+
+ if (config.Contains("emergency")) {
+ Logger.logEmitter.off("log:emergency", Emergency);
+ }
}
-
- if (config.Contains("alert")) {
- Logger.logEmitter.off("log:alert", Alert);
+
+ private string makeLogString(string channel, string context, string message) {
+ return String.Format("[{0}] [{1}] {2}", channel, context, message);
}
-
- if (config.Contains("emergency")) {
- Logger.logEmitter.off("log:emergency", Emergency);
+
+ private void Verbose(object sender, LogEntry logEntry) {
+ UnityEngine.Debug.Log(makeLogString("verbose", logEntry.context, logEntry.message));
+ if (logEntry.data != null) {
+ UnityEngine.Debug.Log(logEntry.data);
+ }
}
- }
- private string makeLogString(string channel, string context, string message) {
- return String.Format("[{0}] [{1}] {2}", channel, context, message);
- }
-
- private void Verbose(object sender, LogEntry logEntry) {
- UnityEngine.Debug.Log(makeLogString("verbose", logEntry.context, logEntry.message));
- if (logEntry.data != null) {
- UnityEngine.Debug.Log(logEntry.data);
+ private void Debug(object sender, LogEntry logEntry) {
+ UnityEngine.Debug.Log(makeLogString("debug", logEntry.context, logEntry.message));
+ if (logEntry.data != null) {
+ UnityEngine.Debug.Log(logEntry.data);
+ }
}
- }
-
- private void Debug(object sender, LogEntry logEntry) {
- UnityEngine.Debug.Log(makeLogString("debug", logEntry.context, logEntry.message));
- if (logEntry.data != null) {
- UnityEngine.Debug.Log(logEntry.data);
+
+ private void Info(object sender, LogEntry logEntry) {
+ UnityEngine.Debug.Log(makeLogString("info", logEntry.context, logEntry.message));
+ if (logEntry.data != null) {
+ UnityEngine.Debug.Log(logEntry.data);
+ }
}
- }
-
- private void Info(object sender, LogEntry logEntry) {
- UnityEngine.Debug.Log(makeLogString("info", logEntry.context, logEntry.message));
- if (logEntry.data != null) {
- UnityEngine.Debug.Log(logEntry.data);
+
+ private void Notice(object sender, LogEntry logEntry) {
+ UnityEngine.Debug.Log(makeLogString("notice", logEntry.context, logEntry.message));
+ if (logEntry.data != null) {
+ UnityEngine.Debug.Log(logEntry.data);
+ }
}
- }
-
- private void Notice(object sender, LogEntry logEntry) {
- UnityEngine.Debug.Log(makeLogString("notice", logEntry.context, logEntry.message));
- if (logEntry.data != null) {
- UnityEngine.Debug.Log(logEntry.data);
+
+ private void Warning(object sender, LogEntry logEntry) {
+ UnityEngine.Debug.LogWarning(makeLogString("warning", logEntry.context, logEntry.message));
+ if (logEntry.data != null) {
+ UnityEngine.Debug.LogWarning(logEntry.data);
+ }
}
- }
-
- private void Warning(object sender, LogEntry logEntry) {
- UnityEngine.Debug.LogWarning(makeLogString("warning", logEntry.context, logEntry.message));
- if (logEntry.data != null) {
- UnityEngine.Debug.LogWarning (logEntry.data);
+
+ private void Error(object sender, LogEntry logEntry) {
+ UnityEngine.Debug.LogError(makeLogString("error", logEntry.context, logEntry.message));
+ if (logEntry.data != null) {
+ UnityEngine.Debug.LogError(logEntry.data);
+ }
}
- }
-
- private void Error(object sender, LogEntry logEntry) {
- UnityEngine.Debug.LogError(makeLogString("error", logEntry.context, logEntry.message));
- if (logEntry.data != null) {
- UnityEngine.Debug.LogError(logEntry.data);
+
+ private void Critical(object sender, LogEntry logEntry) {
+ UnityEngine.Debug.LogError(makeLogString("critical", logEntry.context, logEntry.message));
+ if (logEntry.data != null) {
+ if (logEntry.data is Exception && (logEntry.data as Exception).StackTrace != null) {
+ Exception excpt = logEntry.data as Exception;
+ UnityEngine.Debug.LogError(excpt.ToString() + ":\n" + excpt.StackTrace.ToString());
+ } else {
+ UnityEngine.Debug.LogError(logEntry.data);
+ }
+ }
}
- }
-
- private void Critical(object sender, LogEntry logEntry) {
- UnityEngine.Debug.LogError(makeLogString("critical", logEntry.context, logEntry.message));
- if (logEntry.data != null) {
- if (logEntry.data is Exception && (logEntry.data as Exception).StackTrace != null) {
- Exception excpt = logEntry.data as Exception;
- UnityEngine.Debug.LogError(excpt.ToString() + ":\n" + excpt.StackTrace.ToString());
- } else {
+
+ private void Alert(object sender, LogEntry logEntry) {
+ UnityEngine.Debug.LogError(makeLogString("alert", logEntry.context, logEntry.message));
+ if (logEntry.data != null) {
UnityEngine.Debug.LogError(logEntry.data);
}
}
- }
-
- private void Alert(object sender, LogEntry logEntry) {
- UnityEngine.Debug.LogError(makeLogString("alert", logEntry.context, logEntry.message));
- if (logEntry.data != null) {
- UnityEngine.Debug.LogError(logEntry.data);
- }
- }
-
- private void Emergency(object sender, LogEntry logEntry) {
- UnityEngine.Debug.LogError(makeLogString("emergency", logEntry.context, logEntry.message));
- if (logEntry.data != null) {
- UnityEngine.Debug.LogError(logEntry.data);
+
+ private void Emergency(object sender, LogEntry logEntry) {
+ UnityEngine.Debug.LogError(makeLogString("emergency", logEntry.context, logEntry.message));
+ if (logEntry.data != null) {
+ UnityEngine.Debug.LogError(logEntry.data);
+ }
}
}
}
diff --git a/Mage/Logger/Writers/LogWriter.cs b/Mage/Logger/Writers/LogWriter.cs
index 41c71df..42f3f17 100644
--- a/Mage/Logger/Writers/LogWriter.cs
+++ b/Mage/Logger/Writers/LogWriter.cs
@@ -1,3 +1,5 @@
-public abstract class LogWriter {
- public abstract void Dispose();
-}
\ No newline at end of file
+namespace Wizcorp.MageSDK.Log.Writers {
+ public abstract class LogWriter {
+ public abstract void Dispose();
+ }
+}
diff --git a/Mage/Logger/Writers/ServerWriter.cs b/Mage/Logger/Writers/ServerWriter.cs
index 6171102..7c7676f 100644
--- a/Mage/Logger/Writers/ServerWriter.cs
+++ b/Mage/Logger/Writers/ServerWriter.cs
@@ -3,44 +3,46 @@
using Newtonsoft.Json.Linq;
+using Wizcorp.MageSDK.MageClient;
-public class ServerWriter : LogWriter {
- protected Mage mage { get { return Mage.Instance; } }
+namespace Wizcorp.MageSDK.Log.Writers {
+ public class ServerWriter : LogWriter {
+ protected Mage mage { get { return Mage.Instance; } }
- private List config;
+ private List config;
- public ServerWriter(List