From: <kro...@us...> - 2010-11-23 13:07:30
|
Revision: 3968 http://mp-plugins.svn.sourceforge.net/mp-plugins/?rev=3968&view=rev Author: kroko_koenig Date: 2010-11-23 13:07:24 +0000 (Tue, 23 Nov 2010) Log Message: ----------- little bit more comments and structure Modified Paths: -------------- trunk/plugins/AndroidRemote/Server/AndroidRemote/AndroidRemote.csproj trunk/plugins/AndroidRemote/Server/AndroidRemote/AndroidServer.cs trunk/plugins/AndroidRemote/Server/AndroidRemote/Request.cs trunk/plugins/AndroidRemote/Server/AndroidRemote.suo Modified: trunk/plugins/AndroidRemote/Server/AndroidRemote/AndroidRemote.csproj =================================================================== --- trunk/plugins/AndroidRemote/Server/AndroidRemote/AndroidRemote.csproj 2010-11-22 21:52:51 UTC (rev 3967) +++ trunk/plugins/AndroidRemote/Server/AndroidRemote/AndroidRemote.csproj 2010-11-23 13:07:24 UTC (rev 3968) @@ -38,6 +38,7 @@ <Reference Include="System" /> <Reference Include="System.Data" /> <Reference Include="System.Drawing" /> + <Reference Include="System.Web" /> <Reference Include="System.Windows.Forms" /> <Reference Include="System.Xml" /> <Reference Include="Utils, Version=2.2.7.11849, Culture=neutral, processorArchitecture=x86"> Modified: trunk/plugins/AndroidRemote/Server/AndroidRemote/AndroidServer.cs =================================================================== --- trunk/plugins/AndroidRemote/Server/AndroidRemote/AndroidServer.cs 2010-11-22 21:52:51 UTC (rev 3967) +++ trunk/plugins/AndroidRemote/Server/AndroidRemote/AndroidServer.cs 2010-11-23 13:07:24 UTC (rev 3968) @@ -41,8 +41,8 @@ public class AndroidServer : IPlugin, ISetupForm { private TcpListener myListener; - private string server = string.Empty; - private string port = string.Empty; + public static string Server = string.Empty; + public static string Port = string.Empty; public static string PicturePath = string.Empty; public static string MusicPath = string.Empty; @@ -71,8 +71,8 @@ string dir = Config.GetFolder(MediaPortal.Configuration.Config.Dir.Config); using (MediaPortal.Profile.Settings xmlreader = new MediaPortal.Profile.Settings(dir + "\\MediaPortal.xml")) { - server= xmlreader.GetValueAsString("android", "server", "192.168.0.30"); - port = xmlreader.GetValueAsString("android", "port", "8200"); + Server= xmlreader.GetValueAsString("android", "server", "192.168.0.30"); + Port = xmlreader.GetValueAsString("android", "port", "8200"); PicturePath = xmlreader.GetValueAsString("android", "pictures", Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)); @@ -87,17 +87,17 @@ { try { - objServer_Log("Try to init command controler " + server + ":" + port); + objServer_Log("Try to init command controler " + Server + ":" + Port); - myListener = new TcpListener(IPAddress.Parse(server), Convert.ToInt32(port)); + myListener = new TcpListener(IPAddress.Parse(Server), Convert.ToInt32(Port)); myListener.Start(); myListener.BeginAcceptSocket(new AsyncCallback(DoAcceptSocketCallback), myListener); - objServer_Log("Command controler " + server + ":" + port + " is running"); + objServer_Log("Command controler " + Server + ":" + Port + " is running"); } catch { - objServer_Log("Failed to init command controler " + server + ":" + port); + objServer_Log("Failed to init command controler " + Server + ":" + Port); } } private static void DoAcceptSocketCallback(IAsyncResult ar) Modified: trunk/plugins/AndroidRemote/Server/AndroidRemote/Request.cs =================================================================== --- trunk/plugins/AndroidRemote/Server/AndroidRemote/Request.cs 2010-11-22 21:52:51 UTC (rev 3967) +++ trunk/plugins/AndroidRemote/Server/AndroidRemote/Request.cs 2010-11-23 13:07:24 UTC (rev 3968) @@ -32,6 +32,7 @@ using System.IO; using System.Xml; using System.Drawing; +using System.Web; using MediaPortal.GUI.Library; using MediaPortal.Utils; @@ -44,8 +45,9 @@ private byte[] read; private byte[] buffer; - private static Dictionary<string, Bitmap> myList = new Dictionary<string, Bitmap>(); - private static Dictionary<string, Bitmap> myThumbList = new Dictionary<string, Bitmap>(); + private static Dictionary<string, Bitmap> pictureList = new Dictionary<string, Bitmap>(); + private static Dictionary<string, Bitmap> pictureThumbList = new Dictionary<string, Bitmap>(); + private static Dictionary<string, string> musicList = new Dictionary<string, string>(); public Request(Socket Socket) { @@ -83,11 +85,12 @@ else { // GET - int index1 = clientmessage.IndexOf(' '); int index2 = clientmessage.IndexOf(' ', index1 + 1); string req = clientmessage.Substring(index1 + 1, index2 - index1).Trim().ToLower(); + req = HttpUtility.UrlDecode(req); + #region pictures if (req.StartsWith("/pictures")) { @@ -95,12 +98,10 @@ req = req.Replace("/pictures", ""); if (req.StartsWith("/")) req = req.Substring(1); - req = req.Replace("%20", " "); - if (req == "") { // root - SendDir(AndroidServer.PicturePath, "root"); + ReplyPictureDir(AndroidServer.PicturePath, "root"); } else { @@ -109,16 +110,16 @@ if (req.EndsWith(".jpg")) { string orgPath = AndroidServer.PicturePath + "\\" + req; - SendFile(Path.GetFileName(orgPath)); + ReplyPictureFile(Path.GetFileName(orgPath)); } else if (req.EndsWith(".thb")) { string orgPath = AndroidServer.PicturePath + "\\" + req; - SendFileThb(Path.GetFileName(orgPath)); + ReplyPictureThumbFile(Path.GetFileName(orgPath)); } else { - SendDir(AndroidServer.PicturePath + "\\" + req, req); + ReplyPictureDir(AndroidServer.PicturePath + "\\" + req, req); } } } @@ -131,12 +132,10 @@ req = req.Replace("/music", ""); if (req.StartsWith("/")) req = req.Substring(1); - req = req.Replace("%20", " "); - if (req == "") { // root - SendDirM(AndroidServer.MusicPath, "root"); + ReplyMusicDir(AndroidServer.MusicPath, "root"); } else { @@ -145,11 +144,11 @@ if (req.EndsWith(".mp3")) { string orgPath = AndroidServer.MusicPath + "\\" + req; - SendFileM(Path.GetFileName(orgPath)); + ReplyMusicFile(Path.GetFileName(orgPath)); } else { - SendDirM(AndroidServer.MusicPath + "\\" + req, req); + ReplyMusicDir(AndroidServer.MusicPath + "\\" + req, req); } } } @@ -157,7 +156,7 @@ else { - SendErrorPage(req); + SendErrorURL(req); } } @@ -207,176 +206,236 @@ } } - private void SendDir(string dir, string request) + private void ReplyPictureDir(string dir, string request) { if (Directory.Exists(dir)) { - myList.Clear(); - myThumbList.Clear(); + pictureList.Clear(); + pictureThumbList.Clear(); - sendMessage(socket, "HTTP/1.0 200 Ok\r\n"); - sendMessage(socket, "Content-Type: application/xml; charset=utf-8; filename=info.xml" + "\r\n"); - sendMessage(socket, "Proxy-Connection: close" + "\r\n"); - sendMessage(socket, "\r\n"); - - sendMessage(socket, "<?xml version=\"1.0\"?>\r\n"); - sendMessage(socket, "<Directory>\r\n"); - + string msg = string.Empty; + // header + msg += "HTTP/1.0 200 Ok\r\n"; + msg += "Content-Type: application/xml; charset=utf-8; filename=info.xml" + "\r\n"; + msg += "Proxy-Connection: close" + "\r\n"; + msg += "\r\n"; + // content + msg += "<?xml version=\"1.0\"?>\r\n"; + msg += "<Directory>\r\n"; + // folder string[] dirs = Directory.GetDirectories(dir, "*.*", SearchOption.TopDirectoryOnly); foreach (string f in dirs) { - sendMessage(socket, "<Folder>" + Path.GetFileName(f) + "</Folder>\r\n"); + msg += "<Folder>" + Path.GetFileName(f) + "</Folder>\r\n"; } - + //files string[] files = Directory.GetFiles(dir, "*.jpg", SearchOption.TopDirectoryOnly); foreach (string f in files) { string name = Path.GetFileName(f).ToLower(); - Image img = Bitmap.FromFile(f); - - myList.Add(name, (Bitmap)img); - Bitmap thumb = (Bitmap)img.Clone(); - thumb = MediaPortal.Util.BitmapResize.Resize(ref thumb, 85, 85, false, true); - myThumbList.Add(name, thumb); - - sendMessage(socket, "<File>" + name + "</File>\r\n"); + Image img = null; + try + { + img = Bitmap.FromFile(f); + } + catch { } + if (img != null) + { + pictureList.Add(name, (Bitmap)img); + Bitmap thumb = (Bitmap)img.Clone(); + thumb = MediaPortal.Util.BitmapResize.Resize(ref thumb, 85, 85, false, true); + pictureThumbList.Add(name, thumb); + } + msg += "<File>" + name + "</File>\r\n"; } - - sendMessage(socket, "</Directory>\r\n"); + msg += "</Directory>\r\n"; + // send + sendMessage(socket, msg); } else { - SendErrorPage(request); + // dir does not exist + SendErrorURL(request); } } - private void SendFile(string File) + private void ReplyPictureFile(string File) { - if (myThumbList.ContainsKey(File)) + if (pictureThumbList.ContainsKey(File)) { - Bitmap bit = myList[File]; + Bitmap bit = pictureList[File]; byte[] b = BildToByteArray((Image)bit); if (b != null) { - sendMessage(socket, "HTTP/1.0 200 Ok\r\n"); - sendMessage(socket, "Content-Type: image/jpeg" + "\r\n"); - sendMessage(socket, "Content-Length: " + b.Length + "\r\n"); - sendMessage(socket, "Proxy-Connection: close" + "\r\n"); - sendMessage(socket, "\r\n"); - + string msg = string.Empty; + // header + msg += "HTTP/1.0 200 Ok\r\n"; + msg += "Content-Type: image/jpeg" + "\r\n"; + msg += "Content-Length: " + b.Length + "\r\n"; + msg += "Proxy-Connection: close" + "\r\n"; + msg += "\r\n"; + sendMessage(socket, msg); + // content sendBytes(socket, b); } else { - SendErrorPage(File); + SendErrorFile(File); } } else { - SendErrorPage(File); + SendErrorFile(File); } } - private void SendFileThb(string File) + private void ReplyPictureThumbFile(string File) { - File = File.Replace(".thb", ".jpg"); - if (myThumbList.ContainsKey(File)) + /// + /// we might do cache the thumb different later, right now you need to request the dir first + /// + File = File.Replace(".thb", ""); + if (pictureThumbList.ContainsKey(File)) { - Bitmap bit = myThumbList[File]; + Bitmap bit = pictureThumbList[File]; byte[] b = BildToByteArray((Image)bit); if (b != null) { - sendMessage(socket, "HTTP/1.0 200 Ok\r\n"); - sendMessage(socket, "Content-Type: image/jpeg" + "\r\n"); - sendMessage(socket, "Content-Length: " + b.Length + "\r\n"); - sendMessage(socket, "Proxy-Connection: close" + "\r\n"); - sendMessage(socket, "\r\n"); - + string msg = string.Empty; + // header + msg += "HTTP/1.0 200 Ok\r\n"; + msg += "Content-Type: image/jpeg" + "\r\n"; + msg += "Content-Length: " + b.Length + "\r\n"; + msg += "Proxy-Connection: close" + "\r\n"; + msg += "\r\n"; + sendMessage(socket, msg); + // content sendBytes(socket, b); } else { - SendErrorPage(File); + SendErrorFile(File); } } else { - SendErrorPage(File); + SendErrorFile(File); } } - private void SendDirM(string dir, string request) + private void ReplyMusicDir(string dir, string request) { if (Directory.Exists(dir)) { - sendMessage(socket, "HTTP/1.0 200 Ok\r\n"); - sendMessage(socket, "Content-Type: application/xml; charset=utf-8; filename=info.xml" + "\r\n"); - sendMessage(socket, "Proxy-Connection: close" + "\r\n"); - sendMessage(socket, "\r\n"); + musicList.Clear(); - sendMessage(socket, "<?xml version=\"1.0\"?>\r\n"); - sendMessage(socket, "<Directory>\r\n"); + string msg = string.Empty; + // header + msg += "HTTP/1.0 200 Ok\r\n"; + msg += "Content-Type: application/xml; charset=utf-8; filename=info.xml" + "\r\n"; + msg += "Proxy-Connection: close" + "\r\n"; + msg += "\r\n"; + // content + msg += "<?xml version=\"1.0\"?>\r\n"; + msg += "<Directory>\r\n"; string[] dirs = Directory.GetDirectories(dir, "*.*", SearchOption.TopDirectoryOnly); foreach (string f in dirs) { - sendMessage(socket, "<Folder>" + Path.GetFileName(f) + "</Folder>\r\n"); + msg += "<Folder>" + Path.GetFileName(f) + "</Folder>\r\n"; } string[] files = Directory.GetFiles(dir, "*.mp3", SearchOption.TopDirectoryOnly); foreach (string f in files) { string name = Path.GetFileName(f).ToLower(); - sendMessage(socket, "<File>" + name + "</File>\r\n"); + musicList.Add(name, f); + + msg += "<File>" + name + "</File>\r\n"; } - - sendMessage(socket, "</Directory>\r\n"); + msg += "</Directory>\r\n"; + // send + sendMessage(socket, msg); } else { - SendErrorPage(request); + SendErrorURL(request); } } - private void SendFileM(string Filename) + private void ReplyMusicFile(string Filename) { - string dirfFile = AndroidServer.MusicPath + "\\" + Filename; - - if (File.Exists(dirfFile)) + if (musicList.ContainsKey(Filename)) { - byte[] b = FileToByteArray(dirfFile); + string path = musicList[Filename]; - sendMessage(socket, "HTTP/1.0 200 Ok\r\n"); - sendMessage(socket, "Content-Type: audio/mpeg" + "\r\n"); - sendMessage(socket, "Content-Length: " + b.Length + "\r\n"); - sendMessage(socket, "Proxy-Connection: close" + "\r\n"); - sendMessage(socket, "\r\n"); + if (File.Exists(path)) + { + byte[] b = FileToByteArray(path); - sendBytes(socket, b); + string msg = string.Empty; + // header + msg += "HTTP/1.0 200 Ok\r\n"; + msg += "Content-Type: audio/mpeg" + "\r\n"; + msg += "Content-Length: " + b.Length + "\r\n"; + msg += "Proxy-Connection: close" + "\r\n"; + msg += "\r\n"; + sendMessage(socket, msg); + // content + sendBytes(socket, b); + } + else + { + SendErrorFile(Filename); + } } else { - SendErrorPage(Filename); + SendErrorFile(Filename); } } - private void SendErrorPage(string Filename) + private void SendErrorURL(string Url) { - sendMessage(socket, "HTTP/1.0" + " " + - "404" + " " + "Not Found" + "\r\n"); - sendMessage(socket, "Content-Type: text/html; charset=utf-8" + "\r\n"); - sendMessage(socket, "Proxy-Connection: close" + "\r\n"); - sendMessage(socket, "\r\n"); - - sendMessage(socket, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"); - sendMessage(socket, "<html><head>" + "\r\n"); - sendMessage(socket, "<title>404 Not Found</title>" + "\r\n"); - sendMessage(socket, "</head><body>" + "\r\n"); - sendMessage(socket, "<h1>Not Found</h1>" + "\r\n"); - sendMessage(socket, "<p>The requested URL " + Filename + " was not found on this server.</p>" + "\r\n"); - sendMessage(socket, "<hr>" + "\r\n"); - sendMessage(socket, "<address>TeamMediaportal Server at 127.0.0.1 Port 5050</address>" + "\r\n"); - sendMessage(socket, "</body></html>" + "\r\n"); + string msg = string.Empty; + // header + msg += "HTTP/1.0 404 Not Found\r\n"; + msg += "Content-Type: text/html; charset=utf-8" + "\r\n"; + msg += "Proxy-Connection: close" + "\r\n"; + msg += "\r\n"; + // message + msg += "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"; + msg += "<html><head>" + "\r\n"; + msg += "<title>404 Not Found</title>" + "\r\n"; + msg += "</head><body>" + "\r\n"; + msg += "<h1>Not Found</h1>" + "\r\n"; + msg += "<p>The requested URL " + Url + " was not found on this server.</p>" + "\r\n"; + msg += "<hr>" + "\r\n"; + msg += "<address>TeamMediaportal Server at 127.0.0.1 Port 5050</address>" + "\r\n"; + msg += "</body></html>" + "\r\n"; + // send + sendMessage(socket, msg); } + private void SendErrorFile(string File) + { + string msg = string.Empty; + // header + msg += "HTTP/1.0 404 Not Found\r\n"; + msg += "Content-Type: text/html; charset=utf-8" + "\r\n"; + msg += "Proxy-Connection: close" + "\r\n"; + msg += "\r\n"; + // message + msg += "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"; + msg += "<html><head>" + "\r\n"; + msg += "<title>404 Not Found</title>" + "\r\n"; + msg += "</head><body>" + "\r\n"; + msg += "<h1>Not Found</h1>" + "\r\n"; + msg += "<p>The requested file " + File + " was not found on this server.</p>" + "\r\n"; + msg += "<hr>" + "\r\n"; + msg += "<address>TeamMediaportal Server at " + AndroidServer.Server + " Port " + AndroidServer.Port + "</address>" + "\r\n"; + msg += "</body></html>" + "\r\n"; + // send + sendMessage(socket, msg); + } private int readmessage(byte[] ByteArray, ref Socket s, ref String clientmessage) { @@ -385,6 +444,7 @@ clientmessage = (String)messagefromclient; return bytes; } + private void sendMessage(Socket sock, string strMessage) { try @@ -406,7 +466,8 @@ catch { } } - public byte[] FileToByteArray(string _FileName) + + private byte[] FileToByteArray(string _FileName) { byte[] _Buffer = null; @@ -437,7 +498,6 @@ return _Buffer; } - private byte[] BildToByteArray(Image Bild) { MemoryStream IS = new MemoryStream(); Modified: trunk/plugins/AndroidRemote/Server/AndroidRemote.suo =================================================================== (Binary files differ) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |