Permalink
Browse files

splitting up classes

  • Loading branch information...
1 parent dae08b8 commit e3324e3041191940f70f4d6876b7f3037ff6dc98 @thriqon thriqon committed Apr 28, 2012
Showing with 376 additions and 184 deletions.
  1. +66 −184 API/APIConnection.cs
  2. +127 −0 API/ApiDriver.cs
  3. +26 −0 API/Model/Auth.cs
  4. +157 −0 API/ReturnCode.cs
View
@@ -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
@@ -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)
+ {
+ }
+ }
+
+ }
+
+}
+
Oops, something went wrong.

0 comments on commit e3324e3

Please sign in to comment.