Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
remoteserver.cc
Go to the documentation of this file.
1 
19 
20 #if !ION_PRODUCTION
21 
22 #include "ion/base/invalid.h"
23 #include "ion/base/logging.h"
24 #include "ion/base/once.h"
25 #include "ion/base/stringutils.h"
34 
35 ION_REGISTER_ASSETS(IonRemoteGetUri);
36 ION_REGISTER_ASSETS(IonRemoteRoot);
37 
38 #endif // !ION_PRODUCTION
39 
40 namespace ion {
41 namespace remote {
42 
43 namespace {
44 
45 #if !ION_PRODUCTION
46 
47 static const int kRemoteThreads = 8;
48 static const char kRootPage[] =
49  "<!DOCTYPE html>"
50  "<html>\n"
51  "<head>\n"
52  " <title>Ion Remote</title>\n"
53  " <link rel=\"stylesheet\" href=\"/ion/css/style.css\">\n"
54  " <script type=\"text/javascript\">\n"
55  " window.location = \"/ion/settings/#^\"\n"
56  " </script>\n"
57  "</head>\n"
58  "<body></body>\n"
59  "</html>\n";
60 
61 class IonRootHandler : public HttpServer::RequestHandler {
62  public:
63  IonRootHandler() : RequestHandler("/ion") {}
64  ~IonRootHandler() override {}
65 
66  const std::string HandleRequest(const std::string& path_in,
67  const HttpServer::QueryMap& args,
68  std::string* content_type) override {
69  const std::string path = path_in.empty() ? "index.html" : path_in;
70  if (path == "index.html") {
71  return kRootPage;
72  } else {
73  const std::string& data =
75  return base::IsInvalidReference(data) ? std::string() : data;
76  }
77  }
78 };
79 
82 class RootHandler : public HttpServer::RequestHandler {
83  public:
84  RootHandler() : RequestHandler("/") {}
85  ~RootHandler() override {}
86 
87  const std::string HandleRequest(const std::string& path_in,
88  const HttpServer::QueryMap& args,
89  std::string* content_type) override {
90  if (path_in.empty() || path_in == "index.html") {
91  *content_type = "text/html";
92  return kRootPage;
93  } else {
94  return std::string();
95  }
96  }
97 };
98 
99 static void RegisterAssetsForRemoteServer() {
100  IonRemoteGetUri::RegisterAssets();
101  IonRemoteRoot::RegisterAssets();
102 }
103 
104 #else
105 
106 static const int kRemoteThreads = 0;
107 
108 #endif // !ION_PRODUCTION
109 
110 } // anonymous namespace
111 
113  : HttpServer(port, kRemoteThreads) {
114  Init(port);
115 }
116 
118  const gfx::RendererPtr& renderer,
119  const gfxutils::ShaderManagerPtr& shader_manager,
120  const gfxutils::FramePtr& frame,
121  int port) : HttpServer(port, kRemoteThreads) {
122 #if !ION_PRODUCTION
123  Init(port);
125  node_graph_handler_.Reset(new NodeGraphHandler);
126  node_graph_handler_->SetFrame(frame);
127  RegisterHandler(node_graph_handler_);
128 
132  new CallTraceHandler()));
135  new ResourceHandler(renderer)));
138  new SettingHandler()));
141  new ShaderHandler(shader_manager, renderer)));
144  new TracingHandler(frame, renderer)));
145 #endif
146 }
147 
148 void RemoteServer::Init(int port) {
149 #if !ION_PRODUCTION
150  ION_STATIC_ONCE(RegisterAssetsForRemoteServer);
151 
152  static const char kHeaderHtml[] =
153  "<div class=\"ion_header\">\n"
154  "<span><a href=\"/ion/resources/\">OpenGL resources</a></span>\n"
155  "<span><a href=\"/ion/settings/#^\">Settings</a></span>\n"
156  "<span><a href=\"/ion/shaders/shader_editor\">Shader editor</a></span>\n"
157  "<span><a href=\"/ion/nodegraph\">Node graph display</a></span>\n"
158  "<span><a href=\"/ion/tracing\">OpenGL tracing</a></span>\n"
159  "<span><a href=\"/ion/profile\">Run-time profile "
160  "diagram</a></span></div>\n";
161 
162  SetHeaderHtml(kHeaderHtml);
163 
165  if (!IsRunning() && port) {
166  LOG(ERROR) << "*** ION: Unable to start Remote server.";
167  } else {
171  HttpServer::RequestHandlerPtr(new(base::kLongTerm) IonRootHandler));
172  }
173 #endif
174 }
175 
176 void RemoteServer::AddNode(const gfx::NodePtr& node) const {
177 #if !ION_PRODUCTION
178  HttpServer::HandlerMap handler_map = GetHandlers();
179  auto iter = handler_map.find("/ion/nodegraph");
180  if (iter != handler_map.end()) {
181  dynamic_cast<NodeGraphHandler*>(iter->second.Get())->AddNode(node);
182  }
183 #endif
184 }
185 
186 bool RemoteServer::RemoveNode(const gfx::NodePtr& node) const {
187 #if !ION_PRODUCTION
188  HttpServer::HandlerMap handler_map = GetHandlers();
189  auto iter = handler_map.find("/ion/nodegraph");
190  if (iter != handler_map.end()) {
191  return dynamic_cast<NodeGraphHandler*>(iter->second.Get())
192  ->RemoveNode(node);
193  }
194 #endif
195 
196  return false;
197 }
198 
200 
201 } // namespace remote
202 } // namespace ion
203 
bool IsInvalidReference(const T &value)
IsInvalidReference() returns true if a passed const reference of type T has an address of InvalidRefe...
Definition: invalid.h:41
CallTraceHandler serves a snapshot of the current call trace.
HandlerMap GetHandlers() const
Returns the handlers registered with this server.
Definition: httpserver.cc:729
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
Definition: logging.h:216
std::map< std::string, RequestHandlerPtr > HandlerMap
Definition: httpserver.h:116
ShaderHandler serves files related to shaders, their dependencies, and the shader editor...
Definition: shaderhandler.h:56
ResourceHandler serves files related to OpenGL resources.
static const std::string & GetFileData(const std::string &filename)
Returns the data of the passed filename if the manager contains it.
SettingHandler serves files related to Settings, including an interface for viewing and modifying the...
base::ReferentPtr< RequestHandler >::Type RequestHandlerPtr
Definition: httpserver.h:115
Copyright 2016 Google Inc.
ION_REGISTER_ASSETS(IonRemoteGetUri)
Copyright 2016 Google Inc.
void RegisterHandler(const RequestHandlerPtr &handler)
Registers the passed handler at the path returned by handler->GetBasePath().
Definition: httpserver.cc:714
bool RemoveNode(const gfx::NodePtr &node) const
Removes a Node from the NodeGraphHandler.
NodeGraphHandler serves files to display Ion node graphs as text or HTML using the gfxutils::Printer ...
void Reset(T *new_shared)
Changes the pointer to point to the given shared, which may be NULL.
Definition: sharedptr.h:92
bool IsRunning() const
Returns whether the server is running.
Definition: httpserver.cc:710
RemoteServer(int port)
Starts a RemoteServer on the passed port.
#define ION_STATIC_ONCE(function)
Executes a given static void() or T() function exactly once.
Definition: once.h:133
std::map< std::string, std::string > QueryMap
Definition: httpserver.h:35
kLongTerm is used for objects that have persistent lifetimes, such as managers.
Definition: allocator.h:44
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
void AddNode(const gfx::NodePtr &node) const
Adds a Node to the NodeGraphHandler which allows Remote to inspect it in the web interface.
void SetHeaderHtml(const std::string &str)
Definition: httpserver.h:159
TracingHandler serves files related to OpenGL tracing.