/// <summary> /// Determines how to recieve the incoming request and what type of object needs to be created. /// </summary> /// <param name="ss"></param> /// <param name="contentLength"></param> /// <param name="methodName"></param> private string GetObject(String s) { switch (methodName) { // Used for creating a user. case "CreateUser": UserInfo user = JsonConvert.DeserializeObject <UserInfo>(s); UserTokenObject token = service.CreateUser(user); string result = JsonConvert.SerializeObject(token, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); return(result); // Used for joing a game. case "JoinGame": JoinGameInfo info = JsonConvert.DeserializeObject <JoinGameInfo>(s); GameiD id = service.JoinGame(info); string gID = JsonConvert.SerializeObject(id, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); return(gID); // Used for cancelling a join request. case "CancelJoinRequest": Cancel cancel = JsonConvert.DeserializeObject <Cancel>(s); service.CancelJoinRequest(cancel); return(""); // Used for playing a word case "PlayWord": WordCheck word = JsonConvert.DeserializeObject <WordCheck>(s); WordScore score = service.PlayWord(gameID, word); string wordPlayed = JsonConvert.SerializeObject(score, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); return(wordPlayed); default: return(""); } }
/// <summary> /// Given the proper parameters, it runs the correct method in our BoggleServer and sends the response message. /// This is essentially covering the functionality of IBoggleService from before. /// </summary> /// <param name="Type"></param> /// <param name="GameID"></param> /// <param name="IsBrief"></param> /// <param name="content"></param> private void ParseMessage(string Type, string Url, string GameID, string IsBrief, dynamic content) { HttpStatusCode status; // Each method we call should return the object we to JSON encoded if (Type == "POST") { // CreateUser if (Url == "users") { UserID ReturnID = server.CreateUser(content, out status); CompileMessage(status, ReturnID); } // JoinGame else if (Url == "games") { GameIDReturn IDReturn = server.JoinGame(content, out status); CompileMessage(status, IDReturn); } } else if (Type == "PUT") { // CancelJoinRequest if (Url == "games" && GameID == string.Empty) { server.CancelJoinRequest(content, out status); CompileMessage(status, null); } // PlayWord else { ScoreReturn Score = server.PlayWord(content, GameID, out status); CompileMessage(status, Score); } } // GetGameStatus else { Game CurrentGame = new Game(); CurrentGame = server.GetGameStatus(GameID, IsBrief, out status); CompileMessage(status, CurrentGame); } }
/// <summary> /// Processes content as it is received /// </summary> private void ContentReceived(string s, Exception e, object payload) { string httpstatus; Console.WriteLine("s: " + s); Console.WriteLine("payload: " + ((string[])payload)[0]); Console.WriteLine("payload: " + ((string[])payload)[1]); if (s != null) { //where to store a serialized object for beginsend string result = ""; ���������������� //string representing the request type ����������������string request = ((string[])payload)[0]; //(will this throw a null exception??) ���������������� //string containing the url ����������������string url = ((string[])payload)[1]; //(will this throw a null exception??) ���������������� //switch statement based upon result of request to server passed through "payload" param (ie request) ����������������switch(request) { case "createuser": //deserialize json parameters in s to pass to appropriate method in BS UserInfo userinfo = JsonConvert.DeserializeObject <UserInfo>(s); //Check that object has required fields else bad request //TODO ������������������������//store the return object of the method ������������������������userinfo = boggleservice.CreateUser(userinfo); httpstatus = userinfo.HttpStatus; //remove httpstatus userinfo.HttpStatus = null; ������������������������//serialize the return object ������������������������result = JsonConvert.SerializeObject(userinfo, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); Console.WriteLine(result); ������������������������//get the http status?����������������������� ������������������������break; case "joingame": ������������������������//deserialize json parameters in s to pass to appropriate method in BS ������������������������userinfo = JsonConvert.DeserializeObject <UserInfo>(s); //Check that object has required fields else bad request //TODO //store the return object of the method userinfo = boggleservice.JoinGame(userinfo); httpstatus = userinfo.HttpStatus; //remove httpstatus userinfo.HttpStatus = null; ������������������������//serialize the return object ������������������������result = JsonConvert.SerializeObject(userinfo, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); Console.WriteLine(result); //get the http status?����������������������� ������������������������break; case "cancelgame": ������������������������//deserialize json parameters in s to pass to appropriate method in BS ������������������������userinfo = JsonConvert.DeserializeObject <UserInfo>(s); //Check that object has required fields else bad request //TODO //store the return object of the method userinfo = boggleservice.CancelJoinRequest(userinfo); httpstatus = userinfo.HttpStatus; //remove httpstatus userinfo.HttpStatus = null; ������������������������//serialize the return object ������������������������result = JsonConvert.SerializeObject(userinfo, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); ������������������������//get the http status?����������������������� ������������������������break; case "playword": ������������������������//deserialize json parameters in s to pass to appropriate method in BS ������������������������userinfo = JsonConvert.DeserializeObject <UserInfo>(s); //Check that object has required fields else bad request //TODO //extract gameid from URL string gameid = ((string[])payload)[2]; //store the return object of the method userinfo = boggleservice.PlayWord(gameid, userinfo); httpstatus = userinfo.HttpStatus; //remove httpstatus userinfo.HttpStatus = null; //serialize the return object result = JsonConvert.SerializeObject(userinfo, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); ������������������������//get the http status?����������������������� ������������������������break; case "gamestatus": ������������������������//extract data from the url ������������������������string brief = ""; if (url.Contains("yes")) { brief = "yes"; } //save gameid gameid = ((string[])payload)[2]; ������������������������//serialize the return object ������������������������GameStatus gamestatus = boggleservice.GameStatus(gameid, brief); httpstatus = gamestatus.HttpStatus; //remove httpstatus gamestatus.HttpStatus = null; result = JsonConvert.SerializeObject(gamestatus, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); ������������������������//get the http status? ������������������������break; default: httpstatus = "400 Bad Request"; break; } Console.WriteLine(httpstatus); ss.BeginSend("HTTP/1.1 " + httpstatus + "\r\n", Ignore, null); ss.BeginSend("Content-Type: application/json\r\n", Ignore, null); ss.BeginSend("Content-Length: " + result.Length + "\r\n", Ignore, null); ss.BeginSend("\r\n", Ignore, null); ss.BeginSend(result, (ex, py) => { ss.Shutdown(); }, null); } }