#!ruby require 'socket' # Network stuff require 'timeout' # Timeout functions class Network def Network::start() # Find configuration variables host = $configFile.getParam('networkConfig', 'listenIP') port = $configFile.getParam('networkConfig', 'listenPort') # Create a socket to listen on and bind it to the host and port begin @socket = UDPSocket::new() @socket.bind(host, port) # Rescue the "Address in use" error rescue Errno::EADDRINUSE errorMsg("Network plugin: Port #{port} on host #{host} is already in use.") return -1 # Rescue the "Address not available' error rescue Errno::EADDRNOTAVAIL errorMsg("Network plugin: Address #{host} is not available to bind.") return -1 # Rescue "permission denied errors rescue Errno::EACCES errorMsg("Network plugin: Access denied when binding interface addresses. Did you try to bind a port under 1024 as a regular user?") return -1 # Rescue all other errors rescue errorMsg("Network plugin: An error occured.") return -1 # Rescue all other errors end # Get all acceptable hosts hosts = $configFile.getParam('networkConfig', 'acceptHosts').split() # Do some nice errorchecking if(hosts.type != Array) errorMsg("Network plugin: No hosts found in networkConfig/acceptHosts. Shutting down plugin.") return -1 # Return the errorcode for plugin failure end # Main loop while(true) # Get data and client data from the client data, info = @socket.recvfrom(256) # Is this an acceptable IP address? if(hosts.include?(info[2]) || hosts.include?(info[3])) command, parameter = data.split(';') # Command actions command.downcase!() case command when 'next' MP3Control::next_song() when 'prev' MP3Control::prev_song() when 'play' MP3Control::play() when 'pause' MP3Control::pause() when 'stop' MP3Control::stop() when 'repeat', 'wrap' if(parameter) # Make sure we actually DO have a parameter if(parameter.downcase() == 'true' || parameter.downcase() == 'on' || parameter.downcase() == 'yes') # Positive? $current_playlist.wrap = true # Do wrap else $current_playlist.wrap = false # Don't wrap end end when 'random', 'shuffle' if(parameter) # Make sure we actually DO have a parameter if(parameter.downcase() == 'true' || parameter.downcase() == 'on' || parameter.downcase() == 'yes') # Positive? $current_playlist.shuffle() else $current_playlist.re_read() end end when 'die', 'terminate', 'quit' errorMsg("Network plugin: Shutting down MaGnuX MP3 Control Server", 0) when 'title' @socket.send($current_playlist.song_info["songname"], 0, info[3], info[1].to_i) when 'filename' @socket.send($current_playlist.song_info["filename"], 0, info[3], info[1].to_i) when 'time' @socket.send(MP3Control::getTime('elapsed'), 0, info[3], info[1].to_i) when 'timeleft' @socket.send(MP3Control::getTime('left'), 0, info[3], info[1].to_i) when 'totaltime' @socket.send(MP3Control::getTime('total'), 0, info[3], info[1].to_i) when 'playlist' # Make sure we actually DO have a parameter if(parameter) playlist = $configFile.getParam(parameter, 'listFile') # Check if the playlist entry exists if(playlist.type != String) errorMsg("The listFile entry for #{parameter} doesn't exist in #{$configFile.filename}! Please choose another playlist.") else # Make sure the playlist file exists and is readable if( !FileTest::readable_real?(playlist) ) errorMsg("The playlist file (#{playlist}) doesn't exist. Please create it.") # Everything seems to be in order. Create a new playlist else foo_playlist = Playlist::create_from_file(playlist) # Do we have a new playlist? if(foo_playlist.type == Playlist) if(foo_playlist.length() > 0) # Successful! Make this our current playlist! $current_playlist = foo_playlist $current_playlist_name = parameter $current_playlist.wrap = true if($configFile.getParam(parameter, 'repeat') == 'true') $current_playlist.shuffle() if($configFile.getParam(parameter, 'random') == 'true') $current_playlist.goto(0) # Rewind the playlist else errorMsg("Playlist #{parameter} (#{playlist}) doesn't contain any entries.") end else errorMsg("Playlist creation from playlist '#{parameter}' (#{playlist}) failed.") end # Successfully created playlist? end # End of "is readable" if-test end # End of "playlist configfile section" if-test end # Do we have a parameter? when 'get_playlist' if(parameter) # If user provided a parameter, send him all playlists available res = Array::new() $configFile.each_section { |section| listFile = $configFile.getParam(section, 'listFile') res << section if(listFile) # There is a value } @socket.send(res.join(' | '), 0, info[3], info[1].to_i) else # Find the playlist name. If it can't be found, return the filename pl = '' $configFile.each_section { |section| listFile = $configFile.getParam(section, 'listFile') pl = section if(listFile == $current_playlist.filename) # There is a value } if(pl != '') @socket.send(pl, 0, info[3], info[1].to_i) else @socket.send($current_playlist.filename, 0, info[3], info[1].to_i) end end when 'find' # Make sure we DO have a parameter if(parameter) # Create variables result = Array::new() lastIndex = 0 parameter, fields = parameter.split(/\|\|/) fields = fields.split(/,/) if(fields) fields = ["filename", "songname"] if(!fields) # Find all songs that match this $current_playlist.find(parameter, true, fields).each { |index| songname = $current_playlist.song_info(index)["songname"] filename = $current_playlist.song_info(index)["filename"] result << "#{songname} [#{filename}] [#{index}]" lastIndex = index } puts("Search finished") if($commandline["--verbose"] == 'on') # More than one song matched? if(result.length() == 1) $current_playlist.goto(lastIndex) MP3Control::play() end if(result.join(' | ').length < 65535) @socket.send(result.join(' | '), 0, info[3], info[1].to_i) else while(result.join(' | ').length > 65535) result = result[0..(result.length-100)] puts("Removing search results...") if($commandline["--verbose"] == 'on') end @socket.send(result.join(' | '), 0, info[3], info[1].to_i) end end # End of the "have parameter" if when '5b' MP3Control::backward(5) when '5f' MP3Control::forward(5) when 'index' if(parameter) # User supplied us with a parameter. Go to that index. $current_playlist.goto(parameter.to_i) else @socket.send($current_playlist.index.to_s, 0, info[3], info[1].to_i) end when 'length' @socket.send($current_playlist.length.to_s, 0, info[3], info[1].to_i) when 'info' if(parameter) # Make sure we actually DO have a parameter res = $current_playlist.song_info(parameter.to_i) if(res.type == Hash) # Make sure there is a result # songname, artist, album, year, comment, tracknum, genre_id, genre @socket.send("#{res["filename"]} | #{res["songname"]} | #{res["artist"]} | #{res["album"]} | #{res["year"]} | #{res["comment"]} | #{res["tracknum"]} | #{res["genre_id"]} | #{res["genre"]}", 0, info[3], info[1].to_i) else @socket.send("Index out of bounds", 0, info[3], info[1].to_i) end # End of result check end # Parameter check end # case command end # Access control if end # while(true) end # Network::start() end