Improve:SharedData module.

This commit is contained in:
Fancy code 2024-04-18 22:35:31 +08:00
parent 1e42c00ae2
commit 8aaf9b4074
9 changed files with 439 additions and 365 deletions

View File

@ -24,22 +24,22 @@ TEST(SharedDataTest, Demo)
CreateLogModule(); CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END); ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9; constexpr int PROJECT_ID = 9;
SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataMinor) { if (nullptr != sharedDataMinor) {
int writableData = 99; int writableData = 99;
sharedDataMinor->mMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int)); IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int));
sharedDataMinor->mSetWritableData(sharedDataMinor, &writableData, sizeof(int)); ISetWritableData(sharedDataMinor, &writableData, sizeof(int));
} }
if (nullptr != sharedDataPrimary) { if (nullptr != sharedDataPrimary) {
int readableData = -1; int readableData = -1;
sharedDataPrimary->mMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int));
sharedDataPrimary->mGetReadableData(sharedDataPrimary, &readableData, sizeof(int)); IGetReadableData(sharedDataPrimary, &readableData, sizeof(int));
LogInfo("readableData = %d\n", readableData); LogInfo("readableData = %d\n", readableData);
sharedDataPrimary->mFree(sharedDataPrimary); IShareDataFree(sharedDataPrimary);
} }
if (nullptr != sharedDataMinor) { if (nullptr != sharedDataMinor) {
sharedDataMinor->mFree(sharedDataMinor); IShareDataFree(sharedDataMinor);
} }
ILogUnInit(); ILogUnInit();
} }
@ -50,8 +50,8 @@ TEST(SharedDataTest, Demo2)
CreateLogModule(); CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END); ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9; constexpr int PROJECT_ID = 9;
SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataMinor) { if (nullptr != sharedDataMinor) {
IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int)); IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int));
ISetWritableData(sharedDataMinor, (void *)&WRITABLE_DATA, sizeof(int)); ISetWritableData(sharedDataMinor, (void *)&WRITABLE_DATA, sizeof(int));
@ -76,8 +76,8 @@ TEST(SharedDataTest, Demo3)
CreateLogModule(); CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END); ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9; constexpr int PROJECT_ID = 9;
SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataMinor) { if (nullptr != sharedDataMinor) {
constexpr int WRITABLE_DATA_LENGTH = 9; constexpr int WRITABLE_DATA_LENGTH = 9;
char writableData[WRITABLE_DATA_LENGTH] = {0}; char writableData[WRITABLE_DATA_LENGTH] = {0};
@ -107,8 +107,8 @@ TEST(SharedDataTest, Demo4)
CreateLogModule(); CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END); ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9; constexpr int PROJECT_ID = 9;
SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataMinor) { if (nullptr != sharedDataMinor) {
IMakeSharedData(sharedDataMinor, sizeof(int) * 2, sizeof(int)); IMakeSharedData(sharedDataMinor, sizeof(int) * 2, sizeof(int));
ISetWritableData(sharedDataMinor, (void *)&WRITABLE_DATA, sizeof(int)); ISetWritableData(sharedDataMinor, (void *)&WRITABLE_DATA, sizeof(int));
@ -135,7 +135,7 @@ TEST(SharedDataTest, Demo5)
CreateLogModule(); CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END); ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9; constexpr int PROJECT_ID = 9;
SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataPrimary) { if (nullptr != sharedDataPrimary) {
constexpr int DEFAULT_DATA = 0; constexpr int DEFAULT_DATA = 0;
int readableData = DEFAULT_DATA; int readableData = DEFAULT_DATA;
@ -155,8 +155,8 @@ TEST(SharedDataTest, Demo6)
CreateLogModule(); CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END); ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9; constexpr int PROJECT_ID = 9;
SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataPrimary) { if (nullptr != sharedDataPrimary) {
constexpr int DEFAULT_DATA = 0; constexpr int DEFAULT_DATA = 0;
int readableData = DEFAULT_DATA; int readableData = DEFAULT_DATA;
@ -180,8 +180,8 @@ TEST(SharedDataTest, Demo7)
CreateLogModule(); CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END); ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9; constexpr int PROJECT_ID = 9;
SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataPrimary) { if (nullptr != sharedDataPrimary) {
IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int));
ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int)); ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int));
@ -209,8 +209,8 @@ TEST(SharedDataTest, UNIT_SharedData_DEME_Demo7)
CreateLogModule(); CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END); ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9; constexpr int PROJECT_ID = 9;
SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataPrimary) { if (nullptr != sharedDataPrimary) {
IMakeSharedData(sharedDataPrimary, sizeof(int) * 2, sizeof(int)); IMakeSharedData(sharedDataPrimary, sizeof(int) * 2, sizeof(int));
ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int)); ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int));
@ -237,8 +237,8 @@ TEST(SharedDataTest, UNIT_SharedData_DEME_Demo8)
CreateLogModule(); CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END); ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9; constexpr int PROJECT_ID = 9;
SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataPrimary) { if (nullptr != sharedDataPrimary) {
int writableData[2] = {0}; int writableData[2] = {0};
IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int) * 2); IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int) * 2);
@ -267,8 +267,8 @@ TEST(SharedDataTest, UNIT_SharedData_DEME_Demo9)
CreateLogModule(); CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END); ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9; constexpr int PROJECT_ID = 9;
SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataPrimary) { if (nullptr != sharedDataPrimary) {
IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int));
ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int)); ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int));
@ -296,8 +296,8 @@ TEST(SharedDataTest, UNIT_SharedData_DEME_Demo10)
CreateLogModule(); CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END); ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9; constexpr int PROJECT_ID = 9;
SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataPrimary) { if (nullptr != sharedDataPrimary) {
IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int));
ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int)); ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int));
@ -323,8 +323,8 @@ TEST(SharedDataTest, UNIT_SharedData_DEME_Demo11)
CreateLogModule(); CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END); ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9; constexpr int PROJECT_ID = 9;
SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int)); IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int));
if (nullptr != sharedDataPrimary) { if (nullptr != sharedDataPrimary) {
IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int));

View File

@ -105,7 +105,7 @@ const char *GetConfigBaseModuleName(void)
} }
std::shared_ptr<IConfigBase> *NewConfigBase(const char *fileName) std::shared_ptr<IConfigBase> *NewConfigBase(const char *fileName)
{ {
LogInfo("Create the uart device object.\n"); LogInfo("Create the config base object.\n");
ConfigBase *impl = (ConfigBase *)malloc(sizeof(ConfigBase)); ConfigBase *impl = (ConfigBase *)malloc(sizeof(ConfigBase));
if (nullptr == impl) { if (nullptr == impl) {
LogError("NewConfigBase::malloc failed.\n"); LogError("NewConfigBase::malloc failed.\n");

View File

@ -13,8 +13,6 @@ include_directories(
# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs # ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
# ) # )
aux_source_directory(./src SRC_FILES) aux_source_directory(./src SRC_FILES)
set(TARGET_NAME SharedData) set(TARGET_NAME SharedData)

View File

@ -1,67 +1,42 @@
/* /*
* Copyright (c) 2023 Fancy Code. * Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#ifndef SHARED_DATA_H #ifndef SHARED_DATA_H
#define SHARED_DATA_H #define SHARED_DATA_H
#include "StatusCode.h" #include "StatusCode.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
enum SHARED_DATA_CODE enum SHARED_DATA_CODE
{ {
SHARED_DATA_CODE_INIT_FAILED = STATUS_CODE_END, SHARED_DATA_CODE_INIT_FAILED = STATUS_CODE_END,
SHARED_DATA_CODE_WRONG_PEER_PARAMETERS, SHARED_DATA_CODE_WRONG_PEER_PARAMETERS,
SHARED_DATA_CODE_END SHARED_DATA_CODE_END
}; };
enum SHARER_NAME enum SHARER_NAME
{ {
SHARER_NAME_PRIMARY = 0, SHARER_NAME_PRIMARY = 0,
SHARER_NAME_MINOR, SHARER_NAME_MINOR,
SHARER_NAME_END SHARER_NAME_END
}; };
typedef struct shared_data SharedData; void *CreateSharedData(const enum SHARER_NAME name, const char *path, const int projectId);
typedef struct shared_data const StatusCode IMakeSharedData(void *object, const unsigned int readableSize, const unsigned int writableSize);
{ const StatusCode ICleanSharedData(void *object);
const StatusCode (*mMakeSharedData)(SharedData *, const unsigned int, const unsigned int); const StatusCode IGetReadableData(void *object, void *buf, const unsigned int bufLength);
const StatusCode (*mCleanSharedData)(SharedData *); void ISetWritableData(void *object, void *buf, const unsigned int bufLength);
const StatusCode (*mGetReadableData)(SharedData *, void *, const unsigned int); void IShareDataFree(void *object);
void (*mSetWritableData)(SharedData *, void *, const unsigned int); #ifdef __cplusplus
void (*mFree)(void *); }
} SharedData; #endif
SharedData *CreateSharedData(const enum SHARER_NAME name, const char *path, const int projectId);
static inline const StatusCode IMakeSharedData(SharedData *object, const unsigned int readableSize,
const unsigned int writableSize)
{
return object->mMakeSharedData(object, readableSize, writableSize);
}
static inline const StatusCode ICleanSharedData(SharedData *object)
{
return object->mCleanSharedData(object);
}
static inline const StatusCode IGetReadableData(SharedData *object, void *buf, const unsigned int bufLength)
{
return object->mGetReadableData(object, buf, bufLength);
}
static inline void ISetWritableData(SharedData *object, void *buf, const unsigned int bufLength)
{
object->mSetWritableData(object, buf, bufLength);
}
static inline void IShareDataFree(SharedData *object)
{
object->mFree(object);
}
#ifdef __cplusplus
}
#endif
#endif #endif

View File

@ -0,0 +1,51 @@
/*
* Copyright (c) 2023 Fancy Code.
* Licensed 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 "ISharedData.h"
#include "ILog.h"
#include "SharedDataImpl.h" // TODO: 互相包含,需要修改
#include <string.h>
void ISharedData::MakeSharedMemory(const unsigned int readableSize, const unsigned int writableSize)
{
}
const StatusCode ISharedData::GetReadableMemory(void *buf, const unsigned int &bufLength)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
void ISharedData::SetWritableMemory(void *buf, const unsigned int &bufLength)
{
}
StatusCode ISharedData::FreeSharedMemory(void)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
static const char *SHARED_DATA_NAME = "shared_data";
const char *GetSharedDataModuleName(void)
{
return SHARED_DATA_NAME;
}
std::shared_ptr<ISharedData> *NewSharedData(const enum SHARER_NAME &name, const char *path, const int &projectId)
{
LogInfo("Create the shared data object.\n");
SharedData *impl = (SharedData *)malloc(sizeof(SharedData));
if (nullptr == impl) {
LogError("NewConfigBase::malloc failed.\n");
return nullptr;
}
SharedData tmp;
memcpy((void *)impl, (void *)&tmp, sizeof(SharedData));
impl->mHeader.mCheckName = SHARED_DATA_NAME;
impl->mISharedData = std::make_shared<SharedDataImpl>(name, path, projectId);
return (std::shared_ptr<ISharedData> *)(((char *)impl) + sizeof(ISharedDataHeader));
}

View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2023 Fancy Code.
* Licensed 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 I_SHARED_DATA_H
#define I_SHARED_DATA_H
#include "SharedData.h"
#include "StatusCode.h"
#include <memory>
class ISharedData
{
public:
ISharedData() = default;
virtual ~ISharedData() = default;
virtual void MakeSharedMemory(const unsigned int readableSize, const unsigned int writableSize);
virtual const StatusCode GetReadableMemory(void *buf, const unsigned int &bufLength);
virtual void SetWritableMemory(void *buf, const unsigned int &bufLength);
virtual StatusCode FreeSharedMemory(void);
};
typedef struct i_shared_data_header
{
const char *mCheckName;
} ISharedDataHeader;
typedef struct config_base
{
ISharedDataHeader mHeader;
std::shared_ptr<ISharedData> mISharedData;
} SharedData;
const char *GetSharedDataModuleName(void);
std::shared_ptr<ISharedData> *NewSharedData(const enum SHARER_NAME &name, const char *path, const int &projectId);
#endif

View File

@ -1,20 +1,74 @@
/* /*
* Copyright (c) 2023 Fancy Code. * Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include "SharedData.h" #include "SharedData.h"
#include "SharedDataImpl.h" #include "ILog.h"
SharedData *CreateSharedData(const enum SHARER_NAME name, const char *path, const int projectId) #include "ISharedData.h"
{ static bool ObjectCheck(void *object)
return (SharedData *)NewSharedDataImpl(name, path, projectId); {
if (nullptr == object) {
LogError("nullptr object!\n");
return false;
}
if (*((const char **)(((char *)object) - sizeof(ISharedDataHeader))) != GetSharedDataModuleName()) {
LogError("Illegal object!\n");
return false;
}
return true;
}
void *CreateSharedData(const enum SHARER_NAME name, const char *path, const int projectId)
{
std::shared_ptr<ISharedData> *sharedDataObject = NewSharedData(name, path, projectId);
if (nullptr != sharedDataObject) {
// (*sharedDataObject)->OpenConfigFile();
}
return sharedDataObject;
}
const StatusCode IMakeSharedData(void *object, const unsigned int readableSize, const unsigned int writableSize)
{
if (ObjectCheck(object) == true) {
(*(std::shared_ptr<ISharedData> *)object)->MakeSharedMemory(readableSize, writableSize);
return CreateStatusCode(STATUS_CODE_OK);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
const StatusCode ICleanSharedData(void *object)
{
if (ObjectCheck(object) == true) {
// (*(std::shared_ptr<ISharedData> *)object)->MakeSharedMemory(readableSize, writableSize);
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
const StatusCode IGetReadableData(void *object, void *buf, const unsigned int bufLength)
{
if (ObjectCheck(object) == true) {
return (*(std::shared_ptr<ISharedData> *)object)->GetReadableMemory(buf, bufLength);
}
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
}
void ISetWritableData(void *object, void *buf, const unsigned int bufLength)
{
if (ObjectCheck(object) == true) {
(*(std::shared_ptr<ISharedData> *)object)->SetWritableMemory(buf, bufLength);
}
}
void IShareDataFree(void *object)
{
if (ObjectCheck(object) == true) {
(*(std::shared_ptr<ISharedData> *)object)->FreeSharedMemory();
(*(std::shared_ptr<ISharedData> *)object).reset();
free(((char *)object) - sizeof(ISharedDataHeader));
}
} }

View File

@ -1,180 +1,142 @@
/* /*
* Copyright (c) 2023 Fancy Code. * Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include "SharedDataImpl.h" #include "SharedDataImpl.h"
#include "ILog.h" #include "ILog.h"
#include "SharedDataCode.h" #include "SharedDataCode.h"
#include <cstring> #include <cstring>
static const char *SHARED_DATA_NAME = "shared_data"; static const char *SHARED_DATA_NAME = "shared_data";
constexpr short THERE_TWO_USER_DATA_HEADER = 2; constexpr short THERE_TWO_USER_DATA_HEADER = 2;
SharedDataCpp::SharedDataCpp(const SHARER_NAME &sharerName, const char *path, const int &projectId) SharedDataImpl::SharedDataImpl(const SHARER_NAME &sharerName, const char *path, const int &projectId)
: SharedMemory(path, projectId), mSharerName(sharerName) : SharedMemory(path, projectId), mSharerName(sharerName)
{ {
mPrimaryReadSize = 0; mPrimaryReadSize = 0;
mMinorReadSize = 0; mMinorReadSize = 0;
mSharedMemeory = nullptr; mSharedMemeory = nullptr;
} }
void SharedDataCpp::MakeSharedMemory(const unsigned int readableSize, const unsigned int writableSize) void SharedDataImpl::MakeSharedMemory(const unsigned int readableSize, const unsigned int writableSize)
{ {
if (SHARER_NAME_PRIMARY == mSharerName) { if (SHARER_NAME_PRIMARY == mSharerName) {
mPrimaryReadSize = readableSize; mPrimaryReadSize = readableSize;
mMinorReadSize = writableSize; mMinorReadSize = writableSize;
} }
else if (SHARER_NAME_MINOR == mSharerName) { else if (SHARER_NAME_MINOR == mSharerName) {
mPrimaryReadSize = writableSize; mPrimaryReadSize = writableSize;
mMinorReadSize = readableSize; mMinorReadSize = readableSize;
} }
else { else {
LogError("Make shared memory failed.\n"); LogError("Make shared memory failed.\n");
return; return;
} }
const int SHARED_MEMORY_SIZE = readableSize + writableSize + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER; const int SHARED_MEMORY_SIZE = readableSize + writableSize + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER;
SharedMemory::MakeSharedMemory(SHARED_MEMORY_SIZE); SharedMemory::MakeSharedMemory(SHARED_MEMORY_SIZE);
mSharedMemeory = SharedMemory::GetMemory(); mSharedMemeory = SharedMemory::GetMemory();
WritableDataInit(); WritableDataInit();
} }
void SharedDataCpp::WritableDataInit(void) const StatusCode SharedDataImpl::GetReadableMemory(void *buf, const unsigned int &bufLength)
{ {
if (nullptr == mSharedMemeory) { if (nullptr == mSharedMemeory) {
LogError("mSharedMemeory is nullptr, failed.\n"); LogError("mSharedMemeory is nullptr, failed.\n");
return; return CreateStatusCode(STATUS_CODE_NOT_OK);
} }
if (SHARER_NAME_PRIMARY == mSharerName) { if (SHARER_NAME_PRIMARY == mSharerName && bufLength == mPrimaryReadSize) {
UserDataHeader *writableHeader = UserDataHeader *readableHeader = (UserDataHeader *)((char *)mSharedMemeory);
(UserDataHeader *)((char *)mSharedMemeory + sizeof(UserDataHeader) + mPrimaryReadSize); LogInfo("Want to read %d and can be read %d\n", bufLength, readableHeader->mDataLength);
memcpy(writableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH); if (memcmp(readableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0 &&
writableHeader->mDataLength = mMinorReadSize; bufLength == readableHeader->mDataLength) {
} memcpy(buf, (char *)mSharedMemeory + sizeof(UserDataHeader), bufLength);
else if (SHARER_NAME_MINOR == mSharerName) { }
UserDataHeader *writableHeader = (UserDataHeader *)((char *)mSharedMemeory); else {
memcpy(writableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH); LogError("Readable memory didn't init yet or init error.\n");
writableHeader->mDataLength = mPrimaryReadSize; return CreateSharedDataCode(SHARED_DATA_CODE_WRONG_PEER_PARAMETERS);
} }
else { }
LogError("Make shared memory failed.\n"); else if (SHARER_NAME_MINOR == mSharerName && bufLength == mMinorReadSize) {
return; UserDataHeader *readableHeader =
} (UserDataHeader *)((char *)mSharedMemeory + sizeof(UserDataHeader) + mPrimaryReadSize);
} LogInfo("Want to read %d and can be read %d\n", bufLength, readableHeader->mDataLength);
const StatusCode SharedDataCpp::GetReadableMemory(void *buf, const unsigned int &bufLength) if (memcmp(readableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0 &&
{ bufLength == readableHeader->mDataLength) {
if (nullptr == mSharedMemeory) { memcpy(buf,
LogError("mSharedMemeory is nullptr, failed.\n"); (char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize,
return CreateStatusCode(STATUS_CODE_NOT_OK); bufLength);
} }
if (SHARER_NAME_PRIMARY == mSharerName && bufLength == mPrimaryReadSize) { else {
UserDataHeader *readableHeader = (UserDataHeader *)((char *)mSharedMemeory); LogError("Readable memory didn't init yet or init error.\n");
LogInfo("Want to read %d and can be read %d\n", bufLength, readableHeader->mDataLength); return CreateSharedDataCode(SHARED_DATA_CODE_WRONG_PEER_PARAMETERS);
if (memcmp(readableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0 && }
bufLength == readableHeader->mDataLength) { }
memcpy(buf, (char *)mSharedMemeory + sizeof(UserDataHeader), bufLength); else {
} LogError("Get readable memory failed.\n");
else { return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
LogError("Readable memory didn't init yet or init error.\n"); }
return CreateSharedDataCode(SHARED_DATA_CODE_WRONG_PEER_PARAMETERS); return CreateStatusCode(STATUS_CODE_OK);
} }
} void SharedDataImpl::SetWritableMemory(void *buf, const unsigned int &bufLength)
else if (SHARER_NAME_MINOR == mSharerName && bufLength == mMinorReadSize) { {
UserDataHeader *readableHeader = if (nullptr == mSharedMemeory) {
(UserDataHeader *)((char *)mSharedMemeory + sizeof(UserDataHeader) + mPrimaryReadSize); LogError("mSharedMemeory is nullptr, failed.\n");
LogInfo("Want to read %d and can be read %d\n", bufLength, readableHeader->mDataLength); return;
if (memcmp(readableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0 && }
bufLength == readableHeader->mDataLength) { if (SHARER_NAME_PRIMARY == mSharerName && bufLength == mMinorReadSize) {
memcpy(buf, UserDataHeader *readableHeader = (UserDataHeader *)((char *)mSharedMemeory);
(char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize, if (memcmp(readableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0) {
bufLength); if (mPrimaryReadSize == readableHeader->mDataLength) {
} memcpy((char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize,
else { buf,
LogError("Readable memory didn't init yet or init error.\n"); bufLength);
return CreateSharedDataCode(SHARED_DATA_CODE_WRONG_PEER_PARAMETERS); }
} else {
} LogError("Peer not match.\n");
else { }
LogError("Get readable memory failed.\n"); }
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); else {
} memcpy((char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize,
return CreateStatusCode(STATUS_CODE_OK); buf,
} bufLength);
void SharedDataCpp::SetWritableMemory(void *buf, const unsigned int &bufLength) }
{ }
if (nullptr == mSharedMemeory) { else if (SHARER_NAME_MINOR == mSharerName && bufLength == mPrimaryReadSize) {
LogError("mSharedMemeory is nullptr, failed.\n"); memcpy((char *)mSharedMemeory + sizeof(UserDataHeader), buf, bufLength);
return; }
} else {
if (SHARER_NAME_PRIMARY == mSharerName && bufLength == mMinorReadSize) { LogError("Set writable memory failed.\n");
UserDataHeader *readableHeader = (UserDataHeader *)((char *)mSharedMemeory); }
if (memcmp(readableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0) { }
if (mPrimaryReadSize == readableHeader->mDataLength) { StatusCode SharedDataImpl::FreeSharedMemory(void)
memcpy((char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize, {
buf, return CleanSharedMemory();
bufLength); }
} void SharedDataImpl::WritableDataInit(void)
else { {
LogError("Peer not match.\n"); if (nullptr == mSharedMemeory) {
} LogError("mSharedMemeory is nullptr, failed.\n");
} return;
else { }
memcpy((char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize, if (SHARER_NAME_PRIMARY == mSharerName) {
buf, UserDataHeader *writableHeader =
bufLength); (UserDataHeader *)((char *)mSharedMemeory + sizeof(UserDataHeader) + mPrimaryReadSize);
} memcpy(writableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH);
} writableHeader->mDataLength = mMinorReadSize;
else if (SHARER_NAME_MINOR == mSharerName && bufLength == mPrimaryReadSize) { }
memcpy((char *)mSharedMemeory + sizeof(UserDataHeader), buf, bufLength); else if (SHARER_NAME_MINOR == mSharerName) {
} UserDataHeader *writableHeader = (UserDataHeader *)((char *)mSharedMemeory);
else { memcpy(writableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH);
LogError("Set writable memory failed.\n"); writableHeader->mDataLength = mPrimaryReadSize;
} }
} else {
static const StatusCode MakeSharedData(SharedData *object, const unsigned int readableSize, LogError("Make shared memory failed.\n");
const unsigned int writableSize) return;
{ }
SharedDataImpl *impl = ((SharedDataImpl *)(((char *)object) - sizeof(SharedDataHeader)));
impl->mSharedData->MakeSharedMemory(readableSize, writableSize);
return CreateStatusCode(STATUS_CODE_OK);
}
static const StatusCode GetSharedReadableMemory(SharedData *object, void *buf, const unsigned int bufLength)
{
SharedDataImpl *impl = ((SharedDataImpl *)(((char *)object) - sizeof(SharedDataHeader)));
return impl->mSharedData->GetReadableMemory(buf, bufLength);
}
static void SetSharedWritableMemory(SharedData *object, void *buf, const unsigned int bufLength)
{
SharedDataImpl *impl = ((SharedDataImpl *)(((char *)object) - sizeof(SharedDataHeader)));
impl->mSharedData->SetWritableMemory(buf, bufLength);
}
static void SharedDataImplFree(void *ptr)
{
SharedDataImpl *object = ((SharedDataImpl *)(((char *)ptr) - sizeof(SharedDataHeader)));
if (SHARED_DATA_NAME == object->mHeader.mCheckName) {
object->mSharedData->CleanSharedMemory();
object->mSharedData = nullptr;
free(((char *)ptr) - sizeof(SharedDataHeader));
}
else {
LogError("Unknow ptr.\n");
}
}
SharedData *NewSharedDataImpl(const SHARER_NAME &name, const char *path, const int &projectId)
{
SharedDataImpl *impl = (SharedDataImpl *)malloc(sizeof(SharedDataImpl));
SharedDataImpl tmp;
memcpy((void *)impl, (void *)&tmp, sizeof(SharedDataImpl));
impl->mHeader.mCheckName = SHARED_DATA_NAME;
impl->mBase.mMakeSharedData = MakeSharedData;
impl->mBase.mGetReadableData = GetSharedReadableMemory;
impl->mBase.mSetWritableData = SetSharedWritableMemory;
impl->mBase.mFree = SharedDataImplFree;
impl->mSharedData = std::make_shared<SharedDataCpp>(name, path, projectId);
return (SharedData *)(((char *)impl) + sizeof(SharedDataHeader));
} }

View File

@ -1,70 +1,63 @@
/* /*
* Copyright (c) 2023 Fancy Code. * Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#ifndef SHARED_DATA_IMPL_H #ifndef SHARED_DATA_IMPL_H
#define SHARED_DATA_IMPL_H #define SHARED_DATA_IMPL_H
#include "SharedData.h" #include "ISharedData.h"
#include "SharedMemory.h" #include "SharedMemory.h"
#include <memory> #include <memory>
#include <mutex> // #include <mutex>
constexpr int USER_NAME_BUF_LENGTH = 4; constexpr int USER_NAME_BUF_LENGTH = 4;
#define USER_NAME_INIT_NAME "XING" #define USER_NAME_INIT_NAME "XING"
enum USER_DATA_INIT enum USER_DATA_INIT
{ {
USER_DATA_INIT_SUCCEED = 0, USER_DATA_INIT_SUCCEED = 0,
USER_DATA_INIT_FAILED, USER_DATA_INIT_FAILED,
USER_DATA_INIT_END USER_DATA_INIT_END
}; };
typedef struct shared_data_header typedef struct shared_data_header
{ {
const char *mCheckName; const char *mCheckName;
} SharedDataHeader; } SharedDataHeader;
// TODO: Use mutex to luck memory data? // TODO: Use mutex to luck memory data?
// typedef struct shared_memory_header // typedef struct shared_memory_header
// { // {
// std::mutex mMutex; // std::mutex mMutex;
// }; // };
typedef struct user_data_header typedef struct user_data_header
{ {
char mUserName[USER_NAME_BUF_LENGTH]; char mUserName[USER_NAME_BUF_LENGTH];
unsigned int mDataLength; unsigned int mDataLength;
int mIsInit; int mIsInit;
} UserDataHeader; } UserDataHeader;
class SharedDataCpp : public SharedMemory class SharedDataImpl : public ISharedData, public SharedMemory
{ {
public: public:
SharedDataCpp(const SHARER_NAME &sharerName, const char *path, const int &projectId); SharedDataImpl(const SHARER_NAME &sharerName, const char *path, const int &projectId);
virtual ~SharedDataCpp() = default; virtual ~SharedDataImpl() = default;
void MakeSharedMemory(const unsigned int readableSize, const unsigned int writableSize); void MakeSharedMemory(const unsigned int readableSize, const unsigned int writableSize) override;
const StatusCode GetReadableMemory(void *buf, const unsigned int &bufLength); const StatusCode GetReadableMemory(void *buf, const unsigned int &bufLength) override;
void SetWritableMemory(void *buf, const unsigned int &bufLength); void SetWritableMemory(void *buf, const unsigned int &bufLength) override;
StatusCode FreeSharedMemory(void) override;
private:
void WritableDataInit(void); private:
void WritableDataInit(void);
private:
const SHARER_NAME mSharerName; private:
unsigned int mPrimaryReadSize; const SHARER_NAME mSharerName;
unsigned int mMinorReadSize; unsigned int mPrimaryReadSize;
void *mSharedMemeory; unsigned int mMinorReadSize;
}; void *mSharedMemeory;
typedef struct shared_data_impl SharedDataImpl; };
typedef struct shared_data_impl
{
SharedDataHeader mHeader;
SharedData mBase;
std::shared_ptr<SharedDataCpp> mSharedData;
} SharedDataImpl;
SharedData *NewSharedDataImpl(const SHARER_NAME &name, const char *path, const int &projectId);
#endif #endif