Esempio n. 1
0
        public ActionResponse(XElement Element)
        {
            ID = Element.Attribute("id").Value;

            Payload    = Element.Elements().First();
            Successful = CDO.GetBoolean(Payload, "successful");
        }
Esempio n. 2
0
        public Session(ActionRequest Req)
        {
            ID = CDO.GetString(Req.Payload, "sessionid");
            int SessionLengthSeconds = CDO.GetInt(Req.Payload, "sessionlength", 0);

            SessionEndsAtUTC = DateTime.MaxValue;

            if (SessionLengthSeconds > 0)
            {
                SessionEndsAtUTC = DateTime.UtcNow.AddSeconds(CDO.GetInt(Req.Payload, "sessionlength"));
            }

#warning sessionexpiration is a spelling error in cdo, should be sessionexpires
            SessionEndsAtUTC = CDO.GetTimestamp(Req.Payload, "sessionexpiration", SessionEndsAtUTC);

            XElement ResEl = CDO.GetList(Req.Payload, "resourceaccessrights");
            var      ResourceAccessRights = ResEl.Elements(CDO.Namespace + "resourceaccess");
            foreach (var ResAcc in ResourceAccessRights)
            {
                Resources.Add(new ResourceAccess(ResAcc));
            }

            foreach (var el in Req.Payload.Elements())
            {
                if (el.Name.LocalName.Contains("contract"))
                {
                    throw new Exception("Contracts not supported");
                }
            }
            Requester = CDO.GetAddress(Req.Payload, "requesteeaddress");
        }
Esempio n. 3
0
        public Session(ActionResponse Resp)
        {
            ID = CDO.GetString(Resp.Payload, "sessionid");
            int SessionLengthSeconds = CDO.GetInt(Resp.Payload, "sessionlength", 0);

            SessionEndsAtUTC = DateTime.MaxValue;

            if (SessionLengthSeconds > 0)
            {
                SessionEndsAtUTC = DateTime.UtcNow.AddSeconds(CDO.GetInt(Resp.Payload, "sessionlength"));
            }

            XElement ResEl = CDO.GetList(Resp.Payload, "resourceaccessrights");
            var      ResourceAccessRights = ResEl.Elements(CDO.Namespace + "resourceaccess");

            foreach (var ResAcc in ResourceAccessRights)
            {
                Resources.Add(new ResourceAccess(ResAcc));
            }

            foreach (var el in Resp.Payload.Elements())
            {
                if (el.Name.LocalName.Contains("contract"))
                {
                    throw new Exception("Contracts not supported");
                }
            }
        }
Esempio n. 4
0
        public XElement GetXML()
        {
            XElement RVal = new XElement(CDO.Namespace + "resource");

            CDO.SetResourcePath(RVal, "path", Path);
            XElement CapEL = CDO.StartList(RVal, "capabilities");

            foreach (string Cap in Capabilities)
            {
                CDO.SetString(CapEL, Cap);
            }

            if (MetaAttributes != null && MetaAttributes.Count > 0)
            {
                XElement DictEl = CDO.StartDictionary(RVal, "metaattributes");
                foreach (var item in MetaAttributes)
                {
                    XElement ItemEl = CDO.StartDictionaryItem(DictEl);
                    CDO.SetString(ItemEl, "key", item.Key);
                    CDO.SetString(ItemEl, "value", item.Value);
                }
            }

            XElement SupVerbEl = CDO.StartList(RVal, "supportedverbs");

            foreach (DataVerb verb in SupportedVerbs)
            {
                CDO.SetDataVerb(SupVerbEl, verb);
            }

            return(RVal);
        }
Esempio n. 5
0
        public async Task <bool> SetClaimKey(string Key, int TimeoutSeconds = 60)
        {
            XElement Payload = new XElement(CDO.Namespace + "entitysetclaimkey");

            CDO.SetString(Payload, "claimkey", Key);

            var Resp = await Requester.Request("setclaimkey", Payload, TimeoutSeconds);

            return(Resp.Successful);
        }
Esempio n. 6
0
        public XElement GetXML(string ReturnTypename, string ActionName)
        {
            XElement RespEl = new XElement(CDO.Namespace + "actionresponse");

            RespEl.SetAttributeValue("id", ID);
            RespEl.SetAttributeValue("name", ActionName);
            XElement GenRespEl = new XElement(CDO.Namespace + ReturnTypename);

            CDO.SetBoolean(GenRespEl, "successful", Successful);
            RespEl.Add(GenRespEl);
            return(RespEl);
        }
Esempio n. 7
0
        public async Task <bool> RegisterResources(List <Resource> Resources, int TimeoutSeconds = 60)
        {
            XElement Payload = new XElement(CDO.Namespace + "entitysetresources");

            XElement ResourceEl = CDO.StartList(Payload, "resources");

            foreach (Resource Res in Resources)
            {
                ResourceEl.Add(Res.GetXML());
            }

            var Resp = await Requester.Request("setresources", Payload, TimeoutSeconds);

            return(Resp.Successful);
        }
Esempio n. 8
0
        public void AddToElement(XElement Element)
        {
            XElement MyEl = new XElement(CDO.Namespace + "resourceaccess");

            CDO.SetResourcePath(MyEl, "path", ResourcePath);
            CDO.SetBoolean(MyEl, "subordinates", Subordinates);
            CDO.SetTimeframe(MyEl, "window", WindowFrom, WindowTo);

            XElement VerbsEl = CDO.StartList(MyEl, "verbs");

            foreach (var it in Verbs)
            {
                CDO.SetDataVerb(VerbsEl, it);
            }
            Element.Add(MyEl);
        }
Esempio n. 9
0
        public ResourceAccess(XElement Element)
        {
            this.ResourcePath = CDO.GetResourcePath(Element, "path");
            this.Subordinates = CDO.GetBoolean(Element, "subordinates");
            Tuple <DateTime?, DateTime?> Window = CDO.GetTimeframe(Element, "window");

            if (Window != null)
            {
                WindowFrom = Window.Item1;
                WindowTo   = Window.Item2;
            }
            XElement VerbsEl   = CDO.GetList(Element, "verbs");
            var      VerbsElIt = VerbsEl.Elements(CDO.Namespace + "dataverb");

            foreach (var verbel in VerbsElIt)
            {
                Verbs.Add(CDO.GetDataVerb(verbel));
            }
        }
Esempio n. 10
0
        void HandleSessionTerminated(ActionRequest Req)
        {
            Console.WriteLine(Req.Payload.ToString());
            string  SessionID  = CDO.GetString(Req.Payload, "sessionid");
            Session Terminated = null;

            lock (ActiveSessions)
            {
                if (ActiveSessions.ContainsKey(SessionID))
                {
                    Terminated = ActiveSessions[SessionID];
                    ActiveSessions.Remove(SessionID);
                }
            }
            if (this.SessionTerminated != null)
            {
                this.SessionTerminated(Terminated);
            }
        }
Esempio n. 11
0
        // Returns address to source or null
        public async Task <JID> StartSession(string SessionID, int TimeoutSeconds = 60)
        {
            DateTime StartedAt = DateTime.UtcNow;

            XElement Payload = new XElement(CDO.Namespace + "entitystartsession");

            CDO.SetString(Payload, "sessionid", SessionID);

            var Resp = await Requester.Request("startsession", Payload, TimeoutSeconds);

            if (!Resp.Successful)
            {
                return(null);
            }

            JID BareJID = CDO.GetAddress(Resp.Payload, "targetaddress");

            if (Uplink.Roster.AggregatedPresence.ContainsKey(BareJID.GetBareJID()))
            {
                return(Uplink.Roster.AggregatedPresence[BareJID.GetBareJID()].Last().FullJID);
            }


            Uplink.Roster.Subscribe(BareJID);

            while (!Uplink.Roster.AggregatedPresence.ContainsKey(BareJID.GetBareJID()))
            {
                await Task.Delay(25);

                if ((DateTime.UtcNow - StartedAt).TotalSeconds > TimeoutSeconds)
                {
                    throw new System.TimeoutException();
                }
            }

            return(Uplink.Roster.AggregatedPresence[BareJID.GetBareJID()].Last().FullJID);
        }
Esempio n. 12
0
        public async Task <Session> RequestSession(string TargetEntityID,
                                                   List <ResourceAccess> Resources,
                                                   TimeSpan SessionLength,
                                                   int TimeoutSeconds = 60)
        {
            XElement Payload = new XElement(CDO.Namespace + "entityrequestsession");

            CDO.SetEntityID(Payload, "targetentity", TargetEntityID);
            var ResEl = CDO.StartList(Payload, "resourceaccessrights");

            foreach (ResourceAccess it in Resources)
            {
                it.AddToElement(ResEl);
            }
            CDO.SetInt(Payload, "sessionlength", (int)SessionLength.TotalSeconds);

            var Resp = await Requester.Request("requestsession", Payload, TimeoutSeconds);

            if (Resp.Successful != true)
            {
                return(null);
            }

            Session Data = new Session(Resp);

            lock (ActiveSessions)
            {
                ActiveSessions[Data.ID] = Data;
                if (Data.SessionEndsAtUTC < NextSweepNeeded)
                {
                    NextSweepNeeded = Data.SessionEndsAtUTC;
                }
            }


            return(Data);
        }