Fixes to message processing, now that we are using cockroachdb

This commit is contained in:
Deon George
2022-01-15 13:06:15 +11:00
parent e78e79a8f5
commit 6f1d47a6ab
8 changed files with 119 additions and 82 deletions

View File

@@ -11,7 +11,7 @@ use Illuminate\Validation\Validator as ValidatorResult;
use App\Classes\FTN as FTNBase;
use App\Models\{Address,Domain,Zone};
use App\Rules\TwoByteInteger;
use App\Rules\{TwoByteInteger,TwoByteIntegerWithZero};
use App\Traits\EncodeUTF8;
/**
@@ -117,7 +117,7 @@ class Message extends FTNBase
private Collection $rogue_path; // Collection of FTNs in the Seen-by that are not defined
private Collection $seenby; // FTS-0004.001 The message SEEN-BY lines
private Collection $seenaddress; // Collection of Addresses after parsing seenby
private Collection $rogue_seen; // Collection of FTNs in the Seen-by that are not defined
private Collection $rogue_seenby; // Collection of FTNs in the Seen-by that are not defined
private Collection $via; // The path the message has gone using Via lines (Netmail)
private Collection $unknown; // Temporarily hold attributes we have no logic for.
@@ -203,7 +203,7 @@ class Message extends FTNBase
$this->seenby = collect();
$this->seenaddress = collect();
$this->pathaddress = collect();
$this->rogue_seen = collect();
$this->rogue_seenby = collect();
$this->rogue_path = collect();
$this->via = collect();
$this->unknown = collect();
@@ -262,7 +262,7 @@ class Message extends FTNBase
$o->unpackMessage(substr($msg,self::HEADER_LEN+$ptr));
if (($x=$o->validate())->fails()) {
Log::debug('Message fails validation',['result'=>$x->errors()]);
Log::debug(sprintf('%s:Message fails validation (%s@%s->%s@%s)',self::LOGKEY,$o->user_from,$o->fftn,$o->user_to,$o->tftn),['result'=>$x->errors()]);
//throw new \Exception('Message validation fails:'.join(' ',$x->errors()->all()));
}
@@ -388,7 +388,7 @@ class Message extends FTNBase
case 'pathaddress':
case 'seenaddress':
case 'rogue_path':
case 'rogue_seen':
case 'rogue_seenby':
case 'unknown':
case 'via':
@@ -842,7 +842,7 @@ class Message extends FTNBase
}
// Parse SEEN-BY
if ($this->seenby->count())
$this->seenaddress = $this->parseAddresses('seenby',$this->seenby,$this->rogue_seen);
$this->seenaddress = $this->parseAddresses('seenby',$this->seenby,$this->rogue_seenby);
// Parse PATH
if ($this->path->count())
@@ -861,10 +861,10 @@ class Message extends FTNBase
'user_from' => $this->user_from,
'user_to' => $this->user_to,
'subject' => $this->subject,
'onode' => $this->fn,
'dnode' => $this->ff,
'onet' => $this->tn,
'dnet' => $this->tf,
'onode' => $this->ff,
'dnode' => $this->tf,
'onet' => $this->fn,
'dnet' => $this->tn,
'flags' => $this->flags,
'cost' => $this->cost,
'echoarea' => $this->echoarea,
@@ -874,8 +874,8 @@ class Message extends FTNBase
'user_from' => 'required|min:1|max:'.self::USER_FROM_LEN,
'user_to' => 'required|min:1|max:'.self::USER_TO_LEN,
'subject' => 'present|max:'.self::SUBJECT_LEN,
'onode' => ['required',new TwoByteInteger],
'dnode' => ['required',new TwoByteInteger],
'onode' => ['required',new TwoByteIntegerWithZero],
'dnode' => ['required',new TwoByteIntegerWithZero],
'onet' => ['required',new TwoByteInteger],
'dnet' => ['required',new TwoByteInteger],
'flags' => 'required|numeric',

View File

@@ -5,8 +5,8 @@ namespace App\Classes\FTN;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Symfony\Component\HttpFoundation\File\File;
use App\Classes\FTN as FTNBase;
@@ -59,8 +59,7 @@ class Packet extends FTNBase implements \Iterator, \Countable
public Collection $messages; // Messages in the Packet
public Collection $errors; // Messages that fail validation
private string $name; // Packet name
private ?System $system; // System the packet is from
public bool $use_redis = TRUE; // Use redis for messages.
public bool $use_cache = TRUE; // Use a cache for messages.
private int $index; // Our array index
/**
@@ -71,14 +70,14 @@ class Packet extends FTNBase implements \Iterator, \Countable
return $this->messages->count();
}
public function current()
public function current(): Message
{
return $this->use_redis ? unserialize(Redis::get($this->key())) : $this->messages->get($this->index);
return $this->use_cache ? unserialize(Cache::pull($this->key())) : $this->messages->get($this->index);
}
public function key()
public function key(): mixed
{
return $this->messages->get($this->index);
return $this->use_cache ? $this->messages->get($this->index) : $this->index;
}
public function next(): void
@@ -86,14 +85,14 @@ class Packet extends FTNBase implements \Iterator, \Countable
$this->index++;
}
public function rewind()
public function rewind(): void
{
$this->index = 0;
}
public function valid()
public function valid(): bool
{
return $this->use_redis ? ($this->key() && Redis::exists($this->key())) : $this->key();
return (! is_null($this->key())) && ($this->use_cache ? Cache::has($this->key()) : $this->messages->has($this->key()));
}
public function __construct(Address $o=NULL)
@@ -113,11 +112,11 @@ class Packet extends FTNBase implements \Iterator, \Countable
*
* @param File $file
* @param System|null $system
* @param bool $use_redis
* @param bool $use_cache
* @return Packet
* @throws InvalidPacketException
*/
public static function open(File $file,System $system=NULL,bool $use_redis=TRUE): self
public static function open(File $file,System $system=NULL,bool $use_cache=TRUE): self
{
Log::debug(sprintf('%s:+ Opening Packet [%s]',self::LOGKEY,$file));
@@ -137,7 +136,7 @@ class Packet extends FTNBase implements \Iterator, \Countable
throw new InvalidPacketException('Not a type 2 packet: '.$version);
$o = new self;
$o->use_redis = $use_redis;
$o->use_cache = $use_cache;
$o->name = (string)$file;
$o->header = unpack(self::unpackheader(self::v2header),$header);
@@ -428,7 +427,7 @@ class Packet extends FTNBase implements \Iterator, \Countable
// If the message is invalid, we'll ignore it
if ($msg->errors) {
Log::info(sprintf('%s:- Message has errors',self::LOGKEY));
Log::info(sprintf('%s:- Message [%s] has errors',self::LOGKEY,$msg->msgid));
// If the from address doenst exist, we'll create a new entry
if ($msg->errors->messages()->has('from')) {
@@ -479,9 +478,9 @@ class Packet extends FTNBase implements \Iterator, \Countable
}
}
if ($this->use_redis) {
$key = $msg->msgid ?: sprintf('%s %s',$msg->fftn,Carbon::now()->timestamp);
Redis::set($key,serialize($msg));
if ($this->use_cache) {
$key = urlencode($msg->msgid ?: sprintf('%s %s',$msg->fftn,Carbon::now()->timestamp));
Cache::forever($key,serialize($msg));
$this->messages->push($key);
} else {