Small changes
This commit is contained in:
parent
8c600a1cdb
commit
e50cb9f0e2
7 changed files with 131 additions and 79 deletions
|
|
@ -219,7 +219,7 @@ alni CommandLine::getInt(const String& id) const { auto& arg = getArg(id, Arg::I
|
||||||
alnf CommandLine::getFloat(const String& id) const { auto& arg = getArg(id, Arg::FLOAT); return arg.mFloat.mVal; }
|
alnf CommandLine::getFloat(const String& id) const { auto& arg = getArg(id, Arg::FLOAT); return arg.mFloat.mVal; }
|
||||||
bool CommandLine::getBool(const String& id) const { auto& arg = getArg(id, Arg::BOOL); return arg.mBool.mFlag; }
|
bool CommandLine::getBool(const String& id) const { auto& arg = getArg(id, Arg::BOOL); return arg.mBool.mFlag; }
|
||||||
const String& CommandLine::getString(const String& id) const { auto& arg = getArg(id, Arg::STR); return arg.mStr.mStr; }
|
const String& CommandLine::getString(const String& id) const { auto& arg = getArg(id, Arg::STR); return arg.mStr.mStr; }
|
||||||
const LocalConnectionLocation& CommandLine::getFile(const String& id) const { auto& arg = getArg(id, Arg::FILE_IN); return arg.mFile.mFileLocation; }
|
const LocalConnection::Location& CommandLine::getFile(const String& id) const { auto& arg = getArg(id, Arg::FILE_IN); return arg.mFile.mFileLocation; }
|
||||||
|
|
||||||
|
|
||||||
CommandLine::Arg& CommandLine::getArg(const String& id, Arg::Type type) {
|
CommandLine::Arg& CommandLine::getArg(const String& id, Arg::Type type) {
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ namespace tp {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct FileInputArg {
|
struct FileInputArg {
|
||||||
LocalConnectionLocation mFileLocation;
|
LocalConnection::Location mFileLocation;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Arg {
|
struct Arg {
|
||||||
|
|
@ -81,7 +81,7 @@ namespace tp {
|
||||||
[[nodiscard]] alnf getFloat(const String& id) const;
|
[[nodiscard]] alnf getFloat(const String& id) const;
|
||||||
[[nodiscard]] bool getBool(const String& id) const;
|
[[nodiscard]] bool getBool(const String& id) const;
|
||||||
[[nodiscard]] const String& getString(const String& id) const;
|
[[nodiscard]] const String& getString(const String& id) const;
|
||||||
[[nodiscard]] const LocalConnectionLocation& getFile(const String& id) const;
|
[[nodiscard]] const LocalConnection::Location& getFile(const String& id) const;
|
||||||
|
|
||||||
const CommandLine& operator=(const CommandLine&) = delete;
|
const CommandLine& operator=(const CommandLine&) = delete;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
using namespace tp;
|
using namespace tp;
|
||||||
|
|
||||||
bool LocalConnectionLocation::exists() const {
|
bool LocalConnection::Location::exists() const {
|
||||||
FILE* file = fopen(mLocation.read(), "r");
|
FILE* file = fopen(mLocation.read(), "r");
|
||||||
if (file) {
|
if (file) {
|
||||||
// File exists, close it and return 1
|
// File exists, close it and return 1
|
||||||
|
|
@ -16,18 +16,18 @@ bool LocalConnectionLocation::exists() const {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LocalConnection::connect(const LocalConnectionLocation& location, const LocalConnectionType& connectionInfo) {
|
bool LocalConnection::connect(const Location& location, const Type& connectionInfo) {
|
||||||
DEBUG_ASSERT(!mStatus.isOpened());
|
DEBUG_ASSERT(!mStatus.isOpened());
|
||||||
if (mStatus.isOpened()) return false;
|
if (mStatus.isOpened()) return false;
|
||||||
|
|
||||||
auto handle = new LocalConnectionContext();
|
auto handle = new LocalConnectionContext();
|
||||||
|
|
||||||
switch (connectionInfo.getType()) {
|
switch (connectionInfo.getType()) {
|
||||||
case LocalConnectionType::READ:
|
case Type::READ:
|
||||||
handle->open(location.getLocation().read(), false);
|
handle->open(location.getLocation().read(), false);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case LocalConnectionType::WRITE:
|
case Type::WRITE:
|
||||||
handle->open(location.getLocation().read(), true);
|
handle->open(location.getLocation().read(), true);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
@ -36,12 +36,12 @@ bool LocalConnection::connect(const LocalConnectionLocation& location, const Loc
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!handle->isOpen()) {
|
if (!handle->isOpen()) {
|
||||||
mStatus.setStatus(LocalConnectionStatus::DENIED);
|
mStatus.setStatus(Status::DENIED);
|
||||||
delete handle;
|
delete handle;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
mStatus.setStatus(LocalConnectionStatus::OPENED);
|
mStatus.setStatus(Status::OPENED);
|
||||||
mHandle = handle;
|
mHandle = handle;
|
||||||
mConnectionType = connectionInfo;
|
mConnectionType = connectionInfo;
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -52,7 +52,7 @@ bool LocalConnection::disconnect() {
|
||||||
if (!mStatus.isOpened() || !mHandle) return false;
|
if (!mStatus.isOpened() || !mHandle) return false;
|
||||||
mHandle->close();
|
mHandle->close();
|
||||||
delete mHandle;
|
delete mHandle;
|
||||||
mStatus.setStatus(LocalConnectionStatus::CLOSED);
|
mStatus.setStatus(Status::CLOSED);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,4 +4,62 @@
|
||||||
|
|
||||||
namespace tp {
|
namespace tp {
|
||||||
extern ModuleManifest gModuleConnection;
|
extern ModuleManifest gModuleConnection;
|
||||||
};
|
|
||||||
|
class Connection {
|
||||||
|
public:
|
||||||
|
typedef ualni SizeBytes;
|
||||||
|
typedef ualni BytePointer;
|
||||||
|
typedef int1 Byte;
|
||||||
|
|
||||||
|
class Status {
|
||||||
|
public:
|
||||||
|
enum State {
|
||||||
|
NONE,
|
||||||
|
OPENED,
|
||||||
|
CLOSED,
|
||||||
|
DENIED,
|
||||||
|
INVALID,
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
State mStatus = NONE;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Status() = default;
|
||||||
|
[[nodiscard]] State getStatus() const { return mStatus; }
|
||||||
|
void setStatus(State status) { mStatus = status; }
|
||||||
|
[[nodiscard]] bool isOpened() const { return mStatus == OPENED; }
|
||||||
|
};
|
||||||
|
|
||||||
|
class Type {
|
||||||
|
public:
|
||||||
|
enum State {
|
||||||
|
READ,
|
||||||
|
WRITE,
|
||||||
|
READ_WRITE,
|
||||||
|
NONE,
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
State mHandleType = NONE;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Type() = default;
|
||||||
|
explicit Type(bool read) : mHandleType((State) read) {}
|
||||||
|
explicit Type(State handleType) : mHandleType(handleType) {}
|
||||||
|
[[nodiscard]] State getType() const { return mHandleType; }
|
||||||
|
[[nodiscard]] bool isRead() const { return mHandleType == READ; }
|
||||||
|
[[nodiscard]] bool isWrite() const { return mHandleType == WRITE; }
|
||||||
|
};
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Status mStatus;
|
||||||
|
Type mConnectionType;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Connection() = default;
|
||||||
|
|
||||||
|
virtual const Status& getConnectionStatus() { return mStatus; }
|
||||||
|
virtual const Type& getConnectionType() { return mConnectionType; }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -3,73 +3,20 @@
|
||||||
#include "ConnectionCommon.hpp"
|
#include "ConnectionCommon.hpp"
|
||||||
|
|
||||||
namespace tp {
|
namespace tp {
|
||||||
|
|
||||||
class LocalConnectionContext;
|
class LocalConnectionContext;
|
||||||
|
|
||||||
class LocalConnectionLocation {
|
class LocalConnection : public Connection {
|
||||||
String mLocation;
|
|
||||||
public:
|
public:
|
||||||
LocalConnectionLocation() : mLocation("tmp") {};
|
class Location {
|
||||||
explicit LocalConnectionLocation(const String& location) : mLocation(location) {}
|
String mLocation;
|
||||||
void setLocation(const String& location) { mLocation = location; }
|
public:
|
||||||
[[nodiscard]] const String& getLocation() const { return mLocation; }
|
Location() : mLocation("tmp") {};
|
||||||
[[nodiscard]] bool exists() const;
|
explicit Location(const String& location) : mLocation(location) {}
|
||||||
};
|
void setLocation(const String& location) { mLocation = location; }
|
||||||
|
[[nodiscard]] const String& getLocation() const { return mLocation; }
|
||||||
class LocalConnectionType {
|
[[nodiscard]] bool exists() const;
|
||||||
public:
|
|
||||||
enum HandleType {
|
|
||||||
READ,
|
|
||||||
WRITE,
|
|
||||||
NONE,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
|
||||||
HandleType mHandleType;
|
|
||||||
|
|
||||||
public:
|
|
||||||
LocalConnectionType() : mHandleType(NONE) {}
|
|
||||||
explicit LocalConnectionType(bool read) : mHandleType((HandleType) read) {}
|
|
||||||
explicit LocalConnectionType(HandleType handleType) : mHandleType(handleType) {}
|
|
||||||
[[nodiscard]] HandleType getType() const { return mHandleType; }
|
|
||||||
[[nodiscard]] bool isRead() const { return mHandleType == READ; }
|
|
||||||
[[nodiscard]] bool isWrite() const { return mHandleType == WRITE; }
|
|
||||||
};
|
|
||||||
|
|
||||||
class LocalConnectionStatus {
|
|
||||||
public:
|
|
||||||
enum Status {
|
|
||||||
NONE,
|
|
||||||
OPENED,
|
|
||||||
CLOSED,
|
|
||||||
DENIED,
|
|
||||||
INVALID,
|
|
||||||
};
|
|
||||||
|
|
||||||
private:
|
|
||||||
Status mStatus = NONE;
|
|
||||||
|
|
||||||
public:
|
|
||||||
LocalConnectionStatus() = default;
|
|
||||||
[[nodiscard]] Status getStatus() const { return mStatus; }
|
|
||||||
void setStatus(Status status) { mStatus = status; }
|
|
||||||
[[nodiscard]] bool isOpened() const { return mStatus == OPENED; }
|
|
||||||
};
|
|
||||||
|
|
||||||
class LocalConnection {
|
|
||||||
typedef ualni SizeBytes;
|
|
||||||
typedef ualni BytePointer;
|
|
||||||
typedef int1 Byte;
|
|
||||||
|
|
||||||
private:
|
|
||||||
LocalConnectionContext* mHandle = nullptr;
|
|
||||||
|
|
||||||
LocalConnectionLocation mLocation;
|
|
||||||
LocalConnectionType mConnectionType;
|
|
||||||
LocalConnectionStatus mStatus;
|
|
||||||
|
|
||||||
BytePointer mPointer = 0;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
LocalConnection() {
|
LocalConnection() {
|
||||||
MODULE_SANITY_CHECK(gModuleConnection)
|
MODULE_SANITY_CHECK(gModuleConnection)
|
||||||
|
|
@ -80,13 +27,13 @@ namespace tp {
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual bool connect(const LocalConnectionLocation& location, const LocalConnectionType& connectionInfo);
|
virtual bool connect(const Location& location, const Type& connectionInfo);
|
||||||
virtual bool disconnect();
|
virtual bool disconnect();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual const LocalConnectionStatus& getConnectionStatus() { return mStatus; }
|
virtual const Status& getConnectionStatus() { return mStatus; }
|
||||||
virtual const LocalConnectionType& getConnectionType() { return mConnectionType; }
|
virtual const Type& getConnectionType() { return mConnectionType; }
|
||||||
virtual const LocalConnectionLocation& getLocation() { return mLocation; }
|
virtual const Location& getLocation() { return mLocation; }
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual bool setPointer(BytePointer pointer);
|
virtual bool setPointer(BytePointer pointer);
|
||||||
|
|
@ -95,5 +42,10 @@ namespace tp {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual SizeBytes size();
|
virtual SizeBytes size();
|
||||||
|
|
||||||
|
private:
|
||||||
|
LocalConnectionContext* mHandle = nullptr;
|
||||||
|
Location mLocation;
|
||||||
|
BytePointer mPointer = 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "ConnectionCommon.hpp"
|
||||||
|
#include "List.hpp"
|
||||||
|
|
||||||
|
namespace tp {
|
||||||
|
|
||||||
|
class RemoteConnectionContext;
|
||||||
|
|
||||||
|
class RemoteConnection : public Connection {
|
||||||
|
public:
|
||||||
|
class Location {
|
||||||
|
ualni mId = 0;
|
||||||
|
public:
|
||||||
|
Location() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
RemoteConnection() {
|
||||||
|
MODULE_SANITY_CHECK(gModuleConnection)
|
||||||
|
};
|
||||||
|
|
||||||
|
virtual ~RemoteConnection() {
|
||||||
|
if (mStatus.isOpened()) RemoteConnection::disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual bool connect(const Location& location, const Type& connectionInfo);
|
||||||
|
virtual bool disconnect();
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual const Location& getLocation() { return mLocation; }
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual bool readBytes(Byte* bytes, SizeBytes size);
|
||||||
|
virtual bool writeBytes(const Byte* bytes, SizeBytes size);
|
||||||
|
|
||||||
|
private:
|
||||||
|
RemoteConnectionContext* mHandle = nullptr;
|
||||||
|
Location mLocation;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -11,14 +11,14 @@ TEST_DEF_STATIC(Simple) {
|
||||||
|
|
||||||
{
|
{
|
||||||
LocalConnection file;
|
LocalConnection file;
|
||||||
file.connect(LocalConnectionLocation(String("tmp2.txt")), LocalConnectionType(false));
|
file.connect(LocalConnection::Location(String("tmp2.txt")), LocalConnection::Type(false));
|
||||||
file.writeBytes(data, 6);
|
file.writeBytes(data, 6);
|
||||||
file.disconnect();
|
file.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
LocalConnection file;
|
LocalConnection file;
|
||||||
file.connect(LocalConnectionLocation(String("tmp2.txt")), LocalConnectionType(true));
|
file.connect(LocalConnection::Location(String("tmp2.txt")), LocalConnection::Type(true));
|
||||||
file.readBytes(dataRead, 6);
|
file.readBytes(dataRead, 6);
|
||||||
file.disconnect();
|
file.disconnect();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue