1// Copyright (c) 2009 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 NET_TOOLS_FLIP_SERVER_OUTPUT_ORDERING_H_
6#define NET_TOOLS_FLIP_SERVER_OUTPUT_ORDERING_H_
7
8#include <list>
9#include <map>
10#include <string>
11
12#include "base/basictypes.h"
13#include "net/tools/flip_server/constants.h"
14#include "net/tools/flip_server/epoll_server.h"
15#include "net/tools/flip_server/mem_cache.h"
16
17namespace net {
18
19class SMConnectionInterface;
20
21class OutputOrdering {
22 public:
23  typedef std::list<MemCacheIter> PriorityRing;
24  typedef std::map<uint32, PriorityRing> PriorityMap;
25
26  struct PriorityMapPointer {
27    PriorityMapPointer(): ring(NULL), alarm_enabled(false) {}
28    PriorityRing* ring;
29    PriorityRing::iterator it;
30    bool alarm_enabled;
31    EpollServer::AlarmRegToken alarm_token;
32  };
33
34  typedef std::map<uint32, PriorityMapPointer> StreamIdToPriorityMap;
35
36  StreamIdToPriorityMap stream_ids_;
37  PriorityMap priority_map_;
38  PriorityRing first_data_senders_;
39  uint32 first_data_senders_threshold_;  // when you've passed this, you're no
40                                         // longer a first_data_sender...
41  SMConnectionInterface* connection_;
42  EpollServer* epoll_server_;
43
44  explicit OutputOrdering(SMConnectionInterface* connection);
45  ~OutputOrdering();
46  void Reset();
47  bool ExistsInPriorityMaps(uint32 stream_id);
48
49  struct BeginOutputtingAlarm : public EpollAlarmCallbackInterface {
50   public:
51    BeginOutputtingAlarm(OutputOrdering* oo,
52                         OutputOrdering::PriorityMapPointer* pmp,
53                         const MemCacheIter& mci);
54    virtual ~BeginOutputtingAlarm();
55
56    // EpollAlarmCallbackInterface:
57    virtual int64 OnAlarm();
58    virtual void OnRegistration(const EpollServer::AlarmRegToken& tok,
59                                EpollServer* eps);
60    virtual void OnUnregistration();
61    virtual void OnShutdown(EpollServer* eps);
62
63   private:
64    OutputOrdering* output_ordering_;
65    OutputOrdering::PriorityMapPointer* pmp_;
66    MemCacheIter mci_;
67    EpollServer* epoll_server_;
68  };
69
70  void MoveToActive(PriorityMapPointer* pmp, MemCacheIter mci);
71  void AddToOutputOrder(const MemCacheIter& mci);
72  void SpliceToPriorityRing(PriorityRing::iterator pri);
73  MemCacheIter* GetIter();
74  void RemoveStreamId(uint32 stream_id);
75
76  static double server_think_time_in_s() { return server_think_time_in_s_; }
77  static void set_server_think_time_in_s(double value) {
78    server_think_time_in_s_ = value;
79  }
80
81 private:
82  static double server_think_time_in_s_;
83};
84
85}  // namespace net
86
87#endif  // NET_TOOLS_FLIP_SERVER_OUTPUT_ORDERING_H_
88
89