public void Reset() { nowPlaying = null; currentMedia = null; opResult = null; httpRequest = null; dataMessageReceived = null; dataToSend = null; messageBytesReceived = 0; }
void NewRequestThread(Object o) { SocketAsyncEventArgs e = (SocketAsyncEventArgs)o; DataHolderToken token = (DataHolderToken)e.UserToken; OpResult opResult = new OpResult(OpStatusCode.BadRequest); string sCommand = ""; string sParam = ""; string sBody = ""; string sTempBody = ""; try { // Show error for index if (token.httpRequest.Length == 0) { sCommand = "<i>No command specified.</i>"; sParam = "<i>No parameters specified.</i>"; } else { string[] req = token.httpRequest.Split(new char[] { '?' }, 2); //Strip off "?" string[] cmd_stack = req[0].Split(new char[] { '/' }); for (int idx = 0; idx < cmd_stack.Length; idx++) { sTempBody = ""; string[] command = cmd_stack[idx].Split(new char[] { ' ' }, 2); if (command.Length == 0) return; sCommand = command[0]; sParam = (command.Length == 2 ? command[1] : string.Empty); if (sCommand.Equals("help", StringComparison.InvariantCultureIgnoreCase)) { opResult = m_remoteCommands.CommandListHTML(AddInModule.GetPortNumber(AddInModule.m_basePortNumber)); } else if (sCommand.Equals("format", StringComparison.InvariantCultureIgnoreCase)) { ICommand formatter = new customCmd(sBody); opResult = formatter.Execute(sParam); sBody = ""; } else if (token.opResult != null) { opResult = token.opResult; } else if (sCommand.Equals("music-list-stats")) { opResult = getLibraryStats(); } else { opResult = m_remoteCommands.Execute(sCommand, sParam, token.nowPlaying, token.currentMedia); } //If cache was cleared, start another build if (sCommand.Equals("music-clear-cache") && opResult.StatusCode == OpStatusCode.Ok) { startCacheBuildNow(); } sTempBody = opResult.ToString(); if (sParam.Length == 0) { sParam = "<i>No parameters specified.</i>"; } if (opResult.StatusCode != OpStatusCode.Json) { if (opResult.StatusCode != OpStatusCode.Ok && opResult.StatusCode != OpStatusCode.Success) { sTempBody = string.Format("<h1>ERROR<hr>Command: {0}<br>Params: {1}<br>Returned: {2} - {3}<hr>See <a href='help'>Help</a></h1>", sCommand, sParam, opResult.StatusCode, opResult.ToString()); } else if (opResult.StatusCode != OpStatusCode.OkImage) { if (sTempBody.Length > 0) { if (sTempBody.TrimStart()[0] != '<') { sTempBody = "<pre>" + sTempBody + "</pre>"; } } else { sTempBody = string.Format("<h1>Ok<hr>Last Command: '{0}'<br>Params: {1}<br>Returned: {2}<hr>See <a href='help'>Help</a></h1>", sCommand, sParam, opResult.StatusCode); } } if (sBody.Length > 0) { sBody += "<HR>"; } sBody += sTempBody; } } } //Get bytes to send to browser if (opResult.StatusCode == OpStatusCode.Json) { token.dataToSend = GetPageJsonDataToSend(opResult.ToString()); } else if (opResult.StatusCode == OpStatusCode.OkImage) { token.dataToSend = GetImageDataToSend(opResult.ToString(), opResult.StatusText); } else { token.dataToSend = GetPageDataToSend(string.Format("{0}\r\n", sBody)); } } catch (Exception ex) { token.dataToSend = GetPageDataToSend(string.Format("<html><body>EXCEPTION: {0}<hr></body></html>", ex.Message)); Trace.TraceError(ex.ToString()); } //Set send operation variables token.sendBytesRemainingCount = token.dataToSend.Length; token.bytesSentAlreadyCount = 0; StartSend(e); }
private OpResult getLibraryStats() { OpResult opResult = new OpResult(); opResult.StatusCode = OpStatusCode.Json; int[] list_codes = new int[] {MusicCmd.LIST_ALBUMS, MusicCmd.LIST_ALBUM_ARTISTS, MusicCmd.LIST_ARTISTS, MusicCmd.LIST_GENRES, MusicCmd.LIST_DETAILS, MusicCmd.LIST_PLAYLISTS}; LibraryStats library_stats = new LibraryStats(); foreach (int i in list_codes) { MusicCmd cmd = new MusicCmd(i); cmd.setStatsOnly(); switch (i) { case MusicCmd.LIST_ALBUMS: library_stats.albums = cmd.Execute("").ResultCount; break; case MusicCmd.LIST_ALBUM_ARTISTS: library_stats.album_artists = cmd.Execute("").ResultCount; break; case MusicCmd.LIST_ARTISTS: library_stats.artists = cmd.Execute("").ResultCount; break; case MusicCmd.LIST_GENRES: library_stats.genres = cmd.Execute("").ResultCount; break; case MusicCmd.LIST_DETAILS: library_stats.songs = cmd.Execute("").ResultCount; break; case MusicCmd.LIST_PLAYLISTS: library_stats.playlists = cmd.Execute("").ResultCount; break; } } opResult.ContentText = JsonConvert.SerializeObject(library_stats, Newtonsoft.Json.Formatting.Indented); return opResult; }
/// <summary> /// Executes a command with the given parameter string and returns a string return /// </summary> /// <param name="command">command name string</param> /// <param name="param">parameter string</param> /// <param name="playlist">now playing playlist, may be null</param> /// <param name="result">string</param> /// <returns></returns> public OpResult Execute(String command, string param, NowPlayingList playlist, MediaItem currentItem) { command = command.ToLower(); if (m_commands.ContainsKey(command)) { try { if (command.Equals("music-cache-status")) { OpResult opResult = new OpResult(); CacheStatus status = new CacheStatus(this.isCacheBuilding); opResult.StatusCode = OpStatusCode.Json; opResult.ContentText = JsonConvert.SerializeObject(status, Newtonsoft.Json.Formatting.Indented); return opResult; } else if (m_commands[command] is MusicICommand) { //Make sure cache is not being modified before executing any of the music-* commands lock (cacheLock) { return ((MusicICommand)m_commands[command]).Execute(param, playlist, currentItem); } } else { return m_commands[command].Execute(param); } } catch (Exception ex) { OpResult opResult = new OpResult(); opResult.StatusCode = OpStatusCode.Exception; opResult.StatusText = ex.Message; opResult.AppendFormat(ex.Message); return opResult; } } else { return new OpResult(OpStatusCode.BadRequest); } }
public OpResult CommandList(int port) { OpResult opResult = new OpResult(OpStatusCode.Ok); if (port != 0) { opResult.AppendFormat("=== Ports: =========="); opResult.AppendFormat("TCP/IP Socket port: {0}", port); opResult.AppendFormat("HTTP Server port: {0} (http://your_server:{1}/)", (port + 10), (port + 10)); } opResult.AppendFormat("=== Connection Commands: =========="); opResult.AppendFormat("help - Shows this page"); opResult.AppendFormat("exit - Closes the socket connection"); foreach (KeyValuePair<string, ICommand> cmd in m_commands) { opResult.AppendFormat("{0} {1}", cmd.Key, (cmd.Value == null) ? "" : cmd.Value.ShowSyntax()); } return opResult; }
public OpResult CommandListHTML(int port) { OpResult opResult = new OpResult(OpStatusCode.Ok); string page_start = "<html><head><script LANGUAGE='JavaScript'>\r\n" + "function toggle (o){ var all=o.childNodes; if (all[0].childNodes[0].innerText == '+') open(o,true); else open(o,false);}\r\n" + "function open (o, b) { var all=o.childNodes; if (b) {all[0].childNodes[0].innerText='-';all[1].style.display='inline';} else {all[0].childNodes[0].innerText='+';all[1].style.display='none';}}\r\n" + "function toggleAll (b){ var all=document.childNodes[0].childNodes[1].childNodes; for (var i=0; i<all.length; i++) {if(all[i].id=='section') open(all[i],b)};}\r\n" + "</script></head>\r\n" + "<body><a id='top'>Jump to</a>: <a href='#commands'>Command List</a>, <a href='#examples'>Notes and Examples</a>, <a href='#bottom'>Bottom</a><hr><font size=+3><a id='commands'>Command List</a>: </font>[<a onclick='toggleAll(true);' >Open All</a>] | [<a onclick='toggleAll(false);' >Collapse All</a>]<hr>\r\n"; string page_end = "</pre></span></div>\r\n" + "<br><hr><b><a id='examples'>Note: URLs must be correctly encoded</a></b><hr><br>\r\n" + "<b>Note - The following custom examples require that:</b><br>\r\n" + " 1 - Custom formats artist_browse and artist_list are defined in the "music.template" file<br>\r\n" + " 2 - The "music.template" file has been copied to the ehome directory (usually C:\\Windows\\ehome)<br>\r\n" + " 3 - Vista Media Center has been restarted after #1 and #2<br>\r\n" + "<br><b>Working track browser using custom formats: (can be slow... but this works as an album browser)</b><br>\r\n" + " Display complete artist list linked to albums: <a href='music-list-details%20template:artist_list'>http://hostname:40510/music-list-details%20template:artist_list</a><br>\r\n" + "<br><b>Examples using artist filter: (warning can be very slow with large libraries)</b><br>\r\n" + " All artists: <a href='music-list-artists'>http://hostname:40510/music-list-artists</a><br>\r\n" + " All albums: <a href='music-list-albums'>http://hostname:40510/music-list-albums</a><br>\r\n" + " All albums by artists starting with the letter "A": <a href='music-list-albums%20artist:a'>http://hostname:40510/music-list-albums%20artist:a</a><br>\r\n" + " Play the tenth and thirteenth song in your collection: <a href='music-play%20indexes:10,13'>http://hostname:40510/music-play%20indexes:10,13</a><br>\r\n" + "<br><b>Examples using custom formats and artist match: (can be slow...)</b><br>\r\n" + " Display pretty albums and tracks by the first artist starting with "Jack": <a href='music-list-details%20template:artist_browse%20artist:jack'>http://hostname:40510/music-list-details%20template:artist_browse%20artist:jack</a><br>\r\n" + "<br><b>More help:</b><br>\r\n" + " Help on the music commands: <a href='music-list-artists%20-help'>http://hostname:40510/music-list-artists%20-help</a><br>\r\n" + "<br><hr>\r\n" + "<a id='bottom'>Generated by</a>: Vista Media Center TCP/IP Controller (<a href='http://www.codeplex.com/VmcController'>vmcController Home</a>)\r\n" + "<hr>Jump to: <a href='#top'>Top</a>, <a href='#commands'>Command List</a>, <a href='#examples'>Notes and Examples</a><br>\r\n" + "<script LANGUAGE='JavaScript'>toggleAll(false);</script></body></html>\r\n"; string header_start = "</pre></span></div><br><div id='section' onclick='toggle(this)' style='border:solid 1px black;'><font size=+1 style='font:15pt courier;'><span>+</span>"; string header_end = "</font><span style='display:'><pre>"; opResult.AppendFormat("{0}", page_start); if (port != 0) { opResult.AppendFormat("{0} Ports: {1}", header_start, header_end); opResult.AppendFormat("TCP/IP Socket port: {0}", port); opResult.AppendFormat("HTTP Server port: {0} (http://your_server:{1}/)", (port + 10), (port + 10)); } opResult.AppendFormat("{0} Connection Commands: {1}", header_start, header_end); opResult.AppendFormat("<a href='/help'>help</a> - Shows this page"); opResult.AppendFormat("<a href='/help'>exit</a> - Closes the socket connection"); foreach (KeyValuePair<string, ICommand> cmd in m_commands) { if (cmd.Key.StartsWith("===")) opResult.AppendFormat(cmd.Key.Replace("==========", header_end).Replace("===", header_start)); else opResult.AppendFormat("<a href='/{0}'>{1}</a> {2}", cmd.Key, cmd.Key, (cmd.Value == null) ? "" : cmd.Value.ShowSyntax().Replace("<", "<").Replace(">", ">")); } opResult.AppendFormat("{0}", page_end); return opResult; }
/// <summary> /// Executes a command with the given parameter string and returns a string return /// </summary> /// <param name="command">command name string</param> /// <param name="param">parameter string</param> /// <param name="result">string</param> /// <returns></returns> public OpResult Execute(String command, string param) { command = command.ToLower(); if (m_commands.ContainsKey(command)) { try { return m_commands[command].Execute(param); } catch (Exception ex) { OpResult opResult = new OpResult(); opResult.StatusCode = OpStatusCode.Exception; opResult.StatusText = ex.Message; opResult.AppendFormat(ex.Message); return opResult; } } else { return new OpResult(OpStatusCode.BadRequest); } }
public OpResult CommandListHTML(int port) { OpResult opResult = new OpResult(OpStatusCode.Ok); string page_start = "<html><head><script LANGUAGE='JavaScript'>\r\n" + "function toggle (o){ var all=o.childNodes; if (all[0].childNodes[0].innerText == '+') open(o,true); else open(o,false);}\r\n" + "function open (o, b) { var all=o.childNodes; if (b) {all[0].childNodes[0].innerText='-';all[1].style.display='inline';} else {all[0].childNodes[0].innerText='+';all[1].style.display='none';}}\r\n" + "function toggleAll (b){ var all=document.childNodes[0].childNodes[1].childNodes; for (var i=0; i<all.length; i++) {if(all[i].id=='section') open(all[i],b)};}\r\n" + "</script></head>\r\n" + "<body><a id='top'>Jump to</a>: <a href='#commands'>Command List</a>, <a href='#examples'>Notes and Examples</a>, <a href='#bottom'>Bottom</a><hr><font size=+3><a id='commands'>Command List</a>: </font>[<a onclick='toggleAll(true);' >Open All</a>] | [<a onclick='toggleAll(false);' >Collapse All</a>]<hr>\r\n"; string page_end = "</pre></span></div>\r\n" + "<br><hr><b><a id='examples'>Note: URLs must be correctly encoded</a></b><hr><br>\r\n" + "<b>Note - The following custom examples require that:</b><br>\r\n" + " 1 - Custom formats artist_browse and artist_list are defined in the "music.template" file<br>\r\n" + " 2 - The "music.template" file has been copied to the ehome directory (usually C:\\Windows\\ehome)<br>\r\n" + " 3 - Vista Media Center has been restarted after #1 and #2<br>\r\n" + "<br><b>Working track browser using custom formats: (can be slow... but this works as an album browser)</b><br>\r\n" + " Display complete artist list linked to albums: <a href='music-list-details%20template:artist_list'>http://hostname:40510/music-list-details%20template:artist_list</a><br>\r\n" + "<br><b>Examples using artist filter: (warning can be very slow with large libraries)</b><br>\r\n" + " All artists: <a href='music-list-artists'>http://hostname:40510/music-list-artists</a><br>\r\n" + " All albums: <a href='music-list-albums'>http://hostname:40510/music-list-albums</a><br>\r\n" + " All albums by artists starting with the letter "A": <a href='music-list-albums%20artist:a'>http://hostname:40510/music-list-albums%20artist:a</a><br>\r\n" + " Play the tenth and thirteenth song in your collection: <a href='music-play%20indexes:10,13'>http://hostname:40510/music-play%20indexes:10,13</a><br>\r\n" + "<br><b>Examples using custom formats and artist match: (can be slow...)</b><br>\r\n" + " Display pretty albums and tracks by the first artist starting with "Jack": <a href='music-list-details%20template:artist_browse%20artist:jack'>http://hostname:40510/music-list-details%20template:artist_browse%20artist:jack</a><br>\r\n" + "<br><b>More help:</b><br>\r\n" + " Help on the music commands: <a href='music-list-artists%20-help'>http://hostname:40510/music-list-artists%20-help</a><br>\r\n" + "<br><hr>\r\n" + "<a id='bottom'>Generated by</a>: Vista Media Center TCP/IP Controller (<a href='http://www.codeplex.com/VmcController'>vmcController Home</a>)\r\n" + "<hr>Jump to: <a href='#top'>Top</a>, <a href='#commands'>Command List</a>, <a href='#examples'>Notes and Examples</a><br>\r\n" + "<script LANGUAGE='JavaScript'>toggleAll(false);</script></body></html>\r\n"; string header_start = "</pre></span></div><br><div id='section' onclick='toggle(this)' style='border:solid 1px black;'><font size=+1 style='font:15pt courier;'><span>+</span>"; string header_end = "</font><span style='display:'><pre>"; opResult.AppendFormat("{0}", page_start); if (port != 0) { opResult.AppendFormat("{0} Ports: {1}", header_start, header_end); opResult.AppendFormat("TCP/IP Socket port: {0}", port); opResult.AppendFormat("HTTP Server port: {0} (http://your_server:{1}/)", (port + 10), (port + 10)); } opResult.AppendFormat("{0} Connection Commands: {1}", header_start, header_end); opResult.AppendFormat("<a href='/help'>help</a> - Shows this page"); opResult.AppendFormat("<a href='/help'>exit</a> - Closes the socket connection"); foreach (KeyValuePair <string, ICommand> cmd in m_commands) { if (cmd.Key.StartsWith("===")) { opResult.AppendFormat(cmd.Key.Replace("==========", header_end).Replace("===", header_start)); } else { opResult.AppendFormat("<a href='/{0}'>{1}</a> {2}", cmd.Key, cmd.Key, (cmd.Value == null) ? "" : cmd.Value.ShowSyntax().Replace("<", "<").Replace(">", ">")); } } opResult.AppendFormat("{0}", page_end); return(opResult); }
/// <summary> /// Handles the received commands of the m_socketServer control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="args">The <see cref="SocketServer.SocketEventArgs"/> instance containing the event data.</param> void m_socketServer_NewMessage(object sender, SocketEventArgs e) { OpResult opResult = new OpResult(OpStatusCode.BadRequest); try { if (e.Message.Length == 0) return; if (e.Message.Equals("help", StringComparison.InvariantCultureIgnoreCase)) { opResult = m_remoteCommands.CommandList(GetPortNumber(m_basePortNumber)); } else if (e.Message.Equals("exit", StringComparison.InvariantCultureIgnoreCase)) { m_socketServer.CloseClient(e.TcpClient); return; } else { string[] command = e.Message.Split(new char[] { ' ' }, 2); if (command.Length == 0) return; opResult = m_remoteCommands.Execute(command[0], (command.Length == 2 ? command[1] : string.Empty)); } m_socketServer.SendMessage(string.Format("{0} {1}\r\n", (int)opResult.StatusCode, opResult.StatusText), e.TcpClient); if (opResult.StatusCode == OpStatusCode.Ok) { m_socketServer.SendMessage(opResult.ToString(), e.TcpClient); m_socketServer.SendMessage(".\r\n", e.TcpClient); } } catch (Exception ex) { Trace.TraceError(ex.ToString()); } }
void m_httpServer_NewRequest_thread(HttpEventArgs e) { OpResult opResult = new OpResult(OpStatusCode.BadRequest); string sCommand = ""; string sParam = ""; string sBody = ""; string sTempBody = ""; try { // Show error for index if (e.Request.Length == 0) { sCommand = "<i>No command specified.</i>"; sParam = "<i>No parameters specified.</i>"; } else { string[] req = e.Request.Split(new char[] { '?' }, 2); //Strip off "?" string[] cmd_stack = req[0].Split(new char[] { '/' }); for (int idx = 0; idx < cmd_stack.Length; idx++) { sTempBody = ""; string[] command = cmd_stack[idx].Split(new char[] { ' ' }, 2); if (command.Length == 0) return; sCommand = command[0]; sParam = (command.Length == 2 ? command[1] : string.Empty); if (sCommand.Equals("help", StringComparison.InvariantCultureIgnoreCase)) opResult = m_remoteCommands.CommandListHTML(GetPortNumber(m_basePortNumber)); else if (sCommand.Equals("format", StringComparison.InvariantCultureIgnoreCase)) { ICommand formatter = new customCmd(sBody); opResult = formatter.Execute(sParam); sBody = ""; } else opResult = m_remoteCommands.Execute(sCommand, sParam); sTempBody = opResult.ToString(); if (sParam.Length == 0) sParam = "<i>No parameters specified.</i>"; if (opResult.StatusCode != OpStatusCode.Ok && opResult.StatusCode != OpStatusCode.Success) { sTempBody = string.Format("<h1>ERROR<hr>Command: {0}<br>Params: {1}<br>Returned: {2} - {3}<hr>See <a href='help'>Help</a></h1>", sCommand, sParam, opResult.StatusCode, opResult.ToString()); //if (sBody.Length > 0) sBody += "<HR>"; //sBody += sTempBody; //break; } else if (opResult.StatusCode != OpStatusCode.OkImage) { if (sTempBody.Length > 0) { if (sTempBody.TrimStart()[0] != '<') sTempBody = "<pre>" + sTempBody + "</pre>"; } else { sTempBody = string.Format("<h1>Ok<hr>Last Command: '{0}'<br>Params: {1}<br>Returned: {2}<hr>See <a href='help'>Help</a></h1>", sCommand, sParam, opResult.StatusCode); } //if (sBody.Length > 0) sBody += "<HR>"; //sBody += sTempBody; } if (sBody.Length > 0) sBody += "<HR>"; sBody += sTempBody; } } if (opResult.StatusCode == OpStatusCode.OkImage) m_httpServer.SendImage(opResult.ToString(), opResult.StatusText, e.HttpSocket); else m_httpServer.SendPage(string.Format("{0}\r\n", sBody), e.HttpSocket); } catch (Exception ex) { m_httpServer.SendPage(string.Format("<html><body>EXCEPTION: {0}<hr></body></html>", ex.Message), e.HttpSocket); Trace.TraceError(ex.ToString()); } }
void m_httpServer_NewRequest_thread(HttpEventArgs e) { OpResult opResult = new OpResult(OpStatusCode.BadRequest); string sCommand = ""; string sParam = ""; string sBody = ""; string sTempBody = ""; try { // Show error for index if (e.Request.Length == 0) { sCommand = "<i>No command specified.</i>"; sParam = "<i>No parameters specified.</i>"; } else { string[] req = e.Request.Split(new char[] { '?' }, 2); //Strip off "?" string[] cmd_stack = req[0].Split(new char[] { '/' }); for (int idx = 0; idx < cmd_stack.Length; idx++) { sTempBody = ""; string[] command = cmd_stack[idx].Split(new char[] { ' ' }, 2); if (command.Length == 0) { return; } sCommand = command[0]; sParam = (command.Length == 2 ? command[1] : string.Empty); if (sCommand.Equals("help", StringComparison.InvariantCultureIgnoreCase)) { opResult = m_remoteCommands.CommandListHTML(GetPortNumber(m_basePortNumber)); } else if (sCommand.Equals("format", StringComparison.InvariantCultureIgnoreCase)) { ICommand formatter = new customCmd(sBody); opResult = formatter.Execute(sParam); sBody = ""; } else { opResult = m_remoteCommands.Execute(sCommand, sParam); } sTempBody = opResult.ToString(); if (sParam.Length == 0) { sParam = "<i>No parameters specified.</i>"; } if (opResult.StatusCode != OpStatusCode.Ok && opResult.StatusCode != OpStatusCode.Success) { sTempBody = string.Format("<h1>ERROR<hr>Command: {0}<br>Params: {1}<br>Returned: {2} - {3}<hr>See <a href='help'>Help</a></h1>", sCommand, sParam, opResult.StatusCode, opResult.ToString()); //if (sBody.Length > 0) sBody += "<HR>"; //sBody += sTempBody; //break; } else if (opResult.StatusCode != OpStatusCode.OkImage) { if (sTempBody.Length > 0) { if (sTempBody.TrimStart()[0] != '<') { sTempBody = "<pre>" + sTempBody + "</pre>"; } } else { sTempBody = string.Format("<h1>Ok<hr>Last Command: '{0}'<br>Params: {1}<br>Returned: {2}<hr>See <a href='help'>Help</a></h1>", sCommand, sParam, opResult.StatusCode); } //if (sBody.Length > 0) sBody += "<HR>"; //sBody += sTempBody; } if (sBody.Length > 0) { sBody += "<HR>"; } sBody += sTempBody; } } if (opResult.StatusCode == OpStatusCode.OkImage) { m_httpServer.SendImage(opResult.ToString(), opResult.StatusText, e.HttpSocket); } else { m_httpServer.SendPage(string.Format("{0}\r\n", sBody), e.HttpSocket); } } catch (Exception ex) { m_httpServer.SendPage(string.Format("<html><body>EXCEPTION: {0}<hr></body></html>", ex.Message), e.HttpSocket); Trace.TraceError(ex.ToString()); } }