Esempio n. 1
0
        internal void OnConnect()
        {
            client.VerifyAccess();

            if (!this.client.IsConnected)
            {
                throw new InvalidOperationException("Cannot open object before the Client is connected");
            }

            foreach (var pair in pendingOpenOperations)
            {
                Payload eventData = new ObjectOpenedPayload(pair.Value.Entry.ToPayload(), ObjectMode.Open, this.client.ClientId); // TODO ransomr may need to create pending collection
                this.client.SendPublishEvent(eventData);
            }
            foreach (var pair in this)
            {
                this.pendingOpenOperations[pair.Value.Name] = new OpenOperation()
                {
                    Entry = pair.Value
                };

                Payload eventData = new ObjectOpenedPayload(pair.Value.ToPayload(), ObjectMode.Open, this.client.ClientId);
                this.client.SendPublishEvent(eventData);
            }
            Clear();
        }
Esempio n. 2
0
        /// <summary>
        /// Opens an object with the specified name
        /// </summary>
        /// <param name="name">The object to open</param>
        /// <param name="mode">An ObjectMode value that specifies whether an object is created if one does not exist, and determines whether the contents of existing object is retained or overwritten.</param>
        /// <returns>A shared object with the specified name</returns>
        public T Open <T>(string name, ObjectMode mode, Action <ObjectConnectedEventArgs> callback) where T : INotifyPropertyChanged, new()
        {
            client.VerifyAccess();

            if (mode == ObjectMode.Create || mode == ObjectMode.Truncate)
            {
                throw new ArgumentException("mode", "Create and Truncate are not supported ObjectModes for named objects");
            }

            if (!this.client.IsConnected)
            {
                throw new InvalidOperationException("Cannot open object before the Client is connected");
            }

            if (this.pendingOpenOperations.ContainsKey(name))
            {
                throw new InvalidOperationException("The specified named object is already in the process of being opened");
            }

            ObjectEntry entry = null;

            if (this.ContainsKey(name))
            {
                if (mode != ObjectMode.Open && mode != ObjectMode.OpenOrCreate)
                {
                    throw new ArgumentException("Invalid ObjectMode. Specified object has already been opened on this client. Try using ObjectMode.Open", "mode");
                }

                entry = this[name];
                // Make sure type of object matches T
                if (entry.Type != typeof(T))
                {
                    throw new ArgumentException("Specified type does not match that of existing collection");
                }

                if (callback != null)
                {
                    // If callback was provided, call the opened callback
                    callback(new ObjectConnectedEventArgs(entry.Object, entry.Name, false));
                }
            }
            else
            {
                entry = new ObjectEntry(this.client, name, typeof(T));
                // Add to internal dictionary tracking all the pending open operations
                this.pendingOpenOperations[entry.Name] = new OpenOperation()
                {
                    Entry = entry, Callback = callback
                };

                Payload eventData = new ObjectOpenedPayload(entry.ToPayload(), mode, this.client.ClientId);
                this.client.SendPublishEvent(eventData);

                entry.AddParent(new ParentEntry(this.client.Id, 0));
            }

            return((T)entry.Object);
        }