Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

splitting up classes

  • Loading branch information...
commit e3324e3041191940f70f4d6876b7f3037ff6dc98 1 parent dae08b8
@thriqon thriqon authored
View
250 API/APIConnection.cs
@@ -7,191 +7,73 @@
using System.Text;
using System.Threading;
using System.Windows.Forms;
+using AniSharp.API.Model;
namespace AniSharp.API
{
- class APIConnection
- {
- private Thread senderThread;
- private Thread receiverThread;
- private UdpClient udpClient;
- private BlockingCollection<String> outQueue = new BlockingCollection<String>();
- private String session = null;
- private Dictionary<String, String> results = new Dictionary<string, string>();
-
- public const int WAITING_BETWEEN_PACKETS = 2000;
-
- private void senderThreadInit()
- {
- try
- {
- while (true)
- {
- String toSend = outQueue.Take();
- byte[] bytes = Encoding.ASCII.GetBytes(toSend + (session == null ? "" : "&s=" + session));
- getConnection().Send(bytes, bytes.Length);
-
- Thread.Sleep(WAITING_BETWEEN_PACKETS);
- }
- }
- catch (Exception)
- {
- }
- }
-
- public void establishConnection(String username, String password)
- {
- try
- {
- udpClient = new UdpClient(AniSharp.Properties.Settings.Default.LocalPort);
- udpClient.Connect(AniSharp.Properties.Settings.Default.Address, AniSharp.Properties.Settings.Default.RemotePort);
-
- senderThread = new Thread(senderThreadInit);
- senderThread.Start();
- receiverThread = new Thread(receiverThreadStart);
- receiverThread.Start();
-
- ApiRequest loginQ = new ApiRequest("AUTH");
- loginQ.set("user", username)
- .set("pass", password)
- .set("protover", AniSharp.Properties.Settings.Default.ApiVersion.ToString())
- .set("client", AniSharp.Properties.Settings.Default.ClientName)
- .set("clientver", AniSharp.Properties.Settings.Default.Version.ToString());
-
- /* String loginQ = "AUTH user=" + username + "&pass=" + password + "&protover=" +
- AniSharp.Properties.Settings.Default.ApiVersion + "&client=" +
- AniSharp.Properties.Settings.Default.ClientName + "&clientver=" +
- AniSharp.Properties.Settings.Default.Version;*/
- String loginA = query(loginQ);
-
- MessageBox.Show(loginA);
- this.session = loginA.Split(' ')[2];
-
-
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- }
-
- private UdpClient getConnection()
- {
- return this.udpClient;
- }
-
- public void closeUDPClient()
- {
- ApiRequest logoutQ = new ApiRequest("LOGOUT");
- //String logoutQ = "LOGOUT ";
- String logoutA = query(logoutQ);
- MessageBox.Show(logoutA);
- senderThread.Interrupt();
- receiverThread.Interrupt();
- udpClient.Close();
- }
-
-
- /// <summary>
- /// checks, if this MethodCall will result in an unauthorized error
- /// </summary>
- /// <param name="command">The command-name to execute</param>
- private void checkMethodSessionKey(String command)
- {
- //PING, ENCRYPT, ENCODING, AUTH and VERSION are allowed
- if (session != null) return; // session set -- ok
-
- switch (command)
- {
- case "PING":
- case "ENCRYPT":
- case "ENCODING":
- case "AUTH":
- case "VERSION":
- return;
- default:
- throw new System.InvalidOperationException("Session not set");
- }
- }
-
- /// <summary>
- /// sends a command to AniDB. throws InvalidOperationException if session key is not
- /// set, but mandatory
- /// </summary>
- /// <param name="req">The method-request</param>
- /// <returns>The result</returns>
- public String query(ApiRequest req)
- {
- checkMethodSessionKey(req.Command);
-
- // set a tag
- String tag = generateTag();
- req["tag"] = tag;
-
- // transmit session-key
- if (this.session != null)
- req["s"] = this.session;
-
- outQueue.Add(req.ToString());
- while (!results.ContainsKey(tag))
- lock (results)
- {
- Monitor.Wait(results);
- }
- return results[tag];
- }
-
- public String query(String queryS)
- {
- String tag = generateTag();
- queryS += "&tag=" + tag;
- outQueue.Add(queryS);
- while (!results.ContainsKey(tag))
- lock (results)
- {
- Monitor.Wait(results);
- }
- return results[tag];
- }
-
- //TODO: uniquify TAGS!
-
- private static Random RND = new Random();
- private static String TAG_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- private static int TAG_LEN = 5;
- private static String generateTag()
- {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < TAG_LEN; i++)
- {
- sb.Append(TAG_CHARS.ElementAt(RND.Next(TAG_CHARS.Length)));
- }
- return sb.ToString();
- }
-
- public void receiverThreadStart()
- {
- try
- {
- while (true)
- {
- IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
-
- Byte[] receiveBytes = getConnection().Receive(ref RemoteIpEndPoint);
- String returnData = Encoding.ASCII.GetString(receiveBytes);
-
- String tag = returnData.Split(' ')[0];
-
- results.Add(tag, returnData);
- lock (results)
- {
- Monitor.PulseAll(results);
- };
- }
- }
- catch (Exception)
- {
- }
- }
- }
+ class APIConnection
+ {
+ private String session = null;
+
+ private ApiDriver driver;
+
+ public void establishConnection(String username, String password)
+ {
+ try
+ {
+ driver = new ApiDriver();
+ driver.connect();
+
+ String loginA = driver.query(new AuthReq(username, password));
+
+ MessageBox.Show(loginA);
+ this.session = loginA.Split(' ')[2];
+ }
+ catch (Exception e)
+ {
+ Console.WriteLine(e.ToString());
+ }
+ }
+
+ public void closeUDPClient()
+ {
+ ApiRequest logoutQ = new ApiRequest("LOGOUT");
+ String logoutA = driver.query(logoutQ);
+ MessageBox.Show(logoutA);
+
+ driver.disconnect();
+ }
+
+ public ApiAnswer query(ApiRequest req)
+ {
+ // transmit session-key
+ if (this.session != null)
+ req["s"] = this.session;
+
+ return null;
+ }
+
+
+ /// <summary>
+ /// checks, if this MethodCall will result in an unauthorized error
+ /// </summary>
+ /// <param name="command">The command-name to execute</param>
+ private void checkMethodSessionKey(String command)
+ {
+ //PING, ENCRYPT, ENCODING, AUTH and VERSION are allowed
+ if (session != null) return; // session set -- ok
+
+ switch (command)
+ {
+ case "PING":
+ case "ENCRYPT":
+ case "ENCODING":
+ case "AUTH":
+ case "VERSION":
+ return;
+ default:
+ throw new System.InvalidOperationException("Session not set");
+ }
+ }
+ }
}
View
127 API/ApiDriver.cs
@@ -0,0 +1,127 @@
+
+using System.Net;
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+
+namespace AniSharp.API
+{
+ class ApiDriver
+ {
+ private BlockingCollection<String> outQueue = new BlockingCollection<String>();
+ private Dictionary<String, String> results = new Dictionary<string, string>();
+ private Thread senderThread;
+ private Thread receiverThread;
+ private UdpClient udpClient;
+
+ public const int WAITING_BETWEEN_PACKETS = 2000;
+
+ public ApiDriver()
+ {
+ }
+
+ public void connect()
+ {
+ udpClient = new UdpClient(AniSharp.Properties.Settings.Default.LocalPort);
+ udpClient.Connect(AniSharp.Properties.Settings.Default.Address, AniSharp.Properties.Settings.Default.RemotePort);
+
+ senderThread = new Thread(senderThreadInit);
+ senderThread.Start();
+ receiverThread = new Thread(receiverThreadStart);
+ receiverThread.Start();
+ }
+
+ public void disconnect()
+ {
+ senderThread.Interrupt();
+ receiverThread.Interrupt();
+ udpClient.Close();
+ }
+
+
+ private void senderThreadInit()
+ {
+ try
+ {
+ while (true)
+ {
+ String toSend = outQueue.Take();
+ byte[] bytes = Encoding.ASCII.GetBytes(toSend + (session == null ? "" : "&s=" + session));
+ udpClient.Send(bytes, bytes.Length);
+
+ Thread.Sleep(WAITING_BETWEEN_PACKETS);
+ }
+ }
+ catch (Exception)
+ {
+ }
+ }
+ /// <summary>
+ /// sends a command to AniDB. throws InvalidOperationException if session key is not
+ /// set, but mandatory
+ /// </summary>
+ /// <param name="req">The method-request</param>
+ /// <returns>The result</returns>
+ public String query(ApiRequest req)
+ {
+ checkMethodSessionKey(req.Command);
+
+ // set a tag
+ String tag = generateTag();
+ req["tag"] = tag;
+
+ outQueue.Add(req.ToString());
+ while (!results.ContainsKey(tag))
+ lock (results)
+ {
+ Monitor.Wait(results);
+ }
+ String result = results[tag];
+ // drop answer from dictionary?
+ return result;
+ }
+
+ //TODO: uniquify TAGS!
+
+ private static Random RND = new Random();
+ private static String TAG_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+ private static int TAG_LEN = 5;
+ private static String generateTag()
+ {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < TAG_LEN; i++)
+ {
+ sb.Append(TAG_CHARS.ElementAt(RND.Next(TAG_CHARS.Length)));
+ }
+ return sb.ToString();
+ }
+
+ public void receiverThreadStart()
+ {
+ try
+ {
+ while (true)
+ {
+ IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
+
+ Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
+ String returnData = Encoding.ASCII.GetString(receiveBytes);
+
+ String tag = returnData.Split(' ')[0];
+
+ results.Add(tag, returnData);
+ lock (results)
+ {
+ Monitor.PulseAll(results);
+ };
+ }
+ }
+ catch (Exception)
+ {
+ }
+ }
+
+ }
+
+}
+
View
26 API/Model/Auth.cs
@@ -0,0 +1,26 @@
+
+using AniSharp.API;
+
+namespace AniSharp.API.Model {
+
+ /// <summary>
+ /// Encapsulates a login request
+ /// </summary>
+ class AuthReq : ApiReq
+ {
+ /// <summary>
+ /// logs a user in by username and password
+ /// </summary>
+ /// <param name="user">The username at AniDB</param>
+ /// <param name="pass">The password</param>
+ public Auth(String user, String pass) : ApiReq("AUTH")
+ {
+ set("user", user);
+ set("pass", pass);
+ set("protover", AniSharp.Properties.Settings.Default.ApiVersion.ToString());
+ set("client", AniSharp.Properties.Settings.Default.ClientName);
+ set("clientver", AniSharp.Properties.Settings.Default.Version.ToString());
+ }
+ }
+}
+
View
157 API/ReturnCode.cs
@@ -0,0 +1,157 @@
+using System;
+
+
+namespace API
+{
+ enum ReturnCode
+ {
+ LOGIN_ACCEPTED = 200,
+ LOGIN_ACCEPTED_NEW_VERSION = 201,
+ LOGGED_OUT = 203,
+ RESOURCE = 205,
+ STATS = 206,
+ TOP = 207,
+ UPTIME = 208,
+ ENCRYPTION_ENABLED = 209,
+ MYLIST_ENTRY_ADDED = 210,
+ MYLIST_ENTRY_DELETED = 211,
+ ADDED_FILE = 214,
+ ADDED_STREAM = 215,
+ EXPORT_QUEUED = 217,
+ EXPORT_CANCELLED = 218,
+ ENCODING_CHANGED = 219,
+ FILE = 220,
+ MYLIST = 221,
+ MYLIST_STATS = 222,
+ WISHLIST = 223,
+ NOTIFICATION = 224,
+ GROUP_STATUS = 225,
+ WISHLIST_ENTRY_ADDED = 226,
+ WISHLIST_ENTRY_DELETED = 227,
+ WISHLIST_ENTRY_UPDATED = 228,
+ MULTIPLE_WISHLIST = 229,
+ ANIME = 230,
+ ANIME_BEST_MATCH = 231,
+ RANDOM_ANIME = 232,
+ ANIME_DESCRIPTION = 233,
+ REVIEW = 234,
+ CHARACTER = 235,
+ SONG = 236,
+ ANIMETAG = 237,
+ CHARACTERTAG = 238,
+ EPISODE = 240,
+ UPDATED = 243,
+ TITLE = 244,
+ CREATOR = 245,
+ NOTIFICATION_ENTRY_ADDED = 246,
+ NOTIFICATION_ENTRY_DELETED = 247,
+ NOTIFICATION_ENTRY_UPDATE = 248,
+ MULTIPLE_NOTIFICATION = 249,
+ GROUP = 250,
+ CATEGORY = 251,
+ BUDDY_LIST = 253,
+ BUDDY_STATE = 254,
+ BUDDY_ADDED = 255,
+ BUDDY_DELETED = 256,
+ BUDDY_ACCEPTED = 257,
+ BUDDY_DENIED = 258,
+ VOTED = 260,
+ VOTE_FOUND = 261,
+ VOTE_UPDATED = 262,
+ VOTE_REVOKED = 263,
+ HOT_ANIME = 265,
+ RANDOM_RECOMMENDATION = 266,
+ RANDOM_SIMILAR = 267,
+ NOTIFICATION_ENABLED = 270,
+ NOTIFYACK_SUCCESSFUL_MESSAGE = 281,
+ NOTIFYACK_SUCCESSFUL_NOTIFIATION = 282,
+ NOTIFICATION_STATE = 290,
+ NOTIFYLIST = 291,
+ NOTIFYGET_MESSAGE = 292,
+ NOTIFYGET_NOTIFY = 293,
+ SENDMESSAGE_SUCCESSFUL = 294,
+ USER_ID = 295,
+ CALENDAR = 297,
+
+ PONG = 300,
+ AUTHPONG = 301,
+ NO_SUCH_RESOURCE = 305,
+ API_PASSWORD_NOT_DEFINED = 309,
+ FILE_ALREADY_IN_MYLIST = 310,
+ MYLIST_ENTRY_EDITED = 311,
+ MULTIPLE_MYLIST_ENTRIES = 312,
+ WATCHED = 313,
+ SIZE_HASH_EXISTS = 314,
+ INVALID_DATA = 315,
+ STREAMNOID_USED = 316,
+ EXPORT_NO_SUCH_TEMPLATE = 317,
+ EXPORT_ALREADY_IN_QUEUE = 318,
+ EXPORT_NO_EXPORT_QUEUED_OR_IS_PROCESSING = 319,
+ NO_SUCH_FILE = 320,
+ NO_SUCH_ENTRY = 321,
+ MULTIPLE_FILES_FOUND = 322,
+ NO_SUCH_WISHLIST = 323,
+ NO_SUCH_NOTIFICATION = 324,
+ NO_GROUPS_FOUND = 325,
+ NO_SUCH_ANIME = 330,
+ NO_SUCH_DESCRIPTION = 333,
+ NO_SUCH_REVIEW = 334,
+ NO_SUCH_CHARACTER = 335,
+ NO_SUCH_SONG = 336,
+ NO_SUCH_ANIMETAG = 337,
+ NO_SUCH_CHARACTERTAG = 338,
+ NO_SUCH_EPISODE = 340,
+ NO_SUCH_UPDATES = 343,
+ NO_SUCH_TITLES = 344,
+ NO_SUCH_CREATOR = 345,
+ NO_SUCH_GROUP = 350,
+ NO_SUCH_CATEGORY = 351,
+ BUDDY_ALREADY_ADDED = 355,
+ NO_SUCH_BUDDY = 356,
+ BUDDY_ALREADY_ACCEPTED = 357,
+ BUDDY_ALREADY_DENIED = 358,
+ NO_SUCH_VOTE = 360,
+ INVALID_VOTE_TYPE = 361,
+ INVALID_VOTE_VALUE = 362,
+ PERMVOTE_NOT_ALLOWED = 363,
+ ALREADY_PERMVOTED = 364,
+ HOT_ANIME_EMPTY = 365,
+ RANDOM_RECOMMENDATION_EMPTY = 366,
+ RANDOM_SIMILAR_EMPTY = 367,
+ NOTIFICATION_DISABLED = 370,
+ NO_SUCH_ENTRY_MESSAGE = 381,
+ NO_SUCH_ENTRY_NOTIFICATION = 382,
+ NO_SUCH_MESSAGE = 392,
+ NO_SUCH_NOTIFY = 393,
+ NO_SUCH_USER = 394,
+ CALENDAR_EMPTY = 397,
+ NO_CHANGES = 399,
+
+ NOT_LOGGED_IN = 403,
+ NO_SUCH_MYLIST_FILE = 410,
+ NO_SUCH_MYLIST_ENTRY = 411,
+ MYLIST_UNAVAILABLE = 412,
+
+ LOGIN_FAILED = 500,
+ LOGIN_FIRST = 501,
+ ACCESS_DENIED = 502,
+ CLIENT_VERSION_OUTDATED = 503,
+ CLIENT_BANNED = 504,
+ ILLEGAL_INPUT_OR_ACCESS_DENIED = 505,
+ INVALID_SESSION = 506,
+ NO_SUCH_ENCRYPTION_TYPE = 509,
+ ENCODING_NOT_SUPPORTED = 519,
+ BANNED = 555,
+ UNKNOWN_COMMAND = 598,
+
+ INTERNAL_SERVER_ERROR = 600,
+ ANIDB_OUT_OF_SERVICE = 601,
+ SERVER_BUSY = 602,
+ NO_DATA = 603,
+ TIMEOUT - DELAY AND RESUBMIT = 604,
+ API_VIOLATION = 666,
+
+ PUSHACK_CONFIRMED = 701,
+ NO_SUCH_PACKET_PENDING = 702,
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.