public async Task<ResponseObject> SendDatabaseRequest (RequestObject request) {

			var returnObj = new ResponseObject();
			using (var client = new HttpClient ()) {
				client.BaseAddress = new Uri(this.url);
				client.DefaultRequestHeaders.Accept.Clear();
				client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(@"application/json"));
				string relativePath;

				switch (request.RequestType) {
				case RequestType.GetAuthenticationRequest:
					{
						relativePath = GetAuthRequestURI (request);
						break;
					}
				case RequestType.GetAuthenticationUsernameRequest:
					{
						relativePath = GetAuthUserURI (request);
						break;
					}
				case RequestType.GetFieldRequest:
					{
						relativePath = GetFieldRequestURI (request);
						break;
					}
				case RequestType.PostFieldRequest:
					{
						relativePath = PostFieldRequestURI (request);
						break;
					}
				case RequestType.PostCreateUser:
					{
						relativePath = PostCreatePrintURI (request);
						break;
					}
				case RequestType.DeleteFieldRequest:
					{
						relativePath = DeleteFieldURI (request);
						break;
					}
				default:
					{
						throw new NotImplementedException ();
					}
				}
				StringContent requestContent = new StringContent(JsonConvert.SerializeObject(request));
				using (var response = await client.PostAsync (relativePath, requestContent)) {
					if (response.IsSuccessStatusCode) {
						// We've received a successful response. Sweet :)
						var body = await response.Content.ReadAsStringAsync ();
						returnObj = JsonConvert.DeserializeObject<ResponseObject> (body);
					} else {
						// We've encountered an error.
						Console.WriteLine ("Error Parsing JSON");
					}
				}
			}
			return returnObj;
		}
Ejemplo n.º 2
0
		//Disable the "async" warning until we actually implement the async parts.
		#pragma warning disable 1998
		private static async void HandleRequest (IAsyncResult result) 
		{
			// Note for all of this:
			// We're good as long as async operators work fine across platforms.
			// We will enter into callback hell if they don't.

			HttpListener listener = (HttpListener)result.AsyncState;
			HttpListenerContext context = listener.EndGetContext (result);
			var httpRequest = context.Request;

			RequestObject requestObject = new RequestObject ();
			requestObject.RequestType = RequestType.InvalidRequest;
			ResponseObject databaseResponse = new ResponseObject ();

			try {
				requestObject = ParseRequest (httpRequest);
				
				if (requestObject.RequestType == RequestType.InvalidRequest) {
					// Handle us receiving a malformed request.
					throw new Exception("The request could not be properly parsed");
				}
				
				// Console.WriteLine (requestObject.RequestType);
				
				// =================================================================
				// Open a connection to the peripheral so that we can receive
				// a finger print from the user.
				// =================================================================
				if (requestObject.RequestType == RequestType.GetAuthenticationRequest) {
					// Console.WriteLine ("Please enter your fingerprint:");
					// var user = Console.ReadLine ();
//					var temp = new List<string>() {"Batman", "Superman", "Flash", "Wonderwoman", "Green Lantern", "Spiderman", "Green Arrow"};
//					string user;
//					lock (o) {
//						user = temp [r.Next (temp.Count)];
//					}
//				
//					// Randomly authenticate one of our users.
//					requestObject.Fields.Add ("fingerprint", user);
				
					PeripheralConnection peripheral = PeripheralConnection.Instance;
					// This either opens a new connection or simply verifies the existing
					// connection is good.
					await m.WaitAsync();
					await peripheral.TryOpenConnection ();
					m.Release();
					await peripheral.RequestTemplate(requestObject);
				}
					
				RemoteConnectionManager databaseManager = new RemoteConnectionManager (connectionString);
				databaseResponse = await databaseManager.SendDatabaseRequest (requestObject);
			}
			catch (Exception ex) {
				Console.WriteLine (ex.Message);
			}
			finally {
				var responseText = GenerateResponse (requestObject, databaseResponse);
				var responseBytes = System.Text.Encoding.UTF8.GetBytes (responseText);

				// Pretty simple, get a handle to the response packet and the output stream.
				var httpResponse = context.Response;
				httpResponse.ContentLength64 = responseBytes.Length;
				var outputStream = httpResponse.OutputStream;

				// Write the response to the buffer and then close the connection. Note that
				// These might take a while to do.
				outputStream.Write (responseBytes, 0, responseBytes.Length);
				outputStream.Close ();
			}
		}
Ejemplo n.º 3
0
		private static string GenerateResponse (RequestObject request, ResponseObject dbResponse) {
			switch (request.RequestType) {
			case RequestType.GetAuthenticationRequest:
				{
					List<string> output = new List<string> ();
					output.Add (dbResponse.SessionID);
					output.Add (dbResponse.ResponseCode.ToString ());
					return JsonConvert.SerializeObject (output);
				}
			case RequestType.GetFieldRequest:
				{
					dbResponse.Responses.Add (dbResponse.ResponseCode.ToString ());
					return JsonConvert.SerializeObject (dbResponse.Responses);
				}
			case RequestType.PostFieldRequest:
				{
					List<string> output = new List<string> ();
					output.Add (dbResponse.ResponseCode.ToString ());
					return JsonConvert.SerializeObject (output);
				}
			case RequestType.PostCreateUser:
				{
					break;
					
				}
			case RequestType.DeleteFieldRequest:
				{
					List<string> output = new List<string> ();
					output.Add (dbResponse.ResponseCode.ToString ());
					return JsonConvert.SerializeObject (output);
				}
			}
			return String.Empty;
		}