Small changes
This commit is contained in:
parent
e9dbee6667
commit
7595ae3926
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; }
|
||||
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 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) {
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ namespace tp {
|
|||
};
|
||||
|
||||
struct FileInputArg {
|
||||
LocalConnectionLocation mFileLocation;
|
||||
LocalConnection::Location mFileLocation;
|
||||
};
|
||||
|
||||
struct Arg {
|
||||
|
|
@ -81,7 +81,7 @@ namespace tp {
|
|||
[[nodiscard]] alnf getFloat(const String& id) const;
|
||||
[[nodiscard]] bool getBool(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;
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
using namespace tp;
|
||||
|
||||
bool LocalConnectionLocation::exists() const {
|
||||
bool LocalConnection::Location::exists() const {
|
||||
FILE* file = fopen(mLocation.read(), "r");
|
||||
if (file) {
|
||||
// File exists, close it and return 1
|
||||
|
|
@ -16,18 +16,18 @@ bool LocalConnectionLocation::exists() const {
|
|||
return false;
|
||||
}
|
||||
|
||||
bool LocalConnection::connect(const LocalConnectionLocation& location, const LocalConnectionType& connectionInfo) {
|
||||
bool LocalConnection::connect(const Location& location, const Type& connectionInfo) {
|
||||
DEBUG_ASSERT(!mStatus.isOpened());
|
||||
if (mStatus.isOpened()) return false;
|
||||
|
||||
auto handle = new LocalConnectionContext();
|
||||
|
||||
switch (connectionInfo.getType()) {
|
||||
case LocalConnectionType::READ:
|
||||
case Type::READ:
|
||||
handle->open(location.getLocation().read(), false);
|
||||
break;
|
||||
|
||||
case LocalConnectionType::WRITE:
|
||||
case Type::WRITE:
|
||||
handle->open(location.getLocation().read(), true);
|
||||
break;
|
||||
|
||||
|
|
@ -36,12 +36,12 @@ bool LocalConnection::connect(const LocalConnectionLocation& location, const Loc
|
|||
};
|
||||
|
||||
if (!handle->isOpen()) {
|
||||
mStatus.setStatus(LocalConnectionStatus::DENIED);
|
||||
mStatus.setStatus(Status::DENIED);
|
||||
delete handle;
|
||||
return false;
|
||||
}
|
||||
|
||||
mStatus.setStatus(LocalConnectionStatus::OPENED);
|
||||
mStatus.setStatus(Status::OPENED);
|
||||
mHandle = handle;
|
||||
mConnectionType = connectionInfo;
|
||||
return true;
|
||||
|
|
@ -52,7 +52,7 @@ bool LocalConnection::disconnect() {
|
|||
if (!mStatus.isOpened() || !mHandle) return false;
|
||||
mHandle->close();
|
||||
delete mHandle;
|
||||
mStatus.setStatus(LocalConnectionStatus::CLOSED);
|
||||
mStatus.setStatus(Status::CLOSED);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,4 +4,62 @@
|
|||
|
||||
namespace tp {
|
||||
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"
|
||||
|
||||
namespace tp {
|
||||
|
||||
class LocalConnectionContext;
|
||||
|
||||
class LocalConnectionLocation {
|
||||
class LocalConnection : public Connection {
|
||||
public:
|
||||
class Location {
|
||||
String mLocation;
|
||||
public:
|
||||
LocalConnectionLocation() : mLocation("tmp") {};
|
||||
explicit LocalConnectionLocation(const String& location) : mLocation(location) {}
|
||||
Location() : mLocation("tmp") {};
|
||||
explicit Location(const String& location) : mLocation(location) {}
|
||||
void setLocation(const String& location) { mLocation = location; }
|
||||
[[nodiscard]] const String& getLocation() const { return mLocation; }
|
||||
[[nodiscard]] bool exists() const;
|
||||
};
|
||||
|
||||
class LocalConnectionType {
|
||||
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:
|
||||
LocalConnection() {
|
||||
MODULE_SANITY_CHECK(gModuleConnection)
|
||||
|
|
@ -80,13 +27,13 @@ namespace tp {
|
|||
}
|
||||
|
||||
public:
|
||||
virtual bool connect(const LocalConnectionLocation& location, const LocalConnectionType& connectionInfo);
|
||||
virtual bool connect(const Location& location, const Type& connectionInfo);
|
||||
virtual bool disconnect();
|
||||
|
||||
public:
|
||||
virtual const LocalConnectionStatus& getConnectionStatus() { return mStatus; }
|
||||
virtual const LocalConnectionType& getConnectionType() { return mConnectionType; }
|
||||
virtual const LocalConnectionLocation& getLocation() { return mLocation; }
|
||||
virtual const Status& getConnectionStatus() { return mStatus; }
|
||||
virtual const Type& getConnectionType() { return mConnectionType; }
|
||||
virtual const Location& getLocation() { return mLocation; }
|
||||
|
||||
public:
|
||||
virtual bool setPointer(BytePointer pointer);
|
||||
|
|
@ -95,5 +42,10 @@ namespace tp {
|
|||
|
||||
public:
|
||||
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;
|
||||
file.connect(LocalConnectionLocation(String("tmp2.txt")), LocalConnectionType(false));
|
||||
file.connect(LocalConnection::Location(String("tmp2.txt")), LocalConnection::Type(false));
|
||||
file.writeBytes(data, 6);
|
||||
file.disconnect();
|
||||
}
|
||||
|
||||
{
|
||||
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.disconnect();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue