You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by al...@apache.org on 2017/02/24 15:58:27 UTC
[4/5] nifi-minifi-cpp git commit: MINIFI-206: Implement Extensible
Comm Mechanism
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/include/Site2SitePeer.h
----------------------------------------------------------------------
diff --git a/libminifi/include/Site2SitePeer.h b/libminifi/include/Site2SitePeer.h
index cd082f6..bd49e55 100644
--- a/libminifi/include/Site2SitePeer.h
+++ b/libminifi/include/Site2SitePeer.h
@@ -21,11 +21,6 @@
#define __SITE2SITE_PEER_H__
#include <stdio.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
#include <fcntl.h>
#include <resolv.h>
#include <netdb.h>
@@ -33,83 +28,49 @@
#include <errno.h>
#include <mutex>
#include <atomic>
-#include "TimeUtil.h"
+#include <memory>
#include "Logger.h"
#include "Configure.h"
#include "Property.h"
-// OpenSSL related
-#include <openssl/ssl.h>
-#include <openssl/err.h>
+#include "io/ClientSocket.h"
+#include "io/BaseStream.h"
+#include "utils/TimeUtil.h"
-class CRC32
-{
-public:
- CRC32() {
- crc = 0;
-
- if (tableInit)
- return;
-
- tableInit = true;
- unsigned int poly = 0xedb88320;
- unsigned int temp = 0;
- for(unsigned int i = 0; i < 256; ++i) {
- temp = i;
- for(int j = 8; j > 0; --j) {
- if((temp & 1) == 1) {
- temp = (unsigned int)((temp >> 1) ^ poly);
- }else {
- temp >>= 1;
- }
- }
- table[i] = temp;
- }
- }
-
- unsigned int update(uint8_t * bytes, size_t size) {
- crc = crc ^ ~0U;
- for(unsigned int i = 0; i < size; ++i) {
- uint8_t index = (uint8_t)(((crc) & 0xff) ^ bytes[i]);
- crc = (unsigned int)((crc >> 8) ^ table[index]);
- }
- crc = crc ^ ~0U;
- return crc;
- }
-
- long getCRC()
- {
- return crc;
- }
-
-private:
- static unsigned int table[256];
- static std::atomic<bool> tableInit;
- unsigned int crc;
-};
-static const char MAGIC_BYTES[] = {'N', 'i', 'F', 'i'};
+static const char MAGIC_BYTES[] = { 'N', 'i', 'F', 'i' };
//! Site2SitePeer Class
-class Site2SitePeer
-{
+class Site2SitePeer : public BaseStream{
public:
- //! Constructor
- /*!
+
+
+ Site2SitePeer() : stream_(nullptr),host_(""),port_(-1){
+
+ }
+ /*
* Create a new site2site peer
*/
- Site2SitePeer(std::string host, uint16_t port) {
- _logger = Logger::getLogger();
- _configure = Configure::getConfigure();
- _socket = 0;
- _host = host;
- _port = port;
+ explicit Site2SitePeer(std::unique_ptr<DataStream> injected_socket, const std::string host_, uint16_t port_ ) :
+ host_(host_), port_(port_), stream_(injected_socket.release()){
+ logger_ = Logger::getLogger();
+ configure_ = Configure::getConfigure();
_yieldExpiration = 0;
_timeOut = 30000; // 30 seconds
- _url = "nifi://" + _host + ":" + std::to_string(_port);
- _ssl = NULL;
+ _url = "nifi://" + host_ + ":" + std::to_string(port_);
+ }
+
+ explicit Site2SitePeer(Site2SitePeer &&ss) : stream_( ss.stream_.release()), host_( std::move(ss.host_) ), port_ (std::move(ss.port_) )
+ {
+ logger_ = Logger::getLogger();
+ configure_ = Configure::getConfigure();
+ _yieldExpiration.store(ss._yieldExpiration);
+ _timeOut.store(ss._timeOut);
+ _url = std::move(ss._url);
}
//! Destructor
- virtual ~Site2SitePeer() { Close();}
+ virtual ~Site2SitePeer() {
+ Close();
+ }
//! Set Processor yield period in MilliSecond
void setYieldPeriodMsec(uint64_t period) {
_yieldPeriodMsec = period;
@@ -120,240 +81,179 @@ public:
}
//! Get Processor yield period in MilliSecond
uint64_t getYieldPeriodMsec(void) {
- return(_yieldPeriodMsec);
+ return (_yieldPeriodMsec);
}
//! Yield based on the yield period
- void yield()
- {
+ void yield() {
_yieldExpiration = (getTimeMillis() + _yieldPeriodMsec);
}
//! setHostName
- void setHostName(std::string host)
- {
- _host = host;
- _url = "nifi://" + _host + ":" + std::to_string(_port);
+ void setHostName(std::string host_) {
+ this->host_ = host_;
+ _url = "nifi://" + host_ + ":" + std::to_string(port_);
}
//! setPort
- void setPort(uint16_t port)
- {
- _port = port;
- _url = "nifi://" + _host + ":" + std::to_string(_port);
+ void setPort(uint16_t port_) {
+ this->port_ = port_;
+ _url = "nifi://" + host_ + ":" + std::to_string(port_);
}
//! getHostName
- std::string getHostName()
- {
- return _host;
+ std::string getHostName() {
+ return host_;
}
//! getPort
- uint16_t getPort()
- {
- return _port;
+ uint16_t getPort() {
+ return port_;
}
//! Yield based on the input time
- void yield(uint64_t time)
- {
+ void yield(uint64_t time) {
_yieldExpiration = (getTimeMillis() + time);
}
//! whether need be to yield
- bool isYield()
- {
+ bool isYield() {
if (_yieldExpiration > 0)
return (_yieldExpiration >= getTimeMillis());
else
return false;
}
// clear yield expiration
- void clearYield()
- {
+ void clearYield() {
_yieldExpiration = 0;
}
//! Yield based on the yield period
- void yield(std::string portId)
- {
+ void yield(std::string portId) {
std::lock_guard<std::mutex> lock(_mtx);
uint64_t yieldExpiration = (getTimeMillis() + _yieldPeriodMsec);
_yieldExpirationPortIdMap[portId] = yieldExpiration;
}
//! Yield based on the input time
- void yield(std::string portId, uint64_t time)
- {
+ void yield(std::string portId, uint64_t time) {
std::lock_guard<std::mutex> lock(_mtx);
uint64_t yieldExpiration = (getTimeMillis() + time);
_yieldExpirationPortIdMap[portId] = yieldExpiration;
}
//! whether need be to yield
- bool isYield(std::string portId)
- {
+ bool isYield(std::string portId) {
std::lock_guard<std::mutex> lock(_mtx);
- std::map<std::string, uint64_t>::iterator it = this->_yieldExpirationPortIdMap.find(portId);
- if (it != _yieldExpirationPortIdMap.end())
- {
+ std::map<std::string, uint64_t>::iterator it =
+ this->_yieldExpirationPortIdMap.find(portId);
+ if (it != _yieldExpirationPortIdMap.end()) {
uint64_t yieldExpiration = it->second;
return (yieldExpiration >= getTimeMillis());
- }
- else
- {
+ } else {
return false;
}
}
//! clear yield expiration
- void clearYield(std::string portId)
- {
+ void clearYield(std::string portId) {
std::lock_guard<std::mutex> lock(_mtx);
- std::map<std::string, uint64_t>::iterator it = this->_yieldExpirationPortIdMap.find(portId);
- if (it != _yieldExpirationPortIdMap.end())
- {
+ std::map<std::string, uint64_t>::iterator it =
+ this->_yieldExpirationPortIdMap.find(portId);
+ if (it != _yieldExpirationPortIdMap.end()) {
_yieldExpirationPortIdMap.erase(portId);
}
}
//! setTimeOut
- void setTimeOut(uint64_t time)
- {
+ void setTimeOut(uint64_t time) {
_timeOut = time;
}
//! getTimeOut
- uint64_t getTimeOut()
- {
+ uint64_t getTimeOut() {
return _timeOut;
}
- int write(uint8_t value, CRC32 *crc = NULL)
- {
- return sendData(&value, 1, crc);
+ int write(uint8_t value) {
+ return Serializable::write(value,stream_.get());
}
- int write(char value, CRC32 *crc = NULL)
- {
- return sendData((uint8_t *)&value, 1, crc);
+ int write(char value) {
+ return Serializable::write(value,stream_.get());
}
- int write(uint32_t value, CRC32 *crc = NULL)
- {
- uint8_t temp[4];
+ int write(uint32_t value) {
+
+ return Serializable::write(value,stream_.get());
- temp[0] = (value & 0xFF000000) >> 24;
- temp[1] = (value & 0x00FF0000) >> 16;
- temp[2] = (value & 0x0000FF00) >> 8;
- temp[3] = (value & 0x000000FF);
- return sendData(temp, 4, crc);
}
- int write(uint16_t value, CRC32 *crc = NULL)
- {
- uint8_t temp[2];
- temp[0] = (value & 0xFF00) >> 8;
- temp[1] = (value & 0xFF);
- return sendData(temp, 2, crc);
+ int write(uint16_t value) {
+ return Serializable::write(value,stream_.get());
}
- int write(uint8_t *value, int len, CRC32 *crc = NULL)
- {
- return sendData(value, len, crc);
+ int write(uint8_t *value, int len) {
+ return Serializable::write(value,len,stream_.get());
}
- int write(uint64_t value, CRC32 *crc = NULL)
- {
- uint8_t temp[8];
-
- temp[0] = (value >> 56) & 0xFF;
- temp[1] = (value >> 48) & 0xFF;
- temp[2] = (value >> 40) & 0xFF;
- temp[3] = (value >> 32) & 0xFF;
- temp[4] = (value >> 24) & 0xFF;
- temp[5] = (value >> 16) & 0xFF;
- temp[6] = (value >> 8) & 0xFF;
- temp[7] = (value >> 0) & 0xFF;
- return sendData(temp, 8, crc);
- }
- int write(bool value, CRC32 *crc = NULL)
- {
+ int write(uint64_t value) {
+ return Serializable::write(value,stream_.get());
+ }
+ int write(bool value) {
uint8_t temp = value;
- return write(temp, crc);
+ return Serializable::write(temp,stream_.get());
}
- int writeUTF(std::string str, bool widen = false, CRC32 *crc = NULL);
- int read(uint8_t &value, CRC32 *crc = NULL)
- {
- uint8_t buf;
-
- int ret = readData(&buf, 1, crc);
- if (ret == 1)
- value = buf;
- return ret;
+ int writeUTF(std::string str, bool widen = false){
+ return Serializable::writeUTF(str,stream_.get(),widen);
}
- int read(uint16_t &value, CRC32 *crc = NULL)
- {
- uint8_t buf[2];
-
- int ret = readData(buf, 2, crc);
- if (ret == 2)
- value = (buf[0] << 8) | buf[1];
- return ret;
+ int read(uint8_t &value) {
+ return Serializable::read(value,stream_.get());
}
- int read(char &value, CRC32 *crc = NULL)
- {
- uint8_t buf;
-
- int ret = readData(&buf, 1, crc);
- if (ret == 1)
- value = (char) buf;
- return ret;
+ int read(uint16_t &value) {
+ return Serializable::read(value,stream_.get());
}
- int read(uint8_t *value, int len, CRC32 *crc = NULL)
- {
- return readData(value, len, crc);
+ int read(char &value) {
+ return Serializable::read(value,stream_.get());
}
- int read(uint32_t &value, CRC32 *crc = NULL)
- {
- uint8_t buf[4];
-
- int ret = readData(buf, 4, crc);
- if (ret == 4)
- value = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
- return ret;
+ int read(uint8_t *value, int len) {
+ return Serializable::read(value,len,stream_.get());
+ }
+ int read(uint32_t &value) {
+ return Serializable::read(value,stream_.get());
}
- int read(uint64_t &value, CRC32 *crc = NULL)
+ int read(uint64_t &value) {
+ return Serializable::read(value,stream_.get());
+ }
+ int readUTF(std::string &str, bool widen = false)
{
- uint8_t buf[8];
-
- int ret = readData(buf, 8, crc);
- if (ret == 8)
- {
- value = ((uint64_t) buf[0] << 56) |
- ((uint64_t) (buf[1] & 255) << 48) |
- ((uint64_t) (buf[2] & 255) << 40) |
- ((uint64_t) (buf[3] & 255) << 32) |
- ((uint64_t) (buf[4] & 255) << 24) |
- ((uint64_t) (buf[5] & 255) << 16) |
- ((uint64_t) (buf[6] & 255) << 8) |
- ((uint64_t) (buf[7] & 255) << 0);
- }
- return ret;
+ return Serializable::readUTF(str,stream_.get(),widen);
}
- int readUTF(std::string &str, bool widen = false, CRC32 *crc = NULL);
//! open connection to the peer
bool Open();
//! close connection to the peer
void Close();
- //! Send Data via the socket, return -1 for failure
- int sendData(uint8_t *buf, int buflen, CRC32 *crc = NULL);
- //! Read length into buf, return -1 for failure and 0 for EOF
- int readData(uint8_t *buf, int buflen, CRC32 *crc = NULL);
- //! Select on the socket
- int Select(int msec);
+
+ /**
+ * Move assignment operator.
+ */
+ Site2SitePeer& operator=(Site2SitePeer&& other)
+ {
+ stream_ = std::unique_ptr<DataStream>( other.stream_.release());
+ host_ = std::move(other.host_);
+ port_ = std::move(other.port_);
+ logger_ = Logger::getLogger();
+ configure_ = Configure::getConfigure();
+ _yieldExpiration = 0;
+ _timeOut = 30000; // 30 seconds
+ _url = "nifi://" + host_ + ":" + std::to_string(port_);
+
+ return *this;
+ }
+
+ Site2SitePeer(const Site2SitePeer &parent) = delete;
+ Site2SitePeer &operator=(const Site2SitePeer &parent) = delete;
protected:
private:
+
+ std::unique_ptr<DataStream> stream_;
+
+ std::string host_;
+ uint16_t port_;
+
//! Mutex for protection
std::mutex _mtx;
- //! S2S server Name
- std::string _host;
- //! S2S server port
- uint16_t _port;
- //! socket to server
- int _socket;
//! URL
std::string _url;
//! socket timeout;
std::atomic<uint64_t> _timeOut;
//! Logger
- Logger *_logger;
+ Logger *logger_;
//! Configure
- Configure *_configure;
+ Configure *configure_;
//! Yield Period in Milliseconds
std::atomic<uint64_t> _yieldPeriodMsec;
//! Yield Expiration
@@ -361,11 +261,9 @@ private:
//! Yield Expiration per destination PortID
std::map<std::string, uint64_t> _yieldExpirationPortIdMap;
//! OpenSSL connection state
- SSL* _ssl;
// Prevent default copy constructor and assignment operation
// Only support pass by reference or pointer
- Site2SitePeer(const Site2SitePeer &parent);
- Site2SitePeer &operator=(const Site2SitePeer &parent);
+
};
#endif
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/include/TailFile.h
----------------------------------------------------------------------
diff --git a/libminifi/include/TailFile.h b/libminifi/include/TailFile.h
index 5c4ba09..c2fe555 100644
--- a/libminifi/include/TailFile.h
+++ b/libminifi/include/TailFile.h
@@ -35,7 +35,7 @@ public:
TailFile(std::string name, uuid_t uuid = NULL)
: Processor(name, uuid)
{
- _logger = Logger::getLogger();
+ logger_ = Logger::getLogger();
_stateRecovered = false;
}
//! Destructor
@@ -65,7 +65,7 @@ protected:
private:
//! Logger
- Logger *_logger;
+ Logger *logger_;
std::string _fileLocation;
//! Property Specified Tailed File Name
std::string _fileName;
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/include/TimeUtil.h
----------------------------------------------------------------------
diff --git a/libminifi/include/TimeUtil.h b/libminifi/include/TimeUtil.h
deleted file mode 100644
index b024245..0000000
--- a/libminifi/include/TimeUtil.h
+++ /dev/null
@@ -1,82 +0,0 @@
-/**
- * @file TimeUtil.h
- * Basic Time Utility
- *
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 __TIME_UTIL_H__
-#define __TIME_UTIL_H__
-
-#include <time.h>
-#include <sys/time.h>
-#include <string.h>
-#include <unistd.h>
-#include <string.h>
-#include <iostream>
-
-#ifdef __MACH__
-#include <mach/clock.h>
-#include <mach/mach.h>
-#endif
-
-inline uint64_t getTimeMillis()
-{
- uint64_t value;
-
- timeval time;
- gettimeofday(&time, NULL);
- value = ((uint64_t) (time.tv_sec) * 1000) + (time.tv_usec / 1000);
-
- return value;
-}
-
-inline uint64_t getTimeNano()
-{
- struct timespec ts;
-
-#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
- clock_serv_t cclock;
- mach_timespec_t mts;
- host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
- clock_get_time(cclock, &mts);
- mach_port_deallocate(mach_task_self(), cclock);
- ts.tv_sec = mts.tv_sec;
- ts.tv_nsec = mts.tv_nsec;
-#else
- clock_gettime(CLOCK_REALTIME, &ts);
-#endif
-
- return ((uint64_t) (ts.tv_sec) * 1000000000 + ts.tv_nsec);
-}
-
-//! Convert millisecond since UTC to a time display string
-inline std::string getTimeStr(uint64_t msec)
-{
- char date[120];
- time_t second = (time_t) (msec/1000);
- msec = msec % 1000;
- strftime(date, sizeof(date) / sizeof(*date), "%Y-%m-%d %H:%M:%S",
- localtime(&second));
-
- std::string ret = date;
- date[0] = '\0';
- sprintf(date, ".%03llu", (unsigned long long) msec);
-
- ret += date;
- return ret;
-}
-
-#endif
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/include/io/BaseStream.h
----------------------------------------------------------------------
diff --git a/libminifi/include/io/BaseStream.h b/libminifi/include/io/BaseStream.h
new file mode 100644
index 0000000..c3ebe42
--- /dev/null
+++ b/libminifi/include/io/BaseStream.h
@@ -0,0 +1,150 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 LIBMINIFI_INCLUDE_IO_BASESTREAM_H_
+#define LIBMINIFI_INCLUDE_IO_BASESTREAM_H_
+
+
+#include <cstdint>
+#include "EndianCheck.h"
+#include "DataStream.h"
+#include "Serializable.h"
+class BaseStream: public DataStream, public Serializable {
+
+public:
+ BaseStream() {
+
+ }
+ virtual ~BaseStream() {
+
+ }
+ /**
+ * write 4 bytes to stream
+ * @param base_value non encoded value
+ * @param stream output stream
+ * @param is_little_endian endianness determination
+ * @return resulting write size
+ **/
+ virtual int write(uint32_t base_value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+ /**
+ * write 2 bytes to stream
+ * @param base_value non encoded value
+ * @param stream output stream
+ * @param is_little_endian endianness determination
+ * @return resulting write size
+ **/
+ virtual int write(uint16_t base_value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+ /**
+ * write valueto stream
+ * @param value non encoded value
+ * @param len length of value
+ * @param strema output stream
+ * @return resulting write size
+ **/
+ virtual int write(uint8_t *value, int len);
+
+ /**
+ * write 8 bytes to stream
+ * @param base_value non encoded value
+ * @param stream output stream
+ * @param is_little_endian endianness determination
+ * @return resulting write size
+ **/
+ virtual int write(uint64_t base_value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+ /**
+ * write bool to stream
+ * @param value non encoded value
+ * @return resulting write size
+ **/
+ virtual int write(bool value);
+
+ /**
+ * write UTF string to stream
+ * @param str string to write
+ * @return resulting write size
+ **/
+ virtual int writeUTF(std::string str, bool widen = false);
+
+ /**
+ * reads a byte from the stream
+ * @param value reference in which will set the result
+ * @param stream stream from which we will read
+ * @return resulting read size
+ **/
+ virtual int read(uint8_t &value);
+
+ /**
+ * reads two bytes from the stream
+ * @param value reference in which will set the result
+ * @param stream stream from which we will read
+ * @return resulting read size
+ **/
+ virtual int read(uint16_t &base_value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+ /**
+ * reads a byte from the stream
+ * @param value reference in which will set the result
+ * @param stream stream from which we will read
+ * @return resulting read size
+ **/
+ virtual int read(char &value);
+
+ /**
+ * reads a byte array from the stream
+ * @param value reference in which will set the result
+ * @param len length to read
+ * @param stream stream from which we will read
+ * @return resulting read size
+ **/
+ virtual int read(uint8_t *value, int len);
+
+ /**
+ * reads four bytes from the stream
+ * @param value reference in which will set the result
+ * @param stream stream from which we will read
+ * @return resulting read size
+ **/
+ virtual int read(uint32_t &value,
+ bool is_little_endian = EndiannessCheck::IS_LITTLE);
+
+ /**
+ * reads eight byte from the stream
+ * @param value reference in which will set the result
+ * @param stream stream from which we will read
+ * @return resulting read size
+ **/
+ virtual int read(uint64_t &value,
+ bool is_little_endian = EndiannessCheck::IS_LITTLE);
+
+ /**
+ * read UTF from stream
+ * @param str reference string
+ * @param stream stream from which we will read
+ * @return resulting read size
+ **/
+ virtual int readUTF(std::string &str, bool widen = false);
+};
+
+#endif /* LIBMINIFI_INCLUDE_IO_BASESTREAM_H_ */
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/include/io/CRCStream.h
----------------------------------------------------------------------
diff --git a/libminifi/include/io/CRCStream.h b/libminifi/include/io/CRCStream.h
new file mode 100644
index 0000000..01b6199
--- /dev/null
+++ b/libminifi/include/io/CRCStream.h
@@ -0,0 +1,306 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 LIBMINIFI_INCLUDE_IO_CRCSTREAM_H_
+#define LIBMINIFI_INCLUDE_IO_CRCSTREAM_H_
+
+#include <zlib.h>
+#include <arpa/inet.h>
+#include <memory>
+
+#include "BaseStream.h"
+#include "Serializable.h"
+
+#define htonll_r(x) ((((uint64_t)htonl(x)) << 32) + htonl((x) >> 32))
+
+
+template<typename T>
+class CRCStream: public BaseStream {
+public:
+ /**
+ * Raw pointer because the caller guarantees that
+ * it will exceed our lifetime.
+ */
+ explicit CRCStream(T *stream);
+
+ explicit CRCStream( CRCStream<T> &&move );
+
+ virtual ~CRCStream() {
+
+ }
+
+ /**
+ * Reads data and places it into buf
+ * @param buf buffer in which we extract data
+ * @param buflen
+ */
+ virtual int readData(std::vector<uint8_t> &buf, int buflen);
+ /**
+ * Reads data and places it into buf
+ * @param buf buffer in which we extract data
+ * @param buflen
+ */
+ virtual int readData(uint8_t *buf, int buflen);
+
+ /**
+ * Write value to the stream using std::vector
+ * @param buf incoming buffer
+ * @param buflen buffer to write
+ *
+ */
+ virtual int writeData(std::vector<uint8_t> &buf, int buflen);
+
+ /**
+ * writes value to stream
+ * @param value value to write
+ * @param size size of value
+ */
+ virtual int writeData(uint8_t *value, int size);
+
+ /**
+ * write 4 bytes to stream
+ * @param base_value non encoded value
+ * @param stream output stream
+ * @param is_little_endian endianness determination
+ * @return resulting write size
+ **/
+ virtual int write(uint32_t base_value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+ /**
+ * write 2 bytes to stream
+ * @param base_value non encoded value
+ * @param stream output stream
+ * @param is_little_endian endianness determination
+ * @return resulting write size
+ **/
+ virtual int write(uint16_t base_value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+
+ /**
+ * write 8 bytes to stream
+ * @param base_value non encoded value
+ * @param stream output stream
+ * @param is_little_endian endianness determination
+ * @return resulting write size
+ **/
+ virtual int write(uint64_t base_value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+
+
+ /**
+ * Reads a system word
+ * @param value value to write
+ */
+ virtual int read(uint64_t &value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+ /**
+ * Reads a uint32_t
+ * @param value value to write
+ */
+ virtual int read(uint32_t &value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+ /**
+ * Reads a system short
+ * @param value value to write
+ */
+ virtual int read(uint16_t &value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+
+ virtual short initialize() {
+ child_stream_->initialize();
+ reset();
+ return 0;
+ }
+
+
+ void updateCRC(uint8_t *buffer, uint32_t length);
+
+ uint64_t getCRC() {
+ return crc_;
+ }
+
+ void reset();
+protected:
+
+ /**
+ * Creates a vector and returns the vector using the provided
+ * type name.
+ * @param t incoming object
+ * @returns vector.
+ */
+ template<typename K>
+ std::vector<uint8_t> readBuffer(const K& t){
+ std::vector<uint8_t> buf;
+ buf.resize(sizeof t);
+ readData((uint8_t*) &buf[0], sizeof(t));
+ return buf;
+ }
+
+
+ uint64_t crc_;
+ T *child_stream_;
+};
+
+
+template<typename T>
+CRCStream<T>::CRCStream(T *other) :
+ child_stream_(other) {
+ crc_ = crc32(0L, Z_NULL, 0);
+}
+
+template<typename T>
+CRCStream<T>::CRCStream(CRCStream<T> &&move) :
+ crc_(std::move(move.crc_)), child_stream_(std::move(move.child_stream_)) {
+
+}
+
+template<typename T>
+int CRCStream<T>::readData(std::vector<uint8_t> &buf, int buflen) {
+
+ if (buf.capacity() < buflen)
+ buf.resize(buflen);
+ return readData((uint8_t*) &buf[0], buflen);
+}
+
+template<typename T>
+int CRCStream<T>::readData(uint8_t *buf, int buflen) {
+ int ret = child_stream_->read(buf, buflen);
+ crc_ = crc32(crc_, buf, buflen);
+ return ret;
+}
+
+template<typename T>
+int CRCStream<T>::writeData(std::vector<uint8_t> &buf, int buflen) {
+
+ if (buf.capacity() < buflen)
+ buf.resize(buflen);
+ return writeData((uint8_t*) &buf[0], buflen);
+}
+
+template<typename T>
+int CRCStream<T>::writeData(uint8_t *value, int size) {
+
+ int ret = child_stream_->write(value, size);
+ crc_ = crc32(crc_, value, size);
+ return ret;
+
+}
+template<typename T>
+void CRCStream<T>::reset() {
+ crc_ = crc32(0L, Z_NULL, 0);
+}
+template<typename T>
+void CRCStream<T>::updateCRC(uint8_t *buffer, uint32_t length) {
+ crc_ = crc32(crc_, buffer, length);
+}
+
+template<typename T>
+int CRCStream<T>::write(uint64_t base_value, bool is_little_endian){
+
+ const uint64_t value =
+ is_little_endian == 1 ? htonll_r(base_value) : base_value;
+ uint8_t bytes[sizeof value];
+ std::copy(static_cast<const char*>(static_cast<const void*>(&value)),
+ static_cast<const char*>(static_cast<const void*>(&value)) + sizeof value,
+ bytes);
+ return writeData(bytes,sizeof value);
+}
+
+
+template<typename T>
+int CRCStream<T>::write(uint32_t base_value, bool is_little_endian){
+ const uint32_t value = is_little_endian ? htonl(base_value) : base_value;
+ uint8_t bytes[sizeof value];
+ std::copy(static_cast<const char*>(static_cast<const void*>(&value)),
+ static_cast<const char*>(static_cast<const void*>(&value)) + sizeof value,
+ bytes);
+ return writeData(bytes,sizeof value);
+}
+
+template<typename T>
+int CRCStream<T>::write(uint16_t base_value, bool is_little_endian){
+ const uint16_t value =
+ is_little_endian == 1 ? htons(base_value) : base_value;
+ uint8_t bytes[sizeof value];
+ std::copy(static_cast<const char*>(static_cast<const void*>(&value)),
+ static_cast<const char*>(static_cast<const void*>(&value)) + sizeof value,
+ bytes);
+ return writeData(bytes,sizeof value);
+}
+
+
+template<typename T>
+int CRCStream<T>::read(uint64_t &value, bool is_little_endian) {
+
+ auto buf = readBuffer(value);
+
+ if (is_little_endian) {
+ value = ((uint64_t) buf[0] << 56) | ((uint64_t) (buf[1] & 255) << 48)
+ | ((uint64_t) (buf[2] & 255) << 40)
+ | ((uint64_t) (buf[3] & 255) << 32)
+ | ((uint64_t) (buf[4] & 255) << 24)
+ | ((uint64_t) (buf[5] & 255) << 16)
+ | ((uint64_t) (buf[6] & 255) << 8)
+ | ((uint64_t) (buf[7] & 255) << 0);
+ } else {
+ value = ((uint64_t) buf[0] << 0) | ((uint64_t) (buf[1] & 255) << 8)
+ | ((uint64_t) (buf[2] & 255) << 16)
+ | ((uint64_t) (buf[3] & 255) << 24)
+ | ((uint64_t) (buf[4] & 255) << 32)
+ | ((uint64_t) (buf[5] & 255) << 40)
+ | ((uint64_t) (buf[6] & 255) << 48)
+ | ((uint64_t) (buf[7] & 255) << 56);
+ }
+ return sizeof(value);
+}
+
+template<typename T>
+int CRCStream<T>::read(uint32_t &value, bool is_little_endian) {
+
+ auto buf = readBuffer(value);
+
+ if (is_little_endian) {
+ value = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
+ } else {
+ value = buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
+
+ }
+
+ return sizeof(value);
+}
+
+template<typename T>
+int CRCStream<T>::read(uint16_t &value, bool is_little_endian) {
+
+ auto buf = readBuffer(value);
+
+ if (is_little_endian) {
+ value = (buf[0] << 8) | buf[1];
+ } else {
+ value = buf[0] | buf[1] << 8;
+
+ }
+ return sizeof(value);
+}
+
+
+#endif /* LIBMINIFI_INCLUDE_IO_CRCSTREAM_H_ */
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/include/io/ClientSocket.h
----------------------------------------------------------------------
diff --git a/libminifi/include/io/ClientSocket.h b/libminifi/include/io/ClientSocket.h
new file mode 100644
index 0000000..f73b0b4
--- /dev/null
+++ b/libminifi/include/io/ClientSocket.h
@@ -0,0 +1,245 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 LIBMINIFI_INCLUDE_IO_CLIENTSOCKET_H_
+#define LIBMINIFI_INCLUDE_IO_CLIENTSOCKET_H_
+
+#include <cstdint>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <unistd.h>
+#include <mutex>
+#include <atomic>
+#include "io/BaseStream.h"
+#include "Logger.h"
+
+#include "io/validation.h"
+
+/**
+ * Socket class.
+ * Purpose: Provides a general purpose socket interface that abstracts
+ * connecting information from users
+ * Design: Extends DataStream and allows us to perform most streaming
+ * operations against a BSD socket
+ *
+ *
+ */
+class Socket: public BaseStream {
+public:
+ /**
+ * Constructor that accepts host name, port and listeners. With this
+ * contructor we will be creating a server socket
+ * @param hostname our host name
+ * @param port connecting port
+ * @param listeners number of listeners in the queue
+ */
+ explicit Socket(const std::string &hostname, const uint16_t port,
+ const uint16_t listeners);
+
+ /**
+ * Constructor that creates a client socket.
+ * @param hostname hostname we are connecting to.
+ * @param port port we are connecting to.
+ */
+ explicit Socket(const std::string &hostname, const uint16_t port);
+
+ /**
+ * Move constructor.
+ */
+ explicit Socket(const Socket &&);
+
+ static std::string HOSTNAME;
+
+ /**
+ * Static function to return the current machine's host name
+ */
+ static std::string getMyHostName(std::string *str = &HOSTNAME) {
+ if (__builtin_expect(!IsNullOrEmpty(str), 0))
+ return *str;
+ else {
+ char hostname[1024];
+ gethostname(hostname, 1024);
+ Socket mySock(hostname, 0);
+ mySock.initialize();
+ return mySock.getHostname();
+ }
+ }
+
+ /**
+ * Destructor
+ */
+
+ virtual ~Socket();
+
+ virtual void closeStream();
+ /**
+ * Initializes the socket
+ * @return result of the creation operation.
+ */
+ virtual short initialize();
+
+ std::string getHostname() const;
+
+ /**
+ * Return the port for this socket
+ * @returns port
+ */
+ uint16_t getPort();
+
+ // data stream extensions
+ /**
+ * Reads data and places it into buf
+ * @param buf buffer in which we extract data
+ * @param buflen
+ */
+ virtual int readData(std::vector<uint8_t> &buf, int buflen);
+ /**
+ * Reads data and places it into buf
+ * @param buf buffer in which we extract data
+ * @param buflen
+ */
+ virtual int readData(uint8_t *buf, int buflen);
+
+ /**
+ * Write value to the stream using std::vector
+ * @param buf incoming buffer
+ * @param buflen buffer to write
+ *
+ */
+ virtual int writeData(std::vector<uint8_t> &buf, int buflen);
+
+ /**
+ * writes value to stream
+ * @param value value to write
+ * @param size size of value
+ */
+ virtual int writeData(uint8_t *value, int size);
+
+
+
+ /**
+ * Writes a system word
+ * @param value value to write
+ */
+ virtual int write(uint64_t value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+ /**
+ * Writes a uint32_t
+ * @param value value to write
+ */
+ virtual int write(uint32_t value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+ /**
+ * Writes a system short
+ * @param value value to write
+ */
+ virtual int write(uint16_t value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+
+ /**
+ * Reads a system word
+ * @param value value to write
+ */
+ virtual int read(uint64_t &value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+ /**
+ * Reads a uint32_t
+ * @param value value to write
+ */
+ virtual int read(uint32_t &value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+ /**
+ * Reads a system short
+ * @param value value to write
+ */
+ virtual int read(uint16_t &value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+ /**
+ * Returns the underlying buffer
+ * @return vector's array
+ **/
+ const uint8_t *getBuffer() const {
+ return ::DataStream::getBuffer();
+ }
+
+ /**
+ * Retrieve size of data stream
+ * @return size of data stream
+ **/
+ const uint32_t getSize() const {
+ return ::DataStream::getSize();
+ }
+
+protected:
+
+ /**
+ * Creates a vector and returns the vector using the provided
+ * type name.
+ * @param t incoming object
+ * @returns vector.
+ */
+ template<typename T>
+ std::vector<uint8_t> readBuffer(const T&);
+
+ /**
+ * Creates a connection using the address info object.
+ * @param p addrinfo structure.
+ * @returns fd.
+ */
+ virtual int8_t createConnection(const addrinfo *p);
+
+ /**
+ * Sets socket options depending on the instance.
+ * @param sock socket file descriptor.
+ */
+ virtual short setSocketOptions(const int sock);
+
+ /**
+ * Attempt to select the socket file descriptor
+ * @param msec timeout interval to wait
+ * @returns file descriptor
+ */
+ virtual short select_descriptor(const uint16_t msec);
+
+ Logger *logger_;
+
+ addrinfo *addr_info_;
+
+ std::recursive_mutex selection_mutex_;
+
+ std::string requested_hostname_;
+ std::string canonical_hostname_;
+ uint16_t port_;
+
+ // connection information
+ int32_t socket_file_descriptor_;
+
+ fd_set total_list_;
+ fd_set read_fds_;
+ std::atomic<uint16_t> socket_max_;
+ uint16_t listeners_;
+
+};
+
+#endif /* LIBMINIFI_INCLUDE_IO_CLIENTSOCKET_H_ */
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/include/io/DataStream.h
----------------------------------------------------------------------
diff --git a/libminifi/include/io/DataStream.h b/libminifi/include/io/DataStream.h
new file mode 100644
index 0000000..2f793ff
--- /dev/null
+++ b/libminifi/include/io/DataStream.h
@@ -0,0 +1,120 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 LIBMINIFI_INCLUDE_IO_DATASTREAM_H_
+#define LIBMINIFI_INCLUDE_IO_DATASTREAM_H_
+
+#include <cstdint>
+#include <vector>
+#include "EndianCheck.h"
+
+/**
+ * DataStream defines the mechanism through which
+ * binary data will be written to a sink
+ */
+class DataStream {
+public:
+
+ DataStream() :
+ readBuffer(0) {
+
+ }
+
+ /**
+ * Constructor
+ **/
+ explicit DataStream(const uint8_t *buf, const uint32_t buflen) :
+ DataStream() {
+ writeData((uint8_t*) buf, buflen);
+
+ }
+
+ virtual short initialize() {
+ buffer.clear();
+ readBuffer = 0;
+ return 0;
+ }
+
+ virtual void closeStream()
+ {
+
+ }
+ /**
+ * Reads data and places it into buf
+ * @param buf buffer in which we extract data
+ * @param buflen
+ */
+ virtual int readData(std::vector<uint8_t> &buf, int buflen);
+ /**
+ * Reads data and places it into buf
+ * @param buf buffer in which we extract data
+ * @param buflen
+ */
+ virtual int readData(uint8_t *buf, int buflen);
+
+ /**
+ * writes valiue to buffer
+ * @param value value to write
+ * @param size size of value
+ */
+ virtual int writeData(uint8_t *value, int size);
+
+ /**
+ * Reads a system word
+ * @param value value to write
+ */
+ virtual int read(uint64_t &value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+ /**
+ * Reads a uint32_t
+ * @param value value to write
+ */
+ virtual int read(uint32_t &value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+ /**
+ * Reads a system short
+ * @param value value to write
+ */
+ virtual int read(uint16_t &value, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+ /**
+ * Returns the underlying buffer
+ * @return vector's array
+ **/
+ const uint8_t *getBuffer() const {
+ return &buffer[0];
+ }
+
+ /**
+ * Retrieve size of data stream
+ * @return size of data stream
+ **/
+ const uint32_t getSize() const {
+ return buffer.size();
+ }
+
+protected:
+ // All serialization related method and internal buf
+ std::vector<uint8_t> buffer;
+ uint32_t readBuffer;
+};
+
+#endif /* LIBMINIFI_INCLUDE_IO_DATASTREAM_H_ */
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/include/io/EndianCheck.h
----------------------------------------------------------------------
diff --git a/libminifi/include/io/EndianCheck.h b/libminifi/include/io/EndianCheck.h
new file mode 100644
index 0000000..ef900e0
--- /dev/null
+++ b/libminifi/include/io/EndianCheck.h
@@ -0,0 +1,47 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 LIBMINIFI_INCLUDE_IO_ENDIANCHECK_H_
+#define LIBMINIFI_INCLUDE_IO_ENDIANCHECK_H_
+
+
+
+/**
+ * Mechanism to determine endianness of host.
+ * Accounts for only BIG/LITTLE/BIENDIAN
+ **/
+class EndiannessCheck
+{
+public:
+ static bool IS_LITTLE;
+private:
+
+ static bool is_little_endian() {
+ /* do whatever is needed at static init time */
+ unsigned int x = 1;
+ char *c = (char*) &x;
+ IS_LITTLE=*c==1;
+ return IS_LITTLE;
+ }
+};
+
+
+
+
+#endif /* LIBMINIFI_INCLUDE_IO_ENDIANCHECK_H_ */
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/include/io/Serializable.h
----------------------------------------------------------------------
diff --git a/libminifi/include/io/Serializable.h b/libminifi/include/io/Serializable.h
new file mode 100644
index 0000000..59d3a73
--- /dev/null
+++ b/libminifi/include/io/Serializable.h
@@ -0,0 +1,186 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 LIBMINIFI_INCLUDE_IO_SERIALIZABLE_H_
+#define LIBMINIFI_INCLUDE_IO_SERIALIZABLE_H_
+
+#include <string>
+#include "EndianCheck.h"
+#include "DataStream.h"
+
+
+
+/**
+ * Serializable instances provide base functionality to
+ * write certain objects/primitives to a data stream.
+ *
+ */
+class Serializable {
+
+public:
+
+ /**
+ * Inline function to write T to stream
+ **/
+ template<typename T>
+ inline int writeData(const T &t,DataStream *stream);
+
+ /**
+ * Inline function to write T to to_vec
+ **/
+ template<typename T>
+ inline int writeData(const T &t, uint8_t *to_vec);
+
+ /**
+ * Inline function to write T to to_vec
+ **/
+ template<typename T>
+ inline int writeData(const T &t, std::vector<uint8_t> &to_vec);
+
+
+ /**
+ * write byte to stream
+ * @return resulting write size
+ **/
+ int write(uint8_t value,DataStream *stream);
+
+ /**
+ * write byte to stream
+ * @return resulting write size
+ **/
+ int write(char value,DataStream *stream);
+
+ /**
+ * write 4 bytes to stream
+ * @param base_value non encoded value
+ * @param stream output stream
+ * @param is_little_endian endianness determination
+ * @return resulting write size
+ **/
+ int write(uint32_t base_value,DataStream *stream, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+ /**
+ * write 2 bytes to stream
+ * @param base_value non encoded value
+ * @param stream output stream
+ * @param is_little_endian endianness determination
+ * @return resulting write size
+ **/
+ int write(uint16_t base_value,DataStream *stream, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+ /**
+ * write valueto stream
+ * @param value non encoded value
+ * @param len length of value
+ * @param strema output stream
+ * @return resulting write size
+ **/
+ int write(uint8_t *value, int len,DataStream *stream);
+
+ /**
+ * write 8 bytes to stream
+ * @param base_value non encoded value
+ * @param stream output stream
+ * @param is_little_endian endianness determination
+ * @return resulting write size
+ **/
+ int write(uint64_t base_value,DataStream *stream, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+ /**
+ * write bool to stream
+ * @param value non encoded value
+ * @return resulting write size
+ **/
+ int write(bool value);
+
+ /**
+ * write UTF string to stream
+ * @param str string to write
+ * @return resulting write size
+ **/
+ int writeUTF(std::string str,DataStream *stream, bool widen = false);
+
+ /**
+ * reads a byte from the stream
+ * @param value reference in which will set the result
+ * @param stream stream from which we will read
+ * @return resulting read size
+ **/
+ int read(uint8_t &value,DataStream *stream);
+
+ /**
+ * reads two bytes from the stream
+ * @param value reference in which will set the result
+ * @param stream stream from which we will read
+ * @return resulting read size
+ **/
+ int read(uint16_t &base_value,DataStream *stream, bool is_little_endian =
+ EndiannessCheck::IS_LITTLE);
+
+ /**
+ * reads a byte from the stream
+ * @param value reference in which will set the result
+ * @param stream stream from which we will read
+ * @return resulting read size
+ **/
+ int read(char &value,DataStream *stream);
+
+ /**
+ * reads a byte array from the stream
+ * @param value reference in which will set the result
+ * @param len length to read
+ * @param stream stream from which we will read
+ * @return resulting read size
+ **/
+ int read(uint8_t *value, int len,DataStream *stream);
+
+ /**
+ * reads four bytes from the stream
+ * @param value reference in which will set the result
+ * @param stream stream from which we will read
+ * @return resulting read size
+ **/
+ int read(uint32_t &value,DataStream *stream,
+ bool is_little_endian = EndiannessCheck::IS_LITTLE);
+
+ /**
+ * reads eight byte from the stream
+ * @param value reference in which will set the result
+ * @param stream stream from which we will read
+ * @return resulting read size
+ **/
+ int read(uint64_t &value,DataStream *stream,
+ bool is_little_endian = EndiannessCheck::IS_LITTLE);
+
+ /**
+ * read UTF from stream
+ * @param str reference string
+ * @param stream stream from which we will read
+ * @return resulting read size
+ **/
+ int readUTF(std::string &str,DataStream *stream, bool widen = false);
+
+protected:
+
+};
+
+
+#endif /* LIBMINIFI_INCLUDE_IO_SERIALIZABLE_H_ */
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/include/io/SocketFactory.h
----------------------------------------------------------------------
diff --git a/libminifi/include/io/SocketFactory.h b/libminifi/include/io/SocketFactory.h
new file mode 100644
index 0000000..e7988fc
--- /dev/null
+++ b/libminifi/include/io/SocketFactory.h
@@ -0,0 +1,87 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 SOCKET_FACTORY_H
+#define SOCKET_FACTORY_H
+
+#include "ClientSocket.h"
+#include "TLSSocket.h"
+#include "ClientSocket.h"
+#include "Configure.h"
+#include "utils/StringUtils.h"
+
+/**
+ Purpose: Due to the current design this is the only mechanism by which we can
+ inject different socket types
+
+**/
+class SocketFactory{
+public:
+
+ /**
+ * Build an instance, creating a memory fence, which
+ * allows us to avoid locking. This is tantamount to double checked locking.
+ * @returns new SocketFactory;
+ */
+ static SocketFactory *getInstance() {
+ SocketFactory* atomic_context = context_instance_.load(
+ std::memory_order_relaxed);
+ std::atomic_thread_fence(std::memory_order_acquire);
+ if (atomic_context == nullptr) {
+ std::lock_guard<std::mutex> lock(context_mutex_);
+ atomic_context = context_instance_.load(std::memory_order_relaxed);
+ if (atomic_context == nullptr) {
+ atomic_context = new SocketFactory();
+ std::atomic_thread_fence(std::memory_order_release);
+ context_instance_.store(atomic_context,
+ std::memory_order_relaxed);
+ }
+ }
+ return atomic_context;
+ }
+
+ /**
+ * Creates a socket and returns a unique ptr
+ *
+ */
+ std::unique_ptr<Socket> createSocket(const std::string &host, const uint16_t port) {
+ Socket *socket = 0;
+ if (is_secure_) {
+ socket = new TLSSocket(host, port);
+ } else {
+ socket = new Socket(host, port);
+ }
+ return std::unique_ptr<Socket>(socket);
+ }
+protected:
+ SocketFactory() :
+ configure_(Configure::getConfigure()) {
+ std::string secureStr;
+ is_secure_ = false;
+ if (configure_->get(Configure::nifi_remote_input_secure, secureStr)) {
+ StringUtils::StringToBool(secureStr, is_secure_);
+ }
+ }
+
+ bool is_secure_;
+ static std::atomic<SocketFactory*> context_instance_;
+ static std::mutex context_mutex_;
+
+ Configure *configure_;
+};
+
+#endif
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/include/io/TLSSocket.h
----------------------------------------------------------------------
diff --git a/libminifi/include/io/TLSSocket.h b/libminifi/include/io/TLSSocket.h
new file mode 100644
index 0000000..909cfc2
--- /dev/null
+++ b/libminifi/include/io/TLSSocket.h
@@ -0,0 +1,185 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 LIBMINIFI_INCLUDE_IO_TLSSOCKET_H_
+#define LIBMINIFI_INCLUDE_IO_TLSSOCKET_H_
+
+#include <cstdint>
+#include "ClientSocket.h"
+#include <atomic>
+#include <mutex>
+
+#include "Configure.h"
+#include <openssl/ssl.h>
+#include <openssl/err.h>
+
+#define TLS_ERROR_CONTEXT 1
+#define TLS_ERROR_PEM_MISSING 2
+#define TLS_ERROR_CERT_MISSING 3
+#define TLS_ERROR_KEY_ERROR 4
+#define TLS_ERROR_CERT_ERROR 5
+
+class TLSContext {
+
+public:
+
+ /**
+ * Build an instance, creating a memory fence, which
+ * allows us to avoid locking. This is tantamount to double checked locking.
+ * @returns new TLSContext;
+ */
+ static TLSContext *getInstance() {
+ TLSContext* atomic_context = context_instance.load(
+ std::memory_order_relaxed);
+ std::atomic_thread_fence(std::memory_order_acquire);
+ if (atomic_context == nullptr) {
+ std::lock_guard<std::mutex> lock(context_mutex);
+ atomic_context = context_instance.load(std::memory_order_relaxed);
+ if (atomic_context == nullptr) {
+ atomic_context = new TLSContext();
+ atomic_context->initialize();
+ std::atomic_thread_fence(std::memory_order_release);
+ context_instance.store(atomic_context,
+ std::memory_order_relaxed);
+ }
+ }
+ return atomic_context;
+ }
+
+ virtual ~TLSContext() {
+ if (0 != ctx)
+ SSL_CTX_free(ctx);
+ }
+
+ SSL_CTX *getContext() {
+ return ctx;
+ }
+
+ short getError() {
+ return error_value;
+ }
+
+ short initialize();
+
+private:
+
+ static int pemPassWordCb(char *buf, int size, int rwflag, void *userdata) {
+ std::string passphrase;
+
+ if (Configure::getConfigure()->get(
+ Configure::nifi_security_client_pass_phrase, passphrase)) {
+
+ std::ifstream file(passphrase.c_str(), std::ifstream::in);
+ if (!file.good()) {
+ memset(buf, 0x00, size);
+ return 0;
+ }
+
+ std::string password;
+ password.assign((std::istreambuf_iterator<char>(file)),
+ std::istreambuf_iterator<char>());
+ file.close();
+ memset(buf,0x00,size);
+ memcpy(buf, password.c_str(), password.length()-1);
+
+ return password.length()-1;
+ }
+ return 0;
+ }
+
+ TLSContext();
+
+ Logger *logger;
+ Configure *configuration;
+ SSL_CTX *ctx;
+
+ short error_value;
+
+ static std::atomic<TLSContext*> context_instance;
+ static std::mutex context_mutex;
+};
+
+class TLSSocket: public Socket {
+public:
+
+ /**
+ * Constructor that accepts host name, port and listeners. With this
+ * contructor we will be creating a server socket
+ * @param hostname our host name
+ * @param port connecting port
+ * @param listeners number of listeners in the queue
+ */
+ explicit TLSSocket(const std::string &hostname, const uint16_t port,
+ const uint16_t listeners);
+
+ /**
+ * Constructor that creates a client socket.
+ * @param hostname hostname we are connecting to.
+ * @param port port we are connecting to.
+ */
+ explicit TLSSocket(const std::string &hostname, const uint16_t port);
+
+ /**
+ * Move constructor.
+ */
+ explicit TLSSocket(const TLSSocket &&);
+
+ virtual ~TLSSocket();
+
+ /**
+ * Initializes the socket
+ * @return result of the creation operation.
+ */
+ short initialize();
+
+ /**
+ * Attempt to select the socket file descriptor
+ * @param msec timeout interval to wait
+ * @returns file descriptor
+ */
+ virtual short select_descriptor(const uint16_t msec);
+
+ /**
+ * Reads data and places it into buf
+ * @param buf buffer in which we extract data
+ * @param buflen
+ */
+ virtual int readData(uint8_t *buf, int buflen);
+
+ /**
+ * Write value to the stream using std::vector
+ * @param buf incoming buffer
+ * @param buflen buffer to write
+ *
+ */
+ int writeData(std::vector<uint8_t> &buf, int buflen);
+
+ /**
+ * Write value to the stream using uint8_t ptr
+ * @param buf incoming buffer
+ * @param buflen buffer to write
+ *
+ */
+ int writeData(uint8_t *value, int size);
+
+protected:
+
+ SSL* ssl;
+
+};
+
+#endif /* LIBMINIFI_INCLUDE_IO_TLSSOCKET_H_ */
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/include/io/validation.h
----------------------------------------------------------------------
diff --git a/libminifi/include/io/validation.h b/libminifi/include/io/validation.h
new file mode 100644
index 0000000..e1b4bb6
--- /dev/null
+++ b/libminifi/include/io/validation.h
@@ -0,0 +1,77 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 VALIDATION_H
+#define VALIDATION_H
+#include <string>
+#include <cstring>
+
+/**
+ * A checker that will, at compile time, tell us
+ * if the declared type has a size method.
+ */
+template<typename T>
+class size_function_functor_checker {
+ typedef char hasit;
+ typedef long doesnothaveit;
+
+ // look for the declared type
+ template<typename O> static hasit test(decltype(&O::size));
+ template<typename O> static doesnothaveit test(...);
+
+public:
+ enum {
+ has_size_function = sizeof(test<T>(0)) == sizeof(char)
+ };
+};
+
+/**
+ * Determines if the variable is null or ::size() == 0
+ */
+template<typename T>
+static auto IsNullOrEmpty(
+ T &object) -> typename std::enable_if<size_function_functor_checker<T>::has_size_function==1, bool>::type {
+ return object.size() == 0;
+}
+
+/**
+ * Determines if the variable is null or ::size() == 0
+ */
+template<typename T>
+static auto IsNullOrEmpty(
+ T *object) -> typename std::enable_if<size_function_functor_checker<T>::has_size_function==1, bool>::type {
+ return (0 == object || object->size() == 0);
+}
+
+/**
+ * Determines if the variable is null or ::size() == 0
+ */
+template<typename T>
+static auto IsNullOrEmpty(
+ T *object) -> typename std::enable_if<not size_function_functor_checker<T>::has_size_function , bool>::type {
+ return (0 == object);
+}
+/**
+ * Determines if the variable is null or strlen(str) == 0
+ */
+static auto IsNullOrEmpty(char *str)-> decltype(NULL !=str, bool()) {
+ return (NULL == str || strlen(str) == 0);
+}
+
+
+#endif
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/include/utils/FailurePolicy.h
----------------------------------------------------------------------
diff --git a/libminifi/include/utils/FailurePolicy.h b/libminifi/include/utils/FailurePolicy.h
new file mode 100644
index 0000000..a4a7f9e
--- /dev/null
+++ b/libminifi/include/utils/FailurePolicy.h
@@ -0,0 +1,45 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 LIBMINIFI_INCLUDE_UTILS_FAILUREPOLICY_H_
+#define LIBMINIFI_INCLUDE_UTILS_FAILUREPOLICY_H_
+
+/**
+ * Basic failure policy enumeration
+ *
+ */
+enum FailurePolicy {
+
+ /**
+ * DO NOTHING
+ */
+ NOTHING,
+ /**
+ * Return a response code from the executing function
+ */
+ RETURN,
+ /**
+ * Throw an exception for flow control.
+ */
+ EXCEPT,
+ /**
+ * Exit the program. This should only be used when something
+ * precludes us from continuing
+ */
+ EXIT
+};
+
+#endif /* LIBMINIFI_INCLUDE_UTILS_FAILUREPOLICY_H_ */
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/include/utils/StringUtils.h
----------------------------------------------------------------------
diff --git a/libminifi/include/utils/StringUtils.h b/libminifi/include/utils/StringUtils.h
new file mode 100644
index 0000000..30858c8
--- /dev/null
+++ b/libminifi/include/utils/StringUtils.h
@@ -0,0 +1,125 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 LIBMINIFI_INCLUDE_IO_STRINGUTILS_H_
+#define LIBMINIFI_INCLUDE_IO_STRINGUTILS_H_
+
+#include <algorithm>
+#include <sstream>
+#include "../utils/FailurePolicy.h"
+
+/**
+ * Stateless String utility class.
+ *
+ * Design: Static class, with no member variables
+ *
+ * Purpose: Houses many useful string utilities.
+ */
+class StringUtils {
+public:
+ /**
+ * Converts a string to a boolean
+ * Better handles mixed case.
+ * @param input input string
+ * @param output output string.
+ */
+ static bool StringToBool(std::string input, bool &output) {
+
+ std::transform(input.begin(), input.end(), input.begin(), ::tolower);
+ std::istringstream(input) >> std::boolalpha >> output;
+ return output;
+ }
+
+ // Trim String utils
+
+ /**
+ * Trims a string left to right
+ * @param s incoming string
+ * @returns modified string
+ */
+ static std::string trim(std::string s) {
+ return trimRight(trimLeft(s));
+ }
+
+ /**
+ * Trims left most part of a string
+ * @param s incoming string
+ * @returns modified string
+ */
+ static inline std::string trimLeft(std::string s) {
+ s.erase(s.begin(),
+ std::find_if(s.begin(), s.end(),
+ std::not1(
+ std::pointer_to_unary_function<int, int>(
+ std::isspace))));
+ return s;
+ }
+
+ /**
+ * Trims a string on the right
+ * @param s incoming string
+ * @returns modified string
+ */
+
+ static inline std::string trimRight(std::string s) {
+ s.erase(
+ std::find_if(s.rbegin(), s.rend(),
+ std::not1(
+ std::pointer_to_unary_function<int, int>(
+ std::isspace))).base(), s.end());
+ return s;
+ }
+
+ /**
+ * Converts a string to a float
+ * @param input input string
+ * @param output output float
+ * @param cp failure policy
+ */
+ static bool StringToFloat(std::string input, float &output,
+ FailurePolicy cp = RETURN) {
+ try {
+ output = std::stof(input);
+ } catch (const std::invalid_argument &ie) {
+ switch (cp) {
+ case RETURN:
+ case NOTHING:
+ return false;
+ case EXIT:
+ exit(1);
+ case EXCEPT:
+ throw ie;
+ }
+ } catch (const std::out_of_range &ofr) {
+ switch (cp) {
+ case RETURN:
+ case NOTHING:
+ return false;
+ case EXIT:
+ exit(1);
+ case EXCEPT:
+ throw ofr;
+
+ }
+ }
+
+ return true;
+
+ }
+
+};
+
+#endif /* LIBMINIFI_INCLUDE_IO_STRINGUTILS_H_ */
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/include/utils/TimeUtil.h
----------------------------------------------------------------------
diff --git a/libminifi/include/utils/TimeUtil.h b/libminifi/include/utils/TimeUtil.h
new file mode 100644
index 0000000..76b334b
--- /dev/null
+++ b/libminifi/include/utils/TimeUtil.h
@@ -0,0 +1,74 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 __TIME_UTIL_H__
+#define __TIME_UTIL_H__
+
+#include <time.h>
+#include <sys/time.h>
+#include <string.h>
+#include <unistd.h>
+#include <string.h>
+#include <iomanip>
+#include <sstream>
+#include <chrono>
+
+#define TIME_FORMAT "%Y-%m-%d %H:%M:%S"
+
+/**
+ * Gets the current time in milliseconds
+ * @returns milliseconds since epoch
+ */
+inline uint64_t getTimeMillis() {
+ return std::chrono::duration_cast<std::chrono::milliseconds>(
+ std::chrono::system_clock::now().time_since_epoch()).count();
+}
+
+/**
+ * Gets the current time in nanoseconds
+ * @returns nanoseconds since epoch
+ */
+inline uint64_t getTimeNano() {
+
+ return std::chrono::duration_cast<std::chrono::nanoseconds>(
+ std::chrono::system_clock::now().time_since_epoch()).count();
+
+}
+
+/**
+ * Returns a string based on TIME_FORMAT, converting
+ * the parameter to a string
+ * @param msec milliseconds since epoch
+ * @returns string representing the time
+ */
+inline std::string getTimeStr(uint64_t msec, bool enforce_locale = false)
+{
+ char date[120];
+ time_t second = (time_t) (msec/1000);
+ msec = msec % 1000;
+ strftime(date, sizeof(date) / sizeof(*date), TIME_FORMAT,
+ ( enforce_locale==true ? gmtime(&second) : localtime(&second)));
+
+ std::string ret = date;
+ date[0] = '\0';
+ sprintf(date, ".%03llu", (unsigned long long) msec);
+
+ ret += date;
+ return ret;
+}
+
+
+#endif
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/src/AppendHostInfo.cpp
----------------------------------------------------------------------
diff --git a/libminifi/src/AppendHostInfo.cpp b/libminifi/src/AppendHostInfo.cpp
index c9ce932..d0769c1 100644
--- a/libminifi/src/AppendHostInfo.cpp
+++ b/libminifi/src/AppendHostInfo.cpp
@@ -31,6 +31,8 @@
#include <net/if.h>
#include <arpa/inet.h>
+#include "io/ClientSocket.h"
+
#define __USE_POSIX
#include <limits.h>
@@ -66,14 +68,10 @@ void AppendHostInfo::onTrigger(ProcessContext *context, ProcessSession *session)
return;
//Get Hostname
- char hostname[HOST_NAME_MAX];
- hostname[HOST_NAME_MAX-1] = '\0';
- gethostname(hostname, HOST_NAME_MAX-1);
- struct hostent* h;
- h = gethostbyname(hostname);
- std::string hostAttribute;
- context->getProperty(HostAttribute.getName(), hostAttribute);
- flow->addAttribute(hostAttribute.c_str(), h->h_name);
+
+ std::string hostAttribute = "";
+ context->getProperty(HostAttribute.getName(), hostAttribute);
+ flow->addAttribute(hostAttribute.c_str(), Socket::getMyHostName());
//Get IP address for the specified interface
std::string iface;
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/src/Configure.cpp
----------------------------------------------------------------------
diff --git a/libminifi/src/Configure.cpp b/libminifi/src/Configure.cpp
index 61e782b..dac8408 100644
--- a/libminifi/src/Configure.cpp
+++ b/libminifi/src/Configure.cpp
@@ -18,8 +18,9 @@
* limitations under the License.
*/
#include "Configure.h"
+#include "utils/StringUtils.h"
-Configure *Configure::_configure(NULL);
+Configure *Configure::configure_(NULL);
const char *Configure::nifi_flow_configuration_file = "nifi.flow.configuration.file";
const char *Configure::nifi_administrative_yield_duration = "nifi.administrative.yield.duration";
const char *Configure::nifi_bored_yield_duration = "nifi.bored.yield.duration";
@@ -42,7 +43,7 @@ const char *Configure::nifi_security_client_ca_certificate = "nifi.security.clie
bool Configure::get(std::string key, std::string &value)
{
std::lock_guard<std::mutex> lock(_mtx);
- std::map<std::string,std::string>::iterator it = _properties.find(key);
+ auto it = _properties.find(key);
if (it != _properties.end())
{
@@ -55,25 +56,6 @@ bool Configure::get(std::string key, std::string &value)
}
}
-// Trim String utils
-std::string Configure::trim(const std::string& s)
-{
- return trimRight(trimLeft(s));
-}
-
-std::string Configure::trimLeft(const std::string& s)
-{
- const char *WHITESPACE = " \n\r\t";
- size_t startpos = s.find_first_not_of(WHITESPACE);
- return (startpos == std::string::npos) ? "" : s.substr(startpos);
-}
-
-std::string Configure::trimRight(const std::string& s)
-{
- const char *WHITESPACE = " \n\r\t";
- size_t endpos = s.find_last_not_of(WHITESPACE);
- return (endpos == std::string::npos) ? "" : s.substr(0, endpos+1);
-}
//! Parse one line in configure file like key=value
void Configure::parseConfigureFileLine(char *buf)
@@ -109,8 +91,8 @@ void Configure::parseConfigureFileLine(char *buf)
}
std::string value = equal;
- key = trimRight(key);
- value = trimRight(value);
+ key = StringUtils::trimRight(key);
+ value = StringUtils::trimRight(value);
set(key, value);
}
@@ -124,7 +106,7 @@ void Configure::loadConfigureFile(const char *fileName)
// perform a naive determination if this is a relative path
if (fileName[0] != '/')
{
- adjustedFilename = adjustedFilename + _configure->getHome() + "/" + fileName;
+ adjustedFilename = adjustedFilename + configure_->getHome() + "/" + fileName;
}
else
{
@@ -134,12 +116,12 @@ void Configure::loadConfigureFile(const char *fileName)
char *path = NULL;
char full_path[PATH_MAX];
path = realpath(adjustedFilename.c_str(), full_path);
- _logger->log_info("Using configuration file located at %s", path);
+ logger_->log_info("Using configuration file located at %s", path);
std::ifstream file(path, std::ifstream::in);
if (!file.good())
{
- _logger->log_error("load configure file failed %s", path);
+ logger_->log_error("load configure file failed %s", path);
return;
}
this->clear();
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/src/Connection.cpp
----------------------------------------------------------------------
diff --git a/libminifi/src/Connection.cpp b/libminifi/src/Connection.cpp
index 7beaf7a..791fa63 100644
--- a/libminifi/src/Connection.cpp
+++ b/libminifi/src/Connection.cpp
@@ -51,9 +51,9 @@ Connection::Connection(std::string name, uuid_t uuid, uuid_t srcUUID, uuid_t des
_expiredDuration = 0;
_queuedDataSize = 0;
- _logger = Logger::getLogger();
+ logger_ = Logger::getLogger();
- _logger->log_info("Connection %s created", _name.c_str());
+ logger_->log_info("Connection %s created", _name.c_str());
}
bool Connection::isEmpty()
@@ -89,7 +89,7 @@ void Connection::put(FlowFileRecord *flow)
_queuedDataSize += flow->getSize();
- _logger->log_debug("Enqueue flow file UUID %s to connection %s",
+ logger_->log_debug("Enqueue flow file UUID %s to connection %s",
flow->getUUIDStr().c_str(), _name.c_str());
}
@@ -129,7 +129,7 @@ FlowFileRecord* Connection::poll(std::set<FlowFileRecord *> &expiredFlowRecords)
break;
}
item->setOriginalConnection(this);
- _logger->log_debug("Dequeue flow file UUID %s from connection %s",
+ logger_->log_debug("Dequeue flow file UUID %s from connection %s",
item->getUUIDStr().c_str(), _name.c_str());
return item;
}
@@ -145,7 +145,7 @@ FlowFileRecord* Connection::poll(std::set<FlowFileRecord *> &expiredFlowRecords)
break;
}
item->setOriginalConnection(this);
- _logger->log_debug("Dequeue flow file UUID %s from connection %s",
+ logger_->log_debug("Dequeue flow file UUID %s from connection %s",
item->getUUIDStr().c_str(), _name.c_str());
return item;
}
@@ -165,5 +165,5 @@ void Connection::drain()
delete item;
}
- _logger->log_debug("Drain connection %s", _name.c_str());
+ logger_->log_debug("Drain connection %s", _name.c_str());
}
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/src/ExecuteProcess.cpp
----------------------------------------------------------------------
diff --git a/libminifi/src/ExecuteProcess.cpp b/libminifi/src/ExecuteProcess.cpp
index 569c985..61f96d5 100644
--- a/libminifi/src/ExecuteProcess.cpp
+++ b/libminifi/src/ExecuteProcess.cpp
@@ -17,11 +17,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-#include "TimeUtil.h"
#include "ExecuteProcess.h"
#include "ProcessContext.h"
#include "ProcessSession.h"
#include <cstring>
+#include "utils/StringUtils.h"
+#include "utils/TimeUtil.h"
const std::string ExecuteProcess::ProcessorName("ExecuteProcess");
Property ExecuteProcess::Command("Command", "Specifies the command to be executed; if just the name of an executable is provided, it must be in the user's environment PATH.", "");
@@ -78,7 +79,7 @@ void ExecuteProcess::onTrigger(ProcessContext *context, ProcessSession *session)
}
if (context->getProperty(RedirectErrorStream.getName(), value))
{
- Property::StringToBool(value, _redirectErrorStream);
+ StringUtils::StringToBool(value, _redirectErrorStream);
}
this->_fullCommand = _command + " " + _commandArgument;
if (_fullCommand.length() == 0)
@@ -91,12 +92,12 @@ void ExecuteProcess::onTrigger(ProcessContext *context, ProcessSession *session)
// change to working directory
if (chdir(_workingDir.c_str()) != 0)
{
- _logger->log_error("Execute Command can not chdir %s", _workingDir.c_str());
+ logger_->log_error("Execute Command can not chdir %s", _workingDir.c_str());
yield();
return;
}
}
- _logger->log_info("Execute Command %s", _fullCommand.c_str());
+ logger_->log_info("Execute Command %s", _fullCommand.c_str());
// split the command into array
char cstr[_fullCommand.length()+1];
std::strcpy(cstr, _fullCommand.c_str());
@@ -125,7 +126,7 @@ void ExecuteProcess::onTrigger(ProcessContext *context, ProcessSession *session)
switch (_pid = fork())
{
case -1:
- _logger->log_error("Execute Process fork failed");
+ logger_->log_error("Execute Process fork failed");
_processRunning = false;
close(_pipefd[0]);
close(_pipefd[1]);
@@ -153,7 +154,7 @@ void ExecuteProcess::onTrigger(ProcessContext *context, ProcessSession *session)
int numRead = read(_pipefd[0], buffer, sizeof(buffer));
if (numRead <= 0)
break;
- _logger->log_info("Execute Command Respond %d", numRead);
+ logger_->log_info("Execute Command Respond %d", numRead);
ExecuteProcess::WriteCallback callback(buffer, numRead);
FlowFileRecord *flowFile = session->create();
if (!flowFile)
@@ -178,7 +179,7 @@ void ExecuteProcess::onTrigger(ProcessContext *context, ProcessSession *session)
{
if (totalRead > 0)
{
- _logger->log_info("Execute Command Respond %d", totalRead);
+ logger_->log_info("Execute Command Respond %d", totalRead);
// child exits and close the pipe
ExecuteProcess::WriteCallback callback(buffer, totalRead);
if (!flowFile)
@@ -203,7 +204,7 @@ void ExecuteProcess::onTrigger(ProcessContext *context, ProcessSession *session)
if (numRead == (sizeof(buffer) - totalRead))
{
// we reach the max buffer size
- _logger->log_info("Execute Command Max Respond %d", sizeof(buffer));
+ logger_->log_info("Execute Command Max Respond %d", sizeof(buffer));
ExecuteProcess::WriteCallback callback(buffer, sizeof(buffer));
if (!flowFile)
{
@@ -234,11 +235,11 @@ void ExecuteProcess::onTrigger(ProcessContext *context, ProcessSession *session)
died= wait(&status);
if (WIFEXITED(status))
{
- _logger->log_info("Execute Command Complete %s status %d pid %d", _fullCommand.c_str(), WEXITSTATUS(status), _pid);
+ logger_->log_info("Execute Command Complete %s status %d pid %d", _fullCommand.c_str(), WEXITSTATUS(status), _pid);
}
else
{
- _logger->log_info("Execute Command Complete %s status %d pid %d", _fullCommand.c_str(), WTERMSIG(status), _pid);
+ logger_->log_info("Execute Command Complete %s status %d pid %d", _fullCommand.c_str(), WTERMSIG(status), _pid);
}
close(_pipefd[0]);
http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/7d3e0441/libminifi/src/FlowControlProtocol.cpp
----------------------------------------------------------------------
diff --git a/libminifi/src/FlowControlProtocol.cpp b/libminifi/src/FlowControlProtocol.cpp
index a74e32e..22ef1f9 100644
--- a/libminifi/src/FlowControlProtocol.cpp
+++ b/libminifi/src/FlowControlProtocol.cpp
@@ -45,7 +45,7 @@ int FlowControlProtocol::connectServer(const char *host, uint16_t port)
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
- _logger->log_error("Could not create socket to hostName %s", host);
+ logger_->log_error("Could not create socket to hostName %s", host);
return 0;
}
@@ -57,14 +57,14 @@ int FlowControlProtocol::connectServer(const char *host, uint16_t port)
{
if (setsockopt(sock, SOL_TCP, TCP_NODELAY, (void *)&opt, sizeof(opt)) < 0)
{
- _logger->log_error("setsockopt() TCP_NODELAY failed");
+ logger_->log_error("setsockopt() TCP_NODELAY failed");
close(sock);
return 0;
}
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(char *)&opt, sizeof(opt)) < 0)
{
- _logger->log_error("setsockopt() SO_REUSEADDR failed");
+ logger_->log_error("setsockopt() SO_REUSEADDR failed");
close(sock);
return 0;
}
@@ -73,7 +73,7 @@ int FlowControlProtocol::connectServer(const char *host, uint16_t port)
int sndsize = 256*1024;
if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&sndsize, (int)sizeof(sndsize)) < 0)
{
- _logger->log_error("setsockopt() SO_SNDBUF failed");
+ logger_->log_error("setsockopt() SO_SNDBUF failed");
close(sock);
return 0;
}
@@ -90,7 +90,7 @@ int FlowControlProtocol::connectServer(const char *host, uint16_t port)
socklen = sizeof(sa);
if (bind(sock, (struct sockaddr *)&sa, socklen) < 0)
{
- _logger->log_error("socket bind failed");
+ logger_->log_error("socket bind failed");
close(sock);
return 0;
}
@@ -105,12 +105,12 @@ int FlowControlProtocol::connectServer(const char *host, uint16_t port)
if (status < 0)
{
- _logger->log_error("socket connect failed to %s %d", host, port);
+ logger_->log_error("socket connect failed to %s %d", host, port);
close(sock);
return 0;
}
- _logger->log_info("Flow Control Protocol socket %d connect to server %s port %d success", sock, host, port);
+ logger_->log_info("Flow Control Protocol socket %d connect to server %s port %d success", sock, host, port);
return sock;
}
@@ -220,7 +220,7 @@ void FlowControlProtocol::start()
if (_running)
return;
_running = true;
- _logger->log_info("FlowControl Protocol Start");
+ logger_->log_info("FlowControl Protocol Start");
_thread = new std::thread(run, this);
_thread->detach();
}
@@ -230,7 +230,7 @@ void FlowControlProtocol::stop()
if (!_running)
return;
_running = false;
- _logger->log_info("FlowControl Protocol Stop");
+ logger_->log_info("FlowControl Protocol Stop");
}
void FlowControlProtocol::run(FlowControlProtocol *protocol)
@@ -253,7 +253,7 @@ int FlowControlProtocol::sendRegisterReq()
{
if (_registered)
{
- _logger->log_info("Already registered");
+ logger_->log_info("Already registered");
return -1;
}
@@ -299,7 +299,7 @@ int FlowControlProtocol::sendRegisterReq()
{
close(_socket);
_socket = 0;
- _logger->log_error("Flow Control Protocol Send Register Req failed");
+ logger_->log_error("Flow Control Protocol Send Register Req failed");
return -1;
}
@@ -310,26 +310,26 @@ int FlowControlProtocol::sendRegisterReq()
{
close(_socket);
_socket = 0;
- _logger->log_error("Flow Control Protocol Read Register Resp header failed");
+ logger_->log_error("Flow Control Protocol Read Register Resp header failed");
return -1;
}
- _logger->log_info("Flow Control Protocol receive MsgType %s", FlowControlMsgTypeToStr((FlowControlMsgType) hdr.msgType));
- _logger->log_info("Flow Control Protocol receive Seq Num %d", hdr.seqNumber);
- _logger->log_info("Flow Control Protocol receive Resp Code %s", FlowControlRespCodeToStr((FlowControlRespCode) hdr.status));
- _logger->log_info("Flow Control Protocol receive Payload len %d", hdr.payloadLen);
+ logger_->log_info("Flow Control Protocol receive MsgType %s", FlowControlMsgTypeToStr((FlowControlMsgType) hdr.msgType));
+ logger_->log_info("Flow Control Protocol receive Seq Num %d", hdr.seqNumber);
+ logger_->log_info("Flow Control Protocol receive Resp Code %s", FlowControlRespCodeToStr((FlowControlRespCode) hdr.status));
+ logger_->log_info("Flow Control Protocol receive Payload len %d", hdr.payloadLen);
if (hdr.status == RESP_SUCCESS && hdr.seqNumber == this->_seqNumber)
{
this->_registered = true;
this->_seqNumber++;
- _logger->log_info("Flow Control Protocol Register success");
+ logger_->log_info("Flow Control Protocol Register success");
uint8_t *payload = new uint8_t[hdr.payloadLen];
uint8_t *payloadPtr = payload;
status = readData(payload, hdr.payloadLen);
if (status <= 0)
{
delete[] payload;
- _logger->log_info("Flow Control Protocol Register Read Payload fail");
+ logger_->log_info("Flow Control Protocol Register Read Payload fail");
close(_socket);
_socket = 0;
return -1;
@@ -343,7 +343,7 @@ int FlowControlProtocol::sendRegisterReq()
// Fixed 4 bytes
uint32_t reportInterval;
payloadPtr = this->decode(payloadPtr, reportInterval);
- _logger->log_info("Flow Control Protocol receive report interval %d ms", reportInterval);
+ logger_->log_info("Flow Control Protocol receive report interval %d ms", reportInterval);
this->_reportInterval = reportInterval;
}
else
@@ -358,7 +358,7 @@ int FlowControlProtocol::sendRegisterReq()
}
else
{
- _logger->log_info("Flow Control Protocol Register fail");
+ logger_->log_info("Flow Control Protocol Register fail");
close(_socket);
_socket = 0;
return -1;
@@ -406,7 +406,7 @@ int FlowControlProtocol::sendReportReq()
{
close(_socket);
_socket = 0;
- _logger->log_error("Flow Control Protocol Send Report Req failed");
+ logger_->log_error("Flow Control Protocol Send Report Req failed");
return -1;
}
@@ -417,13 +417,13 @@ int FlowControlProtocol::sendReportReq()
{
close(_socket);
_socket = 0;
- _logger->log_error("Flow Control Protocol Read Report Resp header failed");
+ logger_->log_error("Flow Control Protocol Read Report Resp header failed");
return -1;
}
- _logger->log_info("Flow Control Protocol receive MsgType %s", FlowControlMsgTypeToStr((FlowControlMsgType) hdr.msgType));
- _logger->log_info("Flow Control Protocol receive Seq Num %d", hdr.seqNumber);
- _logger->log_info("Flow Control Protocol receive Resp Code %s", FlowControlRespCodeToStr((FlowControlRespCode) hdr.status));
- _logger->log_info("Flow Control Protocol receive Payload len %d", hdr.payloadLen);
+ logger_->log_info("Flow Control Protocol receive MsgType %s", FlowControlMsgTypeToStr((FlowControlMsgType) hdr.msgType));
+ logger_->log_info("Flow Control Protocol receive Seq Num %d", hdr.seqNumber);
+ logger_->log_info("Flow Control Protocol receive Resp Code %s", FlowControlRespCodeToStr((FlowControlRespCode) hdr.status));
+ logger_->log_info("Flow Control Protocol receive Payload len %d", hdr.payloadLen);
if (hdr.status == RESP_SUCCESS && hdr.seqNumber == this->_seqNumber)
{
@@ -434,7 +434,7 @@ int FlowControlProtocol::sendReportReq()
if (status <= 0)
{
delete[] payload;
- _logger->log_info("Flow Control Protocol Report Resp Read Payload fail");
+ logger_->log_info("Flow Control Protocol Report Resp Read Payload fail");
close(_socket);
_socket = 0;
return -1;
@@ -452,7 +452,7 @@ int FlowControlProtocol::sendReportReq()
payloadPtr = this->decode(payloadPtr, len);
processor = (const char *) payloadPtr;
payloadPtr += len;
- _logger->log_info("Flow Control Protocol receive report resp processor %s", processor.c_str());
+ logger_->log_info("Flow Control Protocol receive report resp processor %s", processor.c_str());
}
else if (((FlowControlMsgID) msgID) == PROPERTY_NAME)
{
@@ -460,7 +460,7 @@ int FlowControlProtocol::sendReportReq()
payloadPtr = this->decode(payloadPtr, len);
propertyName = (const char *) payloadPtr;
payloadPtr += len;
- _logger->log_info("Flow Control Protocol receive report resp property name %s", propertyName.c_str());
+ logger_->log_info("Flow Control Protocol receive report resp property name %s", propertyName.c_str());
}
else if (((FlowControlMsgID) msgID) == PROPERTY_VALUE)
{
@@ -468,7 +468,7 @@ int FlowControlProtocol::sendReportReq()
payloadPtr = this->decode(payloadPtr, len);
propertyValue = (const char *) payloadPtr;
payloadPtr += len;
- _logger->log_info("Flow Control Protocol receive report resp property value %s", propertyValue.c_str());
+ logger_->log_info("Flow Control Protocol receive report resp property value %s", propertyValue.c_str());
this->_controller->updatePropertyValue(processor, propertyName, propertyValue);
}
else
@@ -483,7 +483,7 @@ int FlowControlProtocol::sendReportReq()
}
else if (hdr.status == RESP_TRIGGER_REGISTER && hdr.seqNumber == this->_seqNumber)
{
- _logger->log_info("Flow Control Protocol trigger reregister");
+ logger_->log_info("Flow Control Protocol trigger reregister");
this->_registered = false;
this->_seqNumber++;
close(_socket);
@@ -492,7 +492,7 @@ int FlowControlProtocol::sendReportReq()
}
else if (hdr.status == RESP_STOP_FLOW_CONTROLLER && hdr.seqNumber == this->_seqNumber)
{
- _logger->log_info("Flow Control Protocol stop flow controller");
+ logger_->log_info("Flow Control Protocol stop flow controller");
this->_controller->stop(true);
this->_seqNumber++;
close(_socket);
@@ -501,7 +501,7 @@ int FlowControlProtocol::sendReportReq()
}
else if (hdr.status == RESP_START_FLOW_CONTROLLER && hdr.seqNumber == this->_seqNumber)
{
- _logger->log_info("Flow Control Protocol start flow controller");
+ logger_->log_info("Flow Control Protocol start flow controller");
this->_controller->start();
this->_seqNumber++;
close(_socket);
@@ -510,7 +510,7 @@ int FlowControlProtocol::sendReportReq()
}
else
{
- _logger->log_info("Flow Control Protocol Report fail");
+ logger_->log_info("Flow Control Protocol Report fail");
close(_socket);
_socket = 0;
return -1;