<commit_before>// Copyright 2017 The Ray Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <memory>

#include "gtest/gtest.h"
#include "ray/common/test_util.h"
#include "ray/gcs/pubsub/gcs_pub_sub.h"

namespace ray {

class GcsPubSubTest : public ::testing::Test {
 public:
  GcsPubSubTest() { TestSetupUtil::StartUpRedisServers(std::vector<int>()); }

  virtual ~GcsPubSubTest() { TestSetupUtil::ShutDownRedisServers(); }

 protected:
  virtual void SetUp() override {
    thread_io_service_.reset(new std::thread([this] {
      std::unique_ptr<boost::asio::io_service::work> work(
          new boost::asio::io_service::work(io_service_));
      io_service_.run();
    }));

    gcs::RedisClientOptions redis_client_options(
        "127.0.0.1", TEST_REDIS_SERVER_PORTS.front(), "", true);
    client_ = std::make_shared<gcs::RedisClient>(redis_client_options);
    RAY_CHECK_OK(client_->Connect(io_service_));
    pub_sub_ = std::make_shared<gcs::GcsPubSub>(client_);
  }

  virtual void TearDown() override {
    client_->Disconnect();
    io_service_.stop();
    thread_io_service_->join();
    thread_io_service_.reset();
    pub_sub_.reset();

    // Note: If we immediately reset client_ after io_service_ stop, because client_ still
    // has thread executing logic, such as unsubscribe's callback, the problem of heap
    // used after free will occur.
    client_.reset();
  }

  void Subscribe(const std::string &channel, const std::string &id,
                 std::vector<std::string> &result) {
    std::promise<bool> promise;
    auto done = [&promise](const Status &status) { promise.set_value(status.ok()); };
    auto subscribe = [&result](const std::string &id, const std::string &data) {
      result.push_back(data);
    };
    RAY_CHECK_OK((pub_sub_->Subscribe(channel, id, subscribe, done)));
    WaitReady(promise.get_future(), timeout_ms_);
  }

  void SubscribeAll(const std::string &channel,
                    std::vector<std::pair<std::string, std::string>> &result) {
    std::promise<bool> promise;
    auto done = [&promise](const Status &status) { promise.set_value(status.ok()); };
    auto subscribe = [&result](const std::string &id, const std::string &data) {
      result.push_back(std::make_pair(id, data));
    };
    RAY_CHECK_OK((pub_sub_->SubscribeAll(channel, subscribe, done)));
    WaitReady(promise.get_future(), timeout_ms_);
  }

  bool Unsubscribe(const std::string &channel, const std::string &id) {
    return pub_sub_->Unsubscribe(channel, id).ok();
  }

  bool Publish(const std::string &channel, const std::string &id,
               const std::string &data) {
    std::promise<bool> promise;
    auto done = [&promise](const Status &status) { promise.set_value(status.ok()); };
    RAY_CHECK_OK((pub_sub_->Publish(channel, id, data, done)));
    return WaitReady(promise.get_future(), timeout_ms_);
  }

  bool WaitReady(std::future<bool> future, const std::chrono::milliseconds &timeout_ms) {
    auto status = future.wait_for(timeout_ms);
    return status == std::future_status::ready && future.get();
  }

  template <typename Data>
  void WaitPendingDone(const std::vector<Data> &data, int expected_count) {
    auto condition = [&data, expected_count]() {
      RAY_CHECK((int)data.size() <= expected_count)
          << "Expected " << expected_count << " data " << data.size();
      return (int)data.size() == expected_count;
    };
    EXPECT_TRUE(WaitForCondition(condition, timeout_ms_.count()));
  }

  std::shared_ptr<gcs::RedisClient> client_;
  const std::chrono::milliseconds timeout_ms_{60000};
  std::shared_ptr<gcs::GcsPubSub> pub_sub_;

 private:
  boost::asio::io_service io_service_;
  std::unique_ptr<std::thread> thread_io_service_;
};

TEST_F(GcsPubSubTest, TestPubSubApi) {
  std::string channel("channel");
  std::string id("id");
  std::string data("data");
  std::vector<std::pair<std::string, std::string>> all_result;
  SubscribeAll(channel, all_result);
  std::vector<std::string> result;
  Subscribe(channel, id, result);
  Publish(channel, id, data);

  WaitPendingDone(result, 1);
  WaitPendingDone(all_result, 1);
  Unsubscribe(channel, id);
  Publish(channel, id, data);
  std::this_thread::sleep_for(std::chrono::milliseconds(100));
  EXPECT_EQ(result.size(), 1);

  Subscribe(channel, id, result);
  Publish(channel, id, data);
  WaitPendingDone(result, 2);
  WaitPendingDone(all_result, 3);
}

TEST_F(GcsPubSubTest, TestManyPubsub) {
  std::string channel("channel");
  std::string id("id");
  std::string data("data");
  std::vector<std::pair<std::string, std::string>> all_result;
  SubscribeAll(channel, all_result);
  // Test many concurrent subscribes and unsubscribes.
  for (int i = 0; i < 1000; i++) {
    auto subscribe = [](const std::string &id, const std::string &data) {};
    RAY_CHECK_OK((pub_sub_->Subscribe(channel, id, subscribe, nullptr)));
    RAY_CHECK_OK((pub_sub_->Unsubscribe(channel, id)));
  }
  for (int i = 0; i < 1000; i++) {
    std::vector<std::string> result;
    // Use the synchronous subscribe to make sure our SUBSCRIBE message reaches
    // Redis before the PUBLISH.
    Subscribe(channel, id, result);
    Publish(channel, id, data);

    WaitPendingDone(result, 1);
    WaitPendingDone(all_result, i + 1);
    RAY_CHECK_OK((pub_sub_->Unsubscribe(channel, id)));
  }
}

TEST_F(GcsPubSubTest, TestMultithreading) {
  std::string channel("channel");
  auto sub_message_count = std::make_shared<std::atomic<int>>(0);
  auto sub_finished_count = std::make_shared<std::atomic<int>>(0);
  int size = 5;
  std::vector<std::unique_ptr<std::thread>> threads;
  threads.resize(size);
  for (int index = 0; index < size; ++index) {
    std::stringstream ss;
    ss << index;
    auto id = ss.str();
    threads[index].reset(
        new std::thread([this, sub_message_count, sub_finished_count, id, channel] {
          auto subscribe = [sub_message_count](const std::string &id,
                                               const std::string &data) {
            ++(*sub_message_count);
          };
          auto on_done = [sub_finished_count](const Status &status) {
            RAY_CHECK_OK(status);
            ++(*sub_finished_count);
          };
          RAY_CHECK_OK(pub_sub_->Subscribe(channel, id, subscribe, on_done));
        }));
  }
  auto sub_finished_condition = [sub_finished_count, size]() {
    return sub_finished_count->load() == size;
  };
  EXPECT_TRUE(WaitForCondition(sub_finished_condition, timeout_ms_.count()));
  for (auto &thread : threads) {
    thread->join();
    thread.reset();
  }

  std::string data("data");
  for (int index = 0; index < size; ++index) {
    std::stringstream ss;
    ss << index;
    auto id = ss.str();
    threads[index].reset(new std::thread([this, channel, id, data] {
      RAY_CHECK_OK(pub_sub_->Publish(channel, id, data, nullptr));
    }));
  }

  auto sub_message_condition = [sub_message_count, size]() {
    return sub_message_count->load() == size;
  };
  EXPECT_TRUE(WaitForCondition(sub_message_condition, timeout_ms_.count()));
  for (auto &thread : threads) {
    thread->join();
    thread.reset();
  }
}

TEST_F(GcsPubSubTest, TestPubSubWithTableData) {
  std::string channel("channel");
  std::string data("data");
  std::vector<std::string> result;
  int size = 1000;

  for (int index = 0; index < size; ++index) {
    ObjectID object_id = ObjectID::FromRandom();
    std::promise<bool> promise;
    auto done = [&promise](const Status &status) { promise.set_value(status.ok()); };
    auto subscribe = [this, channel, &result](const std::string &id,
                                              const std::string &data) {
      RAY_CHECK_OK(pub_sub_->Unsubscribe(channel, id));
      result.push_back(data);
    };
    RAY_CHECK_OK((pub_sub_->Subscribe(channel, object_id.Hex(), subscribe, done)));
    WaitReady(promise.get_future(), timeout_ms_);
    RAY_CHECK_OK((pub_sub_->Publish(channel, object_id.Hex(), data, nullptr)));
  }

  WaitPendingDone(result, size);
}

}  // namespace ray

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  RAY_CHECK(argc == 4);
  ray::TEST_REDIS_SERVER_EXEC_PATH = argv[1];
  ray::TEST_REDIS_CLIENT_EXEC_PATH = argv[2];
  ray::TEST_REDIS_MODULE_LIBRARY_PATH = argv[3];
  return RUN_ALL_TESTS();
}
<commit_msg>[Tests]lock vector to avoid potential flaky test (#9656)<commit_after>// Copyright 2017 The Ray Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <memory>

#include "gtest/gtest.h"
#include "ray/common/test_util.h"
#include "ray/gcs/pubsub/gcs_pub_sub.h"

namespace ray {

class GcsPubSubTest : public ::testing::Test {
 public:
  GcsPubSubTest() { TestSetupUtil::StartUpRedisServers(std::vector<int>()); }

  virtual ~GcsPubSubTest() { TestSetupUtil::ShutDownRedisServers(); }

 protected:
  virtual void SetUp() override {
    thread_io_service_.reset(new std::thread([this] {
      std::unique_ptr<boost::asio::io_service::work> work(
          new boost::asio::io_service::work(io_service_));
      io_service_.run();
    }));

    gcs::RedisClientOptions redis_client_options(
        "127.0.0.1", TEST_REDIS_SERVER_PORTS.front(), "", true);
    client_ = std::make_shared<gcs::RedisClient>(redis_client_options);
    RAY_CHECK_OK(client_->Connect(io_service_));
    pub_sub_ = std::make_shared<gcs::GcsPubSub>(client_);
  }

  virtual void TearDown() override {
    client_->Disconnect();
    io_service_.stop();
    thread_io_service_->join();
    thread_io_service_.reset();
    pub_sub_.reset();

    // Note: If we immediately reset client_ after io_service_ stop, because client_ still
    // has thread executing logic, such as unsubscribe's callback, the problem of heap
    // used after free will occur.
    client_.reset();
  }

  void Subscribe(const std::string &channel, const std::string &id,
                 std::vector<std::string> &result) {
    std::promise<bool> promise;
    auto done = [&promise](const Status &status) { promise.set_value(status.ok()); };
    auto subscribe = [this, &result](const std::string &id, const std::string &data) {
      absl::MutexLock lock(&vector_mutex_);
      result.push_back(data);
    };
    RAY_CHECK_OK((pub_sub_->Subscribe(channel, id, subscribe, done)));
    WaitReady(promise.get_future(), timeout_ms_);
  }

  void SubscribeAll(const std::string &channel,
                    std::vector<std::pair<std::string, std::string>> &result) {
    std::promise<bool> promise;
    auto done = [&promise](const Status &status) { promise.set_value(status.ok()); };
    auto subscribe = [this, &result](const std::string &id, const std::string &data) {
      absl::MutexLock lock(&vector_mutex_);
      result.push_back(std::make_pair(id, data));
    };
    RAY_CHECK_OK((pub_sub_->SubscribeAll(channel, subscribe, done)));
    WaitReady(promise.get_future(), timeout_ms_);
  }

  bool Unsubscribe(const std::string &channel, const std::string &id) {
    return pub_sub_->Unsubscribe(channel, id).ok();
  }

  bool Publish(const std::string &channel, const std::string &id,
               const std::string &data) {
    std::promise<bool> promise;
    auto done = [&promise](const Status &status) { promise.set_value(status.ok()); };
    RAY_CHECK_OK((pub_sub_->Publish(channel, id, data, done)));
    return WaitReady(promise.get_future(), timeout_ms_);
  }

  bool WaitReady(std::future<bool> future, const std::chrono::milliseconds &timeout_ms) {
    auto status = future.wait_for(timeout_ms);
    return status == std::future_status::ready && future.get();
  }

  template <typename Data>
  void WaitPendingDone(const std::vector<Data> &data, int expected_count) {
    auto condition = [this, &data, expected_count]() {
      absl::MutexLock lock(&vector_mutex_);
      RAY_CHECK((int)data.size() <= expected_count)
          << "Expected " << expected_count << " data " << data.size();
      return (int)data.size() == expected_count;
    };
    EXPECT_TRUE(WaitForCondition(condition, timeout_ms_.count()));
  }

  std::shared_ptr<gcs::RedisClient> client_;
  const std::chrono::milliseconds timeout_ms_{60000};
  std::shared_ptr<gcs::GcsPubSub> pub_sub_;
  absl::Mutex vector_mutex_;

 private:
  boost::asio::io_service io_service_;
  std::unique_ptr<std::thread> thread_io_service_;
};

TEST_F(GcsPubSubTest, TestPubSubApi) {
  std::string channel("channel");
  std::string id("id");
  std::string data("data");
  std::vector<std::pair<std::string, std::string>> all_result;
  SubscribeAll(channel, all_result);
  std::vector<std::string> result;
  Subscribe(channel, id, result);
  Publish(channel, id, data);

  WaitPendingDone(result, 1);
  WaitPendingDone(all_result, 1);
  Unsubscribe(channel, id);
  Publish(channel, id, data);
  std::this_thread::sleep_for(std::chrono::milliseconds(100));
  EXPECT_EQ(result.size(), 1);

  Subscribe(channel, id, result);
  Publish(channel, id, data);
  WaitPendingDone(result, 2);
  WaitPendingDone(all_result, 3);
}

TEST_F(GcsPubSubTest, TestManyPubsub) {
  std::string channel("channel");
  std::string id("id");
  std::string data("data");
  std::vector<std::pair<std::string, std::string>> all_result;
  SubscribeAll(channel, all_result);
  // Test many concurrent subscribes and unsubscribes.
  for (int i = 0; i < 1000; i++) {
    auto subscribe = [](const std::string &id, const std::string &data) {};
    RAY_CHECK_OK((pub_sub_->Subscribe(channel, id, subscribe, nullptr)));
    RAY_CHECK_OK((pub_sub_->Unsubscribe(channel, id)));
  }
  for (int i = 0; i < 1000; i++) {
    std::vector<std::string> result;
    // Use the synchronous subscribe to make sure our SUBSCRIBE message reaches
    // Redis before the PUBLISH.
    Subscribe(channel, id, result);
    Publish(channel, id, data);

    WaitPendingDone(result, 1);
    WaitPendingDone(all_result, i + 1);
    RAY_CHECK_OK((pub_sub_->Unsubscribe(channel, id)));
  }
}

TEST_F(GcsPubSubTest, TestMultithreading) {
  std::string channel("channel");
  auto sub_message_count = std::make_shared<std::atomic<int>>(0);
  auto sub_finished_count = std::make_shared<std::atomic<int>>(0);
  int size = 5;
  std::vector<std::unique_ptr<std::thread>> threads;
  threads.resize(size);
  for (int index = 0; index < size; ++index) {
    std::stringstream ss;
    ss << index;
    auto id = ss.str();
    threads[index].reset(
        new std::thread([this, sub_message_count, sub_finished_count, id, channel] {
          auto subscribe = [sub_message_count](const std::string &id,
                                               const std::string &data) {
            ++(*sub_message_count);
          };
          auto on_done = [sub_finished_count](const Status &status) {
            RAY_CHECK_OK(status);
            ++(*sub_finished_count);
          };
          RAY_CHECK_OK(pub_sub_->Subscribe(channel, id, subscribe, on_done));
        }));
  }
  auto sub_finished_condition = [sub_finished_count, size]() {
    return sub_finished_count->load() == size;
  };
  EXPECT_TRUE(WaitForCondition(sub_finished_condition, timeout_ms_.count()));
  for (auto &thread : threads) {
    thread->join();
    thread.reset();
  }

  std::string data("data");
  for (int index = 0; index < size; ++index) {
    std::stringstream ss;
    ss << index;
    auto id = ss.str();
    threads[index].reset(new std::thread([this, channel, id, data] {
      RAY_CHECK_OK(pub_sub_->Publish(channel, id, data, nullptr));
    }));
  }

  auto sub_message_condition = [sub_message_count, size]() {
    return sub_message_count->load() == size;
  };
  EXPECT_TRUE(WaitForCondition(sub_message_condition, timeout_ms_.count()));
  for (auto &thread : threads) {
    thread->join();
    thread.reset();
  }
}

TEST_F(GcsPubSubTest, TestPubSubWithTableData) {
  std::string channel("channel");
  std::string data("data");
  std::vector<std::string> result;
  int size = 1000;

  for (int index = 0; index < size; ++index) {
    ObjectID object_id = ObjectID::FromRandom();
    std::promise<bool> promise;
    auto done = [&promise](const Status &status) { promise.set_value(status.ok()); };
    auto subscribe = [this, channel, &result](const std::string &id,
                                              const std::string &data) {
      RAY_CHECK_OK(pub_sub_->Unsubscribe(channel, id));
      absl::MutexLock lock(&vector_mutex_);
      result.push_back(data);
    };
    RAY_CHECK_OK((pub_sub_->Subscribe(channel, object_id.Hex(), subscribe, done)));
    WaitReady(promise.get_future(), timeout_ms_);
    RAY_CHECK_OK((pub_sub_->Publish(channel, object_id.Hex(), data, nullptr)));
  }

  WaitPendingDone(result, size);
}

}  // namespace ray

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  RAY_CHECK(argc == 4);
  ray::TEST_REDIS_SERVER_EXEC_PATH = argv[1];
  ray::TEST_REDIS_CLIENT_EXEC_PATH = argv[2];
  ray::TEST_REDIS_MODULE_LIBRARY_PATH = argv[3];
  return RUN_ALL_TESTS();
}
<|endoftext|>


<commit_before>/******************************************************************************\
 *           ___        __                                                    *
 *          /\_ \    __/\ \                                                   *
 *          \//\ \  /\_\ \ \____    ___   _____   _____      __               *
 *            \ \ \ \/\ \ \ '__`\  /'___\/\ '__`\/\ '__`\  /'__`\             *
 *             \_\ \_\ \ \ \ \L\ \/\ \__/\ \ \L\ \ \ \L\ \/\ \L\.\_           *
 *             /\____\\ \_\ \_,__/\ \____\\ \ ,__/\ \ ,__/\ \__/.\_\          *
 *             \/____/ \/_/\/___/  \/____/ \ \ \/  \ \ \/  \/__/\/_/          *
 *                                          \ \_\   \ \_\                     *
 *                                           \/_/    \/_/                     *
 *                                                                            *
 * Copyright (C) 2011-2013                                                    *
 * Dominik Charousset <dominik.charousset@haw-hamburg.de>                     *
 * Raphael Hiesgen <raphael.hiesgen@haw-hamburg.de>                           *
 *                                                                            *
 * This file is part of libcppa.                                              *
 * libcppa is free software: you can redistribute it and/or modify it under   *
 * the terms of the GNU Lesser General Public License as published by the     *
 * Free Software Foundation, either version 3 of the License                  *
 * or (at your option) any later version.                                     *
 *                                                                            *
 * libcppa is distributed in the hope that it will be useful,                 *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                       *
 * See the GNU Lesser General Public License for more details.                *
 *                                                                            *
 * You should have received a copy of the GNU Lesser General Public License   *
 * along with libcppa. If not, see <http://www.gnu.org/licenses/>.            *
\******************************************************************************/

#include <sstream>
#include <iostream>
#include <stdexcept>

#include "cppa/cppa.hpp"
#include "cppa/opencl/command_dispatcher.hpp"

using namespace std;

namespace cppa { namespace opencl {

struct command_dispatcher::worker {

    command_dispatcher* m_parent;

    typedef command_ptr job_ptr;

    job_queue* m_job_queue;
    thread m_thread;
    job_ptr m_dummy;

    worker(command_dispatcher* parent, job_queue* jq, job_ptr dummy)
        : m_parent(parent), m_job_queue(jq), m_dummy(dummy) { }

    void start() {
        m_thread = thread(&command_dispatcher::worker_loop, this);
    }

    worker(const worker&) = delete;

    worker& operator=(const worker&) = delete;

    void operator()() {
        job_ptr job;
        for (;;) {
            /*
             * todo:
             *  manage device usage
             *  wait for device
             */
            // adopt reference count of job queue
            job.adopt(m_job_queue->pop());
            if(job != m_dummy) {
                try {
                    cl_command_queue cmd_q =
                            m_parent->m_devices.front().cmd_queue.get();
                    job->enqueue(cmd_q);
                }
                catch (exception& e) {
                    cerr << e.what() << endl;
                }
            }
            else {
                cout << "worker done" << endl;
                return;
            }
        }
    }

};

void command_dispatcher::worker_loop(command_dispatcher::worker* w) {
    (*w)();
}

void command_dispatcher::supervisor_loop(command_dispatcher* scheduler,
                                         job_queue* jq, command_ptr m_dummy) {
    unique_ptr<command_dispatcher::worker> worker;
    worker.reset(new command_dispatcher::worker(scheduler, jq, m_dummy));
    worker->start();
    worker->m_thread.join();
    worker.reset();
    cout << "supervisor done" << endl;
}

void command_dispatcher::initialize() {

    m_dummy = make_counted<command_dummy>();

    cl_int err{0};

    /* find up to two available platforms */
    vector<cl_platform_id> ids(2);
    cl_uint number_of_platforms;
    err = clGetPlatformIDs(ids.size(), ids.data(), &number_of_platforms);
    if (err != CL_SUCCESS) {
        throw logic_error("[!!!] clGetPlatformIDs: '"
                               + get_opencl_error(err)
                               + "'.");
    }
    else if (number_of_platforms < 1) {
        throw logic_error("[!!!] clGetPlatformIDs: 'no platforms found'.");
    }

    /* find gpu devices on our platform */
    int pid{0};
    cl_uint num_devices{0};
//    cout << "Currently only looking for cpu devices!" << endl;
    cl_device_type dev_type{CL_DEVICE_TYPE_GPU};
    err = clGetDeviceIDs(ids[pid], dev_type, 0, NULL, &num_devices);
    if (err == CL_DEVICE_NOT_FOUND) {
        cout << "NO GPU DEVICES FOUND! LOOKING FOR CPU DEVICES NOW ..." << endl;
        dev_type = CL_DEVICE_TYPE_CPU;
        err = clGetDeviceIDs(ids[pid], dev_type, 0, NULL, &num_devices);
    }
    if (err != CL_SUCCESS) {
        throw runtime_error("[!!!] clGetDeviceIDs: '"
                                 + get_opencl_error(err)
                                 + "'.");
    }
    vector<cl_device_id> devices(num_devices);
    err = clGetDeviceIDs(ids[pid], dev_type, num_devices, devices.data(), NULL);
    if (err != CL_SUCCESS) {
        throw runtime_error("[!!!] clGetDeviceIDs: '"
                                 + get_opencl_error(err)
                                 + "'.");
    }

    /* create a context */
    m_context.adopt(clCreateContext(0, 1, devices.data(), NULL, NULL, &err));
    if (err != CL_SUCCESS) {
        throw runtime_error("[!!!] clCreateContext: '"
                                 + get_opencl_error(err)
                                 + "'.");
    }

    for (auto& d : devices) {
        device_ptr device;
        device.adopt(d);
        unsigned id{++dev_id_gen};
        command_queue_ptr cmd_queue;
        cmd_queue.adopt(clCreateCommandQueue(m_context.get(),
                                             device.get(),
                                             CL_QUEUE_PROFILING_ENABLE,
                                             &err));
        if (err != CL_SUCCESS) {
            ostringstream oss;
            oss << "[!!!] clCreateCommandQueue (" << id << "): '"
                << get_opencl_error(err) << "'.";
            throw runtime_error(oss.str());
        }
        size_t return_size{0};
        size_t max_work_group_size{0};
        err = clGetDeviceInfo(device.get(),
                              CL_DEVICE_MAX_WORK_GROUP_SIZE,
                              sizeof(size_t),
                              &max_work_group_size,
                              &return_size);
        if (err != CL_SUCCESS) {
            ostringstream oss;
            oss << "[!!!] clGetDeviceInfo ("
                << id
                << ":CL_DEVICE_MAX_WORK_GROUP_SIZE): '"
                << get_opencl_error(err) << "'.";
            throw runtime_error(oss.str());
        }
        cl_uint max_work_item_dimensions = 0;
        err = clGetDeviceInfo(device.get(),
                              CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS,
                              sizeof(cl_uint),
                              &max_work_item_dimensions,
                              &return_size);
        if (err != CL_SUCCESS) {
            ostringstream oss;
            oss << "[!!!] clGetDeviceInfo ("
                << id
                << ":CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS): '"
                << get_opencl_error(err) << "'.";
            throw runtime_error(oss.str());
        }
        vector<size_t> max_work_items_per_dim(max_work_item_dimensions);
        err = clGetDeviceInfo(device.get(),
                              CL_DEVICE_MAX_WORK_ITEM_SIZES,
                              sizeof(size_t)*max_work_item_dimensions,
                              max_work_items_per_dim.data(),
                              &return_size);
        if (err != CL_SUCCESS) {
            ostringstream oss;
            oss << "[!!!] clGetDeviceInfo ("
                << id
                << ":CL_DEVICE_MAX_WORK_ITEM_SIZES): '"
                << get_opencl_error(err) << "'.";
            throw runtime_error(oss.str());
        }
        device_info dev_info{id,
                             cmd_queue,
                             device,
                             max_work_group_size,
                             max_work_item_dimensions,
                             move(max_work_items_per_dim)};
        m_devices.push_back(move(dev_info));
    }
    m_supervisor = thread(&command_dispatcher::supervisor_loop,
                          this,
                          &m_job_queue,
                          m_dummy);
}

void command_dispatcher::destroy() {
    m_dummy->ref(); // reference of m_job_queue
    m_job_queue.push_back(m_dummy.get());
    m_supervisor.join();
    delete this;
}

void command_dispatcher::dispose() {
    delete this;
}

} } // namespace cppa::opencl
<commit_msg>added clFlush after enqueue<commit_after>/******************************************************************************\
 *           ___        __                                                    *
 *          /\_ \    __/\ \                                                   *
 *          \//\ \  /\_\ \ \____    ___   _____   _____      __               *
 *            \ \ \ \/\ \ \ '__`\  /'___\/\ '__`\/\ '__`\  /'__`\             *
 *             \_\ \_\ \ \ \ \L\ \/\ \__/\ \ \L\ \ \ \L\ \/\ \L\.\_           *
 *             /\____\\ \_\ \_,__/\ \____\\ \ ,__/\ \ ,__/\ \__/.\_\          *
 *             \/____/ \/_/\/___/  \/____/ \ \ \/  \ \ \/  \/__/\/_/          *
 *                                          \ \_\   \ \_\                     *
 *                                           \/_/    \/_/                     *
 *                                                                            *
 * Copyright (C) 2011-2013                                                    *
 * Dominik Charousset <dominik.charousset@haw-hamburg.de>                     *
 * Raphael Hiesgen <raphael.hiesgen@haw-hamburg.de>                           *
 *                                                                            *
 * This file is part of libcppa.                                              *
 * libcppa is free software: you can redistribute it and/or modify it under   *
 * the terms of the GNU Lesser General Public License as published by the     *
 * Free Software Foundation, either version 3 of the License                  *
 * or (at your option) any later version.                                     *
 *                                                                            *
 * libcppa is distributed in the hope that it will be useful,                 *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                       *
 * See the GNU Lesser General Public License for more details.                *
 *                                                                            *
 * You should have received a copy of the GNU Lesser General Public License   *
 * along with libcppa. If not, see <http://www.gnu.org/licenses/>.            *
\******************************************************************************/

#include <sstream>
#include <iostream>
#include <stdexcept>

#include "cppa/cppa.hpp"
#include "cppa/opencl/command_dispatcher.hpp"

using namespace std;

namespace cppa { namespace opencl {

struct command_dispatcher::worker {

    command_dispatcher* m_parent;

    typedef command_ptr job_ptr;

    job_queue* m_job_queue;
    thread m_thread;
    job_ptr m_dummy;

    worker(command_dispatcher* parent, job_queue* jq, job_ptr dummy)
        : m_parent(parent), m_job_queue(jq), m_dummy(dummy) { }

    void start() {
        m_thread = thread(&command_dispatcher::worker_loop, this);
    }

    worker(const worker&) = delete;

    worker& operator=(const worker&) = delete;

    void operator()() {
        job_ptr job;
        for (;;) {
            /*
             * todo:
             *  manage device usage
             *  wait for device
             */
            // adopt reference count of job queue
            job.adopt(m_job_queue->pop());
            if(job != m_dummy) {
                try {
                    cl_command_queue cmd_q =
                            m_parent->m_devices.front().cmd_queue.get();
                    job->enqueue(cmd_q);
                    cl_int err{clFlush(cmd_q)};
                    if (err != CL_SUCCESS) {
                        throw runtime_error("[!!!] clFlush: '"
                                                 + get_opencl_error(err)
                                                 + "'.");
                    }
                }
                catch (exception& e) {
                    cerr << e.what() << endl;
                }
            }
            else {
                cout << "worker done" << endl;
                return;
            }
        }
    }

};

void command_dispatcher::worker_loop(command_dispatcher::worker* w) {
    (*w)();
}

void command_dispatcher::supervisor_loop(command_dispatcher* scheduler,
                                         job_queue* jq, command_ptr m_dummy) {
    unique_ptr<command_dispatcher::worker> worker;
    worker.reset(new command_dispatcher::worker(scheduler, jq, m_dummy));
    worker->start();
    worker->m_thread.join();
    worker.reset();
    cout << "supervisor done" << endl;
}

void command_dispatcher::initialize() {

    m_dummy = make_counted<command_dummy>();

    cl_int err{0};

    /* find up to two available platforms */
    vector<cl_platform_id> ids(2);
    cl_uint number_of_platforms;
    err = clGetPlatformIDs(ids.size(), ids.data(), &number_of_platforms);
    if (err != CL_SUCCESS) {
        throw logic_error("[!!!] clGetPlatformIDs: '"
                               + get_opencl_error(err)
                               + "'.");
    }
    else if (number_of_platforms < 1) {
        throw logic_error("[!!!] clGetPlatformIDs: 'no platforms found'.");
    }

    /* find gpu devices on our platform */
    int pid{0};
    cl_uint num_devices{0};
//    cout << "Currently only looking for cpu devices!" << endl;
    cl_device_type dev_type{CL_DEVICE_TYPE_GPU};
    err = clGetDeviceIDs(ids[pid], dev_type, 0, NULL, &num_devices);
    if (err == CL_DEVICE_NOT_FOUND) {
        cout << "NO GPU DEVICES FOUND! LOOKING FOR CPU DEVICES NOW ..." << endl;
        dev_type = CL_DEVICE_TYPE_CPU;
        err = clGetDeviceIDs(ids[pid], dev_type, 0, NULL, &num_devices);
    }
    if (err != CL_SUCCESS) {
        throw runtime_error("[!!!] clGetDeviceIDs: '"
                                 + get_opencl_error(err)
                                 + "'.");
    }
    vector<cl_device_id> devices(num_devices);
    err = clGetDeviceIDs(ids[pid], dev_type, num_devices, devices.data(), NULL);
    if (err != CL_SUCCESS) {
        throw runtime_error("[!!!] clGetDeviceIDs: '"
                                 + get_opencl_error(err)
                                 + "'.");
    }

    /* create a context */
    m_context.adopt(clCreateContext(0, 1, devices.data(), NULL, NULL, &err));
    if (err != CL_SUCCESS) {
        throw runtime_error("[!!!] clCreateContext: '"
                                 + get_opencl_error(err)
                                 + "'.");
    }

    for (auto& d : devices) {
        device_ptr device;
        device.adopt(d);
        unsigned id{++dev_id_gen};
        command_queue_ptr cmd_queue;
        cmd_queue.adopt(clCreateCommandQueue(m_context.get(),
                                             device.get(),
                                             CL_QUEUE_PROFILING_ENABLE,
                                             &err));
        if (err != CL_SUCCESS) {
            ostringstream oss;
            oss << "[!!!] clCreateCommandQueue (" << id << "): '"
                << get_opencl_error(err) << "'.";
            throw runtime_error(oss.str());
        }
        size_t return_size{0};
        size_t max_work_group_size{0};
        err = clGetDeviceInfo(device.get(),
                              CL_DEVICE_MAX_WORK_GROUP_SIZE,
                              sizeof(size_t),
                              &max_work_group_size,
                              &return_size);
        if (err != CL_SUCCESS) {
            ostringstream oss;
            oss << "[!!!] clGetDeviceInfo ("
                << id
                << ":CL_DEVICE_MAX_WORK_GROUP_SIZE): '"
                << get_opencl_error(err) << "'.";
            throw runtime_error(oss.str());
        }
        cl_uint max_work_item_dimensions = 0;
        err = clGetDeviceInfo(device.get(),
                              CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS,
                              sizeof(cl_uint),
                              &max_work_item_dimensions,
                              &return_size);
        if (err != CL_SUCCESS) {
            ostringstream oss;
            oss << "[!!!] clGetDeviceInfo ("
                << id
                << ":CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS): '"
                << get_opencl_error(err) << "'.";
            throw runtime_error(oss.str());
        }
        vector<size_t> max_work_items_per_dim(max_work_item_dimensions);
        err = clGetDeviceInfo(device.get(),
                              CL_DEVICE_MAX_WORK_ITEM_SIZES,
                              sizeof(size_t)*max_work_item_dimensions,
                              max_work_items_per_dim.data(),
                              &return_size);
        if (err != CL_SUCCESS) {
            ostringstream oss;
            oss << "[!!!] clGetDeviceInfo ("
                << id
                << ":CL_DEVICE_MAX_WORK_ITEM_SIZES): '"
                << get_opencl_error(err) << "'.";
            throw runtime_error(oss.str());
        }
        device_info dev_info{id,
                             cmd_queue,
                             device,
                             max_work_group_size,
                             max_work_item_dimensions,
                             move(max_work_items_per_dim)};
        m_devices.push_back(move(dev_info));
    }
    m_supervisor = thread(&command_dispatcher::supervisor_loop,
                          this,
                          &m_job_queue,
                          m_dummy);
}

void command_dispatcher::destroy() {
    m_dummy->ref(); // reference of m_job_queue
    m_job_queue.push_back(m_dummy.get());
    m_supervisor.join();
    delete this;
}

void command_dispatcher::dispose() {
    delete this;
}

} } // namespace cppa::opencl
<|endoftext|>


<commit_before>/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <qtest.h>
#include <QDeclarativeView>
#include <QApplication>
#include <QLibraryInfo>
#include <QDir>
#include <QDebug>
#include <QProcess>
#include <QFile>

#ifdef Q_OS_SYMBIAN
// In Symbian OS test data is located in applications private dir
#define QT_TEST_SOURCE_DIR "."
#endif

enum Mode { Record, RecordNoVisuals, RecordSnapshot, Play, TestVisuals, RemoveVisuals, UpdateVisuals, UpdatePlatformVisuals, Test };

static QString testdir;
class tst_qmlvisual : public QObject
{
    Q_OBJECT
public:
    tst_qmlvisual();

    static QString toTestScript(const QString &, Mode=Test);
    static QString viewer();

    static QStringList findQmlFiles(const QDir &d);
private slots:
    void visual_data();
    void visual();

private:
    QString qmlruntime;
};

tst_qmlvisual::tst_qmlvisual()
{
    qmlruntime = viewer();
}

QString tst_qmlvisual::viewer()
{
    QString binaries = QLibraryInfo::location(QLibraryInfo::BinariesPath);

    QString qmlruntime;

#if defined(Q_WS_MAC)
    qmlruntime = QDir(binaries).absoluteFilePath("QMLViewer.app/Contents/MacOS/QMLViewer");
#elif defined(Q_WS_WIN) || defined(Q_WS_S60)
    qmlruntime = QDir(binaries).absoluteFilePath("qmlviewer.exe");
#else
    qmlruntime = QDir(binaries).absoluteFilePath("qmlviewer");
#endif

    return qmlruntime;
}

void tst_qmlvisual::visual_data()
{
    QTest::addColumn<QString>("file");
    QTest::addColumn<QString>("testdata");

    QStringList files;
    files << findQmlFiles(QDir(QT_TEST_SOURCE_DIR));
    if (qgetenv("QMLVISUAL_ALL") != "1") {
#if defined(Q_WS_MAC)
        //Text on Mac varies per version. Only check the text on 10.6
        if(QSysInfo::MacintoshVersion != QSysInfo::MV_10_6)
            foreach(const QString &str, files.filter(QRegExp(".*text.*")))
                files.removeAll(str);
#endif
#if defined(Q_WS_QWS)
        //We don't want QWS test results to mire down the CI system
        files.clear();
        //Needs at least one test data or it fails anyways
        files << QT_TEST_SOURCE_DIR "/selftest_noimages/selftest_noimages.qml";
#endif
    }

    foreach (const QString &file, files) {
        QString testdata = toTestScript(file);
        if (testdata.isEmpty())
            continue;

        QTest::newRow(file.toLatin1().constData()) << file << testdata;
    }
}

void tst_qmlvisual::visual()
{
    QFETCH(QString, file);
    QFETCH(QString, testdata);

    QStringList arguments;
#ifdef Q_WS_MAC
    arguments << "-no-opengl";
#endif
    arguments << "-script" << testdata
              << "-scriptopts" << "play,testimages,testerror,testskip,exitoncomplete,exitonfailure"
              << file;
#ifdef Q_WS_QWS
    arguments << "-qws";
#endif

    QProcess p;
    p.start(qmlruntime, arguments);
    bool finished = p.waitForFinished();
    QByteArray output = p.readAllStandardOutput() + p.readAllStandardError();
    QVERIFY2(finished, output.data());
    if (p.exitCode() != 0)
        qDebug() << output;
    QCOMPARE(p.exitStatus(), QProcess::NormalExit);
    QCOMPARE(p.exitCode(), 0);
}

QString tst_qmlvisual::toTestScript(const QString &file, Mode mode)
{
    if (!file.endsWith(".qml"))
        return QString();

    int index = file.lastIndexOf(QDir::separator());
    if (index == -1)
        index = file.lastIndexOf('/');
    if (index == -1)
        return QString();

    const char* platformsuffix=0; // platforms with different fonts
#if defined(Q_WS_MACX)
    platformsuffix = "-MAC";
#elif defined(Q_WS_X11)
    platformsuffix = "-X11";
#elif defined(Q_WS_WIN32)
    platformsuffix = "-WIN";
#elif defined(Q_WS_QWS)
    platformsuffix = "-QWS";
#elif defined(Q_WS_S60)
    platformsuffix = "-S60";
#endif

    QString testdata = file.left(index + 1) +
                       QString("data");
    QString testname = file.mid(index + 1, file.length() - index - 5);

    if (platformsuffix && (mode == UpdatePlatformVisuals || QFile::exists(testdata+QLatin1String(platformsuffix)+QDir::separator()+testname+".qml"))) {
        QString platformdir = testdata + QLatin1String(platformsuffix);
        if (mode == UpdatePlatformVisuals) {
            if (!QDir().mkpath(platformdir)) {
                qFatal("Cannot make path %s", qPrintable(platformdir));
            }
            // Copy from base
            QDir dir(testdata,testname+".*");
            dir.setFilter(QDir::Files);
            QFileInfoList list = dir.entryInfoList();
            for (int i = 0; i < list.size(); ++i) {
                QFile in(list.at(i).filePath());
                if (!in.open(QIODevice::ReadOnly)) {
                    qFatal("Cannot open file %s: %s", qPrintable(in.fileName()), qPrintable(in.errorString()));
                }
                QFile out(platformdir + QDir::separator() + list.at(i).fileName());
                if (!out.open(QIODevice::WriteOnly)) {
                    qFatal("Cannot open file %s: %s", qPrintable(out.fileName()), qPrintable(out.errorString()));
                }
                out.write(in.readAll());
            }
        }
        testdata = platformdir;
    }

    testdata += QDir::separator() + testname;

    return testdata;
}

QStringList tst_qmlvisual::findQmlFiles(const QDir &d)
{
    QStringList rv;

    QStringList files = d.entryList(QStringList() << QLatin1String("*.qml"),
                                    QDir::Files);
    foreach (const QString &file, files) {
        if (file.at(0).isLower()) {
            rv << d.absoluteFilePath(file);
        }
    }

    QStringList dirs = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot |
                                   QDir::NoSymLinks);
    foreach (const QString &dir, dirs) {
        if (dir.left(4) == "data")
            continue;

        QDir sub = d;
        sub.cd(dir);
        rv << findQmlFiles(sub);
    }

    return rv;
}

void action(Mode mode, const QString &file)
{
    Q_ASSERT(mode != Test);

    QString testdata = tst_qmlvisual::toTestScript(file,mode);

    QStringList arguments;
#ifdef Q_WS_MAC
    arguments << "-no-opengl";
#endif
    switch (mode) {
        case Test:
            // Don't run qml
            break;
        case Record:
            arguments << "-script" << testdata
                  << "-scriptopts" << "record,testimages,saveonexit"
                  << file;
            break;
        case RecordNoVisuals:
            arguments << "-script" << testdata
                  << "-scriptopts" << "record,saveonexit"
                  << file;
            break;
        case RecordSnapshot:
            arguments << "-script" << testdata
                  << "-scriptopts" << "record,testimages,snapshot,saveonexit"
                  << file;
            break;
        case Play:
            arguments << "-script" << testdata
                  << "-scriptopts" << "play,testimages,testerror,testskip,exitoncomplete"
                  << file;
            break;
        case TestVisuals:
        arguments << "-script" << testdata
                  << "-scriptopts" << "play"
                  << file;
            break;
        case UpdateVisuals:
        case UpdatePlatformVisuals:
            arguments << "-script" << testdata
                  << "-scriptopts" << "play,record,testimages,exitoncomplete,saveonexit"
                  << file;
            break;
        case RemoveVisuals:
            arguments << "-script" << testdata
                  << "-scriptopts" << "play,record,exitoncomplete,saveonexit"
                  << file;
            break;
    }
    if (!arguments.isEmpty()) {
        QProcess p;
        p.setProcessChannelMode(QProcess::ForwardedChannels);
        p.start(tst_qmlvisual::viewer(), arguments);
        p.waitForFinished();
    }
}

void usage()
{
    fprintf(stderr, "\n");
    fprintf(stderr, "QML related options\n");
    fprintf(stderr, " -listtests                  : list all the tests seen by tst_qmlvisual, and then exit immediately\n");
    fprintf(stderr, " -record file                : record new test data for file\n");
    fprintf(stderr, " -recordnovisuals file       : record new test data for file, but ignore visuals\n");
    fprintf(stderr, " -recordsnapshot file        : record new snapshot for file (like record but only records a single frame and then exits)\n");
    fprintf(stderr, " -play file                  : playback test data for file, printing errors\n");
    fprintf(stderr, " -testvisuals file           : playback test data for file, without errors\n");
    fprintf(stderr, " -updatevisuals file         : playback test data for file, accept new visuals for file\n");
    fprintf(stderr, " -updateplatformvisuals file : playback test data for file, accept new visuals for file only on current platform (MacOSX/Win32/X11/QWS/S60)\n");
    fprintf(stderr, "\n"
        "Visual tests are recordings of manual interactions with a QML test,\n"
        "that can then be run automatically. To record a new test, run:\n"
        "\n"
        "  tst_qmlvisual -record yourtestdir/yourtest.qml\n"
        "\n"
        "This records everything you do (try to keep it short).\n"
        "To play back a test, run:\n"
        "\n"
        "  tst_qmlvisual -play yourtestdir/yourtest.qml\n"
        "\n"
        "Your test may include QML code to test itself, reporting any error to an\n"
        "'error' property on the root object - the test will fail if this property\n"
        "gets set to anything non-empty.\n"
        "\n"
        "If your test changes slightly but is still correct (check with -play), you\n"
        "can update the visuals by running:\n"
        "\n"
        "  tst_qmlvisual -updatevisuals yourtestdir/yourtest.qml\n"
        "\n"
        "If your test includes platform-sensitive visuals (eg. text in system fonts),\n"
        "you should create platform-specific visuals, using -updateplatformvisuals\n"
        "instead.\n"
        "\n"
        "If you ONLY wish to use the 'error' property, you can record your test with\n"
        "-recordnovisuals, or discard existing visuals with -removevisuals; the test\n"
        "will then only fail on a syntax error, crash, or non-empty 'error' property.\n"
        "\n"
        "If your test has anything set to the 'skip' property on the root object then\n"
        "test failures will be ignored. This allows for an opt-out of automated\n"
        "aggregation of test results. The value of the 'skip' property (usually a\n"
        "string) will then be printed to stdout when the test is run as part of the\n"
        "message saying the test has been skipped.\n"
    );
}

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    Mode mode = Test;
    QString file;
    bool showHelp = false;

    int newArgc = 1;
    char **newArgv = new char*[argc];
    newArgv[0] = argv[0];

    for (int ii = 1; ii < argc; ++ii) {
        QString arg(argv[ii]);
        if (arg == "-play" && (ii + 1) < argc) {
            mode = Play;
            file = argv[++ii];
        } else if (arg == "-record" && (ii + 1) < argc) {
            mode = Record;
            file = argv[++ii];
        } else if (arg == "-recordnovisuals" && (ii + 1) < argc) {
            mode = RecordNoVisuals;
            file = argv[++ii];
        }  else if (arg == "-recordsnapshot" && (ii + 1) < argc) {
            mode = RecordSnapshot;
            file = argv[++ii];
        } else if (arg == "-testvisuals" && (ii + 1) < argc) {
            mode = TestVisuals;
            file = argv[++ii];
        } else if (arg == "-removevisuals" && (ii + 1) < argc) {
            mode = RemoveVisuals;
            file = argv[++ii];
        } else if (arg == "-updatevisuals" && (ii + 1) < argc) {
            mode = UpdateVisuals;
            file = argv[++ii];
        } else if (arg == "-updateplatformvisuals" && (ii + 1) < argc) {
            mode = UpdatePlatformVisuals;
            file = argv[++ii];
        } else {
            newArgv[newArgc++] = argv[ii];
        }

        if (arg == "-help" || arg == "-?" || arg == "--help") {
            atexit(usage);
            showHelp = true;
        }

        if (arg == "-listtests") {
            QStringList list = tst_qmlvisual::findQmlFiles(QDir(QT_TEST_SOURCE_DIR));
            foreach (QString test, list) {
                qWarning() << qPrintable(test);
            }
            return 0;
        }
    }

    if (mode == Test || showHelp) {
        tst_qmlvisual tc;
        return QTest::qExec(&tc, newArgc, newArgv);
    } else {
        if (!file.endsWith(QLatin1String(".qml"))) {
            qWarning() << "Error: Invalid file name (must end in .qml)";
            return -1;
        }
        QDir d = QDir::current();
        QString absFile = d.absoluteFilePath(file);
        if (!QFile::exists(absFile)) {
            qWarning() << "Error: File does not exist";
            return -1;
        }

        action(mode, absFile);
        return 0;
    }
}

#include "tst_qmlvisual.moc"
<commit_msg>Remove redundant Q_ASSERT from qmlvisual autotest.<commit_after>/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <qtest.h>
#include <QDeclarativeView>
#include <QApplication>
#include <QLibraryInfo>
#include <QDir>
#include <QDebug>
#include <QProcess>
#include <QFile>

#ifdef Q_OS_SYMBIAN
// In Symbian OS test data is located in applications private dir
#define QT_TEST_SOURCE_DIR "."
#endif

enum Mode { Record, RecordNoVisuals, RecordSnapshot, Play, TestVisuals, RemoveVisuals, UpdateVisuals, UpdatePlatformVisuals, Test };

static QString testdir;
class tst_qmlvisual : public QObject
{
    Q_OBJECT
public:
    tst_qmlvisual();

    static QString toTestScript(const QString &, Mode=Test);
    static QString viewer();

    static QStringList findQmlFiles(const QDir &d);
private slots:
    void visual_data();
    void visual();

private:
    QString qmlruntime;
};

tst_qmlvisual::tst_qmlvisual()
{
    qmlruntime = viewer();
}

QString tst_qmlvisual::viewer()
{
    QString binaries = QLibraryInfo::location(QLibraryInfo::BinariesPath);

    QString qmlruntime;

#if defined(Q_WS_MAC)
    qmlruntime = QDir(binaries).absoluteFilePath("QMLViewer.app/Contents/MacOS/QMLViewer");
#elif defined(Q_WS_WIN) || defined(Q_WS_S60)
    qmlruntime = QDir(binaries).absoluteFilePath("qmlviewer.exe");
#else
    qmlruntime = QDir(binaries).absoluteFilePath("qmlviewer");
#endif

    return qmlruntime;
}

void tst_qmlvisual::visual_data()
{
    QTest::addColumn<QString>("file");
    QTest::addColumn<QString>("testdata");

    QStringList files;
    files << findQmlFiles(QDir(QT_TEST_SOURCE_DIR));
    if (qgetenv("QMLVISUAL_ALL") != "1") {
#if defined(Q_WS_MAC)
        //Text on Mac varies per version. Only check the text on 10.6
        if(QSysInfo::MacintoshVersion != QSysInfo::MV_10_6)
            foreach(const QString &str, files.filter(QRegExp(".*text.*")))
                files.removeAll(str);
#endif
#if defined(Q_WS_QWS)
        //We don't want QWS test results to mire down the CI system
        files.clear();
        //Needs at least one test data or it fails anyways
        files << QT_TEST_SOURCE_DIR "/selftest_noimages/selftest_noimages.qml";
#endif
    }

    foreach (const QString &file, files) {
        QString testdata = toTestScript(file);
        if (testdata.isEmpty())
            continue;

        QTest::newRow(file.toLatin1().constData()) << file << testdata;
    }
}

void tst_qmlvisual::visual()
{
    QFETCH(QString, file);
    QFETCH(QString, testdata);

    QStringList arguments;
#ifdef Q_WS_MAC
    arguments << "-no-opengl";
#endif
    arguments << "-script" << testdata
              << "-scriptopts" << "play,testimages,testerror,testskip,exitoncomplete,exitonfailure"
              << file;
#ifdef Q_WS_QWS
    arguments << "-qws";
#endif

    QProcess p;
    p.start(qmlruntime, arguments);
    bool finished = p.waitForFinished();
    QByteArray output = p.readAllStandardOutput() + p.readAllStandardError();
    QVERIFY2(finished, output.data());
    if (p.exitCode() != 0)
        qDebug() << output;
    QCOMPARE(p.exitStatus(), QProcess::NormalExit);
    QCOMPARE(p.exitCode(), 0);
}

QString tst_qmlvisual::toTestScript(const QString &file, Mode mode)
{
    if (!file.endsWith(".qml"))
        return QString();

    int index = file.lastIndexOf(QDir::separator());
    if (index == -1)
        index = file.lastIndexOf('/');
    if (index == -1)
        return QString();

    const char* platformsuffix=0; // platforms with different fonts
#if defined(Q_WS_MACX)
    platformsuffix = "-MAC";
#elif defined(Q_WS_X11)
    platformsuffix = "-X11";
#elif defined(Q_WS_WIN32)
    platformsuffix = "-WIN";
#elif defined(Q_WS_QWS)
    platformsuffix = "-QWS";
#elif defined(Q_WS_S60)
    platformsuffix = "-S60";
#endif

    QString testdata = file.left(index + 1) +
                       QString("data");
    QString testname = file.mid(index + 1, file.length() - index - 5);

    if (platformsuffix && (mode == UpdatePlatformVisuals || QFile::exists(testdata+QLatin1String(platformsuffix)+QDir::separator()+testname+".qml"))) {
        QString platformdir = testdata + QLatin1String(platformsuffix);
        if (mode == UpdatePlatformVisuals) {
            if (!QDir().mkpath(platformdir)) {
                qFatal("Cannot make path %s", qPrintable(platformdir));
            }
            // Copy from base
            QDir dir(testdata,testname+".*");
            dir.setFilter(QDir::Files);
            QFileInfoList list = dir.entryInfoList();
            for (int i = 0; i < list.size(); ++i) {
                QFile in(list.at(i).filePath());
                if (!in.open(QIODevice::ReadOnly)) {
                    qFatal("Cannot open file %s: %s", qPrintable(in.fileName()), qPrintable(in.errorString()));
                }
                QFile out(platformdir + QDir::separator() + list.at(i).fileName());
                if (!out.open(QIODevice::WriteOnly)) {
                    qFatal("Cannot open file %s: %s", qPrintable(out.fileName()), qPrintable(out.errorString()));
                }
                out.write(in.readAll());
            }
        }
        testdata = platformdir;
    }

    testdata += QDir::separator() + testname;

    return testdata;
}

QStringList tst_qmlvisual::findQmlFiles(const QDir &d)
{
    QStringList rv;

    QStringList files = d.entryList(QStringList() << QLatin1String("*.qml"),
                                    QDir::Files);
    foreach (const QString &file, files) {
        if (file.at(0).isLower()) {
            rv << d.absoluteFilePath(file);
        }
    }

    QStringList dirs = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot |
                                   QDir::NoSymLinks);
    foreach (const QString &dir, dirs) {
        if (dir.left(4) == "data")
            continue;

        QDir sub = d;
        sub.cd(dir);
        rv << findQmlFiles(sub);
    }

    return rv;
}

void action(Mode mode, const QString &file)
{
    QString testdata = tst_qmlvisual::toTestScript(file,mode);

    QStringList arguments;
#ifdef Q_WS_MAC
    arguments << "-no-opengl";
#endif
    switch (mode) {
        case Test:
            // Don't run qml
            break;
        case Record:
            arguments << "-script" << testdata
                  << "-scriptopts" << "record,testimages,saveonexit"
                  << file;
            break;
        case RecordNoVisuals:
            arguments << "-script" << testdata
                  << "-scriptopts" << "record,saveonexit"
                  << file;
            break;
        case RecordSnapshot:
            arguments << "-script" << testdata
                  << "-scriptopts" << "record,testimages,snapshot,saveonexit"
                  << file;
            break;
        case Play:
            arguments << "-script" << testdata
                  << "-scriptopts" << "play,testimages,testerror,testskip,exitoncomplete"
                  << file;
            break;
        case TestVisuals:
        arguments << "-script" << testdata
                  << "-scriptopts" << "play"
                  << file;
            break;
        case UpdateVisuals:
        case UpdatePlatformVisuals:
            arguments << "-script" << testdata
                  << "-scriptopts" << "play,record,testimages,exitoncomplete,saveonexit"
                  << file;
            break;
        case RemoveVisuals:
            arguments << "-script" << testdata
                  << "-scriptopts" << "play,record,exitoncomplete,saveonexit"
                  << file;
            break;
    }
    if (!arguments.isEmpty()) {
        QProcess p;
        p.setProcessChannelMode(QProcess::ForwardedChannels);
        p.start(tst_qmlvisual::viewer(), arguments);
        p.waitForFinished();
    }
}

void usage()
{
    fprintf(stderr, "\n");
    fprintf(stderr, "QML related options\n");
    fprintf(stderr, " -listtests                  : list all the tests seen by tst_qmlvisual, and then exit immediately\n");
    fprintf(stderr, " -record file                : record new test data for file\n");
    fprintf(stderr, " -recordnovisuals file       : record new test data for file, but ignore visuals\n");
    fprintf(stderr, " -recordsnapshot file        : record new snapshot for file (like record but only records a single frame and then exits)\n");
    fprintf(stderr, " -play file                  : playback test data for file, printing errors\n");
    fprintf(stderr, " -testvisuals file           : playback test data for file, without errors\n");
    fprintf(stderr, " -updatevisuals file         : playback test data for file, accept new visuals for file\n");
    fprintf(stderr, " -updateplatformvisuals file : playback test data for file, accept new visuals for file only on current platform (MacOSX/Win32/X11/QWS/S60)\n");
    fprintf(stderr, "\n"
        "Visual tests are recordings of manual interactions with a QML test,\n"
        "that can then be run automatically. To record a new test, run:\n"
        "\n"
        "  tst_qmlvisual -record yourtestdir/yourtest.qml\n"
        "\n"
        "This records everything you do (try to keep it short).\n"
        "To play back a test, run:\n"
        "\n"
        "  tst_qmlvisual -play yourtestdir/yourtest.qml\n"
        "\n"
        "Your test may include QML code to test itself, reporting any error to an\n"
        "'error' property on the root object - the test will fail if this property\n"
        "gets set to anything non-empty.\n"
        "\n"
        "If your test changes slightly but is still correct (check with -play), you\n"
        "can update the visuals by running:\n"
        "\n"
        "  tst_qmlvisual -updatevisuals yourtestdir/yourtest.qml\n"
        "\n"
        "If your test includes platform-sensitive visuals (eg. text in system fonts),\n"
        "you should create platform-specific visuals, using -updateplatformvisuals\n"
        "instead.\n"
        "\n"
        "If you ONLY wish to use the 'error' property, you can record your test with\n"
        "-recordnovisuals, or discard existing visuals with -removevisuals; the test\n"
        "will then only fail on a syntax error, crash, or non-empty 'error' property.\n"
        "\n"
        "If your test has anything set to the 'skip' property on the root object then\n"
        "test failures will be ignored. This allows for an opt-out of automated\n"
        "aggregation of test results. The value of the 'skip' property (usually a\n"
        "string) will then be printed to stdout when the test is run as part of the\n"
        "message saying the test has been skipped.\n"
    );
}

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    Mode mode = Test;
    QString file;
    bool showHelp = false;

    int newArgc = 1;
    char **newArgv = new char*[argc];
    newArgv[0] = argv[0];

    for (int ii = 1; ii < argc; ++ii) {
        QString arg(argv[ii]);
        if (arg == "-play" && (ii + 1) < argc) {
            mode = Play;
            file = argv[++ii];
        } else if (arg == "-record" && (ii + 1) < argc) {
            mode = Record;
            file = argv[++ii];
        } else if (arg == "-recordnovisuals" && (ii + 1) < argc) {
            mode = RecordNoVisuals;
            file = argv[++ii];
        }  else if (arg == "-recordsnapshot" && (ii + 1) < argc) {
            mode = RecordSnapshot;
            file = argv[++ii];
        } else if (arg == "-testvisuals" && (ii + 1) < argc) {
            mode = TestVisuals;
            file = argv[++ii];
        } else if (arg == "-removevisuals" && (ii + 1) < argc) {
            mode = RemoveVisuals;
            file = argv[++ii];
        } else if (arg == "-updatevisuals" && (ii + 1) < argc) {
            mode = UpdateVisuals;
            file = argv[++ii];
        } else if (arg == "-updateplatformvisuals" && (ii + 1) < argc) {
            mode = UpdatePlatformVisuals;
            file = argv[++ii];
        } else {
            newArgv[newArgc++] = argv[ii];
        }

        if (arg == "-help" || arg == "-?" || arg == "--help") {
            atexit(usage);
            showHelp = true;
        }

        if (arg == "-listtests") {
            QStringList list = tst_qmlvisual::findQmlFiles(QDir(QT_TEST_SOURCE_DIR));
            foreach (QString test, list) {
                qWarning() << qPrintable(test);
            }
            return 0;
        }
    }

    if (mode == Test || showHelp) {
        tst_qmlvisual tc;
        return QTest::qExec(&tc, newArgc, newArgv);
    } else {
        if (!file.endsWith(QLatin1String(".qml"))) {
            qWarning() << "Error: Invalid file name (must end in .qml)";
            return -1;
        }
        QDir d = QDir::current();
        QString absFile = d.absoluteFilePath(file);
        if (!QFile::exists(absFile)) {
            qWarning() << "Error: File does not exist";
            return -1;
        }

        action(mode, absFile);
        return 0;
    }
}

#include "tst_qmlvisual.moc"
<|endoftext|>


<commit_before>/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qqmldebugclient.h"
#include "../../../../../src/plugins/qmltooling/shared/qpacketprotocol.h"

#include <QtCore/qdebug.h>
#include <QtCore/qeventloop.h>
#include <QtCore/qstringlist.h>
#include <QtCore/qtimer.h>
#include <QtNetwork/qnetworkproxy.h>

const int protocolVersion = 1;
const QString serverId = QLatin1String("QDeclarativeDebugServer");
const QString clientId = QLatin1String("QDeclarativeDebugClient");

class QQmlDebugClientPrivate
{
public:
    QQmlDebugClientPrivate();

    QString name;
    QQmlDebugConnection *connection;
};

class QQmlDebugConnectionPrivate : public QObject
{
    Q_OBJECT
public:
    QQmlDebugConnectionPrivate(QQmlDebugConnection *c);
    QQmlDebugConnection *q;
    QPacketProtocol *protocol;
    QIODevice *device;
    QEventLoop handshakeEventLoop;
    QTimer handshakeTimer;

    bool gotHello;
    QHash <QString, float> serverPlugins;
    QHash<QString, QQmlDebugClient *> plugins;

    void advertisePlugins();
    void connectDeviceSignals();

public Q_SLOTS:
    void connected();
    void readyRead();
    void deviceAboutToClose();
    void handshakeTimeout();
};

QQmlDebugConnectionPrivate::QQmlDebugConnectionPrivate(QQmlDebugConnection *c)
    : QObject(c), q(c), protocol(0), device(0), gotHello(false)
{
    protocol = new QPacketProtocol(q, this);
    QObject::connect(c, SIGNAL(connected()), this, SLOT(connected()));
    QObject::connect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));

    handshakeTimer.setSingleShot(true);
    handshakeTimer.setInterval(3000);
    connect(&handshakeTimer, SIGNAL(timeout()), SLOT(handshakeTimeout()));
}

void QQmlDebugConnectionPrivate::advertisePlugins()
{
    if (!q->isConnected())
        return;

    QPacket pack;
    pack << serverId << 1 << plugins.keys();
    protocol->send(pack);
    q->flush();
}

void QQmlDebugConnectionPrivate::connected()
{
    QPacket pack;
    pack << serverId << 0 << protocolVersion << plugins.keys()
         << q->m_dataStreamVersion;
    protocol->send(pack);
    q->flush();
}

void QQmlDebugConnectionPrivate::readyRead()
{
    if (!gotHello) {
        QPacket pack = protocol->read();
        QString name;

        pack >> name;

        bool validHello = false;
        if (name == clientId) {
            int op = -1;
            pack >> op;
            if (op == 0) {
                int version = -1;
                pack >> version;
                if (version == protocolVersion) {
                    QStringList pluginNames;
                    QList<float> pluginVersions;
                    pack >> pluginNames;
                    if (!pack.isEmpty())
                        pack >> pluginVersions;

                    const int pluginNamesSize = pluginNames.size();
                    const int pluginVersionsSize = pluginVersions.size();
                    for (int i = 0; i < pluginNamesSize; ++i) {
                        float pluginVersion = 1.0;
                        if (i < pluginVersionsSize)
                            pluginVersion = pluginVersions.at(i);
                        serverPlugins.insert(pluginNames.at(i), pluginVersion);
                    }

                    pack >> q->m_dataStreamVersion;
                    validHello = true;
                }
            }
        }

        if (!validHello) {
            qWarning("QQmlDebugConnection: Invalid hello message");
            QObject::disconnect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
            return;
        }
        gotHello = true;

        QHash<QString, QQmlDebugClient *>::Iterator iter = plugins.begin();
        for (; iter != plugins.end(); ++iter) {
            QQmlDebugClient::State newState = QQmlDebugClient::Unavailable;
            if (serverPlugins.contains(iter.key()))
                newState = QQmlDebugClient::Enabled;
            iter.value()->stateChanged(newState);
        }

        handshakeTimer.stop();
        handshakeEventLoop.quit();
    }

    while (protocol->packetsAvailable()) {
        QPacket pack = protocol->read();
        QString name;
        pack >> name;

        if (name == clientId) {
            int op = -1;
            pack >> op;

            if (op == 1) {
                // Service Discovery
                QHash<QString, float> oldServerPlugins = serverPlugins;
                serverPlugins.clear();

                QStringList pluginNames;
                QList<float> pluginVersions;
                pack >> pluginNames;
                if (!pack.isEmpty())
                    pack >> pluginVersions;

                const int pluginNamesSize = pluginNames.size();
                const int pluginVersionsSize = pluginVersions.size();
                for (int i = 0; i < pluginNamesSize; ++i) {
                    float pluginVersion = 1.0;
                    if (i < pluginVersionsSize)
                        pluginVersion = pluginVersions.at(i);
                    serverPlugins.insert(pluginNames.at(i), pluginVersion);
                }

                QHash<QString, QQmlDebugClient *>::Iterator iter = plugins.begin();
                for (; iter != plugins.end(); ++iter) {
                    const QString pluginName = iter.key();
                    QQmlDebugClient::State newSate = QQmlDebugClient::Unavailable;
                    if (serverPlugins.contains(pluginName))
                        newSate = QQmlDebugClient::Enabled;

                    if (oldServerPlugins.contains(pluginName)
                            != serverPlugins.contains(pluginName)) {
                        iter.value()->stateChanged(newSate);
                    }
                }
            } else {
                qWarning() << "QQmlDebugConnection: Unknown control message id" << op;
            }
        } else {
            QByteArray message;
            pack >> message;

            QHash<QString, QQmlDebugClient *>::Iterator iter =
                    plugins.find(name);
            if (iter == plugins.end()) {
                qWarning() << "QQmlDebugConnection: Message received for missing plugin" << name;
            } else {
                (*iter)->messageReceived(message);
            }
        }
    }
}

void QQmlDebugConnectionPrivate::deviceAboutToClose()
{
    // This is nasty syntax but we want to emit our own aboutToClose signal (by calling QIODevice::close())
    // without calling the underlying device close fn as that would cause an infinite loop
    q->QIODevice::close();
}

void QQmlDebugConnectionPrivate::handshakeTimeout()
{
    if (!gotHello) {
        qWarning() << "Qml Debug Client: Did not get handshake answer in time";
        handshakeEventLoop.quit();
    }
}

QQmlDebugConnection::QQmlDebugConnection(QObject *parent)
    : QIODevice(parent), d(new QQmlDebugConnectionPrivate(this)),
      m_dataStreamVersion(QDataStream::Qt_5_0)
{
}

QQmlDebugConnection::~QQmlDebugConnection()
{
    QHash<QString, QQmlDebugClient*>::iterator iter = d->plugins.begin();
    for (; iter != d->plugins.end(); ++iter) {
        iter.value()->d->connection = 0;
        iter.value()->stateChanged(QQmlDebugClient::NotConnected);
    }
}

void QQmlDebugConnection::setDataStreamVersion(int dataStreamVersion)
{
    m_dataStreamVersion = dataStreamVersion;
}

int QQmlDebugConnection::dataStreamVersion()
{
    return m_dataStreamVersion;
}

bool QQmlDebugConnection::isConnected() const
{
    return state() == QAbstractSocket::ConnectedState;
}

qint64 QQmlDebugConnection::readData(char *data, qint64 maxSize)
{
    return d->device->read(data, maxSize);
}

qint64 QQmlDebugConnection::writeData(const char *data, qint64 maxSize)
{
    return d->device->write(data, maxSize);
}

qint64 QQmlDebugConnection::bytesAvailable() const
{
    return d->device->bytesAvailable();
}

bool QQmlDebugConnection::isSequential() const
{
    return true;
}

void QQmlDebugConnection::close()
{
    if (isOpen()) {
        QIODevice::close();
        d->device->close();
        emit stateChanged(QAbstractSocket::UnconnectedState);

        QHash<QString, QQmlDebugClient*>::iterator iter = d->plugins.begin();
        for (; iter != d->plugins.end(); ++iter) {
            iter.value()->stateChanged(QQmlDebugClient::NotConnected);
        }
    }
}

bool QQmlDebugConnection::waitForConnected(int msecs)
{
    QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
    if (!socket)
        return false;
    if (!socket->waitForConnected(msecs))
        return false;
    // wait for handshake
    d->handshakeTimer.start();
    d->handshakeEventLoop.exec();
    return d->gotHello;
}

QString QQmlDebugConnection::stateString() const
{
   QString state;

   if (isConnected())
       state = "Connected";
   else
       state = "Not connected";

   if (d->gotHello)
       state += ", got hello";
   else
       state += ", did not get hello!";

   return state;
}

QAbstractSocket::SocketState QQmlDebugConnection::state() const
{
    QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
    if (socket)
        return socket->state();

    return QAbstractSocket::UnconnectedState;
}

void QQmlDebugConnection::flush()
{
    QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
    if (socket) {
        socket->flush();
        return;
    }
}

void QQmlDebugConnection::connectToHost(const QString &hostName, quint16 port)
{
    QTcpSocket *socket = new QTcpSocket(d);
    socket->setProxy(QNetworkProxy::NoProxy);
    d->device = socket;
    d->connectDeviceSignals();
    d->gotHello = false;
    connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SIGNAL(stateChanged(QAbstractSocket::SocketState)));
    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SIGNAL(error(QAbstractSocket::SocketError)));
    connect(socket, SIGNAL(connected()), this, SIGNAL(connected()));
    socket->connectToHost(hostName, port);
    QIODevice::open(ReadWrite | Unbuffered);
}

void QQmlDebugConnectionPrivate::connectDeviceSignals()
{
    connect(device, SIGNAL(bytesWritten(qint64)), q, SIGNAL(bytesWritten(qint64)));
    connect(device, SIGNAL(readyRead()), q, SIGNAL(readyRead()));
    connect(device, SIGNAL(aboutToClose()), this, SLOT(deviceAboutToClose()));
}

//

QQmlDebugClientPrivate::QQmlDebugClientPrivate()
    : connection(0)
{
}

QQmlDebugClient::QQmlDebugClient(const QString &name,
                                                 QQmlDebugConnection *parent)
    : QObject(parent),
      d(new QQmlDebugClientPrivate)
{
    d->name = name;
    d->connection = parent;

    if (!d->connection)
        return;

    if (d->connection->d->plugins.contains(name)) {
        qWarning() << "QQmlDebugClient: Conflicting plugin name" << name;
        d->connection = 0;
    } else {
        d->connection->d->plugins.insert(name, this);
        d->connection->d->advertisePlugins();
    }
}

QQmlDebugClient::~QQmlDebugClient()
{
    if (d->connection && d->connection->d) {
        d->connection->d->plugins.remove(d->name);
        d->connection->d->advertisePlugins();
    }
    delete d;
}

QString QQmlDebugClient::name() const
{
    return d->name;
}

float QQmlDebugClient::serviceVersion() const
{
    if (d->connection->d->serverPlugins.contains(d->name))
        return d->connection->d->serverPlugins.value(d->name);
    return -1;
}

QQmlDebugClient::State QQmlDebugClient::state() const
{
    if (!d->connection
            || !d->connection->isConnected()
            || !d->connection->d->gotHello)
        return NotConnected;

    if (d->connection->d->serverPlugins.contains(d->name))
        return Enabled;

    return Unavailable;
}

QString QQmlDebugClient::stateString() const
{
    switch (state()) {
    case NotConnected: return QLatin1String("Not connected");
    case Unavailable: return QLatin1String("Unavailable");
    case Enabled: return QLatin1String("Enabled");
    }
}

void QQmlDebugClient::sendMessage(const QByteArray &message)
{
    if (state() != Enabled)
        return;

    QPacket pack;
    pack << d->name << message;
    d->connection->d->protocol->send(pack);
    d->connection->flush();
}

void QQmlDebugClient::stateChanged(State)
{
}

void QQmlDebugClient::messageReceived(const QByteArray &)
{
}

#include <qqmldebugclient.moc>
<commit_msg>qqmldebugclient.cpp: Fix warning about missing return.<commit_after>/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qqmldebugclient.h"
#include "../../../../../src/plugins/qmltooling/shared/qpacketprotocol.h"

#include <QtCore/qdebug.h>
#include <QtCore/qeventloop.h>
#include <QtCore/qstringlist.h>
#include <QtCore/qtimer.h>
#include <QtNetwork/qnetworkproxy.h>

const int protocolVersion = 1;
const QString serverId = QLatin1String("QDeclarativeDebugServer");
const QString clientId = QLatin1String("QDeclarativeDebugClient");

class QQmlDebugClientPrivate
{
public:
    QQmlDebugClientPrivate();

    QString name;
    QQmlDebugConnection *connection;
};

class QQmlDebugConnectionPrivate : public QObject
{
    Q_OBJECT
public:
    QQmlDebugConnectionPrivate(QQmlDebugConnection *c);
    QQmlDebugConnection *q;
    QPacketProtocol *protocol;
    QIODevice *device;
    QEventLoop handshakeEventLoop;
    QTimer handshakeTimer;

    bool gotHello;
    QHash <QString, float> serverPlugins;
    QHash<QString, QQmlDebugClient *> plugins;

    void advertisePlugins();
    void connectDeviceSignals();

public Q_SLOTS:
    void connected();
    void readyRead();
    void deviceAboutToClose();
    void handshakeTimeout();
};

QQmlDebugConnectionPrivate::QQmlDebugConnectionPrivate(QQmlDebugConnection *c)
    : QObject(c), q(c), protocol(0), device(0), gotHello(false)
{
    protocol = new QPacketProtocol(q, this);
    QObject::connect(c, SIGNAL(connected()), this, SLOT(connected()));
    QObject::connect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));

    handshakeTimer.setSingleShot(true);
    handshakeTimer.setInterval(3000);
    connect(&handshakeTimer, SIGNAL(timeout()), SLOT(handshakeTimeout()));
}

void QQmlDebugConnectionPrivate::advertisePlugins()
{
    if (!q->isConnected())
        return;

    QPacket pack;
    pack << serverId << 1 << plugins.keys();
    protocol->send(pack);
    q->flush();
}

void QQmlDebugConnectionPrivate::connected()
{
    QPacket pack;
    pack << serverId << 0 << protocolVersion << plugins.keys()
         << q->m_dataStreamVersion;
    protocol->send(pack);
    q->flush();
}

void QQmlDebugConnectionPrivate::readyRead()
{
    if (!gotHello) {
        QPacket pack = protocol->read();
        QString name;

        pack >> name;

        bool validHello = false;
        if (name == clientId) {
            int op = -1;
            pack >> op;
            if (op == 0) {
                int version = -1;
                pack >> version;
                if (version == protocolVersion) {
                    QStringList pluginNames;
                    QList<float> pluginVersions;
                    pack >> pluginNames;
                    if (!pack.isEmpty())
                        pack >> pluginVersions;

                    const int pluginNamesSize = pluginNames.size();
                    const int pluginVersionsSize = pluginVersions.size();
                    for (int i = 0; i < pluginNamesSize; ++i) {
                        float pluginVersion = 1.0;
                        if (i < pluginVersionsSize)
                            pluginVersion = pluginVersions.at(i);
                        serverPlugins.insert(pluginNames.at(i), pluginVersion);
                    }

                    pack >> q->m_dataStreamVersion;
                    validHello = true;
                }
            }
        }

        if (!validHello) {
            qWarning("QQmlDebugConnection: Invalid hello message");
            QObject::disconnect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
            return;
        }
        gotHello = true;

        QHash<QString, QQmlDebugClient *>::Iterator iter = plugins.begin();
        for (; iter != plugins.end(); ++iter) {
            QQmlDebugClient::State newState = QQmlDebugClient::Unavailable;
            if (serverPlugins.contains(iter.key()))
                newState = QQmlDebugClient::Enabled;
            iter.value()->stateChanged(newState);
        }

        handshakeTimer.stop();
        handshakeEventLoop.quit();
    }

    while (protocol->packetsAvailable()) {
        QPacket pack = protocol->read();
        QString name;
        pack >> name;

        if (name == clientId) {
            int op = -1;
            pack >> op;

            if (op == 1) {
                // Service Discovery
                QHash<QString, float> oldServerPlugins = serverPlugins;
                serverPlugins.clear();

                QStringList pluginNames;
                QList<float> pluginVersions;
                pack >> pluginNames;
                if (!pack.isEmpty())
                    pack >> pluginVersions;

                const int pluginNamesSize = pluginNames.size();
                const int pluginVersionsSize = pluginVersions.size();
                for (int i = 0; i < pluginNamesSize; ++i) {
                    float pluginVersion = 1.0;
                    if (i < pluginVersionsSize)
                        pluginVersion = pluginVersions.at(i);
                    serverPlugins.insert(pluginNames.at(i), pluginVersion);
                }

                QHash<QString, QQmlDebugClient *>::Iterator iter = plugins.begin();
                for (; iter != plugins.end(); ++iter) {
                    const QString pluginName = iter.key();
                    QQmlDebugClient::State newSate = QQmlDebugClient::Unavailable;
                    if (serverPlugins.contains(pluginName))
                        newSate = QQmlDebugClient::Enabled;

                    if (oldServerPlugins.contains(pluginName)
                            != serverPlugins.contains(pluginName)) {
                        iter.value()->stateChanged(newSate);
                    }
                }
            } else {
                qWarning() << "QQmlDebugConnection: Unknown control message id" << op;
            }
        } else {
            QByteArray message;
            pack >> message;

            QHash<QString, QQmlDebugClient *>::Iterator iter =
                    plugins.find(name);
            if (iter == plugins.end()) {
                qWarning() << "QQmlDebugConnection: Message received for missing plugin" << name;
            } else {
                (*iter)->messageReceived(message);
            }
        }
    }
}

void QQmlDebugConnectionPrivate::deviceAboutToClose()
{
    // This is nasty syntax but we want to emit our own aboutToClose signal (by calling QIODevice::close())
    // without calling the underlying device close fn as that would cause an infinite loop
    q->QIODevice::close();
}

void QQmlDebugConnectionPrivate::handshakeTimeout()
{
    if (!gotHello) {
        qWarning() << "Qml Debug Client: Did not get handshake answer in time";
        handshakeEventLoop.quit();
    }
}

QQmlDebugConnection::QQmlDebugConnection(QObject *parent)
    : QIODevice(parent), d(new QQmlDebugConnectionPrivate(this)),
      m_dataStreamVersion(QDataStream::Qt_5_0)
{
}

QQmlDebugConnection::~QQmlDebugConnection()
{
    QHash<QString, QQmlDebugClient*>::iterator iter = d->plugins.begin();
    for (; iter != d->plugins.end(); ++iter) {
        iter.value()->d->connection = 0;
        iter.value()->stateChanged(QQmlDebugClient::NotConnected);
    }
}

void QQmlDebugConnection::setDataStreamVersion(int dataStreamVersion)
{
    m_dataStreamVersion = dataStreamVersion;
}

int QQmlDebugConnection::dataStreamVersion()
{
    return m_dataStreamVersion;
}

bool QQmlDebugConnection::isConnected() const
{
    return state() == QAbstractSocket::ConnectedState;
}

qint64 QQmlDebugConnection::readData(char *data, qint64 maxSize)
{
    return d->device->read(data, maxSize);
}

qint64 QQmlDebugConnection::writeData(const char *data, qint64 maxSize)
{
    return d->device->write(data, maxSize);
}

qint64 QQmlDebugConnection::bytesAvailable() const
{
    return d->device->bytesAvailable();
}

bool QQmlDebugConnection::isSequential() const
{
    return true;
}

void QQmlDebugConnection::close()
{
    if (isOpen()) {
        QIODevice::close();
        d->device->close();
        emit stateChanged(QAbstractSocket::UnconnectedState);

        QHash<QString, QQmlDebugClient*>::iterator iter = d->plugins.begin();
        for (; iter != d->plugins.end(); ++iter) {
            iter.value()->stateChanged(QQmlDebugClient::NotConnected);
        }
    }
}

bool QQmlDebugConnection::waitForConnected(int msecs)
{
    QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
    if (!socket)
        return false;
    if (!socket->waitForConnected(msecs))
        return false;
    // wait for handshake
    d->handshakeTimer.start();
    d->handshakeEventLoop.exec();
    return d->gotHello;
}

QString QQmlDebugConnection::stateString() const
{
   QString state;

   if (isConnected())
       state = "Connected";
   else
       state = "Not connected";

   if (d->gotHello)
       state += ", got hello";
   else
       state += ", did not get hello!";

   return state;
}

QAbstractSocket::SocketState QQmlDebugConnection::state() const
{
    QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
    if (socket)
        return socket->state();

    return QAbstractSocket::UnconnectedState;
}

void QQmlDebugConnection::flush()
{
    QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
    if (socket) {
        socket->flush();
        return;
    }
}

void QQmlDebugConnection::connectToHost(const QString &hostName, quint16 port)
{
    QTcpSocket *socket = new QTcpSocket(d);
    socket->setProxy(QNetworkProxy::NoProxy);
    d->device = socket;
    d->connectDeviceSignals();
    d->gotHello = false;
    connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SIGNAL(stateChanged(QAbstractSocket::SocketState)));
    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SIGNAL(error(QAbstractSocket::SocketError)));
    connect(socket, SIGNAL(connected()), this, SIGNAL(connected()));
    socket->connectToHost(hostName, port);
    QIODevice::open(ReadWrite | Unbuffered);
}

void QQmlDebugConnectionPrivate::connectDeviceSignals()
{
    connect(device, SIGNAL(bytesWritten(qint64)), q, SIGNAL(bytesWritten(qint64)));
    connect(device, SIGNAL(readyRead()), q, SIGNAL(readyRead()));
    connect(device, SIGNAL(aboutToClose()), this, SLOT(deviceAboutToClose()));
}

//

QQmlDebugClientPrivate::QQmlDebugClientPrivate()
    : connection(0)
{
}

QQmlDebugClient::QQmlDebugClient(const QString &name,
                                                 QQmlDebugConnection *parent)
    : QObject(parent),
      d(new QQmlDebugClientPrivate)
{
    d->name = name;
    d->connection = parent;

    if (!d->connection)
        return;

    if (d->connection->d->plugins.contains(name)) {
        qWarning() << "QQmlDebugClient: Conflicting plugin name" << name;
        d->connection = 0;
    } else {
        d->connection->d->plugins.insert(name, this);
        d->connection->d->advertisePlugins();
    }
}

QQmlDebugClient::~QQmlDebugClient()
{
    if (d->connection && d->connection->d) {
        d->connection->d->plugins.remove(d->name);
        d->connection->d->advertisePlugins();
    }
    delete d;
}

QString QQmlDebugClient::name() const
{
    return d->name;
}

float QQmlDebugClient::serviceVersion() const
{
    if (d->connection->d->serverPlugins.contains(d->name))
        return d->connection->d->serverPlugins.value(d->name);
    return -1;
}

QQmlDebugClient::State QQmlDebugClient::state() const
{
    if (!d->connection
            || !d->connection->isConnected()
            || !d->connection->d->gotHello)
        return NotConnected;

    if (d->connection->d->serverPlugins.contains(d->name))
        return Enabled;

    return Unavailable;
}

QString QQmlDebugClient::stateString() const
{
    switch (state()) {
    case NotConnected: return QLatin1String("Not connected");
    case Unavailable: return QLatin1String("Unavailable");
    case Enabled: return QLatin1String("Enabled");
    }
    return QLatin1String("Invalid");
}

void QQmlDebugClient::sendMessage(const QByteArray &message)
{
    if (state() != Enabled)
        return;

    QPacket pack;
    pack << d->name << message;
    d->connection->d->protocol->send(pack);
    d->connection->flush();
}

void QQmlDebugClient::stateChanged(State)
{
}

void QQmlDebugClient::messageReceived(const QByteArray &)
{
}

#include <qqmldebugclient.moc>
<|endoftext|>