Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integrate the mDNS client into the publisher. Start mDNS client, if there is a existing server running on the system. #16

Merged
merged 1 commit into from
Jun 7, 2017

Conversation

LeZhang2016
Copy link

No description provided.

@LeZhang2016 LeZhang2016 changed the title integrate the mDNS client into the publisher. Start mDNS client, if there is a server running on the system. integrate the mDNS client into the publisher. Start mDNS client, if there is a existing server running on the system. Jun 4, 2017
@LeZhang2016 LeZhang2016 force-pushed the feature-mdns branch 2 times, most recently from 2bae93b to f62583d Compare June 4, 2017 14:58
@LeZhang2016 LeZhang2016 requested a review from bukepo June 4, 2017 15:36

#include "common/code_utils.hpp"
#include "mdns_publisher.hpp"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"mdns_publisher.hpp" should be kept as the first included file.

Preprocessor #include directives should be grouped, ordered, or sorted as follows:
This compilation unit's corresponding header, if any.
C++ Standard Library headers
C Standard Library headers
Third-party library headers
First-party library headers
Private or local headers
Alphanumeric order within each subgroup

@@ -222,20 +376,58 @@ void Publisher::SetServiceName(const char *aServiceName)
mServiceName = avahi_strdup(aServiceName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to leak memory here? It seems the previous memory of mServiceNames is not freed.

sNetworkName = "nn=" + sNetworkName;
sExtPanId = "xp=" + sExtPanId;
ot::Mdns::Publisher::SetNetworkNameTxT(sNetworkName.c_str());
ot::Mdns::Publisher::SetEPANIDTxT(sExtPanId.c_str());
ot::Mdns::Publisher::Instance().SetNetworkNameTxT(sNetworkName.c_str());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it safe to let Publisher save the pointer returned by std::string::c_str()? Below is the doc of std::string::c_str()

The pointer returned may be invalidated by further calls to other member functions that modify the object.

avahi_simple_poll_quit(mSimplePoll);
if (mServer)
avahi_server_free(mServer);
StartClient();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing break; below?

@LeZhang2016 LeZhang2016 force-pushed the feature-mdns branch 3 times, most recently from d6822be to 9113a9a Compare June 5, 2017 12:40
@jwhui jwhui requested a review from bukepo June 5, 2017 23:05
void PeriodicallyPublish(AVAHI_GCC_UNUSED AvahiTimeout *aTimeout);
void Free(void);

static Publisher &Instance(void)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Methods should have a verb (e.g. GetInstance()).

@@ -31,6 +31,9 @@
* This file implements starting an mDNS server, and publish border router service.
*/

#include <avahi-client/publish.h>
#include <avahi-core/publish.h>

#include "common/code_utils.hpp"

#include "mdns_publisher.hpp"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compilation unit's corresponding header should appear first.

break;

case AVAHI_CLIENT_CONNECTING:
;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know break is not needed, but it is cleaner to include it.

break;

case AVAHI_SERVER_FAILURE:

syslog(LOG_ERR, "Server failure: %s\n",
avahi_strerror(avahi_server_errno(aServer)));
avahi_simple_poll_quit(mSimplePoll);
isStart = false;
break;

case AVAHI_SERVER_INVALID:
;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include break here.


if ((mClient != NULL) && (avahi_client_get_state(mClient) == AVAHI_CLIENT_S_RUNNING))
{

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary newline.


Free();
return ret;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary newline.


case AVAHI_ENTRY_GROUP_UNCOMMITED:
case AVAHI_ENTRY_GROUP_REGISTERING:
;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include break.

static uint16_t mPort;
static char *mServiceName;
static bool isStart;
void ServerCallback(AvahiServer *aServer, AvahiServerState aState);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Methods should include verbs. For example HandleServerCallback(), or something more appropriate.

AVAHI_GCC_UNUSED void *aUserData);
void ClientCallback(AvahiClient *aClient, AvahiClientState aState);

static void PeriodicallyPublish(AVAHI_GCC_UNUSED AvahiTimeout *aTimeout, AVAHI_GCC_UNUSED void *aUserData);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Methods should include verbs. PeriodicallyPublish does not tell me what this method actually does.

@LeZhang2016 LeZhang2016 force-pushed the feature-mdns branch 10 times, most recently from 75237de to 333ad64 Compare June 7, 2017 03:02

if (avahi_entry_group_is_empty(mClientGroup))
{

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space line

Copy link
Member

@bukepo bukepo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing this issue. I think we should consider if core api of avahi should be avoided later, as suggested in avahi's doc.

@@ -70,6 +101,11 @@ void Publisher::Free(void)
avahi_server_free(mServer);
}

if (mClient)
{
avahi_client_free(mClient);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add mClient = NULL?

@@ -70,6 +101,11 @@ void Publisher::Free(void)
avahi_server_free(mServer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add mServer = NULL?

@@ -78,21 +114,80 @@ void Publisher::Free(void)
avahi_free(mServiceName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialize mServiceName and check if it's NULL here?

mNetworkNameTxt, mExtPanIdTxt, NULL) == 0,
ret = kMdnsPublisher_FailedAddSevice);

syslog(LOG_INFO, " Service Name: %s \n Port: %d \n Network Name: %s \n Extended Pan ID: %s \n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syslog should not include \n

serviceName = avahi_alternative_service_name(mServiceName);
avahi_free(mServiceName);
mServiceName = serviceName;
syslog(LOG_INFO, "Service name collision, renaming service to '%s'\n", mServiceName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\n as suggested above.

avahi_simple_poll_quit(mSimplePoll);
if (mServer)
{
avahi_server_free(mServer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it safer to add mServer = NULL here?

{
mNetworkNameTxt = aTxtData;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary new line.

{
mNetworkNameTxt = aTxtData;

avahi_free(mNetworkNameTxt);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mNetworkNameTxt is not initialized?

{
mExtPanIdTxt = aTxtData;
avahi_free(mExtPanIdTxt);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not initialized?

}

void Publisher::SetType(const char *aType)
{

mType = aType;
avahi_free(mType);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not initialized?

@LeZhang2016 LeZhang2016 force-pushed the feature-mdns branch 6 times, most recently from 32e569b to 22e23ad Compare June 7, 2017 10:05
script/bootstrap Outdated
@@ -44,7 +44,7 @@ install_packages_apt()
sudo apt-get install -y autoconf-archive

# mDNS
sudo apt-get install -y libavahi-core-dev
sudo apt-get install -y libavahi-common-dev libavahi-client-dev
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is avahi-daemon required?

}

avahi_free(mServiceName);
if (mServiceName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep same style of checking pointer is NULL or not? I found

if (xxx)
if (!xxx)
if (xxx != NULL)
(xxx == NULL)


if (mClientGroup != NULL)
{
avahi_entry_group_free(mClientGroup);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set it to NULL?

mNetworkNameTxt, mExtPanIdTxt, NULL) == 0,
ret = kMdnsPublisher_FailedAddSevice);
VerifyOrExit(avahi_entry_group_add_service(mClientGroup, AVAHI_IF_UNSPEC,
AVAHI_PROTO_UNSPEC, (AvahiPublishFlags) 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use static_cast<AvahiPublishFlags>?


syslog(LOG_INFO, " Service Name: %s Port: %d Network Name: %s Extended Pan ID: %s ",
mServiceName, mPort, mNetworkNameTxt, mExtPanIdTxt);
syslog(LOG_INFO, " Service Name: %s Port: %d Network Name: %s Extended Pan ID: %s ",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary space at end of syslog format string. And is the preceding space in intended?


exit:
if (ret != kMdnsPublisher_OK)

if (ret == kMdnsPublisher_FailedAddSevice)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use switch?

mServiceName = serviceName;
syslog(LOG_INFO, "Service name collision, renaming service to '%s'", mServiceName);
avahi_entry_group_reset(mClientGroup);
CreateService(aClient);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intended to ignore error from CreateService()?

{
avahi_simple_poll_quit(mSimplePoll);
case AVAHI_CLIENT_CONNECTING:
break;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing default case?

@LeZhang2016
Copy link
Author

@bukepo, Thanks a lot for your comments and suggestions, I have updated this RP according to your comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants