You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2018/11/13 03:33:23 UTC

[GitHub] duhengforever closed pull request #4: Add C Samples and Initialize Sigaction

duhengforever closed pull request #4: Add C Samples and Initialize Sigaction
URL: https://github.com/apache/rocketmq-client-cpp/pull/4
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1513ba2..5052367 100755
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -62,7 +62,7 @@ endif()
 #set(LIBEVENT_INCLUDE_DIR /usr/local/include/)
 set(Libevent_USE_STATIC_LIBS    OFF) # only find static libs
 #set(Libevent_DIR    /usr/local/lib/) # only find static libs
-find_package(Libevent 2.0.22 REQUIRED COMPONENTS) 
+find_package(Libevent 2.0.22 REQUIRED COMPONENTS)
 
 if(LIBEVENT_FOUND)
     include_directories(${LIBEVENT_INCLUDE_DIRS})
diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt
index 1b66192..04bf9b8 100755
--- a/example/CMakeLists.txt
+++ b/example/CMakeLists.txt
@@ -25,7 +25,7 @@ link_directories(${LIBEVENT_LIBRARY})
 link_directories(${JSONCPP_LIBRARY})
 
 
-file(GLOB files "*.cpp")
+file(GLOB files "*.c*")
 foreach(file ${files})
     get_filename_component(basename ${file} NAME_WE)
     add_executable(${basename} ${file})
diff --git a/example/Producer.c b/example/Producer.c
new file mode 100644
index 0000000..5888fc8
--- /dev/null
+++ b/example/Producer.c
@@ -0,0 +1,65 @@
+/*
+* 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 WIN32
+#include <unistd.h>
+#endif
+#include <stdio.h>
+
+#include "CProducer.h"
+#include "CCommon.h"
+#include "CMessage.h"
+#include "CSendResult.h"
+
+void startSendMessage(CProducer* producer)
+{
+    int i = 0;
+    char DestMsg[256];
+    CMessage* msg = CreateMessage("T_TestTopic");
+    SetMessageTags(msg,"Test_Tag");
+    SetMessageKeys(msg,"Test_Keys");
+    CSendResult result;
+    for( i=0; i<10; i++)
+    {
+        printf("send one message : %d\n",i);
+	memset(DestMsg,0,sizeof(DestMsg));
+        snprintf(DestMsg,255,"New message body: index %d",i);
+        SetMessageBody(msg,DestMsg);
+        SendMessageSync(producer, msg, &result);
+        printf("Msg Send ID:%s\n",result.msgId);
+#ifndef WIN32
+        sleep(1);
+#endif
+    }
+}
+
+
+int main(int argc,char * argv [ ])
+{
+    printf("Producer Initializing.....\n");
+
+    CProducer* producer = CreateProducer("Group_producer");
+    SetProducerNameServerAddress(producer,"172.17.0.2:9876");
+    StartProducer(producer);
+    printf("Producer start.....\n");
+    startSendMessage(producer);
+
+    ShutdownProducer(producer);
+    DestroyProducer(producer);
+    printf("Producer Shutdown!\n");
+    return 0;
+}
+
diff --git a/example/PushConsumeMessage.c b/example/PushConsumeMessage.c
new file mode 100644
index 0000000..f6ffda9
--- /dev/null
+++ b/example/PushConsumeMessage.c
@@ -0,0 +1,59 @@
+/*
+* 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 WIN32
+#include <unistd.h>
+#endif
+#include <stdio.h>
+
+
+
+#include "CPushConsumer.h"
+#include "CCommon.h"
+#include "CMessageExt.h"
+
+
+int doConsumeMessage(struct CPushConsumer * consumer, CMessageExt * msgExt)
+{
+    printf("Hello,doConsumeMessage by Application!\n");
+    printf("Msg Topic:%s\n",GetMessageTopic(msgExt));
+    printf("Msg Tags:%s\n",GetMessageTags(msgExt));
+    printf("Msg Keys:%s\n",GetMessageKeys(msgExt));
+    printf("Msg Body:%s\n",GetMessageBody(msgExt));
+    return E_CONSUME_SUCCESS;
+}
+
+
+int main(int argc,char * argv [])
+{
+    int i = 0;
+    printf("PushConsumer Initializing....\n");
+    CPushConsumer* consumer = CreatePushConsumer("Group_Consumer_Test");
+    SetPushConsumerNameServerAddress(consumer,"172.17.0.2:9876");
+    Subscribe(consumer,"T_TestTopic","*");
+    RegisterMessageCallback(consumer,doConsumeMessage);
+    StartPushConsumer(consumer);
+    printf("Push Consumer Start...\n");
+    for( i=0; i<10; i++)
+    {
+        printf("Now Running : %d S\n",i*10);
+        sleep(10);
+    }
+    ShutdownPushConsumer(consumer);
+    DestroyPushConsumer(consumer);
+    printf("PushConsumer Shutdown!\n");
+    return 0;
+}
diff --git a/include/CCommon.h b/include/CCommon.h
index c97c719..44ed12d 100644
--- a/include/CCommon.h
+++ b/include/CCommon.h
@@ -25,13 +25,13 @@ extern "C" {
 #define  MAX_MESSAGE_ID_LENGTH 256
 #define  MAX_TOPIC_LENGTH 512
 #define  MAX_BROKER_NAME_ID_LENGTH 256
-typedef enum {
+typedef enum _CStatus_{
     // Success
     OK = 0,
     // Failed, null pointer value
     NULL_POINTER = 1,
 } CStatus;
-typedef enum {
+typedef enum _CLogLevel_{
     E_LOG_LEVEL_FATAL = 1,
     E_LOG_LEVEL_ERROR = 2,
     E_LOG_LEVEL_WARN = 3,
diff --git a/include/CMessage.h b/include/CMessage.h
index c14e47e..2a83eae 100644
--- a/include/CMessage.h
+++ b/include/CMessage.h
@@ -23,7 +23,7 @@ extern "C" {
 #endif
 
 //typedef struct _CMessage_ CMessage;
-struct CMessage;
+typedef struct CMessage CMessage;
 
 
 CMessage *CreateMessage(const char *topic);
diff --git a/include/CMessageExt.h b/include/CMessageExt.h
index 79df10a..d00bcb4 100644
--- a/include/CMessageExt.h
+++ b/include/CMessageExt.h
@@ -24,7 +24,7 @@ extern "C" {
 #endif
 
 //typedef struct _CMessageExt_ _CMessageExt;
-struct CMessageExt;
+typedef struct CMessageExt CMessageExt;
 
 const char *GetMessageTopic(CMessageExt *msgExt);
 const char *GetMessageTags(CMessageExt *msgExt);
diff --git a/include/CProducer.h b/include/CProducer.h
index ae8e16e..09f4a0b 100644
--- a/include/CProducer.h
+++ b/include/CProducer.h
@@ -26,7 +26,7 @@ extern "C" {
 #endif
 
 //typedef struct _CProducer_ _CProducer;
-struct CProducer;
+typedef struct CProducer CProducer;
 
 
 CProducer *CreateProducer(const char *groupId);
diff --git a/include/CPullConsumer.h b/include/CPullConsumer.h
index 8413e3f..77df836 100644
--- a/include/CPullConsumer.h
+++ b/include/CPullConsumer.h
@@ -26,7 +26,7 @@
 extern "C" {
 #endif
 
-struct CPullConsumer;
+typedef struct CPullConsumer CPullConsumer;
 
 
 CPullConsumer *CreatePullConsumer(const char *groupId);
diff --git a/include/CPushConsumer.h b/include/CPushConsumer.h
index b7e7efb..17d8209 100644
--- a/include/CPushConsumer.h
+++ b/include/CPushConsumer.h
@@ -26,9 +26,9 @@ extern "C" {
 #endif
 
 //typedef struct _CConsumer_ _CConsumer;
-struct CPushConsumer;
+typedef struct CPushConsumer CPushConsumer;
 
-typedef enum {
+typedef enum E_CConsumeStatus{
     E_CONSUME_SUCCESS = 0,
     E_RECONSUME_LATER = 1
 } CConsumeStatus;
diff --git a/include/CSendResult.h b/include/CSendResult.h
index b9a7bdb..14100b4 100644
--- a/include/CSendResult.h
+++ b/include/CSendResult.h
@@ -23,7 +23,7 @@
 extern "C" {
 #endif
 
-typedef enum {
+typedef enum E_CSendStatus_{
     E_SEND_OK = 0,
     E_SEND_FLUSH_DISK_TIMEOUT = 1,
     E_SEND_FLUSH_SLAVE_TIMEOUT = 2,
diff --git a/src/consumer/DefaultMQPullConsumer.cpp b/src/consumer/DefaultMQPullConsumer.cpp
index 0b8dd81..ff5fbbb 100755
--- a/src/consumer/DefaultMQPullConsumer.cpp
+++ b/src/consumer/DefaultMQPullConsumer.cpp
@@ -58,7 +58,7 @@ DefaultMQPullConsumer::~DefaultMQPullConsumer() {
 void DefaultMQPullConsumer::start() {
 #ifndef WIN32
   /* Ignore the SIGPIPE */
-  struct sigaction sa;
+  struct sigaction sa = {0};
   sa.sa_handler = SIG_IGN;
   sa.sa_flags = 0;
   sigaction(SIGPIPE, &sa, 0);
diff --git a/src/consumer/DefaultMQPushConsumer.cpp b/src/consumer/DefaultMQPushConsumer.cpp
index a78a7df..afa44f0 100644
--- a/src/consumer/DefaultMQPushConsumer.cpp
+++ b/src/consumer/DefaultMQPushConsumer.cpp
@@ -284,7 +284,7 @@ void DefaultMQPushConsumer::persistConsumerOffsetByResetOffset() {
 void DefaultMQPushConsumer::start() {
 #ifndef WIN32
   /* Ignore the SIGPIPE */
-  struct sigaction sa;
+  struct sigaction sa ={0};
   sa.sa_handler = SIG_IGN;
   sa.sa_flags = 0;
   sigaction(SIGPIPE, &sa, 0);
diff --git a/src/producer/DefaultMQProducer.cpp b/src/producer/DefaultMQProducer.cpp
index 7eac3af..9e32f3c 100755
--- a/src/producer/DefaultMQProducer.cpp
+++ b/src/producer/DefaultMQProducer.cpp
@@ -50,7 +50,7 @@ DefaultMQProducer::~DefaultMQProducer() {}
 void DefaultMQProducer::start() {
 #ifndef WIN32
   /* Ignore the SIGPIPE */
-  struct sigaction sa;
+  struct sigaction sa = {0};
   sa.sa_handler = SIG_IGN;
   sa.sa_flags = 0;
   sigaction(SIGPIPE, &sa, 0);


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


With regards,
Apache Git Services