// Player // // Douglas Thrift // // $Id$ #include #include #include #include #include #include #include "Player.hpp" inline void Sleep() { ::timespec sleep = { 1, 0 }; ::nanosleep(&sleep, NULL); } Player::Player(const _R& connection, int player) : state(STOPPED), remote(player), player_(remote.IsRunning() ? NULL : new _H(_B("/usr/X11R6/bin/beep-media-player"))), position(remote.GetPlaylistPosition()), adder(hop::BindAll(&Player::Adder, this)), copier(hop::BindAll(&Player::Copier, this)) { api::Posix::SetSignalHandler(SIGUSR2, hop::BindAll(&Player::Change, this)); if (!player_.IsEmpty()) { player_->ClearReader(); player_->ClearWriter(); } _forall (int, position, 0, remote.GetPlaylistLength()) playlist.InsertLast(MediaFile(connection, remote.GetPlaylistFile(position))); if (remote.IsPlaying()) state = PLAYING; else if (remote.IsPaused()) state = PAUSED; if (!Share::shares.Exists()) Share::shares.CreateDirectory(); Share::shares = Share::shares.GetRealPath(); running = true; } Player::~Player() { running = false; adder.Join(); copier.Join(); if (!player_.IsEmpty()) { remote.Quit(); player_->Join(); } } void Player::Add(const _R& share) { ext::ScopeWriteLock writeLock(sharesLock); shares[share->host][share->share] = share; } int Player::Adder() { while (!running) Sleep(); while (running) { playlistLock.ReadLock(); /*if (remote.GetPlaylistLength() != playlist.GetSize()) { // playlistLock.ReadUnlock(); } else*/ { playlistLock.ReadUnlock(); Sleep(); } } return 0; } int Player::Copier() const { while (!running) Sleep(); api::Pcre::RegEx share_("^(.+)/([0-9a-f]+)/(.+)$"); while (running) _synchronized (queueLock) if (!queue.empty()) { api::Path path(queue.front()); queue.pop(); _desynchronized (queueLock) { _assert(path.GetPath().StartsWithAll(Share::shares.GetPath())); api::Pcre::RegEx::Match match(share_(path.GetPath().Substring(Share::shares.GetSize()))); _assert(match); { api::Path parent(path); ext::Stack stack; while ((parent = parent.GetParent()) != Share::shares) stack.Push(parent); while (stack.Pop(parent)) if (!parent.Exists()) parent.CreateDirectory(); } ext::ScopeReadLock readLock(sharesLock); const _R& share(shares[api::Address::Resolve(match[1], _B("6996")).First()][match[2]]); } } else _desynchronized (queueLock) Sleep(); return 0; } void Player::Change() { position = remote.GetPlaylistPosition(); }