#ifndef CHROME_TEST_CHROMEDRIVER_NET_PORT_SERVER_H_
#define CHROME_TEST_CHROMEDRIVER_NET_PORT_SERVER_H_
#include <list>
#include <set>
#include <string>
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "base/synchronization/lock.h"
class Status;
class PortReservation {
 public:
  PortReservation(const base::Closure& on_free_func, int port);
  ~PortReservation();
  void Leak();
 private:
  base::Closure on_free_func_;
  int port_;
};
class PortServer {
 public:
  
  
  explicit PortServer(const std::string& path);
  ~PortServer();
  Status ReservePort(int* port, scoped_ptr<PortReservation>* reservation);
 private:
  Status RequestPort(int* port);
  void ReleasePort(int port);
  std::string path_;
  base::Lock free_lock_;
  std::list<int> free_;
};
class PortManager {
 public:
  PortManager(int min_port, int max_port);
  ~PortManager();
  Status ReservePort(int* port, scoped_ptr<PortReservation>* reservation);
  
  
  Status ReservePortFromPool(int* port,
                             scoped_ptr<PortReservation>* reservation);
 private:
  int FindAvailablePort() const;
  void ReleasePort(int port);
  void ReleasePortToPool(int port);
  base::Lock lock_;
  std::set<int> taken_;
  std::list<int> unused_forwarded_port_;
  int min_port_;
  int max_port_;
};
#endif