Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / remoting / test / fake_network_dispatcher.h
blob7ecbee9deb4368561f1001a14014e38eaaa9818f
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef REMOTING_TEST_FAKE_NETWORK_DISPATCHER_H_
6 #define REMOTING_TEST_FAKE_NETWORK_DISPATCHER_H_
8 #include <map>
10 #include "base/compiler_specific.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/synchronization/lock.h"
13 #include "third_party/webrtc/p2p/base/packetsocketfactory.h"
15 namespace base {
16 class SingleThreadTaskRunner;
17 } // namespace base
19 namespace net {
20 class IOBuffer;
21 } // namespace net
23 namespace remoting {
25 class FakeNetworkDispatcher
26 : public base::RefCountedThreadSafe<FakeNetworkDispatcher> {
27 public:
28 class Node {
29 public:
30 virtual ~Node() {}
32 // Return thread on which ReceivePacket() should be called.
33 virtual const scoped_refptr<base::SingleThreadTaskRunner>& GetThread()
34 const = 0;
35 virtual const rtc::IPAddress& GetAddress() const = 0;
37 // Deliver a packet sent by a different node.
38 virtual void ReceivePacket(const rtc::SocketAddress& from,
39 const rtc::SocketAddress& to,
40 const scoped_refptr<net::IOBuffer>& data,
41 int data_size) = 0;
44 FakeNetworkDispatcher();
46 rtc::IPAddress AllocateAddress();
48 // Must be called on the thread that the |node| works on.
49 void AddNode(Node* node);
50 void RemoveNode(Node* node);
52 void DeliverPacket(const rtc::SocketAddress& from,
53 const rtc::SocketAddress& to,
54 const scoped_refptr<net::IOBuffer>& data,
55 int data_size);
57 private:
58 typedef std::map<rtc::IPAddress, Node*> NodesMap;
60 friend class base::RefCountedThreadSafe<FakeNetworkDispatcher>;
61 virtual ~FakeNetworkDispatcher();
63 NodesMap nodes_;
64 base::Lock nodes_lock_;
66 // A counter used to allocate unique addresses in AllocateAddress().
67 int allocated_address_;
69 DISALLOW_COPY_AND_ASSIGN(FakeNetworkDispatcher);
72 } // namespace remoting
74 #endif // REMOTING_TEST_FAKE_NETWORK_DISPATCHER_H_