protected virtual OSDMap OnMessageReceived(OSDMap message)
        {
            if (!message.ContainsKey("Method"))
                return null;

            UUID AgentID = message["AgentID"].AsUUID();
            ulong requestingRegion = message["RequestingRegion"].AsULong();
            ICapsService capsService = m_registry.RequestModuleInterface<ICapsService>();
            if (capsService == null)
                return new OSDMap();

            IClientCapsService clientCaps = capsService.GetClientCapsService(AgentID);

            IRegionClientCapsService regionCaps = null;
            if (clientCaps != null)
                regionCaps = clientCaps.GetCapsService(requestingRegion);
            if (message["Method"] == "LogoutRegionAgents")
            {
                LogOutAllAgentsForRegion(requestingRegion);
            }
            else if (message["Method"] == "RegionIsOnline")
                //This gets fired when the scene is fully finished starting up
            {
                //Log out all the agents first, then add any child agents that should be in this region
                LogOutAllAgentsForRegion(requestingRegion);
                IGridService GridService = m_registry.RequestModuleInterface<IGridService>();
                if (GridService != null)
                {
                    int x, y;
                    Util.UlongToInts(requestingRegion, out x, out y);
                    GridRegion requestingGridRegion = GridService.GetRegionByPosition(null, x, y);
                    if (requestingGridRegion != null)
                        Util.FireAndForget((o) => EnableChildAgentsForRegion(requestingGridRegion));
                }
            }
            else if (message["Method"] == "DisableSimulator")
            {
                //KILL IT!
                if (regionCaps == null || clientCaps == null)
                    return null;
                IEventQueueService eventQueue = m_registry.RequestModuleInterface<IEventQueueService>();
                eventQueue.DisableSimulator(regionCaps.AgentID, regionCaps.RegionHandle);
                //regionCaps.Close();
                //clientCaps.RemoveCAPS(requestingRegion);
                regionCaps.Disabled = true;
            }
            else if (message["Method"] == "ArrivedAtDestination")
            {
                if (regionCaps == null || clientCaps == null)
                    return null;
                //Recieved a callback
                if (clientCaps.InTeleport) //Only set this if we are in a teleport, 
                    //  otherwise (such as on login), this won't check after the first tp!
                    clientCaps.CallbackHasCome = true;

                regionCaps.Disabled = false;

                //The agent is getting here for the first time (eg. login)
                OSDMap body = ((OSDMap) message["Message"]);

                //Parse the OSDMap
                int DrawDistance = body["DrawDistance"].AsInteger();

                AgentCircuitData circuitData = new AgentCircuitData();
                circuitData.UnpackAgentCircuitData((OSDMap) body["Circuit"]);

                //Now do the creation
                EnableChildAgents(AgentID, requestingRegion, DrawDistance, circuitData);
            }
            else if (message["Method"] == "CancelTeleport")
            {
                if (regionCaps == null || clientCaps == null)
                    return null;
                //Only the region the client is root in can do this
                IRegionClientCapsService rootCaps = clientCaps.GetRootCapsService();
                if (rootCaps != null && rootCaps.RegionHandle == regionCaps.RegionHandle)
                {
                    //The user has requested to cancel the teleport, stop them.
                    clientCaps.RequestToCancelTeleport = true;
                    regionCaps.Disabled = false;
                }
            }
            else if (message["Method"] == "AgentLoggedOut")
            {
                //ONLY if the agent is root do we even consider it
                if (regionCaps != null)
                {
                    if (regionCaps.RootAgent)
                    {
                        LogoutAgent(regionCaps, false); //The root is killing itself
                    }
                }
            }
            else if (message["Method"] == "SendChildAgentUpdate")
            {
                if (regionCaps == null || clientCaps == null)
                    return null;
                IRegionClientCapsService rootCaps = clientCaps.GetRootCapsService();
                if (rootCaps != null && rootCaps.RegionHandle == regionCaps.RegionHandle) //Has to be root
                {
                    OSDMap body = ((OSDMap) message["Message"]);

                    AgentPosition pos = new AgentPosition();
                    pos.Unpack((OSDMap) body["AgentPos"]);

                    SendChildAgentUpdate(pos, regionCaps);
                    regionCaps.Disabled = false;
                }
            }
            else if (message["Method"] == "TeleportAgent")
            {
                if (regionCaps == null || clientCaps == null)
                    return null;
                IRegionClientCapsService rootCaps = clientCaps.GetRootCapsService();
                if (rootCaps != null && rootCaps.RegionHandle == regionCaps.RegionHandle)
                {
                    OSDMap body = ((OSDMap) message["Message"]);

                    GridRegion destination = new GridRegion();
                    destination.FromOSD((OSDMap) body["Region"]);

                    uint TeleportFlags = body["TeleportFlags"].AsUInteger();
                    int DrawDistance = body["DrawDistance"].AsInteger();

                    AgentCircuitData Circuit = new AgentCircuitData();
                    Circuit.UnpackAgentCircuitData((OSDMap) body["Circuit"]);

                    AgentData AgentData = new AgentData();
                    AgentData.Unpack((OSDMap) body["AgentData"]);
                    regionCaps.Disabled = false;
                    string ResponseURL = message["ResponseURL"].AsString();
                    if (ResponseURL == "")
                    {
                        OSDMap result = new OSDMap();
                        string reason = "";
                        result["success"] = TeleportAgent(ref destination, TeleportFlags, DrawDistance,
                                                          Circuit, AgentData, AgentID, requestingRegion, out reason);
                        result["Reason"] = reason;
                        //Remove the region flags, not the regions problem
                        destination.Flags = 0;
                        result["Destination"] = destination.ToOSD(); //Send back the new destination
                        return result;
                    }
                    else
                    {
                        Util.FireAndForget(delegate
                        {
                            OSDMap result = new OSDMap();
                            string reason = "";
                            result["success"] = TeleportAgent(ref destination, TeleportFlags, DrawDistance,
                                                              Circuit, AgentData, AgentID, requestingRegion, out reason);
                            result["Reason"] = reason;
                            //Remove the region flags, not the regions problem
                            destination.Flags = 0;
                            result["Destination"] = destination.ToOSD(); //Send back the new destination
                            WebUtils.PostToService(ResponseURL, result);
                        });
                        return new OSDMap() { new KeyValuePair<string, OSD>("WillHaveResponse", true) };
                    }
                }
            }
            else if (message["Method"] == "CrossAgent")
            {
                if (regionCaps == null || clientCaps == null)
                    return null;
                IRegionClientCapsService rootCaps = clientCaps.GetRootCapsService();
                if (rootCaps == null || rootCaps.RegionHandle == regionCaps.RegionHandle)
                {
                    //This is a simulator message that tells us to cross the agent
                    OSDMap body = ((OSDMap) message["Message"]);

                    Vector3 pos = body["Pos"].AsVector3();
                    Vector3 Vel = body["Vel"].AsVector3();
                    GridRegion Region = new GridRegion();
                    Region.FromOSD((OSDMap) body["Region"]);
                    AgentCircuitData Circuit = new AgentCircuitData();
                    Circuit.UnpackAgentCircuitData((OSDMap) body["Circuit"]);
                    AgentData AgentData = new AgentData();
                    AgentData.Unpack((OSDMap) body["AgentData"]);
                    regionCaps.Disabled = false;

                    string ResponseURL = message["ResponseURL"].AsString();
                    if (ResponseURL == "")
                    {
                        OSDMap result = new OSDMap();
                        string reason = "";
                        result["success"] = CrossAgent(Region, pos, Vel, Circuit, AgentData,
                                                       AgentID, requestingRegion, out reason);
                        result["reason"] = reason;
                        return result;
                    }
                    else
                    {
                        Util.FireAndForget(delegate
                        {
                            OSDMap result = new OSDMap();
                            string reason = "";
                            result["success"] = CrossAgent(Region, pos, Vel, Circuit, AgentData,
                                                           AgentID, requestingRegion, out reason);
                            result["reason"] = reason;
                            if (ResponseURL != null)
                                WebUtils.PostToService(ResponseURL, result);
                        });
                        return new OSDMap() { new KeyValuePair<string, OSD>("WillHaveResponse", true) };
                    }
                }
                else if (clientCaps.InTeleport)
                {
                    OSDMap result = new OSDMap();
                    result["success"] = false;
                    result["Note"] = false;
                    return result;
                }
                else
                {
                    OSDMap result = new OSDMap();
                    result["success"] = false;
                    result["Note"] = false;
                    return result;
                }
            }
            return null;
        }
Beispiel #2
0
        protected void DoAgentPost(Hashtable request, Hashtable responsedata, UUID id)
        {
            OSDMap args = WebUtils.GetOSDMap ((string)request["body"]);
            if (args == null)
            {
                responsedata["int_response_code"] = HttpStatusCode.BadRequest;
                responsedata["str_response_string"] = "Bad request";
                return;
            }

            // retrieve the input arguments
            int x = 0, y = 0;
            UUID uuid = UUID.Zero;
            string regionname = string.Empty;
            uint teleportFlags = 0;
            if (args.ContainsKey ("destination_x") && args["destination_x"] != null)
                Int32.TryParse (args["destination_x"].AsString (), out x);
            else
                MainConsole.Instance.WarnFormat ("  -- request didn't have destination_x");
            if (args.ContainsKey ("destination_y") && args["destination_y"] != null)
                Int32.TryParse (args["destination_y"].AsString (), out y);
            else
                MainConsole.Instance.WarnFormat ("  -- request didn't have destination_y");
            if (args.ContainsKey ("destination_uuid") && args["destination_uuid"] != null)
                UUID.TryParse (args["destination_uuid"].AsString (), out uuid);
            if (args.ContainsKey ("destination_name") && args["destination_name"] != null)
                regionname = args["destination_name"].ToString ();
            if (args.ContainsKey ("teleport_flags") && args["teleport_flags"] != null)
                teleportFlags = args["teleport_flags"].AsUInteger ();

            GridRegion destination = new GridRegion
                                         {RegionID = uuid, RegionLocX = x, RegionLocY = y, RegionName = regionname};

            AgentCircuitData aCircuit = new AgentCircuitData ();
            try
            {
                aCircuit.UnpackAgentCircuitData (args);
            }
            catch (Exception ex)
            {
                MainConsole.Instance.InfoFormat ("[AGENT HANDLER]: exception on unpacking ChildCreate message {0}", ex.Message);
                responsedata["int_response_code"] = HttpStatusCode.BadRequest;
                responsedata["str_response_string"] = "Bad request";
                return;
            }

            OSDMap resp = new OSDMap (2);
            string reason = String.Empty;

            // This is the meaning of POST agent
            //m_regionClient.AdjustUserInformation(aCircuit);
            //bool result = m_SimulationService.CreateAgent(destination, aCircuit, teleportFlags, out reason);
            bool result = CreateAgent (destination, aCircuit, teleportFlags, out reason);

            resp["reason"] = OSD.FromString (reason);
            resp["success"] = OSD.FromBoolean (result);
            // Let's also send out the IP address of the caller back to the caller (HG 1.5)
            resp["your_ip"] = OSD.FromString (GetCallerIP (request));

            // TODO: add reason if not String.Empty?
            responsedata["int_response_code"] = HttpStatusCode.OK;
            responsedata["str_response_string"] = OSDParser.SerializeJsonString (resp);
        }
        public bool RetrieveAgent(GridRegion destination, UUID id, bool agentIsLeaving, out AgentData agent,
                                  out AgentCircuitData circuitData)
        {
            agent = null;
            // Try local first
            if (m_localBackend.RetrieveAgent(destination, id, agentIsLeaving, out agent, out circuitData))
                return true;

            // else do the remote thing
            if (!m_localBackend.IsLocalRegion(destination.RegionHandle))
            {
                // Eventually, we want to use a caps url instead of the agentID
                string uri = MakeUri(destination, true) + id + "/" + destination.RegionID.ToString() + "/" +
                             agentIsLeaving.ToString() + "/";

                try
                {
                    string resultStr = WebUtils.GetFromService(uri);
                    if (resultStr != "")
                    {
                        OSDMap result = OSDParser.DeserializeJson(resultStr) as OSDMap;
                        if (result["Result"] == "Not Found")
                            return false;
                        agent = new AgentData();

                        if (!result.ContainsKey("AgentData"))
                            return false; //Disable old simulators

                        agent.Unpack((OSDMap)result["AgentData"]);
                        circuitData = new AgentCircuitData();
                        circuitData.UnpackAgentCircuitData((OSDMap)result["CircuitData"]);
                        return true;
                    }
                }
                catch (Exception e)
                {
                    MainConsole.Instance.Warn("[REMOTE SIMULATION CONNECTOR]: UpdateAgent failed with exception: " + e);
                }

                return false;
            }

            return false;
        }
        protected void DoAgentPost(Hashtable request, Hashtable responsedata, UUID id)
        {
            OSDMap args = WebUtils.GetOSDMap ((string)request["body"]);
            if (args == null)
            {
                responsedata["int_response_code"] = HttpStatusCode.BadRequest;
                responsedata["str_response_string"] = "Bad request";
                return;
            }

            // retrieve the input arguments
            int x = 0, y = 0;
            UUID uuid = UUID.Zero;
            string regionname = string.Empty;
            string gatekeeper_host = string.Empty;
            string gatekeeper_serveruri = string.Empty;
            string destination_serveruri = string.Empty;
            int gatekeeper_port = 0;
            IPEndPoint client_ipaddress = null;

            if (args.ContainsKey ("gatekeeper_host") && args["gatekeeper_host"] != null)
                gatekeeper_host = args["gatekeeper_host"].AsString ();
            if (args.ContainsKey ("gatekeeper_port") && args["gatekeeper_port"] != null)
                Int32.TryParse (args["gatekeeper_port"].AsString (), out gatekeeper_port);
            if (args.ContainsKey ("gatekeeper_serveruri") && args["gatekeeper_serveruri"] != null)
                gatekeeper_serveruri = args["gatekeeper_serveruri"];
            if (args.ContainsKey ("destination_serveruri") && args["destination_serveruri"] != null)
                destination_serveruri = args["destination_serveruri"];

            GridRegion gatekeeper = new GridRegion
                                        {
                                            ServerURI = gatekeeper_serveruri,
                                            ExternalHostName = gatekeeper_host,
                                            HttpPort = (uint) gatekeeper_port,
                                            InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 0)
                                        };

            if (args.ContainsKey ("destination_x") && args["destination_x"] != null)
                Int32.TryParse (args["destination_x"].AsString (), out x);
            else
                MainConsole.Instance.WarnFormat ("  -- request didn't have destination_x");
            if (args.ContainsKey ("destination_y") && args["destination_y"] != null)
                Int32.TryParse (args["destination_y"].AsString (), out y);
            else
                MainConsole.Instance.WarnFormat ("  -- request didn't have destination_y");
            if (args.ContainsKey ("destination_uuid") && args["destination_uuid"] != null)
                UUID.TryParse (args["destination_uuid"].AsString (), out uuid);
            if (args.ContainsKey ("destination_name") && args["destination_name"] != null)
                regionname = args["destination_name"].ToString ();

            if (args.ContainsKey ("client_ip") && args["client_ip"] != null)
            {
                string ip_str = args["client_ip"].ToString ();
                try
                {
                    string callerIP = GetCallerIP (request);
                    // Verify if this caller has authority to send the client IP
                    if (callerIP == m_loginServerIP)
                        client_ipaddress = new IPEndPoint (IPAddress.Parse (ip_str), 0);
                    else // leaving this for now, but this warning should be removed
                        MainConsole.Instance.WarnFormat ("[HOME AGENT HANDLER]: Unauthorized machine {0} tried to set client ip to {1}", callerIP, ip_str);
                }
                catch
                {
                    MainConsole.Instance.DebugFormat ("[HOME AGENT HANDLER]: Exception parsing client ip address from {0}", ip_str);
                }
            }

            GridRegion destination = new GridRegion
                                         {
                                             RegionID = uuid,
                                             RegionLocX = x,
                                             RegionLocY = y,
                                             RegionName = regionname,
                                             ServerURI = destination_serveruri
                                         };

            AgentCircuitData aCircuit = new AgentCircuitData ();
            try
            {
                aCircuit.UnpackAgentCircuitData (args);
            }
            catch (Exception ex)
            {
                MainConsole.Instance.InfoFormat ("[HOME AGENT HANDLER]: exception on unpacking ChildCreate message {0}", ex.Message);
                responsedata["int_response_code"] = HttpStatusCode.BadRequest;
                responsedata["str_response_string"] = "Bad request";
                return;
            }

            OSDMap resp = new OSDMap (2);
            string reason = String.Empty;

            bool result = m_UserAgentService.LoginAgentToGrid (aCircuit, gatekeeper, destination, client_ipaddress, out reason);

            resp["reason"] = OSD.FromString (reason);
            resp["success"] = OSD.FromBoolean (result);

            // TODO: add reason if not String.Empty?
            responsedata["int_response_code"] = HttpStatusCode.OK;
            responsedata["str_response_string"] = OSDParser.SerializeJsonString (resp);
        }