Skip to content

Commit

Permalink
Added GetLightSessionId().
Browse files Browse the repository at this point in the history
  • Loading branch information
Quantum-Sicarius committed Aug 29, 2018
1 parent c063dc7 commit 92c0890
Show file tree
Hide file tree
Showing 12 changed files with 603 additions and 441 deletions.
23 changes: 13 additions & 10 deletions protos/server.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ package session;

// The sessions manager service definition.
service SessionsManager {
// [EXPENSIVE] Creates a new session.
// EXPENSIVE Creates a new session.
rpc Create (NewSessionRequest) returns (Session) {
}
// [EXPENSIVE] Join a session.
// EXPENSIVE Join a session.
rpc Join (JoinRequest) returns (Session) {
}
// [EXPENSIVE] Leave a session.
// EXPENSIVE Leave a session.
rpc Leave (LeaveRequest) returns (LeaveReply) {
}
// Set the max amount of players that can be in the session.
Expand All @@ -25,13 +25,16 @@ service SessionsManager {
// Set the privacy status of the session.
rpc SetPrivate (SetPrivateRequest) returns (Session) {
}
// [EXPENSIVE] List available sessions.
// EXPENSIVE List available sessions.
rpc List (ListRequest) returns (ListReply) {
}
// [EXPENSIVE] Get session by ID
// EXPENSIVE Get session by ID
rpc GetSessionById (GetSessionRequest) returns (Session) {
}
// [EXPENSIVE] Get all sessions of user
// CHEAP Get light session by ID
rpc GetLightSessionById (GetSessionRequest) returns (LightSession) {
}
// EXPENSIVE Get all sessions of user
rpc GetSessionsOfUser (GetSessionsOfUserRequest) returns (GetSessionsOfUserReply) {
}
// Ready up a user in the requested session
Expand All @@ -49,7 +52,7 @@ service SessionsManager {
rpc RemoveCharacterFromSession(RemoveCharacterFromSessionRequest) returns (Session) {
}

// [CHEAP] Returns an array of light characters.
// CHEAP Returns an array of light characters.
rpc GetCharactersInSession(GetCharactersInSessionRequest) returns (GetCharactersInSessionResponse) {
}
}
Expand All @@ -59,13 +62,13 @@ service CharactersManager {
}
rpc DeleteCharacter (DeleteCharacterRequest) returns (DeleteCharacterReply) {
}
// [CHEAP]
// CHEAP
rpc GetCharacters (GetCharactersRequest) returns (GetCharactersReply) {
}
// [EXPENSIVE]
// EXPENSIVE
rpc UpdateCharacter (UpdateCharacterRequest) returns (Character) {
}
// [EXPENSIVE]
// EXPENSIVE
rpc GetCharacterById (GetCharacterByIdRequest) returns (Character) {
}
}
Expand Down
31 changes: 20 additions & 11 deletions server/server_pb2.py

Large diffs are not rendered by default.

38 changes: 27 additions & 11 deletions server/server_pb2_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from . import server_pb2 as server__pb2


class SessionsManagerStub(object):
"""The sessions manager service definition.
"""
Expand Down Expand Up @@ -59,6 +58,11 @@ def __init__(self, channel):
request_serializer=server__pb2.GetSessionRequest.SerializeToString,
response_deserializer=server__pb2.Session.FromString,
)
self.GetLightSessionById = channel.unary_unary(
'/session.SessionsManager/GetLightSessionById',
request_serializer=server__pb2.GetSessionRequest.SerializeToString,
response_deserializer=server__pb2.LightSession.FromString,
)
self.GetSessionsOfUser = channel.unary_unary(
'/session.SessionsManager/GetSessionsOfUser',
request_serializer=server__pb2.GetSessionsOfUserRequest.SerializeToString,
Expand Down Expand Up @@ -101,21 +105,21 @@ class SessionsManagerServicer(object):
"""

def Create(self, request, context):
"""[EXPENSIVE] Creates a new session.
"""EXPENSIVE Creates a new session.
"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')

def Join(self, request, context):
"""[EXPENSIVE] Join a session.
"""EXPENSIVE Join a session.
"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')

def Leave(self, request, context):
"""[EXPENSIVE] Leave a session.
"""EXPENSIVE Leave a session.
"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
Expand Down Expand Up @@ -150,21 +154,28 @@ def SetPrivate(self, request, context):
raise NotImplementedError('Method not implemented!')

def List(self, request, context):
"""[EXPENSIVE] List available sessions.
"""EXPENSIVE List available sessions.
"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')

def GetSessionById(self, request, context):
"""[EXPENSIVE] Get session by ID
"""EXPENSIVE Get session by ID
"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')

def GetLightSessionById(self, request, context):
"""CHEAP Get light session by ID
"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')

def GetSessionsOfUser(self, request, context):
"""[EXPENSIVE] Get all sessions of user
"""EXPENSIVE Get all sessions of user
"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
Expand Down Expand Up @@ -206,7 +217,7 @@ def RemoveCharacterFromSession(self, request, context):
raise NotImplementedError('Method not implemented!')

def GetCharactersInSession(self, request, context):
"""[CHEAP] Returns an array of light characters.
"""CHEAP Returns an array of light characters.
"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
Expand Down Expand Up @@ -260,6 +271,11 @@ def add_SessionsManagerServicer_to_server(servicer, server):
request_deserializer=server__pb2.GetSessionRequest.FromString,
response_serializer=server__pb2.Session.SerializeToString,
),
'GetLightSessionById': grpc.unary_unary_rpc_method_handler(
servicer.GetLightSessionById,
request_deserializer=server__pb2.GetSessionRequest.FromString,
response_serializer=server__pb2.LightSession.SerializeToString,
),
'GetSessionsOfUser': grpc.unary_unary_rpc_method_handler(
servicer.GetSessionsOfUser,
request_deserializer=server__pb2.GetSessionsOfUserRequest.FromString,
Expand Down Expand Up @@ -357,21 +373,21 @@ def DeleteCharacter(self, request, context):
raise NotImplementedError('Method not implemented!')

def GetCharacters(self, request, context):
"""[CHEAP]
"""CHEAP
"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')

def UpdateCharacter(self, request, context):
"""[EXPENSIVE]
"""EXPENSIVE
"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')

def GetCharacterById(self, request, context):
"""[EXPENSIVE]
"""EXPENSIVE
"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
Expand Down
67 changes: 65 additions & 2 deletions server/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,20 +983,83 @@ def GetSessionById(self, request, context):

return self._convertToGrpcSession(session, "SUCCESS")
except exc.SQLAlchemyError as err:
self.logger.error("[SETPRIVATE] SQLAlchemyError! " + str(err))
self.logger.error("[GetSessionsOfUser] SQLAlchemyError! " + str(err))
return server_pb2.Session(
session_id="NULL",
name="NULL",
status="FAILED",
status_message="Database error!")
except Exception:
self.logger.exception("[GetSessionsOfUser] Unhandled exception occurred!")
return server_pb2.GetSessionsOfUserReply(
return server_pb2.Session(
status="FAILED",
status_message="[GetSessionsOfUser] Internal server error! Blame Thomas!")
finally:
self.conn.close()

def GetLightSessionById(self, request, context):
self.logger.info("GetLightSessionById called!")

_session_id = request.session_id
_auth_id_token = request.auth_id_token

try:
decoded_token = firebase.auth.verify_id_token(_auth_id_token)
uid = decoded_token["uid"]
except ValueError:
self.logger.warning("Failed to verify login!")
return server_pb2.LightSession(
session_id="NULL",
name="NULL",
status="FAILED",
status_message="[GetLightSessionById] Failed to verify token!")

self.logger.debug("Successfully verified token! UID=" + uid)

try:
self.conn = self._connectDatabase()
session = self.conn.query(db.Session).filter(
db.Session.session_id == _session_id).first()

if not session:
self.logger.warning(
"[GetLightSessionById] Failed to get session,"
" that ID does not exist!")
return server_pb2.LightSession(
session_id="NULL",
name="NULL",
status="FAILED",
status_message="[GetLightSessionById] No session"
" with that ID exists!")

if session.state_ready_start_time < datetime.datetime.now() - datetime.timedelta(
seconds=session.ready_up_expiry_time):
# Expired ready up phase reset.
# Delete all ready users in session

for _user in session.ready_users:
session.ready_users.remove(_user)

# Update state
session.state = "PAUSED"
self.conn.commit()

return self._convertToGrpcLightSession(session, "SUCCESS")
except exc.SQLAlchemyError as err:
self.logger.error("[GetLightSessionById] SQLAlchemyError! " + str(err))
return server_pb2.LightSession(
session_id="NULL",
name="NULL",
status="FAILED",
status_message="Database error!")
except Exception:
self.logger.exception("[GetLightSessionById] Unhandled exception occurred!")
return server_pb2.LightSession(
status="FAILED",
status_message="[GetLightSessionById] Internal server error! Blame Thomas!")
finally:
self.conn.close()

def GetSessionsOfUser(self, request, context):
self.logger.info("GetSessionsOfUser sessions called!")

Expand Down
Loading

0 comments on commit 92c0890

Please sign in to comment.