<commit_before>// Copyright (c) 2011 <NAME> and MIPT students. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <map>
#include <set>

#include "third_party/googletest/include/gtest/gtest.h"
#include "third_party/googlemock/include/gmock/gmock.h"

#include "base/common.h"
#include "base/random.h"

using namespace testing;

// Dependency injection
// http://en.wikipedia.org/wiki/Dependency_injection

class UdpChannel {
 public:
  virtual void Send(const char *buffer, size_t size) = 0;
  virtual bool Receive(char *buffer, size_t *size) = 0;
};

class MockUdpChannel : public UdpChannel {
 public:
  MOCK_METHOD2(Send, void(const char *buffer, size_t size));
  MOCK_METHOD2(Receive, bool(char *buffer, size_t *size));
};

class ReliableUdpChannel {
 public:
  ReliableUdpChannel(UdpChannel *chan) : channel_(chan), next_receive_(0) { }

  void Heartbeat() {
    const size_t MAX_BUFFER = 1024;

    char local_buffer[MAX_BUFFER];
    size_t local_size = MAX_BUFFER;

    if (!retry_ids_.empty()) {
      // TODO(timurrrr): throttle to avoid excessive traffic
      local_buffer[0] = 'R';
      local_buffer[1] = '0' + *retry_ids_.begin();
      local_buffer[2] = '\0';
      channel_->Send(local_buffer, 3);
    }

    // TODO(timurrrr): check return value
    channel_->Receive(local_buffer, &local_size);

    // TODO(timurrrr): the header should be int or larger...
    CHECK(*local_buffer >= '0' && *local_buffer <= '9');
    size_t index = *local_buffer - '0';
    if (received_.find(index) != received_.end() || index < next_receive_) {
      // Hm, packet received twice?.. Drop it.
      return;
    }
    char * payload = new char[local_size - 1];
    memcpy(payload, local_buffer + 1, local_size - 1);

    received_[index] = std::pair<size_t,char*>(local_size - 1, payload);<commit_msg>Add one more TODO to mocks_and_fakes<commit_after>// Copyright (c) 2011 <NAME> and MIPT students. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <map>
#include <set>

#include "third_party/googletest/include/gtest/gtest.h"
#include "third_party/googlemock/include/gmock/gmock.h"

#include "base/common.h"
#include "base/random.h"

using namespace testing;

// Dependency injection
// http://en.wikipedia.org/wiki/Dependency_injection

class UdpChannel {
 public:
  virtual void Send(const char *buffer, size_t size) = 0;
  virtual bool Receive(char *buffer, size_t *size) = 0;
};

class MockUdpChannel : public UdpChannel {
 public:
  MOCK_METHOD2(Send, void(const char *buffer, size_t size));
  MOCK_METHOD2(Receive, bool(char *buffer, size_t *size));
};

class ReliableUdpChannel {
 public:
  ReliableUdpChannel(UdpChannel *chan) : channel_(chan), next_receive_(0) { }

  void Heartbeat() {
    const size_t MAX_BUFFER = 1024;

    char local_buffer[MAX_BUFFER];
    size_t local_size = MAX_BUFFER;

    if (!retry_ids_.empty()) {
      // TODO(timurrrr): throttle to avoid excessive traffic
      local_buffer[0] = 'R';
      local_buffer[1] = '0' + *retry_ids_.begin();
      local_buffer[2] = '\0';
      channel_->Send(local_buffer, 3);
    }

    // TODO(timurrrr): check return value
    channel_->Receive(local_buffer, &local_size);

    // TODO(timurrrr): the header should be int or larger...
    CHECK(*local_buffer >= '0' && *local_buffer <= '9');
    size_t index = *local_buffer - '0';
    if (received_.find(index) != received_.end() || index < next_receive_) {
      // Hm, packet received twice?.. Drop it.
      return;
    }

    // TODO(timurrrr): drop packets if received_.size() is too large.

    char * payload = new char[local_size - 1];
    memcpy(payload, local_buffer + 1, local_size - 1);

    received_[index] = std::pair<size_t,char*>(local_size - 1, payload);
{'fraction_non_alphanumeric': 0.13531694695989652, 'fraction_numerical': 0.013454075032341526, 'mean_word_length': 3.697448359659781, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1066254', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1276, 'n_words': 342}