summaryrefslogtreecommitdiffstats
path: root/src/stateless
diff options
context:
space:
mode:
authorimarom <imarom@cisco.com>2016-04-03 18:19:20 +0300
committerimarom <imarom@cisco.com>2016-04-04 09:49:54 +0300
commit4eacb570cf24927de536d23671f50609f1a9ffa5 (patch)
tree83b8dcd86994c7668a054413d0ba0449a1cf2816 /src/stateless
parent0eb15b2e851b5f50669633678143c5a1d3a7d95b (diff)
API classes (versions)
Diffstat (limited to 'src/stateless')
-rw-r--r--src/stateless/cp/trex_api_class.h110
-rw-r--r--src/stateless/cp/trex_exception.h41
-rw-r--r--src/stateless/cp/trex_stateless.cpp3
-rw-r--r--src/stateless/cp/trex_stateless.h38
-rw-r--r--src/stateless/cp/trex_stateless_port.cpp21
-rw-r--r--src/stateless/cp/trex_stateless_port.h4
6 files changed, 176 insertions, 41 deletions
diff --git a/src/stateless/cp/trex_api_class.h b/src/stateless/cp/trex_api_class.h
new file mode 100644
index 00000000..78933d23
--- /dev/null
+++ b/src/stateless/cp/trex_api_class.h
@@ -0,0 +1,110 @@
+/*
+ Itay Marom
+ Cisco Systems, Inc.
+*/
+
+/*
+Copyright (c) 2015-2015 Cisco Systems, Inc.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+#ifndef __TREX_API_CLASS_H__
+#define __TREX_API_CLASS_H__
+
+#include <assert.h>
+
+#include "common/basic_utils.h"
+#include "trex_exception.h"
+
+/**
+ * API exception
+ *
+ * @author imarom (03-Apr-16)
+ */
+class TrexAPIException : public TrexException {
+public:
+ TrexAPIException(const std::string &what) : TrexException(what) {
+ }
+};
+
+/**
+ * define an API class
+ *
+ * @author imarom (03-Apr-16)
+ */
+class APIClass {
+public:
+
+ enum type_e {
+ API_CLASS_TYPE_CORE = 0,
+ API_CLASS_TYPE_MAX,
+
+ API_CLASS_TYPE_NO_API
+ };
+
+ static const char * type_to_name(type_e type) {
+ switch (type) {
+ case API_CLASS_TYPE_CORE:
+ return "core";
+ default:
+ assert(0);
+ }
+ }
+
+ APIClass() {
+ /* invalid */
+ m_type = API_CLASS_TYPE_MAX;
+ }
+
+ void init(type_e type, int major, int minor) {
+ m_type = type;
+ m_major = major;
+ m_minor = minor;
+
+ unsigned int seed = time(NULL);
+ m_handler = utl_generate_random_str(seed, 8);
+ }
+
+ std::string & verify_api(int major, int minor) {
+ std::stringstream ss;
+ ss << "API type '" << type_to_name(m_type) << "': ";
+
+ assert(m_type < API_CLASS_TYPE_MAX);
+
+ /* for now a simple major check */
+ if (major < m_major) {
+ ss << "server has a major newer API version - server: '" << m_major << "', client: '" << major << "'";
+ throw TrexAPIException(ss.str());
+ }
+
+ if (major > m_major) {
+ ss << "server has an older API version - server: '" << m_major << "', client: '" << major << "'";
+ throw TrexAPIException(ss.str());
+ }
+
+ return get_api_handler();
+ }
+
+ std::string & get_api_handler() {
+ return m_handler;
+ }
+
+private:
+ type_e m_type;
+ int m_major;
+ int m_minor;
+ std::string m_handler;
+
+};
+
+#endif /* __TREX_API_CLASS_H__ */
diff --git a/src/stateless/cp/trex_exception.h b/src/stateless/cp/trex_exception.h
new file mode 100644
index 00000000..b9e20761
--- /dev/null
+++ b/src/stateless/cp/trex_exception.h
@@ -0,0 +1,41 @@
+/*
+ Itay Marom
+ Cisco Systems, Inc.
+*/
+
+/*
+Copyright (c) 2015-2015 Cisco Systems, Inc.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+#ifndef __TREX_EXCEPTION_H__
+#define __TREX_EXCEPTION_H__
+
+#include <stdexcept>
+#include <string>
+
+/**
+ * generic exception for errors
+ * TODO: move this to a better place
+ */
+class TrexException : public std::runtime_error
+{
+public:
+ TrexException() : std::runtime_error("") {
+
+ }
+ TrexException(const std::string &what) : std::runtime_error(what) {
+ }
+};
+
+#endif /* __TREX_EXCEPTION_H__ */
diff --git a/src/stateless/cp/trex_stateless.cpp b/src/stateless/cp/trex_stateless.cpp
index 9df57a50..f6f81b96 100644
--- a/src/stateless/cp/trex_stateless.cpp
+++ b/src/stateless/cp/trex_stateless.cpp
@@ -53,6 +53,8 @@ TrexStateless::TrexStateless(const TrexStatelessCfg &cfg) {
m_platform_api = cfg.m_platform_api;
m_publisher = cfg.m_publisher;
+ /* API core version */
+ m_api_classes[APIClass::API_CLASS_TYPE_CORE].init(APIClass::API_CLASS_TYPE_CORE, 1, 0);
}
/**
@@ -175,3 +177,4 @@ TrexStateless::generate_publish_snapshot(std::string &snapshot) {
snapshot = writer.write(root);
}
+
diff --git a/src/stateless/cp/trex_stateless.h b/src/stateless/cp/trex_stateless.h
index 7db86174..b506da61 100644
--- a/src/stateless/cp/trex_stateless.h
+++ b/src/stateless/cp/trex_stateless.h
@@ -27,27 +27,18 @@ limitations under the License.
#include <mutex>
-#include <trex_stream.h>
-#include <trex_stateless_port.h>
-#include <trex_rpc_server_api.h>
-#include <publisher/trex_publisher.h>
+#include "trex_stream.h"
+#include "trex_stateless_port.h"
+#include "trex_rpc_server_api.h"
-#include <flow_stat.h>
-#include <internal_api/trex_platform_api.h>
+#include "publisher/trex_publisher.h"
+#include "internal_api/trex_platform_api.h"
-/**
- * generic exception for errors
- * TODO: move this to a better place
- */
-class TrexException : public std::runtime_error
-{
-public:
- TrexException() : std::runtime_error("") {
+#include "flow_stat.h"
- }
- TrexException(const std::string &what) : std::runtime_error(what) {
- }
-};
+
+#include "trex_exception.h"
+#include "trex_api_class.h"
class TrexStatelessPort;
@@ -81,6 +72,7 @@ public:
} m_stats;
};
+
/**
* config object for stateless object
*
@@ -167,6 +159,14 @@ public:
return m_rpc_server;
}
+ const std::string & verify_api(APIClass::type_e type, int major, int minor) {
+ return m_api_classes[type].verify_api(major, minor);
+ }
+
+ const std::string & get_api_handler(APIClass::type_e type) {
+ return m_api_classes[type].get_api_handler();
+ }
+
CFlowStatRuleMgr m_rx_flow_stat;
protected:
@@ -187,6 +187,8 @@ protected:
TrexPublisher *m_publisher;
+ /* API */
+ APIClass m_api_classes[APIClass::API_CLASS_TYPE_MAX];
};
/**
diff --git a/src/stateless/cp/trex_stateless_port.cpp b/src/stateless/cp/trex_stateless_port.cpp
index 6a33fcee..2239f3f6 100644
--- a/src/stateless/cp/trex_stateless_port.cpp
+++ b/src/stateless/cp/trex_stateless_port.cpp
@@ -781,26 +781,5 @@ TrexPortOwner::TrexPortOwner() {
m_seed = time(NULL);
}
-/**
- * generate a random connection handler
- *
- */
-std::string
-TrexPortOwner::generate_handler() {
- std::stringstream ss;
-
- static const char alphanum[] =
- "0123456789"
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- "abcdefghijklmnopqrstuvwxyz";
-
- /* generate 8 bytes of random handler */
- for (int i = 0; i < 8; ++i) {
- ss << alphanum[rand_r(&m_seed) % (sizeof(alphanum) - 1)];
- }
-
- return (ss.str());
-}
-
const std::string TrexPortOwner::g_unowned_name = "<FREE>";
const std::string TrexPortOwner::g_unowned_handler = "";
diff --git a/src/stateless/cp/trex_stateless_port.h b/src/stateless/cp/trex_stateless_port.h
index 7aa3bfa8..2167e735 100644
--- a/src/stateless/cp/trex_stateless_port.h
+++ b/src/stateless/cp/trex_stateless_port.h
@@ -21,6 +21,7 @@ limitations under the License.
#ifndef __TREX_STATELESS_PORT_H__
#define __TREX_STATELESS_PORT_H__
+#include "common/basic_utils.h"
#include "internal_api/trex_platform_api.h"
#include "trex_dp_port_events.h"
#include "trex_stream.h"
@@ -65,7 +66,7 @@ public:
m_owner_name = owner_name;
/* internal data */
- m_handler = generate_handler();
+ m_handler = utl_generate_random_str(m_seed, 8);
m_is_free = false;
}
@@ -83,7 +84,6 @@ public:
private:
- std::string generate_handler();
/* is this port owned by someone ? */
bool m_is_free;