Revision: 7412
          http://playerstage.svn.sourceforge.net/playerstage/?rev=7412&view=rev
Author:   gerkey
Date:     2009-03-10 02:04:52 +0000 (Tue, 10 Mar 2009)

Log Message:
-----------
first cut at websim

Added Paths:
-----------
    code/websim/CMakeLists.txt
    code/websim/src/
    code/websim/src/websim.cc
    code/websim/src/websim.h

Added: code/websim/CMakeLists.txt
===================================================================
--- code/websim/CMakeLists.txt                          (rev 0)
+++ code/websim/CMakeLists.txt  2009-03-10 02:04:52 UTC (rev 7412)
@@ -0,0 +1,13 @@
+project(WebSim)
+
+cmake_minimum_required( VERSION 2.4 FATAL_ERROR )
+
+include_directories(${PROJECT_SOURCE_DIR})
+add_library(websim SHARED src/websim.cc)
+add_library(websim-static STATIC src/websim.cc)
+# Set output name to be the same as shared lib (may not work on Windows)
+set_target_properties(websim-static PROPERTIES OUTPUT_NAME websim)
+# Prevent deletion of existing lib of same name
+set_target_properties(websim-static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
+
+target_link_libraries(websim event)

Added: code/websim/src/websim.cc
===================================================================
--- code/websim/src/websim.cc                           (rev 0)
+++ code/websim/src/websim.cc   2009-03-10 02:04:52 UTC (rev 7412)
@@ -0,0 +1,109 @@
+/*
+ *  WebSim - Library for web-enabling and federating simulators.
+ *  Copyright (C) 2009
+ *    Richard Vaughan, Brian Gerkey, and Nate Koenig
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */ 
+
+/* Desc: HTTP portal to libgazebo
+ * Author: Brian Gerkey
+ * Date: 9 March 2009
+ * SVN: $Id: gazebo.h 7398 2009-03-09 07:21:49Z natepak $
+ */
+
+#include "websim.h"
+
+#include <assert.h>
+#include <stdlib.h>
+
+WebSim::WebSim(const std::string& _fedfile,
+               const std::string& _host, 
+               int _port) :
+        fedfile(_fedfile), host(_host), port(_port)
+{
+  // Set up the HTTP server
+  // Not sure whether it's safe to do this more that once in one process
+  event_init();
+
+  printf("[websim] Starting HTTP server listening on %s:%d...",
+         this->host.c_str(), this->port);
+  fflush(stdout);
+  this->eh = evhttp_start(this->host.c_str(), this->port);
+  assert(eh);
+  evhttp_set_gencb(eh, &WebSim::EventCallback, (void*)this);
+  puts("Done.");
+
+  puts("[websim] Ready");
+}
+
+
+WebSim::~WebSim()
+{
+  // No event_fini() to call...
+}
+
+void
+WebSim::Update()
+{
+  event_dispatch();
+}
+
+void 
+WebSim::StringSplit(const std::string &s, 
+                    std::vector<std::string> &t, 
+                    const std::string &d)
+{
+  t.clear();
+  size_t start = 0, end;
+  while ((end = s.find_first_of(d, start)) != std::string::npos)
+  {
+    t.push_back(s.substr(start, end-start));
+    start = end + 1;
+  }
+  t.push_back(s.substr(start));
+}
+
+// Strange that libevent doesn't provide a way to free the evkeyvalq
+// structure.
+void
+WebSim::DeleteKeyVal(struct evkeyvalq* query_args)
+{
+  struct evkeyval* kv;
+  TAILQ_FOREACH(kv, query_args, next)
+  {
+    free(kv->key);
+    free(kv->value);
+    TAILQ_REMOVE(query_args, kv, next);
+  }
+}
+
+bool
+WebSim::GetValue(std::string& value,
+                 struct evkeyvalq* query_args, 
+                 const std::string& key)
+{
+  struct evkeyval* kv;
+  TAILQ_FOREACH(kv, query_args, next)
+  {
+    if(key == kv->key)
+    {
+      value = std::string(kv->value);
+      return true;
+    }
+  }
+  return false;
+}

Added: code/websim/src/websim.h
===================================================================
--- code/websim/src/websim.h                            (rev 0)
+++ code/websim/src/websim.h    2009-03-10 02:04:52 UTC (rev 7412)
@@ -0,0 +1,86 @@
+/*
+ *  WebSim - Library for web-enabling and federating simulators.
+ *  Copyright (C) 2009
+ *    Richard Vaughan, Brian Gerkey, and Nate Koenig
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */ 
+
+/* Desc: HTTP portal to libgazebo
+ * Author: Brian Gerkey
+ * Date: 9 March 2009
+ * SVN: $Id: gazebo.h 7398 2009-03-09 07:21:49Z natepak $
+ */
+
+#include <string>
+#include <vector>
+#include <map>
+
+// These headers must be included prior to the libevent headers
+#include <sys/types.h>
+#include <sys/queue.h>
+
+// libevent
+#include <event.h>
+#include <evhttp.h>
+
+class WebSim
+{
+  public:
+    WebSim(const std::string& _fedfile, 
+           const std::string& _host,
+           int _port); // TODO: 3 callbacks
+    ~WebSim();
+
+    void Update();
+
+  private:
+    std::string fedfile;
+    std::string host;
+    int port;
+
+    struct evhttp* eh;
+
+    // Static, so that it can be passed as a callback to libevent
+    static void EventCallback(evhttp_request* req, void* arg);
+
+    void StringSplit(const std::string &s, 
+                     std::vector<std::string> &t, 
+                     const std::string &d);
+    bool GetValue(std::string& value,
+                  struct evkeyvalq* query_args, 
+                  const std::string& key);
+    bool HandleURI(const std::string& model,
+                   const std::string& prop,
+                   const std::string& action,
+                   struct evkeyvalq* kv,
+                   std::string& response);
+    bool HandleSimRequest(const std::string& prop,
+                          const std::string& action,
+                          struct evkeyvalq* kv,
+                          std::string& response);
+    bool HandleModelRequest(const std::string& model,
+                            const std::string& prop,
+                            const std::string& action,
+                            struct evkeyvalq* kv,
+                            std::string& response);
+    bool ParseURI(std::string& model,
+                  std::string& prop,
+                  std::string& action,
+                  std::string uri,
+                  std::string& response);
+    void DeleteKeyVal(struct evkeyvalq* query_args);
+};


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
_______________________________________________
Playerstage-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to