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

Update the Web service to reduce compilation time #67

Merged
merged 1 commit into from
Jun 26, 2017

Conversation

LeZhang2016
Copy link

@LeZhang2016 LeZhang2016 commented Jun 22, 2017

The previous version involved http server and backend business logic (such as wpan and mDNS) into the same module, which is not suitable for code maintenance and migration, in addition, it cost lots of resource during compilation.

This PR implements the following features:

@LeZhang2016 LeZhang2016 requested review from bukepo and jwhui June 22, 2017 09:04
@LeZhang2016 LeZhang2016 changed the title Update the Web service Update the Web service to reduce compilation time Jun 22, 2017
Json::Value root;
Json::FastWriter jsonWriter;
Json::Reader reader;
static std::string response;
Copy link
Member

Choose a reason for hiding this comment

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

Is there any special consideration to make response static?

Copy link
Author

Choose a reason for hiding this comment

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

if we don't add static, there will be a warning message like warning: reference to local variable ‘response’ returned during compilation.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, this is caused by returning reference. I think the prototype should be changed to return value instead of reference.

Setting as static variable is not thread safe.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks a lot for your comments, I have updated this part.

});

mdnsPublisherThread.detach();
(void) aNetworkName;
Copy link
Member

Choose a reason for hiding this comment

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

aNetworkName is actually used, same for aExtPanid.

ot::Mdns::Publisher::GetInstance().SetNetworkNameTxt(networkNameTxt.c_str());
ot::Mdns::Publisher::GetInstance().SetExtPanIdTxt(extPanIdTxt.c_str());
ot::Mdns::Publisher::GetInstance().UpdateService();
(void) aNetworkName;
Copy link
Member

Choose a reason for hiding this comment

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

used.

ot::Mdns::Publisher::GetInstance().SetExtPanIdTxt(extPanIdTxt.c_str());
ot::Mdns::Publisher::GetInstance().UpdateService();
(void) aNetworkName;
(void) aExtPanId;
Copy link
Member

Choose a reason for hiding this comment

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

used.


WebServer::~WebServer(void)
{
delete &mServer;
Copy link
Member

Choose a reason for hiding this comment

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

I think it should be

delete mServer;

void DefaultResourceSend(const HttpServer &aServer, const std::shared_ptr<HttpServer::Response> &aResponse,
const std::shared_ptr<std::ifstream> &aIfStream)
{
static std::vector<char> buffer(131072); // Safe when server is running on one thread
Copy link
Member

Choose a reason for hiding this comment

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

magic number, and can this be smaller?

@@ -241,13 +255,18 @@
data: data,
});

data = {
Copy link
Member

Choose a reason for hiding this comment

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

var data at function begining?

Copy link
Author

Choose a reason for hiding this comment

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

done thanks.

@@ -219,12 +233,12 @@
.targetEvent(ev)
.ok('Okay')
.cancel('Cancel');

Copy link
Member

Choose a reason for hiding this comment

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

nit: line only space.

otbrLog(OTBR_LOG_ERR, "wpan service error: %d", ret);
root["result"] = "failed";
}
root["result"] = "successful";
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 better to define "successful" and "fail" as macro or const string?

otbrLog(OTBR_LOG_ERR, "wpan service error: %d", ret);
root["result"] = "failed";
}
root["result"] = "successful";
Copy link
Member

Choose a reason for hiding this comment

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

It seems line 198 has no effect with this line.

otbrLog(OTBR_LOG_ERR, "wpan service error: %d", ret);
root["result"] = "failed";
}
root["result"] = "successful";
Copy link
Member

Choose a reason for hiding this comment

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

same as above.

ot::Dbus::WpanNetworkInfo mNetworks[DBUS_MAXIMUM_NAME_LENGTH];
int mNetworksCount;
char mIfName[IFNAMSIZ];
std::string mNetworkName, mExtPanId;
Copy link
Member

Choose a reason for hiding this comment

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

Although not listed in the code style, it seems OpenThread defines member variables one by one.

@@ -68,7 +68,7 @@ DBusMessage *DBusBase::GetMessage(void)
{
int ret = kWpantundStatus_Ok;

VerifyOrExit(mDestination != NULL, ret = kWpantundStatus_InvalidArgument);
VerifyOrExit(strlen(mDestination) != 0, ret = kWpantundStatus_InvalidArgument);
Copy link
Member

Choose a reason for hiding this comment

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

strnlen() preferred.

@LeZhang2016 LeZhang2016 force-pushed the feature-web-update branch 2 times, most recently from 9105e04 to 4e793ef Compare June 22, 2017 15:25
}
root["result"] = mResponseSuccess;
exit:
(void)aMdnsRequest;
Copy link
Member

Choose a reason for hiding this comment

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

used

@@ -127,6 +147,15 @@ class Publisher
return publisherInstance;
}

/**
* This method returns the boolean result of mDNS publisher.
Copy link
Member

Choose a reason for hiding this comment

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

Does this line exactly describe the functionality?

{
ret = StartMdnsService(networkName, extPanId);
}
root["result"] = mResponseSuccess;
Copy link
Member

Choose a reason for hiding this comment

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

Does this line take effect?


int MdnsService::UpdateMdnsService(const std::string &aNetworkName, const std::string &aExtPanId)
{
int ret = kMndsServiceStatus_OK;
Copy link
Member

Choose a reason for hiding this comment

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

It seems this method will always success?

Copy link
Author

Choose a reason for hiding this comment

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

Done, Thanks.

ret = ot::Dbus::kWpantundStatus_SetGatewayFailed);

ot::Utils::Long2Hex(mNetworks[index].mExtPanId, extPanId);
root["result"] = mResponseSuccess;
Copy link
Member

Choose a reason for hiding this comment

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

does this line take effect?

wpanController.SetInterfaceName(mIfName);
VerifyOrExit(wpanController.AddGateway(prefix.c_str(), defaultRoute) == ot::Dbus::kWpantundStatus_Ok,
ret = ot::Dbus::kWpantundStatus_SetGatewayFailed);
root["result"] = mResponseSuccess;
Copy link
Member

Choose a reason for hiding this comment

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

Does this take effect?

wpanController.SetInterfaceName(mIfName);
VerifyOrExit(wpanController.RemoveGateway(prefix.c_str()) == ot::Dbus::kWpantundStatus_Ok,
ret = ot::Dbus::kWpantundStatus_SetGatewayFailed);
root["result"] = mResponseSuccess;
Copy link
Member

Choose a reason for hiding this comment

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

same as above.

Json::FastWriter jsonWriter;
ot::Dbus::WPANController wpanController;
std::string response, networkName, extPanId;
int ret = ot::Dbus::kWpantundStatus_Ok;
Copy link
Member

Choose a reason for hiding this comment

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

it seems this variable will not be changed at all.

status = kWpanStatus_Down;
ExitNow();
}
if (wpantundState == "associated")
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 better to list all possible states and define them as cons string or macros?

* WPAN parameter constants
*
*/
#define OT_BORDER_ROUTER_PORT 49191
Copy link
Member

Choose a reason for hiding this comment

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

This port is defined twice, is it possible to define it only once?

@LeZhang2016 LeZhang2016 force-pushed the feature-web-update branch 2 times, most recently from e1ca276 to 781799d Compare June 23, 2017 03:26
* This method gets status of wpan service.
*
* @param[in] aNetworkName The pointer to the network name.
* @param[in] aIfName The pointer to the extended PAN ID.
Copy link
Member

Choose a reason for hiding this comment

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

[inout]

* @param[in] aIfName The pointer to the extended PAN ID.
*
* @retval kWpanStatus_OK Started the wpan service.
* @retval kWpanStatus_OffLine Not started the wpan service.
Copy link
Member

Choose a reason for hiding this comment

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

kWpanStatus_Offline Offline is a single word.

#define OT_EXTENDED_PANID_LENGTH 8
#define OT_HARDWARE_ADDRESS_LENGTH 8
#define OT_NETWORK_NAME_LENGTH 16
#define OT_PANID_LENGTH 4
Copy link
Member

Choose a reason for hiding this comment

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

This is confusing, OT_PANID_LENGTH means the hex length of PANID, while other *_LENGTH means the bytes length.
Shall we keep consistent?

enum
{
kPropertyType_String = 0,
kPropertyType_Data
Copy link
Member

Choose a reason for hiding this comment

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

kPropertyType_Data => kPropertyType_Data,?

{
aNetworkName = wpanController.Get(kWPANTUNDProperty_NetworkName);
aExtPanId = wpanController.Get(kWPANTUNDProperty_NetworkXPANID);
aExtPanId = aExtPanId.substr(2, aExtPanId.length() - 1);
Copy link
Member

Choose a reason for hiding this comment

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

It seems this line should be

aExtPanId = aExtPanId.substr(2, aExtPanId.length() - 2);

or simply

aExtPanId = aExtPanId.substr(2);

else if (wpantundState.find(kWPANTUNDStateAssociating) != std::string::npos)
{
status = kWpanStatus_Associating;
}
Copy link
Member

Choose a reason for hiding this comment

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

missing else?

Copy link
Author

Choose a reason for hiding this comment

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

Done, Thanks.

break;

default:
break;
Copy link
Member

Choose a reason for hiding this comment

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

What happens in this case?

@LeZhang2016 LeZhang2016 force-pushed the feature-web-update branch 3 times, most recently from 6b52dec to 705be5e Compare June 23, 2017 06:28
WebServer(void);

/**
* This method is disconstructor to free the WebServer.
Copy link
Member

Choose a reason for hiding this comment

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

typo

*/
enum
{
kMdnsPublisher_OK = 0,
Copy link
Member

Choose a reason for hiding this comment

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

Add doxygen for each enum.

@@ -127,6 +147,15 @@ class Publisher
return publisherInstance;
}

/**
* This method returns the boolean result of mDNS service status.
Copy link
Member

Choose a reason for hiding this comment

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

Change to: "This method indicates whether or not the mDNS service is running."

* @retval false Not started the mdns service.
*
*/
bool IsStarted() const { return mIsStarted; }
Copy link
Member

Choose a reason for hiding this comment

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

Prefer: IsRunning().

*
* @param[in] aNetworkName The reference to the network name.
* @param[in] aExtPanId The reference to the extend PAN ID.
*
Copy link
Member

Choose a reason for hiding this comment

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

Document return value(s).

*
* @param[in] aNetworkName The reference to the network name.
* @param[in] aExtPanId The reference to the extend PAN ID.
*
Copy link
Member

Choose a reason for hiding this comment

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

Document return value(s).


/**
* This method handles the http request to add on-mesh prefix.
*
Copy link
Member

Choose a reason for hiding this comment

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

Document parameters.


/**
* This method handles the http request to delete on-mesh prefix http request.
*
Copy link
Member

Choose a reason for hiding this comment

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

Document parameters.

* This method gets status of wpan service.
*
* @param[inout] aNetworkName The pointer to the network name.
* @param[inout] aIfName The pointer to the extended PAN ID.
Copy link
Member

Choose a reason for hiding this comment

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

Fix alignment.


/**
* This method handles http request to advertise service.
*
Copy link
Member

Choose a reason for hiding this comment

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

Document parameters.

* @retval false Not started the mdns service.
*
*/
bool IsStartedService() { return ot::Mdns::Publisher::GetInstance().IsStarted(); };
Copy link
Member

Choose a reason for hiding this comment

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

All void functions or methods shall explicitly declare and specify the void type keyword.

- Divide the web service into HTTP server, WPAN service and mDNS service.
- Support configurable HTTP server port (default 80).
- Show services status on Web page.
- Support google-chrome display the Web page normally.
@jwhui jwhui merged commit 851789d into openthread:master Jun 26, 2017
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.

None yet

4 participants