Beispiel #1
0
        internal Database(Client client, ContentNode dbNode)
            : this()
        {
            this.client = client;

            Parse (dbNode);
        }
Beispiel #2
0
        public static byte[] Write(ContentCodeBag bag, ContentNode node)
        {
            MemoryStream stream = new MemoryStream ();
            BinaryWriter writer = new BinaryWriter (stream);
            Write (bag, node, writer);
            writer.Flush ();

            byte[] buf = stream.GetBuffer ();
            long len = stream.Length;
            writer.Close ();

            byte[] ret = new byte[len];
            Array.Copy (buf, ret, len);
            return ret;
        }
Beispiel #3
0
        internal static ServerInfo FromNode(ContentNode node)
        {
            ServerInfo info = new ServerInfo ();

            if (node.Name != "dmap.serverinforesponse")
                return null;

            foreach (ContentNode child in (node.Value as ContentNode[])) {
                switch (child.Name) {
                case "dmap.itemname":
                    info.Name = (string) child.Value;
                    break;
                case "dmap.authenticationmethod":
                    info.AuthenticationMethod = (AuthenticationMethod) child.Value;
                    break;
                case "dmap.supportsupdate":
                    info.SupportsUpdate = (byte) child.Value == 1;
                    break;
                }
            }

            return info;
        }
 private bool IsUpdateResponse(ContentNode node)
 {
     return(node.Name == "dmap.updateresponse");
 }
Beispiel #5
0
 private bool IsUpdateResponse(ContentNode node)
 {
     return node.Name == "dmap.updateresponse";
 }
Beispiel #6
0
        private void Parse(ContentNode dbNode)
        {
            foreach (ContentNode item in (ContentNode[]) dbNode.Value) {

                switch (item.Name) {
                case "dmap.itemid":
                    id = (int) item.Value;
                    break;
                case "dmap.persistentid":
                    persistentId = (long) item.Value;
                    break;
                case "dmap.itemname":
                    name = (string) item.Value;
                    break;
                default:
                    break;
                }
            }
        }
Beispiel #7
0
        internal static Track FromNode(ContentNode node)
        {
            Track track = new Track ();

            foreach (ContentNode field in (ContentNode[]) node.Value) {
                switch (field.Name) {
                case "dmap.itemid":
                    track.id = (int) field.Value;
                    break;
                case "daap.songartist":
                    track.artist = (string) field.Value;
                    break;
                case "dmap.itemname":
                    track.title = (string) field.Value;
                    break;
                case "daap.songalbum":
                    track.album = (string) field.Value;
                    break;
                case "daap.songtime":
                    track.duration = TimeSpan.FromMilliseconds ((int) field.Value);
                    break;
                case "daap.songformat":
                    track.format = (string) field.Value;
                    break;
                case "daap.songgenre":
                    track.genre = (string) field.Value;
                    break;
                case "daap.songsize":
                    track.size = (int) field.Value;
                    break;
                case "daap.songtrackcount":
                    track.trackCount = (short) field.Value;
                    break;
                case "daap.songtracknumber":
                    track.trackNumber = (short) field.Value;
                    break;
                case "daap.bitrate":
                    track.bitrate = (short) field.Value;
                    break;
                case "daap.songdateadded":
                    track.dateAdded = (DateTime) field.Value;
                    break;
                case "daap.songdatemodified":
                    track.dateModified = (DateTime) field.Value;
                    break;
                default:
                    break;
                }
            }

            return track;
        }
Beispiel #8
0
        private static void Write(ContentCodeBag bag, ContentNode node, BinaryWriter writer)
        {
            ContentCode code = bag.Lookup (node.Name);
            if (code.Equals (ContentCode.Zero)) {
                throw new ContentException ("Failed to get content code for: " + node.Name);
            }

            writer.Write (IPAddress.HostToNetworkOrder (code.Number));

            switch (code.Type) {
            case ContentType.Char:
                writer.Write (IPAddress.HostToNetworkOrder (1));
                writer.Write ((byte) node.Value);
                break;
            case ContentType.Short:
                writer.Write (IPAddress.HostToNetworkOrder (2));
                writer.Write (IPAddress.HostToNetworkOrder ((short) node.Value));
                break;
            case ContentType.SignedLong:
            case ContentType.Long:
                writer.Write (IPAddress.HostToNetworkOrder (4));
                writer.Write (IPAddress.HostToNetworkOrder ((int) node.Value));
                break;
            case ContentType.LongLong:
                writer.Write (IPAddress.HostToNetworkOrder (8));
                writer.Write (IPAddress.HostToNetworkOrder ((long) node.Value));
                break;
            case ContentType.String:
                byte[] data = Encoding.UTF8.GetBytes ((string) node.Value);
                writer.Write (IPAddress.HostToNetworkOrder (data.Length));
                writer.Write (data);
                break;
            case ContentType.Date:
                writer.Write (IPAddress.HostToNetworkOrder (4));
                writer.Write (IPAddress.HostToNetworkOrder (Utility.FromDateTime ((DateTime) node.Value)));
                break;
            case ContentType.Version:
                Version version = (Version) node.Value;
                writer.Write (IPAddress.HostToNetworkOrder (4));

                writer.Write ((short) IPAddress.HostToNetworkOrder ((short) version.Major));
                writer.Write ((byte) version.Minor);
                writer.Write ((byte) version.Build);
                break;
            case ContentType.Container:
                MemoryStream childStream = new MemoryStream ();
                BinaryWriter childWriter = new BinaryWriter (childStream);

                foreach (ContentNode child in (ContentNode[]) node.Value) {
                    Write (bag, child, childWriter);
                }

                childWriter.Flush ();
                byte[] bytes = childStream.GetBuffer ();
                int len = (int) childStream.Length;

                writer.Write (IPAddress.HostToNetworkOrder (len));
                writer.Write (bytes, 0, len);
                childWriter.Close ();
                break;
            default:
                Console.Error.WriteLine ("Cannot write node of type: " + code.Type);
                break;
            }
        }
Beispiel #9
0
        internal ContentNode ToNode()
        {
            ArrayList nodes = new ArrayList();

              foreach (int number in codes.Keys)
              {
            ContentCode code = (ContentCode)codes[number];

            ArrayList contents = new ArrayList();
            contents.Add(new ContentNode("dmap.contentcodesnumber", code.Number));
            contents.Add(new ContentNode("dmap.contentcodesname", code.Name));
            contents.Add(new ContentNode("dmap.contentcodestype", code.Type));

            ContentNode dict = new ContentNode("dmap.dictionary", contents);
            nodes.Add(dict);
              }

              ContentNode status = new ContentNode("dmap.status", 200);
              return new ContentNode("dmap.contentcodesresponse", status, nodes);
        }
Beispiel #10
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)) {
                throw new ContentException (String.Format ("Failed to find content code for '{0}'.  Data length is {1}",
                                                           ContentCodeBag.GetStringFormat (num), length));
            }

            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;
            }
        }
Beispiel #11
0
        internal static Playlist FromNode(ContentNode node)
        {
            Playlist pl = new Playlist ();

            foreach (ContentNode child in (ContentNode[]) node.Value) {
                switch (child.Name) {
                case  "daap.baseplaylist":
                    return null;
                case "dmap.itemid":
                    pl.Id = (int) child.Value;
                    break;
                case "dmap.itemname":
                    pl.Name = (string) child.Value;
                    break;
                default:
                    break;
                }
            }

            return pl;
        }
Beispiel #12
0
        private static void Write(ContentCodeBag bag, ContentNode node, BinaryWriter writer)
        {
            ContentCode code = bag.Lookup(node.Name);

            if (code.Equals(ContentCode.Zero))
            {
                throw new ContentException("Failed to get content code for: " + node.Name);
            }

            writer.Write(IPAddress.HostToNetworkOrder(code.Number));

            switch (code.Type)
            {
            case ContentType.Char:
                writer.Write(IPAddress.HostToNetworkOrder(1));
                writer.Write((byte)node.Value);
                break;

            case ContentType.Short:
                writer.Write(IPAddress.HostToNetworkOrder(2));
                writer.Write(IPAddress.HostToNetworkOrder((short)node.Value));
                break;

            case ContentType.SignedLong:
            case ContentType.Long:
                writer.Write(IPAddress.HostToNetworkOrder(4));
                writer.Write(IPAddress.HostToNetworkOrder((int)node.Value));
                break;

            case ContentType.LongLong:
                writer.Write(IPAddress.HostToNetworkOrder(8));
                writer.Write(IPAddress.HostToNetworkOrder((long)node.Value));
                break;

            case ContentType.String:
                byte[] data = Encoding.UTF8.GetBytes((string)node.Value);
                writer.Write(IPAddress.HostToNetworkOrder(data.Length));
                writer.Write(data);
                break;

            case ContentType.Date:
                writer.Write(IPAddress.HostToNetworkOrder(4));
                writer.Write(IPAddress.HostToNetworkOrder(Utility.FromDateTime((DateTime)node.Value)));
                break;

            case ContentType.Version:
                Version version = (Version)node.Value;
                writer.Write(IPAddress.HostToNetworkOrder(4));

                writer.Write((short)IPAddress.HostToNetworkOrder((short)version.Major));
                writer.Write((byte)version.Minor);
                writer.Write((byte)version.Build);
                break;

            case ContentType.Container:
                MemoryStream childStream = new MemoryStream();
                BinaryWriter childWriter = new BinaryWriter(childStream);

                foreach (ContentNode child in (ContentNode[])node.Value)
                {
                    Write(bag, child, childWriter);
                }

                childWriter.Flush();
                byte[] bytes = childStream.GetBuffer();
                int    len   = (int)childStream.Length;

                writer.Write(IPAddress.HostToNetworkOrder(len));
                writer.Write(bytes, 0, len);
                childWriter.Close();
                break;

            default:
                Console.Error.WriteLine("Cannot write node of type: " + code.Type);
                break;
            }
        }
Beispiel #13
0
        internal static Track FromNode(ContentNode node)
        {
            Track track = new Track();

            foreach (ContentNode field in (ContentNode[])node.Value)
            {
                switch (field.Name)
                {
                case "dmap.itemid":
                    track.id = (int)field.Value;
                    break;

                case "daap.songartist":
                    track.artist = (string)field.Value;
                    break;

                case "dmap.itemname":
                    track.title = (string)field.Value;
                    break;

                case "daap.songalbum":
                    track.album = (string)field.Value;
                    break;

                case "daap.songtime":
                    track.duration = TimeSpan.FromMilliseconds((int)field.Value);
                    break;

                case "daap.songformat":
                    track.format = (string)field.Value;
                    break;

                case "daap.songgenre":
                    track.genre = (string)field.Value;
                    break;

                case "daap.songsize":
                    track.size = (int)field.Value;
                    break;

                case "daap.songtrackcount":
                    track.trackCount = (short)field.Value;
                    break;

                case "daap.songtracknumber":
                    track.trackNumber = (short)field.Value;
                    break;

                case "daap.bitrate":
                    track.bitrate = (short)field.Value;
                    break;

                case "daap.songdateadded":
                    track.dateAdded = (DateTime)field.Value;
                    break;

                case "daap.songdatemodified":
                    track.dateModified = (DateTime)field.Value;
                    break;

                default:
                    break;
                }
            }

            return(track);
        }
Beispiel #14
0
 private void ParseSessionId(ContentNode node)
 {
     fetcher.SessionId = (int) node.GetChild ("dmap.sessionid").Value;
 }
Beispiel #15
0
 public void WriteResponse(Socket client, ContentNode node)
 {
     WriteResponse (client, HttpStatusCode.OK,
                    ContentWriter.Write (ContentCodeBag.Default, node));
 }
Beispiel #16
0
        private ContentNode GetDatabasesNode()
        {
            ArrayList databaseNodes = new ArrayList ();

            List<Database> dbs = revmgr.GetRevision (revmgr.Current);
            if (dbs != null) {
                foreach (Database db in revmgr.GetRevision (revmgr.Current)) {
                    databaseNodes.Add (db.ToDatabaseNode ());
                }
            }

            ContentNode node = new ContentNode ("daap.serverdatabases",
                                                new ContentNode ("dmap.status", 200),
                                                new ContentNode ("dmap.updatetype", (byte) 0),
                                                new ContentNode ("dmap.specifiedtotalcount", databases.Count),
                                                new ContentNode ("dmap.returnedcount", databases.Count),
                                                new ContentNode ("dmap.listing", databaseNodes));

            return node;
        }
Beispiel #17
0
        internal static void FromPlaylistNode(Database db, ContentNode node, out Track track, out int containerId)
        {
            track = null;
            containerId = 0;

            foreach (ContentNode field in (ContentNode[]) node.Value) {
                switch (field.Name) {
                case "dmap.itemid":
                    track = db.LookupTrackById ((int) field.Value);
                    break;
                case "dmap.containeritemid":
                    containerId = (int) field.Value;
                    break;
                default:
                    break;
                }
            }
        }