def Deactivate_Session(self, session_id, state): self.Validate_Session_Id(session_id) if state == "active": raise SOCKET.COMMAND_EXCEPTION("state must not be active") if self.sessions[session_id]["state"] != "active": raise SOCKET.COMMAND_EXCEPTION("session already inactive") if self.sessions[session_id]["machine"] == None: raise SOCKET.COMMAND_EXCEPTION( "session is not bound to machine but session is inactive: PANIC" ) self.hosts_client.Release_Host(self.sessions[session_id]["machine"], self.sessions[session_id]["claim_id"]) self.sessions[session_id]["claim_id"] = None self.sessions[session_id]["machine"] = None self.sessions[session_id]["state"] = state self.Write_Session(session_id)
def Unlock_Host(self, host): if not self.hosts.has_key(host): raise SOCKET.COMMAND_EXCEPTION("Invalid host") if not self.hosts[host]["locked"]: raise SOCKED.COMMAND_EXCEPTION("Host already unlocked") self.hosts[host]["locked"] = 0 self.Write_Host_List()
def Register_Client(self, client_id, user, host): if self.Registered(client_id): raise SOCKET.COMMAND_EXCEPTION( "Connection already registered for user %s" % self.clientid_to_username[client_id]) if not self.users.has_key(user): self.users[user] = [] self.clientid_to_username[client_id] = user self.users[user].append(client_id) print "Registered user=%s client=%s" % (user, client_id)
def Activate_Session(self, session_id, desired_hostname): self.Validate_Session_Id(session_id) if self.sessions[session_id]["machine"] != None: raise SOCKET.COMMAND_EXCEPTION( "session is already bound to machine %s" % self.sessions[session_id]["machine"]) if self.sessions[session_id]["state"] == "active": raise SOCKET.COMMAND_EXCEPTION( "session already activated but no machine: PANIC") claim_id = self.hosts_client.Claim_Host( desired_hostname, self.server_id, self.sessions[session_id]["username"], self.sessions[session_id]["cpus"], self.sessions[session_id]["memory"]) self.sessions[session_id]["machine"] = desired_hostname self.sessions[session_id]["claim_id"] = claim_id self.sessions[session_id]["state"] = "active" self.Write_Session(session_id)
def Send(self, client_id, users, data): if not self.Registered(client_id): raise SOCKET.COMMAND_EXCEPTION("Your client is not registered") not_found_users = [] users_and_clientids = [] for user in users: if not self.users.has_key(user) or len(self.users.keys()) == 0: not_found_users.append(user) else: users_and_clientids.extend( map(lambda x: (user, x), self.users[user])) if len(not_found_users) > 0: raise SOCKET.COMMAND_EXCEPTION("No registration for users: %s" % ",".join(not_found_users)) for user, clientid in users_and_clientids: client = self.clients[clientid] client.queueWrite( (-100, None, ("SEND", (self.clientid_to_username[client_id], client_id), users_and_clientids, data)))
def Validate_Session_Id(self, session_id): if type(session_id) != int: raise SOCKET.COMMAND_EXCEPTION("Invalid session id") elif not self.sessions.has_key(session_id): raise SOCKET.COMMAND_EXCEPTION("Invalid session id %d" % session_id)
def Set_User(self, host, user): if not self.hosts.has_key(host): raise SOCKET.COMMAND_EXCEPTION("Invalid host") self.hosts[host]["user"] = user
def Delete_Host(self, host): if not self.hosts.has_key(host): raise SOCKET.COMMAND_EXCEPTION("Invalid host") del self.hosts[host] self.Write_Host_List()
def Add_Host(self, host): if self.hosts.has_key(host): raise SOCKET.COMMAND_EXCEPTION("Host already added") self.hosts[host] = {"user": None} self.Write_Host_List()