示例#1
0
        private int WaitForRevision(int currentRevision)
        {
            ContentNode revNode = ContentParser.Parse(bag, fetcher.Fetch("/update",
                                                                         "revision-number=" + currentRevision));

            return((int)revNode.GetChild("dmap.serverrevision").Value);
        }
示例#2
0
        private void RefreshTracks(string revquery)
        {
            byte[] tracksData = client.Fetcher.Fetch(String.Format("/databases/{0}/items", id),
                                                     TrackQuery + "&" + revquery);
            ContentNode tracksNode = ContentParser.Parse(client.Bag, tracksData);

            if (IsUpdateResponse(tracksNode))
            {
                return;
            }

            // handle track additions/changes
            foreach (ContentNode trackNode in (ContentNode[])tracksNode.GetChild("dmap.listing").Value)
            {
                Track track    = Track.FromNode(trackNode);
                Track existing = LookupTrackById(track.Id);

                if (existing == null)
                {
                    AddTrack(track);
                }
                else
                {
                    existing.Update(track);
                }
            }

            if ((byte)tracksNode.GetChild("dmap.updatetype").Value == 1)
            {
                // handle track deletions
                ContentNode deleteList = tracksNode.GetChild("dmap.deletedidlisting");

                if (deleteList != null)
                {
                    foreach (ContentNode deleted in (ContentNode[])deleteList.Value)
                    {
                        Track track = LookupTrackById((int)deleted.Value);

                        if (track != null)
                        {
                            RemoveTrack(track);
                        }
                    }
                }
            }
        }
示例#3
0
        private void RefreshPlaylists(string revquery)
        {
            byte[] playlistsData;

            try {
                playlistsData = client.Fetcher.Fetch(String.Format("/databases/{0}/containers", id), revquery);
            } catch (WebException) {
                return;
            }

            ContentNode playlistsNode = ContentParser.Parse(client.Bag, playlistsData);

            if (IsUpdateResponse(playlistsNode))
            {
                return;
            }

            // handle playlist additions/changes
            List <int> plids = new List <int> ();

            foreach (ContentNode playlistNode in (ContentNode[])playlistsNode.GetChild("dmap.listing").Value)
            {
                Playlist pl = Playlist.FromNode(playlistNode);

                if (pl != null)
                {
                    plids.Add(pl.Id);
                    Playlist existing = LookupPlaylistById(pl.Id);

                    if (existing == null)
                    {
                        AddPlaylist(pl);
                    }
                    else
                    {
                        existing.Update(pl);
                    }
                }
            }

            // delete playlists that no longer exist
            foreach (Playlist pl in new List <Playlist> (playlists))
            {
                if (!plids.Contains(pl.Id))
                {
                    RemovePlaylist(pl);
                }
            }

            plids = null;

            // add/remove tracks in the playlists
            foreach (Playlist pl in playlists)
            {
                byte [] playlistTracksData = client.Fetcher.Fetch(String.Format(
                                                                      "/databases/{0}/containers/{1}/items", id, pl.Id), String.Format("meta=dmap.itemid,dmap.containeritemid&{0}", revquery)
                                                                  );
                ContentNode playlistTracksNode = ContentParser.Parse(client.Bag, playlistTracksData);

                if (IsUpdateResponse(playlistTracksNode))
                {
                    return;
                }

                if ((byte)playlistTracksNode.GetChild("dmap.updatetype").Value == 1)
                {
                    // handle playlist track deletions
                    ContentNode deleteList = playlistTracksNode.GetChild("dmap.deletedidlisting");

                    if (deleteList != null)
                    {
                        foreach (ContentNode deleted in (ContentNode[])deleteList.Value)
                        {
                            int index = pl.LookupIndexByContainerId((int)deleted.Value);

                            if (index < 0)
                            {
                                continue;
                            }

                            pl.RemoveAt(index);
                        }
                    }
                }

                // add new tracks, or reorder existing ones

                int plindex = 0;
                foreach (ContentNode plTrackNode in (ContentNode[])playlistTracksNode.GetChild("dmap.listing").Value)
                {
                    Track pltrack     = null;
                    int   containerId = 0;
                    Track.FromPlaylistNode(this, plTrackNode, out pltrack, out containerId);

                    if (pl[plindex] != null && pl.GetContainerId(plindex) != containerId)
                    {
                        pl.RemoveAt(plindex);
                        pl.InsertTrack(plindex, pltrack, containerId);
                    }
                    else if (pl[plindex] == null)
                    {
                        pl.InsertTrack(plindex, pltrack, containerId);
                    }

                    plindex++;
                }
            }
        }
示例#4
0
 private void ParseSessionId(ContentNode node)
 {
     fetcher.SessionId = (int)node.GetChild("dmap.sessionid").Value;
 }
示例#5
0
        public static ContentNode Parse(ContentCodeBag bag, byte[] buffer, string root,
                                        ref int offset)
        {
            ContentNode node = new ContentNode();

            int         num  = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, offset));
            ContentCode code = bag.Lookup(num);

            if (code.Equals(ContentCode.Zero))
            {
                // probably a buggy server.  fallback to our internal code bag
                code = ContentCodeBag.Default.Lookup(num);
            }

            int length = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, offset + 4));

            if (code.Equals(ContentCode.Zero))
            {
                string s = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
                throw new ContentException(String.Format("Failed to find content code for '{0}'. Data length is {1}; content is {2}",
                                                         ContentCodeBag.GetStringFormat(num), length, s));
            }

            node.Name = code.Name;

            switch (code.Type)
            {
            case ContentType.Char:
                node.Value = (byte)buffer[offset + 8];
                break;

            case ContentType.Short:
                node.Value = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, offset + 8));
                break;

            case ContentType.SignedLong:
            case ContentType.Long:
                node.Value = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, offset + 8));
                break;

            case ContentType.LongLong:
                node.Value = IPAddress.NetworkToHostOrder(BitConverter.ToInt64(buffer, offset + 8));
                break;

            case ContentType.String:
                node.Value = Encoding.UTF8.GetString(buffer, offset + 8, length);
                break;

            case ContentType.Date:
                node.Value = Utility.ToDateTime(IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, offset + 8)));
                break;

            case ContentType.Version:
                int major = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, offset + 8));
                int minor = (int)buffer[offset + 10];
                int micro = (int)buffer[offset + 11];

                node.Value = new Version(major, minor, micro);
                break;

            case ContentType.Container:
                node.Value = ParseChildren(bag, buffer, offset + 8, length);
                break;

            default:
                throw new ContentException(String.Format("Unknown content type '{0}' for '{1}'",
                                                         code.Type, code.Name));
            }

            offset += length + 8;

            if (root != null)
            {
                ContentNode rootNode = node.GetChild(root);

                if (rootNode == null)
                {
                    throw new ContentException(String.Format("Could not find root node '{0}'", root));
                }

                return(rootNode);
            }
            else
            {
                return(node);
            }
        }
示例#6
0
        public static ContentNode Parse(ContentCodeBag bag, byte[] buffer, string root,
                                         ref int offset)
        {
            ContentNode node = new ContentNode ();

            int num = IPAddress.NetworkToHostOrder (BitConverter.ToInt32 (buffer, offset));
            ContentCode code = bag.Lookup (num);
            if (code.Equals (ContentCode.Zero)) {
                // probably a buggy server.  fallback to our internal code bag
                code = ContentCodeBag.Default.Lookup (num);
            }

            int length = IPAddress.NetworkToHostOrder (BitConverter.ToInt32 (buffer, offset + 4));

            if (code.Equals (ContentCode.Zero)) {
                string s = System.Text.ASCIIEncoding.ASCII.GetString (buffer);
                throw new ContentException (String.Format ("Failed to find content code for '{0}'. Data length is {1}; content is {2}",
                                                           ContentCodeBag.GetStringFormat (num), length, s));
            }

            node.Name = code.Name;

            switch (code.Type) {
            case ContentType.Char:
                node.Value = (byte) buffer[offset + 8];
                break;
            case ContentType.Short:
                node.Value = IPAddress.NetworkToHostOrder (BitConverter.ToInt16 (buffer, offset + 8));
                break;
            case ContentType.SignedLong:
            case ContentType.Long:
                node.Value = IPAddress.NetworkToHostOrder (BitConverter.ToInt32 (buffer, offset + 8));
                break;
            case ContentType.LongLong:
                node.Value = IPAddress.NetworkToHostOrder (BitConverter.ToInt64 (buffer, offset + 8));
                break;
            case ContentType.String:
                node.Value = Encoding.UTF8.GetString (buffer, offset + 8, length);
                break;
            case ContentType.Date:
                node.Value = Utility.ToDateTime (IPAddress.NetworkToHostOrder (BitConverter.ToInt32 (buffer, offset + 8)));
                break;
            case ContentType.Version:
                int major = IPAddress.NetworkToHostOrder (BitConverter.ToInt16 (buffer, offset + 8));
                int minor = (int) buffer[offset + 10];
                int micro = (int) buffer[offset + 11];

                node.Value = new Version (major, minor, micro);
                break;
            case ContentType.Container:
                node.Value = ParseChildren (bag, buffer, offset + 8, length);
                break;
            default:
                throw new ContentException (String.Format ("Unknown content type '{0}' for '{1}'",
                                                           code.Type, code.Name));
            }

            offset += length + 8;

            if (root != null) {
                ContentNode rootNode = node.GetChild (root);

                if (rootNode == null)
                    throw new ContentException (String.Format ("Could not find root node '{0}'", root));

                return rootNode;
            } else {
                return node;
            }
        }
示例#7
0
文件: Client.cs 项目: knocte/banshee
 private void ParseSessionId(ContentNode node)
 {
     fetcher.SessionId = (int) node.GetChild ("dmap.sessionid").Value;
 }