Red5 フリーソフトのFlashストリーミングサーバ ~開発クライアントの構築 その4~
(3)「demoservice.py」の編集
「demoservice.py」も「demoservice.js」と同様に、red5アプリケーションの「oflaDemo2」を動作するための
サービス用のクラスファイルとなります。
ファイルはPythonで構成されています。
変更する箇所は、15行目・28行目となります。
- 変更前
""" demoservice.py - a translation into Python of the ofla demo application, a Red5 example. @author The Red5 Project (red5@osflash.org) @author Joachim Bauch (jojo@struktur.de) """ from java.io import File from java.lang import System from java.text import SimpleDateFormat from java.util import Date from java.util import HashMap from org.red5.server.api import Red5 from org.red5.server.webapp.oflaDemo import IDemoService class DemoService(IDemoService): def getListOfAvailableFLVs(self): """Return list of .flv files that can be streamed.""" scope = Red5.getConnectionLocal().getScope() serverRoot = System.getProperty('red5.root') filesMap = HashMap() try: print 'Getting the FLV files' flvs = scope.getResources("streams/*.flv") for file in flvs: fso = File(serverRoot + '/webapps/oflaDemo' + file.path) flvName = fso.getName() flvBytes = 0 if hasattr(fso, 'length'): flvBytes = fso.length() else: print 'Length not found' lastMod = '0' if hasattr(fso, 'lastModified'): lastMod = self.formatDate(Date(fso.lastModified())) else: log.debug('Last modified not found') print 'FLV Name:', flvName print 'Last modified date:', lastMod print 'Size:', flvBytes print '-------' fileInfo = HashMap(3); fileInfo["name"] = flvName fileInfo["lastModified"] = lastMod fileInfo["size"] = flvBytes filesMap[flvName] = fileInfo except Exception, e: print 'Error in getListOfAvailableFLVs:', e return filesMap; def formatDate(self, date): return SimpleDateFormat("dd/MM/yyyy hh:mm:ss").format(date) def getInstance(*args): print 'Arguments:', args return DemoService()
from java.util import Date from java.util import HashMap from org.red5.server.api import Red5 from org.red5.server.webapp.oflaDemo2 import IDemoService class DemoService(IDemoService): def getListOfAvailableFLVs(self):
try: print 'Getting the FLV files' flvs = scope.getResources("streams/*.flv") for file in flvs: fso = File(serverRoot + '/webapps/oflaDemo2' + file.path) flvName = fso.getName() flvBytes = 0 if hasattr(fso, 'length'): flvBytes = fso.length() else: print 'Length not found'
次に「demoservice.rb」について見ていきます。
(4)「demoservice.rb」の編集
「demoservice.rb」も「demoservice.js」と同様に、red5アプリケーションの「oflaDemo2」を動作するための
サービス用のクラスファイルとなります。
ファイルはJRubyで構成されています。
変更する箇所は、5行目・33行目となります。
- 変更前
# JRuby - style require 'java' module RedFive include_package "org.springframework.core.io" include_package "org.red5.server.webapp.oflaDemo" end include_class "org.red5.server.api.Red5" include_class "java.util.HashMap" # # demoservice.rb - a translation into Ruby of the ofla demo application, a red5 example. # # @author Paul Gregoire # class DemoService < RedFive::DemoServiceImpl attr_reader :filesMap attr_writer :filesMap def initialize puts "Initializing ruby demoservice" super @filesMap = HashMap.new end def getListOfAvailableFLVs puts "Getting the FLV files" begin # puts "R5 con local: #{Red5::getConnectionLocal}" # puts "Scope: #{Red5::getConnectionLocal.getScope}" # puts "Root path: #{File.expand_path('/')}" # puts "Current path: #{File.expand_path('webapps/oflaDemo/')}" dirname = File.expand_path('webapps/oflaDemo/streams').to_s Dir.open(dirname).entries.grep(/.flv$/) do |dir| dir.each do |flvName| fileInfo = HashMap.new stats = File.stat(dirname+'/'+flvName) fileInfo["name"] = flvName fileInfo["lastModified"] = stats.mtime fileInfo["size"] = stats.size || 0 @filesMap[flvName] = fileInfo print 'FLV Name:', flvName print 'Last modified date:', stats.mtime print 'Size:', stats.size || 0 print '-------' end end rescue Exception => ex puts "Error in getListOfAvailableFLVs #{errorType} n" puts "Exception: #{ex} n" puts caller.join("n"); end return filesMap end def formatDate(date) return date.strftime("%d/%m/%Y %I:%M:%S") end def method_missing(m, *args) super unless @value.respond_to?(m) return @value.send(m, *args) end end
# JRuby - style require 'java' module RedFive include_package "org.springframework.core.io" include_package "org.red5.server.webapp.oflaDemo2" end include_class "org.red5.server.api.Red5" include_class "java.util.HashMap"
# puts "R5 con local: #{Red5::getConnectionLocal}" # puts "Scope: #{Red5::getConnectionLocal.getScope}" # puts "Root path: #{File.expand_path('/')}" # puts "Current path: #{File.expand_path('webapps/oflaDemo/')}" dirname = File.expand_path('webapps/oflaDemo2/streams').to_s Dir.open(dirname).entries.grep(/.flv$/) do |dir| dir.each do |flvName| fileInfo = HashMap.new stats = File.stat(dirname+'/'+flvName)
次に「main.groovy」・「main.js」・「main.py」・「main.rb」について見ていきます。