You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2020/08/13 09:54:08 UTC

[GitHub] [nifi-minifi-cpp] arpadboda commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

arpadboda commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r469787490



##########
File path: libminifi/include/io/OutputStream.h
##########
@@ -0,0 +1,107 @@
+/**
+ *
+ * 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.
+ */
+
+#pragma once
+
+#include <stdexcept>
+#include <vector>
+#include <string>
+#include "Stream.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+/**
+ * Serializable instances provide base functionality to
+ * write certain objects/primitives to a data stream.
+ *
+ */
+class OutputStream : public virtual Stream {
+ public:
+  /**
+   * write valueto stream
+   * @param value non encoded value
+   * @param len length of value
+   * @return resulting write size
+   **/
+  virtual int write(const uint8_t *value, int len) {
+    throw std::runtime_error("Stream is not writable");

Review comment:
       Wonder if something like "not yet implemented" would be a bit more talkativee. As a human being I would expect an  output stream to be writeable. :)

##########
File path: libminifi/src/io/BufferStream.cpp
##########
@@ -0,0 +1,56 @@
+/**
+ *
+ * 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.
+ */
+#include <vector>
+#include <cstdint>
+#include <string>
+#include <algorithm>

Review comment:
       I suspect that we can remove some of these. 

##########
File path: libminifi/test/unit/FileStreamTests.cpp
##########
@@ -235,12 +235,12 @@ TEST_CASE("TestFileExceedSize", "[TestLoader]") {
   std::vector<uint8_t> verifybuffer;
 
   for (int i = 0; i < 10; i++)
-    REQUIRE(stream.readData(verifybuffer, 8192) == 8192);
-  REQUIRE(stream.readData(verifybuffer, 8192) == 0);
+    REQUIRE(stream.read(verifybuffer, 8192) == 8192);

Review comment:
       I know that this is not new code, but the first require being in the loop is quite misleading here, could you add brackets?

##########
File path: libminifi/src/io/OutputStream.cpp
##########
@@ -0,0 +1,118 @@
+/**
+ *
+ * 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.
+ */
+#include <cstdio>
+#include <cstring>
+#include <iostream>
+#include <vector>
+#include <string>
+#include <algorithm>
+#include "io/OutputStream.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+int OutputStream::write(const std::vector<uint8_t>& buffer, int len) {
+  if (buffer.size() < len) {
+    return -1;
+  }
+  return write(buffer.data(), len);
+}
+
+int OutputStream::write(uint8_t value) {
+  return write(&value, 1);
+}
+
+int OutputStream::write(bool value) {
+  uint8_t temp = value;
+  return write(&temp, 1);
+}
+
+int OutputStream::write(const std::string& str, bool widen) {
+  return write_str(str.c_str(), str.length(), widen);
+}
+
+int OutputStream::write(const char* str, bool widen) {
+  return write_str(str, std::strlen(str), widen);
+}
+
+int OutputStream::write_str(const char* str, uint32_t len, bool widen) {
+  int ret = 0;
+  if (!widen) {
+    uint16_t shortLen = len;
+    if (len != shortLen) {
+      return -1;
+    }
+    ret = write(shortLen);
+  } else {
+    ret = write(len);
+  }
+
+  if (ret <= 0) {
+    return ret;
+  }
+
+  if (len == 0) {
+    return ret;
+  }
+
+  return ret + write(reinterpret_cast<const uint8_t *>(str), len);
+}
+
+int OutputStream::write(uint16_t value) {
+  uint8_t buffer[2]{};
+
+  buffer[0] = value >> 8;
+  buffer[1] = value;
+
+  return write(buffer, 2);
+}
+
+int OutputStream::write(uint32_t value) {
+  uint8_t buffer[4]{};
+
+  buffer[0] = value >> 24;
+  buffer[1] = value >> 16;
+  buffer[2] = value >> 8;
+  buffer[3] = value;
+
+  return write(buffer, 4);
+}
+
+int OutputStream::write(uint64_t value) {
+  uint8_t buffer[8]{};
+
+  buffer[0] = value >> 56;

Review comment:
       Doesn't this generate warnings?
   value being shifted is still an uint64_t

##########
File path: extensions/coap/tests/CoapC2VerifyHeartbeat.cpp
##########
@@ -132,18 +132,18 @@ class VerifyCoAPServer : public CoapIntegrationBase {
 
     {
       // should result in valid operation
-      minifi::io::BaseStream stream;
+      minifi::io::BufferStream stream;
       uint16_t version = 0, size = 1;
       uint8_t operation = 1;
       stream.write(version);
       stream.write(size);
       stream.write(&operation, 1);
-      stream.writeUTF("id");
-      stream.writeUTF("operand");
+      stream.write("id");
+      stream.write("operand");
 
-      uint8_t *data = new uint8_t[stream.getSize()];
-      memcpy(data, stream.getBuffer(), stream.getSize());
-      minifi::coap::CoapResponse response(205, std::unique_ptr<uint8_t>(data), stream.getSize());
+      uint8_t *data = new uint8_t[stream.size()];

Review comment:
       As we touch it: what about make_unique and move?

##########
File path: libminifi/src/io/InputStream.cpp
##########
@@ -0,0 +1,141 @@
+/**
+ *
+ * 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.
+ */
+#include <cstdio>
+#include <iostream>
+#include <vector>
+#include <string>
+#include <algorithm>
+#include "io/InputStream.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+int InputStream::read(std::vector<uint8_t>& buffer, int len) {
+  if (buffer.size() < len) {
+    buffer.resize(len);
+  }
+  int ret = read(buffer.data(), len);
+  buffer.resize((std::max)(ret, 0));
+  return ret;
+}
+
+
+int InputStream::read(uint8_t &value) {
+  uint8_t buf = 0;
+
+  if (read(&buf, 1) != 1) {
+    return -1;
+  }
+  value = buf;
+  return 1;
+}
+
+int InputStream::read(bool &value) {
+  uint8_t buf = 0;
+
+  if (read(&buf, 1) != 1) {
+    return -1;
+  }
+  value = buf;
+  return 1;
+}
+
+int InputStream::read(std::string &str, bool widen) {
+  uint32_t len = 0;
+  int ret = 0;
+  if (!widen) {
+    uint16_t shortLength = 0;
+    ret = read(shortLength);
+    len = shortLength;
+  } else {
+    ret = read(len);
+  }
+
+  if (ret <= 0) {
+    return ret;
+  }
+
+  if (len == 0) {
+    str = "";
+    return ret;
+  }
+
+  std::vector<uint8_t> buffer(len);
+  if (read(buffer.data(), len) != len) {
+    return -1;
+  }
+
+  str = std::string(reinterpret_cast<const char*>(buffer.data()), len);
+  return ret + len;
+}
+
+int InputStream::read(uint64_t &value) {
+  uint8_t buf[8]{};
+  if (read(buf, 8) != 8) {
+    return -1;
+  }
+
+  value = 0;
+  value |= (uint64_t) buf[0] << 56;

Review comment:
       Maybe I need a bit more coffee, but it feels that we can simply sum this, don't need |=
   
   for (int i = 0; i < 8 ; ++i) {
     value += (uint64_t) buf[i] << (56 - 8*i);
   }




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org