X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=blobdiff_plain;f=cis%2Fcisd.py;h=d286ef7958b4a379c254473e98d7b71cb3e93847;hb=2aecadba1f242a0dcf68614a6aa602282f020b41;hp=6a6c310f4f3f7fcec0912f75fc89b0e6fcaf0dac;hpb=17430bb7ba2fdc1ffcd6e08ffd9dc4f027c7d4fc;p=living-lab-site.git
diff --git a/cis/cisd.py b/cis/cisd.py
index 6a6c310..d286ef7 100755
--- a/cis/cisd.py
+++ b/cis/cisd.py
@@ -2,10 +2,13 @@
import sys
import os
+import fnmatch
import shutil
import time
import threading
from Queue import Queue
+import web
+import json
import config
import bt
@@ -24,26 +27,16 @@ class CIWorker(threading.Thread):
thumbs_dir = 'tmp/thumbs'
torrents_dir = 'tmp/torrents'
- def __init__(self, queue, bit_torrent):
+ def __init__(self, shared, bit_torrent):
"""
Initialize Content Ingestion Worker.
- @param queue a list of dictionaries with the following keys:
-
- - raw_video
- - name: a video name which must be a valid file name
- - transcode_configs: a list of transcode configuration
- dictionaries having the keys as the parameters of
- api.BaseTranscoder.transcode(...)
- - thumbs: string 'random' for extracting a thumbnail
- image from a random video position or a positive integer which
- represents the number of summary thumbnails to be extracted
-
+ @param shared data shared with the front-end (Service)
"""
- threading.Thread.__init__(self)
+ threading.Thread.__init__(self, name='CIWorker')
- self.queue = queue
+ self.shared = shared
self.bit_torrent = bit_torrent
def transfer_in(self, raw_video):
@@ -55,7 +48,7 @@ class CIWorker(threading.Thread):
file_transfer = config.FILE_TRANSFERER_CLASS( \
self.raw_videos_dir, config.INPUT_PATH)
- file_transfer.get(raw_video)
+ file_transfer.get([raw_video])
file_transfer.close()
print '** Transfering in finished.'
@@ -70,24 +63,15 @@ class CIWorker(threading.Thread):
"""
transcoder = config.TRANSCODER_CLASS( \
- input_file = video_name, \
+ input_file = os.path.join(self.raw_videos_dir, input_video), \
name = video_name, prog_bin = config.TRANSCODER_BIN)
transcoder.dest_path = self.transcoded_videos_dir
# Transcode the raw video in each requested format.
# TODO report partial errors
for transcode_config in transcode_configs:
- transcode_config['output_file'] = transcoder.transcode( \
- container = transcode_config['container'], \
- a_codec = transcode_config['a_codec'], \
- a_bitrate = transcode_config['a_bitrate'], \
- a_samplingrate = transcode_config['a_samplingrate'], \
- a_channels = transcode_config['a_channels'], \
- v_codec = transcode_config['v_codec'], \
- v_bitrate = transcode_config['v_bitrate'], \
- v_framerate = transcode_config['v_framerate'], \
- v_resolution = transcode_config['v_resolution'], \
- v_dar = transcode_config['dar'])
+ transcode_config['output_file'] = \
+ transcoder.transcode(**transcode_config)
print '** Transcoding finished.'
@@ -104,7 +88,8 @@ class CIWorker(threading.Thread):
# TODO report partial errors
thumb_extractor = config.THUMB_EXTRACTOR_CLASS( \
- input_file = input_video, name = video_name, \
+ input_file = os.path.join(self.raw_videos_dir, input_video), \
+ name = video_name, \
prog_bin = config.THUMB_EXTRACTOR_BIN)
thumb_extractor.dest_path = self.thumbs_dir
if thumbs == 'random':
@@ -121,7 +106,7 @@ class CIWorker(threading.Thread):
@param transcode_configs a list of dictionaries with format settings
"""
- for transcode_config in transcode_cofigs:
+ for transcode_config in transcode_configs:
# * CREATE TORRENTS FOR EACH TRANSCODED VIDEO
# Create torrent file.
bt.create_torrent(transcode_config['output_file'])
@@ -131,10 +116,13 @@ class CIWorker(threading.Thread):
shutil.move(transcode_config['output_file'] + '.tstream', \
self.torrents_dir)
+ output_file = transcode_config['output_file'] + '.tstream'
+ output_file = output_file[(output_file.rindex('/') + 1):]
+
# * SEED TORRENTS
bit_torrent.start_download( \
- transcode_config['output_file'] + '.tstream',
- self_transcoded_videos_dir)
+ os.path.join(self.torrents_dir, output_file),
+ self.transcoded_videos_dir)
print '** Creating torrents and seeding finished.'
@@ -153,7 +141,7 @@ class CIWorker(threading.Thread):
print '** Creating torrents and seeding finished.'
- def remove_file(self, files, path):
+ def remove_files(self, files, path):
"""
Deletes files from a specified path.
"""
@@ -165,7 +153,7 @@ class CIWorker(threading.Thread):
def run(self):
while True:
- job = self.queue.get()
+ job = self.shared.queue.get()
# * TRANSFER RAW VIDEO IN
self.transfer_in(job['raw_video'])
@@ -186,13 +174,13 @@ class CIWorker(threading.Thread):
files = [f for f in os.listdir(self.torrents_dir) \
if os.path.isfile(os.path.join( \
self.torrents_dir, f))]
- torrent_files = fnmatch.filter(files, name + "_*")
+ torrent_files = fnmatch.filter(files, job['name'] + "_*")
# Thumbnail images files.
files = [f for f in os.listdir(self.thumbs_dir) \
if os.path.isfile(os.path.join( \
self.thumbs_dir, f))]
- thumb_files = fnmatch.filter(files, name + "_*")
+ thumb_files = fnmatch.filter(files, job['name'] + "_*")
# Raw video files.
raw_files = [f for f in os.listdir(self.raw_videos_dir) \
@@ -210,52 +198,95 @@ class CIWorker(threading.Thread):
self.remove_files(thumb_files, self.thumbs_dir)
# * JOB FINISHED
- queue.task_done()
+ self.shared.queue.task_done()
+ print 'load in run is', self.shared.load
+ self.shared.load -= job['weight']
+class Shared:
+ """
+ Shared data between Service (front-end) and CIWorker (back-end).
-if __name__ == '__main__':
- # Jobs queue.
- queue = Queue()
+ @member queue a list of dictionaries with the following keys:
+
+ - raw_video
+ - name: a video name which must be a valid file name
+ - transcode_configs: a list of transcode configuration
+ dictionaries having the keys as the parameters of
+ api.BaseTranscoder.transcode(...)
+ - thumbs: string 'random' for extracting a thumbnail
+ image from a random video position or a positive integer which
+ represents the number of summary thumbnails to be extracted
+
+ @member load total weight of the jobs from the queue
+ """
- # The BitTorrent object implements a NextShare (Tribler) BitTorrent client
- # for seeding, downloading etc.
- bit_torrent = bt.BitTorrent()
+ def __init__(self):
+ # Jobs queue.
+ self.queue = Queue()
+
+ # Sever load.
+ self.load = 0
+
+
+class Service:
+ """
+ Implementation of the RESTful web service which constitutes the interface
+ with the client (web server).
+ """
+
+ def __init__(self):
+ # Shared data with back-end (CIWorker).
+ self.shared = Shared()
+
+ global bit_torrent
- # Worker thread.
- ci_worker = CIWorker(queue, bit_torrent)
- ci_worker.daemon = True
- ci_worker.start()
-
- while True:
- raw_video = sys.stdin.readline().strip()
- if raw_video == 'x':
- break
-
- container = 'webm'
- a_codec = 'vorbis'
- a_bitrate = '128k'
- v_codec = 'vp8'
- v_bitrate = '480k'
- v_resolution = '640x480'
+ # Worker thread.
+ ci_worker = CIWorker(self.shared, bit_torrent)
+ ci_worker.daemon = True
+ ci_worker.start()
- name = raw_video[:raw_video.rindex('.')]
- transcode_config = {
- 'container': container,
- 'a_codec': a_codec,
- 'a_bitrate': a_bitrate,
- 'v_codec': v_codec,
- 'v_bitrate': v_bitrate,
- 'v_resolution': v_resolution
- }
- thumbs = 'random'
-
- job = {
- 'raw_video': raw_video,
- 'name': name,
- 'transcode_configs': [transcode_config],
- 'thumbs': thumbs
- }
-
- queue.put(job)
+ def __del__(self):
+ self.shared.queue.join()
+
+ def GET(self, request):
+ #web.header('Cache-Control', 'no-cache')
+
+ if request == 'get_load':
+ resp = {"load": self.shared.load}
+ print 'load in GET is', self.shared.load
+ web.header('Content-Type', 'application/json')
+ return json.dumps(resp)
+ else:
+ web.badrequest()
+ return ""
+
+
+ def POST(self, request):
+ if request == 'ingest_content':
+ # Read JSON parameters.
+ json_data = web.data()
+ data = json.loads(json_data)
+
+ # Add job weight to CIS load.
+ self.shared.load += data["weight"]
+ print 'load in POST is', self.shared.load
+
+ # Submit job.
+ self.shared.queue.put(data)
+
+ return 'Job submitted.'
+ else:
+ web.badrequest()
+ return ""
+
+
+if __name__ == '__main__':
+ # The BitTorrent object implements a NextShare (Tribler) BitTorrent
+ # client for seeding, downloading etc.
+ global bit_torrent
+ bit_torrent = bt.BitTorrent()
- queue.join()
+ # Web service.
+ urls = ('/(.*)', 'Service')
+ service = web.application(urls, globals())
+ service.run()