Browse Source

Change Avarice to work with Acedia's multi-config

pull/8/head
Anton Tarasenko 3 years ago
parent
commit
84f90e13bc
  1. 155
      sources/Avarice/Avarice.uc
  2. 5
      sources/Avarice/AvariceAPI.uc
  3. 2
      sources/Avarice/AvariceLink.uc
  4. BIN
      sources/Avarice/AvariceTcpStream.uc
  5. 206
      sources/Avarice/Avarice_Feature.uc
  6. 2
      sources/Manifest.uc

155
sources/Avarice/Avarice.uc

@ -1,24 +1,5 @@
/** /**
* This feature makes it possible to use TCP connection to exchange * Config object for `Avarice_Feature`.
* messages (represented by JSON objects) with external applications.
* There are some peculiarities to UnrealEngine's `TCPLink`, so to simplify
* communication process for external applications, they are expected to
* connect to the server through the "Avarice" utility that can accept a stream
* of utf8-encoded JSON messageand feed them to our `TCPLink` (we use child
* class `AvariceTcpStream`) in a way it can receive them.
* Every message sent to us must have the following structure:
* { "s": "<service_name>", "t": "<command_type>", "p": <any_json_value> }
* where
* * <service_name> describes a particular source of messages
* (it can be a name of the database or an alias for
* a connected application);
* * <command_type> simply states the name of a command, for a database it
* can be "get", "set", "delete", etc..
* * <any_json_value> can be an arbitrary json value and can be used to
* pass any additional information along with the message.
* Acedia provides a special treatment for any messages that have their
* service set to "echo" - it always returns them back as-is, except for the
* message type that gets set to "end".
* Copyright 2021 Anton Tarasenko * Copyright 2021 Anton Tarasenko
*------------------------------------------------------------------------------ *------------------------------------------------------------------------------
* This file is part of Acedia. * This file is part of Acedia.
@ -36,13 +17,10 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with Acedia. If not, see <https://www.gnu.org/licenses/>. * along with Acedia. If not, see <https://www.gnu.org/licenses/>.
*/ */
class Avarice extends Feature class Avarice extends FeatureConfig
perobjectconfig
config(AcediaAvarice); config(AcediaAvarice);
// The feature itself is dead simple - it simply creates list of
// `AvariceLink` objects, according to its settings, and stores them
var private array<AvariceLink> createdLinks;
struct AvariceLinkRecord struct AvariceLinkRecord
{ {
var string name; var string name;
@ -65,108 +43,71 @@ var private config array<AvariceLinkRecord> link;
// connection too long. // connection too long.
var private config float reconnectTime; var private config float reconnectTime;
var private const int TECHO, TEND, TCOLON; protected function AssociativeArray ToData()
var private LoggerAPI.Definition errorBadAddress;
protected function OnEnabled()
{ {
local int i; local int i;
local Text name; local AssociativeArray data;
local MutableText host; local AssociativeArray linkData;
local int port; local DynamicArray linksArray;
local AvariceLink nextLink; data = __().collections.EmptyAssociativeArray();
data.SetFloat(P("reconnectTime"), reconnectTime, true);
linksArray = __().collections.EmptyDynamicArray();
data.SetItem(P("link"), linksArray);
for (i = 0; i < link.length; i += 1) for (i = 0; i < link.length; i += 1)
{ {
name = _.text.FromString(link[i].name); linkData = __().collections.EmptyAssociativeArray();
if (ParseAddress(link[i].address, host, port)) linkData.SetItem(P("name"), __().text.FromString(link[i].name));
{ linkData.SetItem(P("address"), __().text.FromString(link[i].address));
nextLink = AvariceLink(_.memory.Allocate(class'AvariceLink')); linksArray.AddItem(linkData);
nextLink.Initialize(name, host, port);
nextLink.StartUp();
nextLink.OnMessage(self, T(TECHO)).connect = EchoHandler;
createdLinks[createdLinks.length] = nextLink;
}
else {
_.logger.Auto(errorBadAddress).Arg(_.text.FromString(link[i].name));
}
_.memory.Free(name);
_.memory.Free(host);
} }
return data;
} }
protected function OnDisabled() protected function FromData(AssociativeArray source)
{ {
_.memory.FreeMany(createdLinks); local int i;
local Text nextText;
local DynamicArray linksArray;
local AssociativeArray nextLink;
local AvariceLinkRecord nextRecord;
if (source == none) {
return;
} }
reconnectTime = source.GetFloat(P("reconnectTime"));
// Reply back any messages from "echo" service link.length = 0;
private function EchoHandler(AvariceLink link, AvariceMessage message) linksArray = source.GetDynamicArray(P("link"));
{ if (linksArray == none) {
link.SendMessage(T(TECHO), T(TEND), message.parameters); return;
} }
for (i = 0; i < linksArray.GetLength(); i += 1)
private final function bool ParseAddress(
string address,
out MutableText host,
out int port)
{ {
local bool success; nextLink = linksArray.GetAssociativeArray(i);
local Parser parser; if (nextLink == none) {
parser = _.text.ParseString(address); continue;
parser.Skip()
.MUntil(host, T(TCOLON).GetCharacter(0))
.Match(T(TCOLON))
.MUnsignedInteger(port)
.Skip();
success = parser.Ok() && parser.GetRemainingLength() == 0;
parser.FreeSelf();
return success;
} }
nextText = nextLink.GetText(P("name"));
/** if (nextText != none) {
* Method that returns all the `AvariceLink` created by this feature. nextRecord.name = nextText.ToPlainString();
*
* @return Array of links created by this feature.
* Guaranteed to not contain `none` values.
*/
public final function array<AvariceLink> GetAllLinks()
{
local int i;
while (i < createdLinks.length)
{
if (createdLinks[i] == none) {
createdLinks.Remove(i, 1);
} }
else { nextText = nextLink.GetText(P("address"));
i += 1; if (nextText != none) {
nextRecord.address = nextText.ToPlainString();
} }
link[i] = nextRecord;
} }
return createdLinks;
} }
/** protected function DefaultIt()
* Returns its current `reconnectTime` setting that describes amount of time
* between connection attempts.
*
* @return Value of `reconnectTime` config variable.
*/
public final static function float GetReconnectTime()
{ {
return default.reconnectTime; local AvariceLinkRecord defaultRecord;
reconnectTime = 10.0;
link.length = 0;
defaultRecord.name = "avarice";
defaultRecord.address = "127.0.0.1:1234";
link[0] = defaultRecord;
} }
defaultproperties defaultproperties
{ {
// Settings configName = "AcediaAvarice"
reconnectTime = 10.0
// `Text` constants
TECHO = 0
stringConstants(0) = "echo"
TEND = 1
stringConstants(1) = "end"
TCOLON = 2
stringConstants(2) = ":"
// Log messages
errorBadAddress = (l=LOG_Error,m="Cannot parse address \"%1\"")
} }

5
sources/Avarice/AvariceAPI.uc

@ -28,9 +28,10 @@ class AvariceAPI extends AcediaObject;
*/ */
public final function array<AvariceLink> GetAllLinks() public final function array<AvariceLink> GetAllLinks()
{ {
local Avarice avariceFeature; local Avarice_Feature avariceFeature;
local array<AvariceLink> emptyResult; local array<AvariceLink> emptyResult;
avariceFeature = Avarice(class'Avarice'.static.GetInstance()); avariceFeature =
Avarice_Feature(class'Avarice_Feature'.static.GetInstance());
if (avariceFeature != none) { if (avariceFeature != none) {
return avariceFeature.GetAllLinks(); return avariceFeature.GetAllLinks();
} }

2
sources/Avarice/AvariceLink.uc

@ -246,7 +246,7 @@ public final function StartUp()
return; return;
} }
tcpStream.Set(newStream); tcpStream.Set(newStream);
newStream.StartUp(self, class'Avarice'.static.GetReconnectTime()); newStream.StartUp(self, class'Avarice_Feature'.static.GetReconnectTime());
} }
/** /**

BIN
sources/Avarice/AvariceTcpStream.uc

Binary file not shown.

206
sources/Avarice/Avarice_Feature.uc

@ -0,0 +1,206 @@
/**
* This feature makes it possible to use TCP connection to exchange
* messages (represented by JSON objects) with external applications.
* There are some peculiarities to UnrealEngine's `TCPLink`, so to simplify
* communication process for external applications, they are expected to
* connect to the server through the "Avarice" utility that can accept a stream
* of utf8-encoded JSON messageand feed them to our `TCPLink` (we use child
* class `AvariceTcpStream`) in a way it can receive them.
* Every message sent to us must have the following structure:
* { "s": "<service_name>", "t": "<command_type>", "p": <any_json_value> }
* where
* * <service_name> describes a particular source of messages
* (it can be a name of the database or an alias for
* a connected application);
* * <command_type> simply states the name of a command, for a database it
* can be "get", "set", "delete", etc..
* * <any_json_value> can be an arbitrary json value and can be used to
* pass any additional information along with the message.
* Acedia provides a special treatment for any messages that have their
* service set to "echo" - it always returns them back as-is, except for the
* message type that gets set to "end".
* Copyright 2021 Anton Tarasenko
*------------------------------------------------------------------------------
* This file is part of Acedia.
*
* Acedia is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License, or
* (at your option) any later version.
*
* Acedia is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Acedia. If not, see <https://www.gnu.org/licenses/>.
*/
class Avarice_Feature extends Feature
dependson(Avarice);
var private /*config*/ array<Avarice.AvariceLinkRecord> link;
var private /*config*/ float reconnectTime;
// The feature itself is dead simple - it simply creates list of
// `AvariceLink` objects, according to its settings, and stores them
var private array<AvariceLink> createdLinks;
var private const int TECHO, TEND, TCOLON;
var private LoggerAPI.Definition errorBadAddress;
public static function StaticConstructor()
{
if (StaticConstructorGuard()) return;
super.StaticConstructor();
}
protected function OnEnabled()
{
local int i;
local Text name;
local MutableText host;
local int port;
local AvariceLink nextLink;
for (i = 0; i < link.length; i += 1)
{
name = _.text.FromString(link[i].name);
if (ParseAddress(link[i].address, host, port))
{
nextLink = AvariceLink(_.memory.Allocate(class'AvariceLink'));
nextLink.Initialize(name, host, port);
nextLink.StartUp();
nextLink.OnMessage(self, T(TECHO)).connect = EchoHandler;
createdLinks[createdLinks.length] = nextLink;
}
else
{
_.logger.Auto(errorBadAddress)
.Arg(_.text.FromString(link[i].address))
.Arg(_.text.FromString(link[i].name));
}
_.memory.Free(name);
_.memory.Free(host);
}
}
protected function OnDisabled()
{
_.memory.FreeMany(createdLinks);
}
protected function SwapConfig(
AssociativeArray previousConfigData,
AssociativeArray newConfigData)
{
local int i;
local Text nextText;
local DynamicArray linksArray;
local AssociativeArray nextLink;
local Avarice.AvariceLinkRecord nextRecord;
// Simply restart all the links
for (i = 0; i < createdLinks.length; i += 1)
{
if (createdLinks[i] != none) {
createdLinks[i].FreeSelf();
}
}
createdLinks.length = 0;
link.length = 0;
if (newConfigData == none) {
return;
}
reconnectTime = newConfigData.GetFloat(P("reconnectTime"));
default.reconnectTime = reconnectTime;
linksArray = newConfigData.GetDynamicArray(P("link"));
if (linksArray == none) {
return;
}
for (i = 0; i < linksArray.GetLength(); i += 1)
{
nextLink = linksArray.GetAssociativeArray(i);
if (nextLink == none) {
continue;
}
nextText = nextLink.GetText(P("name"));
if (nextText != none) {
nextRecord.name = nextText.ToPlainString();
}
nextText = nextLink.GetText(P("address"));
if (nextText != none) {
nextRecord.address = nextText.ToPlainString();
}
link[i] = nextRecord;
}
}
// Reply back any messages from "echo" service
private function EchoHandler(AvariceLink link, AvariceMessage message)
{
link.SendMessage(T(TECHO), T(TEND), message.parameters);
}
private final function bool ParseAddress(
string address,
out MutableText host,
out int port)
{
local bool success;
local Parser parser;
parser = _.text.ParseString(address);
parser.Skip()
.MUntil(host, T(TCOLON).GetCharacter(0))
.Match(T(TCOLON))
.MUnsignedInteger(port)
.Skip();
success = parser.Ok() && parser.GetRemainingLength() == 0;
parser.FreeSelf();
return success;
}
/**
* Method that returns all the `AvariceLink` created by this feature.
*
* @return Array of links created by this feature.
* Guaranteed to not contain `none` values.
*/
public final function array<AvariceLink> GetAllLinks()
{
local int i;
while (i < createdLinks.length)
{
if (createdLinks[i] == none) {
createdLinks.Remove(i, 1);
}
else {
i += 1;
}
}
return createdLinks;
}
/**
* Returns its current `reconnectTime` setting that describes amount of time
* between connection attempts.
*
* @return Value of `reconnectTime` config variable.
*/
public final static function float GetReconnectTime()
{
return default.reconnectTime;
}
defaultproperties
{
configClass = class'Avarice'
// `Text` constants
TECHO = 0
stringConstants(0) = "echo"
TEND = 1
stringConstants(1) = "end"
TCOLON = 2
stringConstants(2) = ":"
// Log messages
errorBadAddress = (l=LOG_Error,m="Cannot parse address \"%1\" for \"%2\"")
}

2
sources/Manifest.uc

@ -23,7 +23,7 @@
defaultproperties defaultproperties
{ {
features(0) = class'Commands_Feature' features(0) = class'Commands_Feature'
features(1) = class'Avarice' features(1) = class'Avarice_Feature'
commands(0) = class'ACommandHelp' commands(0) = class'ACommandHelp'
commands(1) = class'ACommandDosh' commands(1) = class'ACommandDosh'
commands(2) = class'ACommandNick' commands(2) = class'ACommandNick'

Loading…
Cancel
Save