Esempio n. 1
0
        /// <summary>
        /// When a game status request is recieved, processes the request.
        /// </summary>
        /// <param name="gameID"></param>
        /// <param name="brief"></param>
        private void GameStatusRequestRecieved(string gameID, string brief)
        {
            Game gameStatus = service.GameStatus(gameID, brief);

            string result =
                JsonConvert.SerializeObject(gameStatus,
                                            new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });

            ss.BeginSend("HTTP/1.1 " + (int)service.statusCode + " " + service.statusCode + " \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);
        }
Esempio n. 2
0
        /// <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);
            }
        }