Skip to content

Commit

Permalink
5.8.1.2 refactor bofserviceendpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bha-evs committed Aug 23, 2024
1 parent 64b683e commit d0ee22e
Show file tree
Hide file tree
Showing 12 changed files with 358 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ set(FS_FILES
src/bofpath.cpp
include/bofstd/bofuri.h
src/bofuri.cpp
include/bofstd/bofserviceendpoint.h
src/bofserviceendpoint.cpp
include/bofstd/bofhttprequest.h
src/bofhttprequest.cpp
include/bofstd/boffs.h
Expand Down
56 changes: 56 additions & 0 deletions lib/include/bofstd/bofserviceendpoint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2013-2033, Onbings. All rights reserved.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
* PURPOSE.
*
* This module defines the bofserviceendpoint module of the bofstd library
*
* Name: bofserviceendpoint.h
* Author: Bernard HARMEL: [email protected]
* Web: onbings.dscloud.me
* Revision: 1.0
*
* Rem: None
*
* History:
*
* V 1.00 Aug 15 2024 BHA : Initial release
*/
#pragma once

#include <bofstd/bofuri.h>

BEGIN_BOF_NAMESPACE()

class BOFSTD_EXPORT BofServiceEndpoint
{
public:
BofServiceEndpoint();
BofServiceEndpoint(const std::string &_rServiceUrl_S);
BofServiceEndpoint(const std::string &_rServiceName_S, const std::string &_rServiceInstance_S, const std::string &_rServiceUrl_S,
const std::set<std::string> &_rServiceTagCollection);
~BofServiceEndpoint();
bool IsValid();
std::string ToString(bool _ShowName_B);

bool SetServiceName(const std::string &_rServiceName_S);
bool SetServiceInstance(const std::string &_rServiceInstance_S);
bool SetServiceUrl(const std::string &_rServiceUrl_S);
bool SetServiceTagCollection(const std::set<std::string> &_rServiceTagCollection);

const std::string GetServiceName() const;
const std::string GetServiceInstance() const;
const std::string GetServiceUrl() const;
const std::set<std::string> GetServiceTagCollection() const;

private:
BofUri mUri;
std::string mServiceName_S;
std::string mServiceInstance_S;
std::set<std::string> mServiceTagCollection;
};

END_BOF_NAMESPACE()
3 changes: 3 additions & 0 deletions lib/include/bofstd/bofsocketos.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <cstdint>
#include <string.h>
#include <vector>
#include <set>

#if defined(_WIN32)
#define NOMINMAX
Expand Down Expand Up @@ -789,4 +790,6 @@ BOFSTD_EXPORT BOFERR Bof_GetCompatibleIpAddress(const std::vector<BOF_NETWORK_IN
BOFSTD_EXPORT BOFERR Bof_PollFdSocket(uint32_t _TimeoutInMs_U32, uint32_t _NbPollOpInList_U32, BOF_POLL_SOCKET *_pListOfPollOp_X, uint32_t &_rNbPollSet_U32);
BOFSTD_EXPORT bool Bof_IsIpAddressPingable(uint32_t _TimeoutInMs_U32, const std::string &_rIpAddress_S);
BOFSTD_EXPORT bool Bof_IsIpAddressOpened(uint32_t _TimeoutInMs_U32, const std::string &_rIpAddress_S);


END_BOF_NAMESPACE()
1 change: 1 addition & 0 deletions lib/include/bofstd/bofuri.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,5 @@ class BOFSTD_EXPORT BofUri
/// @remarks None
BOFERR ExtractQueryParamIntoCollection(const std::string &_rQueryParam_S, std::map<std::string, std::string> &_rQueryParamCollection);
};

END_BOF_NAMESPACE()
102 changes: 102 additions & 0 deletions lib/src/bofserviceendpoint.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2013-2033, Onbings. All rights reserved.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
* PURPOSE.
*
* This module implements the bofserviceendpoint module of the bofstd library
*
* Name: bofserviceendpoint.cpp
* Author: Bernard HARMEL: [email protected]
* Web: onbings.dscloud.me
* Revision: 1.0
*
* Rem: Nothing
*
* History:
*
* V 1.00 Aug 15 2024 BHA : Initial release
*/
#include <bofstd/bofserviceendpoint.h>

BEGIN_BOF_NAMESPACE()

BofServiceEndpoint::BofServiceEndpoint():mUri()
{
}
BofServiceEndpoint::BofServiceEndpoint(const std::string &_rServiceUrl_S) :mUri(_rServiceUrl_S)
{
}
BofServiceEndpoint::BofServiceEndpoint(const std::string &_rServiceName_S, const std::string &_rServiceInstance_S, const std::string &_rServiceUrl_S,
const std::set<std::string> &_rServiceTagCollection)
: mServiceName_S(_rServiceName_S), mServiceInstance_S(_rServiceInstance_S), mUri(_rServiceUrl_S),mServiceTagCollection(_rServiceTagCollection)
{
}
BofServiceEndpoint::~BofServiceEndpoint()
{
}
bool BofServiceEndpoint::IsValid()
{
return mUri.IsValid();
}
std::string BofServiceEndpoint::ToString(bool _ShowName_B)
{
std::string Rts;

if (_ShowName_B)
{
Rts = mServiceName_S + '(' + mServiceInstance_S + ")@";
}
Rts += mUri.ToString();
return Rts;
}
bool BofServiceEndpoint::SetServiceName(const std::string &_rServiceName_S)
{
mServiceName_S = _rServiceName_S;
return true;
}

bool BofServiceEndpoint::SetServiceInstance(const std::string &_rServiceInstance_S)
{
mServiceInstance_S = _rServiceInstance_S;
return true;
}

bool BofServiceEndpoint::SetServiceUrl(const std::string &_rServiceUrl_S)
{
return (mUri.SetAuthority(_rServiceUrl_S) == BOF_ERR_NO_ERROR);
}

bool BofServiceEndpoint::SetServiceTagCollection(const std::set<std::string> &_rServiceTagCollection)
{
mServiceTagCollection = _rServiceTagCollection;
return true;
}

const std::string BofServiceEndpoint::GetServiceName() const
{
return mServiceName_S;
}

const std::string BofServiceEndpoint::GetServiceInstance() const
{
return mServiceInstance_S;
}

const std::string BofServiceEndpoint::GetServiceUrl() const
{
std::string Rts_S;
BOF::BOF_SOCKET_ADDRESS SocketAddress_X;

SocketAddress_X = mUri.IpAddress(Rts_S);
return Rts_S;
}

const std::set<std::string> BofServiceEndpoint::GetServiceTagCollection() const
{
return mServiceTagCollection;
}

END_BOF_NAMESPACE()
77 changes: 76 additions & 1 deletion lib/src/bofsocketos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <bofstd/bofstring.h>
#include <bofstd/bofsystem.h>
#include <bofstd/bofsocket.h>
#include <bofstd/bofuri.h>

#include <map>
#include <regex>
Expand Down Expand Up @@ -1495,14 +1496,46 @@ var regexMailto = / ^ (mailto) : ((? : [a - z0 - 9 - ._~!$ & '()*+,;=:@]|%[0-9A-
BOFERR Bof_SplitUri(const std::string &_rUri_S, BOF_SOCKET_ADDRESS_COMPONENT &_rUri_X, std::string &_rPath_S, std::string &_rQuery_S, std::string &_rFragment_S)
{
BOFERR Rts_E = BOF_ERR_FORMAT;

static const std::regex S_RegExUri(
"^([a-z][a-z0-9+.-]*):(?:\\/\\/((?:(?=((?:[a-z0-9-._~!$&'()*+,;=:]|%[0-9A-F]{2})*))(\\3)@)?(?=(\\[[0-9A-F:.]{2,}\\]|(?:[a-z0-9-._~!$&'()*+,;=]|%[0-9A-F]{2})*))\\5(?::(?=(\\d*))\\6)?)(\\/(?=((?:[a-z0-9-._~!$&'()*+,;=:@\\/]|%[0-9A-F]{2})*))\\8)?|(\\/"
"?(?!\\/)(?=((?:[a-z0-9-._~!$&'()*+,;=:@\\/]|%[0-9A-F]{2})*))\\10)?)(?:\\?(?=((?:[a-z0-9-._~!$&'()*+,;=:@\\/?]|%[0-9A-F]{2})*))\\11)?(?:#(?=((?:[a-z0-9-._~!$&'()*+,;=:@\\/?]|%[0-9A-F]{2})*))\\12)?$"); // Static as it can takes time (on gcc 4.9 for
// example)

std::smatch MatchString;
std::string::size_type PosColumn;
std::vector<BOF_SOCKET_ADDRESS> ListOfIpAddress_X;

/*
{ size=13 }
[0] "myprotocol://john.doe:[email protected]:123/forum/questions/file.txt?justkey&order=newest;tag=networking#top"
[1] "myprotocol"
[2] "john.doe:[email protected]:123"
[3] "john.doe:password"
[4] "john.doe:password"
[5] "1.2.3.4"
[6] "123"
[7] "/forum/questions/file.txt"
[8] "forum/questions/file.txt"
[9] false
[10] false
[11] "justkey&order=newest;tag=networking"
[12] "top"
[0] "protocol:/forum/questions/thefile.txt?justkey;order=newest&tag=networking#top"
[1] "protocol"
[2] false
[3] false
[4] false
[5] false
[6] false
[7] false
[8] false
[9] "/forum/questions/thefile.txt"
[10] "forum/questions/thefile.txt"
[11] "justkey;order=newest&tag=networking"
[12] "top"
*/

_rUri_X.Reset();
if (std::regex_search(_rUri_S, MatchString, S_RegExUri))
{
Expand Down Expand Up @@ -2338,13 +2371,53 @@ bool Bof_IsIpAddressPingable(uint32_t _TimeoutInMs_U32, const std::string &_rIpA
int ExitCode_i;

#if defined(_WIN32)
/*
https://stackoverflow.com/questions/9329749/batch-errorlevel-ping-response
ping errorlevel (exit code) is always 0 and in case of bad ip address it need a n value of 2 to "see" the problem....
Problem:
C:\tmp>ping -n 1 -w 1000 1.2.3.4
Pinging 1.2.3.4 with 32 bytes of data:
Reply from 194.42.74.25: Destination net unreachable.
Ping statistics for 1.2.3.4:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),<=====!!!!
Batch example:
set error=failure
ping -n 2 -w 1000 1.2.3.4 2>&1 && set error=success
REM ping -n 2 -w 1000 10.129.170.14 2>&1 && set error=success
echo %errorlevel%
echo %error%
Case Ok in ipv4:
C:\tmp>ping 10.129.170.14
Pinging 10.129.170.14 with 32 bytes of data:
Reply from 10.129.170.14: bytes=32 time=1ms TTL=63
Reply from 10.129.170.14: bytes=32 time=1ms TTL=63
Ping statistics for 10.129.170.14:
Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 1ms, Maximum = 1ms, Average = 1ms
=>Check that TTL= is present in output
*/
snprintf(pPingCmd_c, sizeof(pPingCmd_c), "ping -n 1 -w %d %s", _TimeoutInMs_U32, _rIpAddress_S.c_str());
#else
snprintf(pPingCmd_c, sizeof(pPingCmd_c), "ping -c 1 -w %d %s", _TimeoutInMs_U32 / 1000, _rIpAddress_S.c_str());
#endif
if (BofProcess::S_Execute_popen(pPingCmd_c, Output_S, ExitCode_i) == BOF_ERR_NO_ERROR)
{
Rts_B = (ExitCode_i == 0);
#if defined(_WIN32)
if (Rts_B)
{
if (Output_S.find("TTL=") == std::string::npos)
{
Rts_B = false;
}
}
#endif
}
return Rts_B;
}
Expand Down Expand Up @@ -2402,4 +2475,6 @@ bool Bof_IsIpAddressOpened(uint32_t _TimeoutInMs_U32, const std::string &_rIpAdd
}
return Rts_B;
}


END_BOF_NAMESPACE()
3 changes: 3 additions & 0 deletions lib/src/bofuri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,13 @@ std::string BofUri::Authority() const

return Rts_X.ToString(true, true, true, true);
}

const BOF_SOCKET_ADDRESS &BofUri::IpAddress(std::string &_rIpAddress_S) const
{
_rIpAddress_S = mSchemeAuthority_X.Protocol_S + "://" + Bof_SocketAddressToString(mSchemeAuthority_X.Ip_X, false, true);
return mSchemeAuthority_X.Ip_X;
}

const BofPath &BofUri::Path(std::string &_rPath_S) const
{
_rPath_S = mPath.FullPathName(false);
Expand Down Expand Up @@ -423,6 +425,7 @@ BOFERR BofUri::InitUriField(const std::string &_rUri_S)
if (Rts_E == BOF_ERR_NO_ERROR)
{
mSchemeAuthority_X = Uri_X;
Bof_IpAddressToSocketAddress(mSchemeAuthority_X.ToString(true,false,false,true), mSchemeAuthority_X.Ip_X);
mPath = BofPath(Path_S);
mFragment_S = Fragment_S;
if (mPath.IsValid())
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ set(ENUM_FLAG_FILES
set(FS_FILES
src/ut_path.cpp
src/ut_uri.cpp
src/ut_serviceendpoint.cpp
src/ut_fs.cpp
)

Expand Down
3 changes: 2 additions & 1 deletion tests/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ int main(int argc, char *argv[])
//::testing::GTEST_FLAG(filter) = "RawCircularBuffer_Test.*:CircularBuffer_Test.*:RawCircularBufferInSlotMode_Test.*";
//::testing::GTEST_FLAG(filter) = "BofThreadPool_Test.Dispatch";
//::testing::GTEST_FLAG(filter) = "ConIo_Test.*";
//::testing::GTEST_FLAG(filter) = "SocketOs_Test.*";
// ::testing::GTEST_FLAG(filter) = "ServiceEndPoint_Test.*:SocketOs_Test.*:Uri_Test.*";
//::testing::GTEST_FLAG(filter) = "ServiceEndPoint_Test.*";
// ::testing::GTEST_FLAG(filter) = "RawCircularBufferAlwaysContiguous_Test.*:RawCircularBuffer_Test.*:RawCircularBufferInSlotMode_Test.*";
// std::string CrtDir_S;
// BOF::Bof_GetCurrentDirectory(CrtDir_S);
Expand Down
Loading

0 comments on commit d0ee22e

Please sign in to comment.