Home | History | Annotate | Download | only in test
      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.
      4 
      5 #ifndef REMOTING_TEST_FAKE_NETWORK_DISPATCHER_H_
      6 #define REMOTING_TEST_FAKE_NETWORK_DISPATCHER_H_
      7 
      8 #include <map>
      9 
     10 #include "base/compiler_specific.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "base/synchronization/lock.h"
     13 #include "third_party/libjingle/source/talk/p2p/base/packetsocketfactory.h"
     14 
     15 namespace base {
     16 class SingleThreadTaskRunner;
     17 }  // namespace base
     18 
     19 namespace net {
     20 class IOBuffer;
     21 }  // namespace net
     22 
     23 namespace remoting {
     24 
     25 class FakeNetworkDispatcher
     26     : public base::RefCountedThreadSafe<FakeNetworkDispatcher> {
     27  public:
     28   class Node {
     29    public:
     30     virtual ~Node() {};
     31 
     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;
     36 
     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;
     42   };
     43 
     44   FakeNetworkDispatcher();
     45 
     46   rtc::IPAddress AllocateAddress();
     47 
     48   // Must be called on the thread that the |node| works on.
     49   void AddNode(Node* node);
     50   void RemoveNode(Node* node);
     51 
     52   void DeliverPacket(const rtc::SocketAddress& from,
     53                      const rtc::SocketAddress& to,
     54                      const scoped_refptr<net::IOBuffer>& data,
     55                      int data_size);
     56 
     57  private:
     58   typedef std::map<rtc::IPAddress, Node*> NodesMap;
     59 
     60   friend class base::RefCountedThreadSafe<FakeNetworkDispatcher>;
     61   virtual ~FakeNetworkDispatcher();
     62 
     63   NodesMap nodes_;
     64   base::Lock nodes_lock_;
     65 
     66   // A counter used to allocate unique addresses in AllocateAddress().
     67   int allocated_address_;
     68 
     69   DISALLOW_COPY_AND_ASSIGN(FakeNetworkDispatcher);
     70 };
     71 
     72 }  // namespace remoting
     73 
     74 #endif  // REMOTING_TEST_FAKE_NETWORK_DISPATCHER_H_
     75