/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2012-2014 University of California, Los Angeles
 *
 * This file is part of ChronoSync, synchronization library for distributed realtime
 * applications for NDN.
 *
 * ChronoSync is free software: you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * ChronoSync 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * ChronoSync, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "socket.hpp"
#include "../unit-test-time-fixture.hpp"
#include <ndn-cxx/util/dummy-client-face.hpp>
#include "boost-test.hpp"

namespace chronosync {
namespace test {

using std::string;
using std::vector;
using std::map;
using ndn::util::DummyClientFace;
using ndn::util::makeDummyClientFace;


/**
 * @brief Emulate an app that use the Socket class
 *
 * The app has two types of data set: one is simply string while the other is integer array.
 * For each type of data set, the app has a specific fetching strategy.
 */
class SocketTestApp : noncopyable
{
public:
  SocketTestApp(const Name& syncPrefix,
                const Name& userPrefix,
                DummyClientFace& face,
                bool isNum)
    : sum(0)
    , socket(syncPrefix,
             userPrefix,
             face,
             isNum ? bind(&SocketTestApp::fetchNumbers, this, _1) :
                     bind(&SocketTestApp::fetchAll, this, _1))
  {
  }

  void
  set(const shared_ptr<const Data>& dataPacket)
  {
    // std::cerr << "set Data" << std::endl;
    Name dataName(dataPacket->getName());
    string str2(reinterpret_cast<const char*>(dataPacket->getContent().value()),
                dataPacket->getContent().value_size());
    data.insert(make_pair(dataName, str2));
  }

  void
  set(Name name, const char* buf, int len)
  {
    string str2(buf, len);
    data.insert(make_pair(name, str2));
  }

  void
  setNum(const shared_ptr<const Data>& dataPacket)
  {
    // std::cerr << "setNum Data" << std::endl;
    size_t n = dataPacket->getContent().value_size() / 4;
    const uint32_t* numbers = reinterpret_cast<const uint32_t*>(dataPacket->getContent().value());
    for (size_t i = 0; i < n; i++) {
      sum += numbers[i];
    }
  }

  void
  setNum(Name name, const uint8_t* buf, int len)
  {
    BOOST_ASSERT(len >= 4);

    int n = len / 4;
    const uint32_t* numbers = reinterpret_cast<const uint32_t*>(buf);
    for (int i = 0; i < n; i++) {
      sum += numbers[i];
    }
  }

  void
  fetchAll(const vector<MissingDataInfo>& v)
  {
    // std::cerr << "fetchAll" << std::endl;
    for (int i = 0; i < v.size(); i++) {
      for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
        socket.fetchData(v[i].session, s, [this] (const shared_ptr<const Data>& dataPacket) {
            this->set(dataPacket);
          });
      }
    }
  }

  void
  fetchNumbers(const vector<MissingDataInfo> &v)
  {
    // std::cerr << "fetchNumbers" << std::endl;
    for (int i = 0; i < v.size(); i++) {
      for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
        socket.fetchData(v[i].session, s, [this] (const shared_ptr<const Data>& dataPacket) {
            this->setNum(dataPacket);
          });
      }
    }
  }

  string
  toString()
  {
    string str = "\n";
    for (map<Name, string>::iterator it = data.begin(); it != data.end(); ++it) {
      str += "<";
      str += it->first.toUri();
      str += "|";
      str += it->second;
      str += ">";
      str += "\n";
    }

    return str;
  }

  map<ndn::Name, string> data;
  uint32_t sum;
  Socket socket;
};

class SocketFixture : public ndn::tests::UnitTestTimeFixture
{
public:
  SocketFixture()
    : syncPrefix("/ndn/broadcast/sync")
  {
    syncPrefix.appendVersion();
    userPrefix[0] = Name("/user0");
    userPrefix[1] = Name("/user1");
    userPrefix[2] = Name("/user2");

    faces[0] = makeDummyClientFace(ref(io), {true, true});
    faces[1] = makeDummyClientFace(ref(io), {true, true});
    faces[2] = makeDummyClientFace(ref(io), {true, true});

    for (int i = 0; i < 3; i++) {
      readInterestOffset[i] = 0;
      readDataOffset[i] = 0;
    }
  }

  void
  passPacket()
  {
    for (int i = 0; i < 3; i++)
      checkFace(i);
  }

  void
  checkFace(int sender)
  {
    while (faces[sender]->sentInterests.size() > readInterestOffset[sender]) {
      for (int i = 0; i < 3; i++) {
        if (sender != i)
          faces[i]->receive(faces[sender]->sentInterests[readInterestOffset[sender]]);
      }
      readInterestOffset[sender]++;
    }
    while (faces[sender]->sentDatas.size() > readDataOffset[sender]) {
      for (int i = 0; i < 3; i++) {
        if (sender != i)
          faces[i]->receive(faces[sender]->sentDatas[readDataOffset[sender]]);
      }
      readDataOffset[sender]++;
    }
  }

  void
  createSocket(size_t idx, bool isNum)
  {
    app[idx] = make_shared<SocketTestApp>(syncPrefix, userPrefix[idx], ref(*faces[idx]), isNum);
    sessionName[idx] = app[idx]->socket.getLogic().getSessionName();
  }

  void
  publishAppData(size_t idx, const string& data)
  {
    app[idx]->socket.publishData(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(),
                                 ndn::time::milliseconds(1000));
  }

  void
  setAppData(size_t idx, SeqNo seqNo, const string& data)
  {
    Name dataName = sessionName[idx];
    dataName.appendNumber(seqNo);
    app[idx]->set(dataName, data.c_str(), data.size());
  }

  void
  publishAppNum(size_t idx, const uint8_t* buf, size_t size)
  {
    app[idx]->socket.publishData(buf, size, ndn::time::milliseconds(1000));
  }

  void
  setAppNum(size_t idx, SeqNo seqNo, const uint8_t* buf, size_t size)
  {
    Name dataName = sessionName[idx];
    dataName.appendNumber(seqNo);
    app[idx]->setNum(dataName, buf, size);
  }

  Name syncPrefix;
  Name userPrefix[3];
  Name sessionName[3];

  shared_ptr<DummyClientFace> faces[3];
  shared_ptr<SocketTestApp> app[3];

  size_t readInterestOffset[3];
  size_t readDataOffset[3];
};



BOOST_FIXTURE_TEST_SUITE(SocketTests, SocketFixture)

BOOST_AUTO_TEST_CASE(BasicData)
{
  createSocket(0, false);
  advanceClocks(ndn::time::milliseconds(10), 5);
  createSocket(1, false);
  advanceClocks(ndn::time::milliseconds(10), 5);
  createSocket(2, false);
  advanceClocks(ndn::time::milliseconds(10), 5);

  string data0 = "Very funny Scotty, now beam down my clothes";
  publishAppData(0, data0);

  for (int i = 0; i < 50; i++) {
    advanceClocks(ndn::time::milliseconds(2), 10);
    passPacket();
  }
  setAppData(0, 1, data0);

  advanceClocks(ndn::time::milliseconds(10), 1);
  BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
  BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());

  string data1 = "Yes, give me that ketchup";
  string data2 = "Don't look conspicuous, it draws fire";
  publishAppData(0, data1);
  advanceClocks(ndn::time::milliseconds(10), 1);
  publishAppData(0, data2);

  for (int i = 0; i < 50; i++) {
    advanceClocks(ndn::time::milliseconds(2), 10);
    passPacket();
  }
  setAppData(0, 2, data1);
  advanceClocks(ndn::time::milliseconds(10), 1);
  setAppData(0, 3, data2);

  advanceClocks(ndn::time::milliseconds(10), 1);
  BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
  BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());

  string data3 = "You surf the Internet, I surf the real world";
  string data4 = "I got a fortune cookie once that said 'You like Chinese food'";
  string data5 = "Real men wear pink. Why? Because their wives make them";
  publishAppData(2, data3);
  advanceClocks(ndn::time::milliseconds(10), 2);
  publishAppData(1, data4);
  advanceClocks(ndn::time::milliseconds(10), 1);
  publishAppData(1, data5);

  for (int i = 0; i < 100; i++) {
    advanceClocks(ndn::time::milliseconds(2), 10);
    passPacket();
  }
  setAppData(2, 1, data3);
  advanceClocks(ndn::time::milliseconds(10), 1);
  setAppData(1, 1, data4);
  advanceClocks(ndn::time::milliseconds(10), 1);
  setAppData(1, 2, data5);

  advanceClocks(ndn::time::milliseconds(10), 7);
  BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
  BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());

  string data6 = "Shakespeare says: 'Prose before hos.'";
  string data7 = "Pick good people, talent never wears out";
  publishAppData(0, data6);
  publishAppData(1, data7);

  for (int i = 0; i < 100; i++) {
    advanceClocks(ndn::time::milliseconds(2), 10);
    passPacket();
  }
  setAppData(0, 4, data6);
  setAppData(1, 3, data7);

  BOOST_CHECK_EQUAL(app[0]->toString(), app[1]->toString());
  BOOST_CHECK_EQUAL(app[0]->toString(), app[2]->toString());
}

BOOST_AUTO_TEST_CASE(BasicNumber)
{
  createSocket(0, true);
  advanceClocks(ndn::time::milliseconds(10), 5);
  createSocket(1, true);
  advanceClocks(ndn::time::milliseconds(10), 5);

  uint32_t num1[5] = {0, 1, 2, 3, 4};
  uint8_t* buf1 = reinterpret_cast<uint8_t*>(num1);
  size_t size1 = sizeof(num1);
  publishAppNum(0, buf1, size1);
  advanceClocks(ndn::time::milliseconds(10), 5);
  setAppNum(0, 0, buf1, size1);

  for (int i = 0; i < 100; i++) {
    advanceClocks(ndn::time::milliseconds(2), 10);
    passPacket();
  }
  BOOST_CHECK_EQUAL(app[0]->sum, app[1]->sum);
  BOOST_CHECK_EQUAL(app[1]->sum, 10);

  uint32_t num2[5] = {9, 7, 2, 1, 1};
  uint8_t* buf2 = reinterpret_cast<uint8_t*>(num2);
  size_t size2 = sizeof(num2);
  publishAppNum(1, buf2, size2);
  setAppNum(1, 0, buf2, size2);

  for (int i = 0; i < 50; i++) {
    advanceClocks(ndn::time::milliseconds(2), 10);
    passPacket();
  }
  BOOST_CHECK_EQUAL(app[0]->sum, app[1]->sum);
  BOOST_CHECK_EQUAL(app[1]->sum, 30);
}

BOOST_AUTO_TEST_SUITE_END()

} // namespace test
} // namespace chronosync
