From 3b7ce4b9ceed9672ab444403a5bca8f0e38cfbeb Mon Sep 17 00:00:00 2001 From: Deon George Date: Sat, 2 Nov 2024 23:30:42 +1100 Subject: [PATCH] Optimise the setting of our configuration via Setup::class, optimise the calculation of our_addresses() --- app/Classes/FTN/Message.php | 6 +-- app/Classes/Protocol.php | 12 +++--- app/Classes/Protocol/Binkp.php | 2 +- app/Classes/Protocol/EMSI.php | 2 +- app/Console/Commands/ServerStart.php | 8 +++- app/Http/Middleware/AddUserToView.php | 5 ++- app/Mail/TestEmail.php | 58 +++++++++++++-------------- app/Models/Setup.php | 13 ++++-- app/helpers.php | 32 +++++++-------- 9 files changed, 72 insertions(+), 66 deletions(-) diff --git a/app/Classes/FTN/Message.php b/app/Classes/FTN/Message.php index 1569738..96858fe 100644 --- a/app/Classes/FTN/Message.php +++ b/app/Classes/FTN/Message.php @@ -504,8 +504,6 @@ class Message extends FTNBase */ public function __toString(): string { - $s = Setup::findOrFail(config('app.id')); - $return = pack(collect(self::HEADER)->pluck(1)->join(''), $this->mo->fftn->node_id, // Originating Node $this->mo->tftn->node_id, // Destination Node @@ -529,7 +527,7 @@ class Message extends FTNBase $this->mo->kludges->put('TOPT',$this->mo->tftn->point_id); } - $this->mo->kludges->put($this->mo->isFlagSet(self::FLAG_LOCAL) ? 'PID:' : 'TID:',sprintf('%s %s',Setup::PRODUCT_NAME_SHORT,$s->version)); + $this->mo->kludges->put($this->mo->isFlagSet(self::FLAG_LOCAL) ? 'PID:' : 'TID:',sprintf('%s %s',Setup::PRODUCT_NAME_SHORT,Setup::version())); $this->mo->kludges->put('DBID:',$this->mo->id); if ($this->mo instanceof Echomail) @@ -561,7 +559,7 @@ class Message extends FTNBase $return .= sprintf("\x01Via %s @%s.UTC %s %s\r", $this->us->ftn3d, Carbon::now()->format('Ymd.His'), - Setup::PRODUCT_NAME_SHORT,$s->version); + Setup::PRODUCT_NAME_SHORT,Setup::version()); } else { // FTS-0004.001/FSC-0068.001 The message SEEN-BY lines diff --git a/app/Classes/Protocol.php b/app/Classes/Protocol.php index 46a2637..1c5fe8b 100644 --- a/app/Classes/Protocol.php +++ b/app/Classes/Protocol.php @@ -3,6 +3,7 @@ namespace App\Classes; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Log; use App\Classes\File\{Receive,Send}; @@ -107,7 +108,7 @@ abstract class Protocol public const TCP_SPEED = 115200; protected SocketClient $client; /* Our socket details */ - protected ?Setup $setup; /* Our setup */ + protected Setup $setup; /* Our setup */ protected Node $node; /* The node we are communicating with */ /** The list of files we are sending */ protected Send $send; @@ -134,12 +135,9 @@ abstract class Protocol abstract protected function protocol_session(bool $force_queue=FALSE): int; - public function __construct(Setup $o=NULL) + public function __construct() { - if ($o && ! $o->system->akas->count()) - throw new \Exception('We dont have any ACTIVE FTN addresses assigned'); - - $this->setup = $o; + $this->setup = Config::get('setup'); } /** @@ -318,7 +316,7 @@ abstract class Protocol Log::debug(sprintf('%s:- Presenting limited AKAs [%s]',self::LOGKEY,$addresses->pluck('ftn')->join(','))); } else { - $addresses = $this->setup->system->akas; + $addresses = our_address(); Log::debug(sprintf('%s:- Presenting ALL our AKAs [%s]',self::LOGKEY,$addresses->pluck('ftn')->join(','))); } diff --git a/app/Classes/Protocol/Binkp.php b/app/Classes/Protocol/Binkp.php index ed554dd..8605fca 100644 --- a/app/Classes/Protocol/Binkp.php +++ b/app/Classes/Protocol/Binkp.php @@ -195,7 +195,7 @@ final class Binkp extends BaseProtocol $this->msgs(self::BPM_NUL,sprintf('NDL %d,TCP,BINKP',$this->client->speed)); $this->msgs(self::BPM_NUL,sprintf('TIME %s',Carbon::now()->toRfc2822String())); $this->msgs(self::BPM_NUL, - sprintf('VER %s-%s %s/%s',Setup::PRODUCT_NAME_SHORT,$this->setup->version,self::PROT,self::VERSION)); + sprintf('VER %s-%s %s/%s',Setup::PRODUCT_NAME_SHORT,Setup::version(),self::PROT,self::VERSION)); if ($this->originate) { $opt = $this->capGet(self::F_NOREL,self::O_WANT) ? ' NR' : ''; diff --git a/app/Classes/Protocol/EMSI.php b/app/Classes/Protocol/EMSI.php index 7d08f63..c80292e 100644 --- a/app/Classes/Protocol/EMSI.php +++ b/app/Classes/Protocol/EMSI.php @@ -208,7 +208,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface $makedata .= sprintf('{%s}{%s}{%s}{%s}', Setup::product_id(), Setup::PRODUCT_NAME_SHORT, - $this->setup->version, + Setup::version(), '#000000' // Serial Numbers ); diff --git a/app/Console/Commands/ServerStart.php b/app/Console/Commands/ServerStart.php index cb1b9a9..303f1ff 100644 --- a/app/Console/Commands/ServerStart.php +++ b/app/Console/Commands/ServerStart.php @@ -3,12 +3,12 @@ namespace App\Console\Commands; use Illuminate\Console\Command; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Log; use App\Classes\Protocol\{Binkp,DNS,EMSI}; use App\Classes\Sock\Exception\SocketException; use App\Classes\Sock\SocketServer; -use App\Models\Setup; class ServerStart extends Command { @@ -37,7 +37,11 @@ class ServerStart extends Command public function handle(): int { Log::info(sprintf('%s:+ Server Starting (%d)',self::LOGKEY,getmypid())); - $o = Setup::findOrFail(config('app.id')); + + if (! our_address()->count()) + throw new \Exception('We dont have any ACTIVE FTN addresses assigned'); + + $o = Config::get('setup'); $start = collect(); diff --git a/app/Http/Middleware/AddUserToView.php b/app/Http/Middleware/AddUserToView.php index b06f0b9..8a085f1 100644 --- a/app/Http/Middleware/AddUserToView.php +++ b/app/Http/Middleware/AddUserToView.php @@ -5,6 +5,7 @@ namespace App\Http\Middleware; use Closure; use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\Auth\Authenticatable; +use Illuminate\Support\Facades\Config; use App\Models\Setup; @@ -45,8 +46,10 @@ class AddUserToView */ public function handle($request, Closure $next) { + Config::set('setup',$x=Setup::find(config('app.id'))->load('system')); + $this->factory->share('user',$this->user); - $this->factory->share('setup',Setup::find(config('app.id'))); + $this->factory->share('setup',$x); return $next($request); } diff --git a/app/Mail/TestEmail.php b/app/Mail/TestEmail.php index d2f01e3..ddbf498 100644 --- a/app/Mail/TestEmail.php +++ b/app/Mail/TestEmail.php @@ -12,37 +12,37 @@ use App\Models\{Setup,User}; class TestEmail extends Mailable { - use Queueable, SerializesModels; + use Queueable, SerializesModels; - /* User to send mail to */ - public User $user; - /* Our setup object */ - public Setup $setup; + /* User to send mail to */ + public User $user; + /* Our setup object */ + public Setup $setup; - /** - * Create a new message instance. - * - * @return void - */ - public function __construct(User $o) - { - $this->user = $o; - } + /** + * Create a new message instance. + * + * @return void + */ + public function __construct(User $o) + { + $this->user = $o; + } - /** - * Build the message. - * - * @return $this - */ - public function build() - { - $this->setup = Setup::findOrFail(config('app.id')); + /** + * Build the message. + * + * @return $this + */ + public function build() + { + $this->setup = Setup::findOrFail(config('app.id')); - return $this - ->markdown('mail.system.test_email') - ->subject('Just a test...') - ->with([ - 'url'=>'https://localhost', + return $this + ->markdown('mail.system.test_email') + ->subject('Just a test...') + ->with([ + 'url'=>sprintf('https://%s',gethostname()), ]); - } -} + } +} \ No newline at end of file diff --git a/app/Models/Setup.php b/app/Models/Setup.php index 508106b..413438e 100644 --- a/app/Models/Setup.php +++ b/app/Models/Setup.php @@ -71,9 +71,6 @@ class Setup extends Model case 'msgs_pkt': return Arr::get($this->options,$key,self::MAX_MSGS_PKT); - case 'version': - return File::exists('VERSION') ? chop(File::get('VERSION')) : 'dev'; - default: return parent::__get($key); } @@ -113,6 +110,16 @@ class Setup extends Model return hexstr($c); } + /** + * Application version + * + * @return string + */ + public static function version(): string + { + return File::exists('VERSION') ? chop(File::get('VERSION')) : 'dev'; + } + /* RELATIONS */ /** diff --git a/app/helpers.php b/app/helpers.php index 76fcfc7..948b8ca 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -2,7 +2,7 @@ use Carbon\Carbon; use Illuminate\Support\Collection; -use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Config; use App\Models\{Address,Domain,Setup}; @@ -93,33 +93,29 @@ if (! function_exists('hexstr')) { */ function our_address(Domain|Address $o=NULL): Collection|Address|NULL { - $so = Cache::remember('so',5,function() { - return Setup::findOrFail(config('app.id')); - }); + if (! Config::has('setup')) + Config::set('setup',Setup::findOrFail(config('app.id'))); - $our = Cache::remember(sprintf('%d-akas',$so->system_id),60,function() use ($so) { - $so->load([ - 'system:id,name', - 'system.akas:addresses.id,addresses.zone_id,region_id,host_id,node_id,point_id,addresses.system_id,addresses.active,role', - 'system.akas.zone:id,domain_id,zone_id', - 'system.akas.zone.domain:id,name', - ]); - - return $so->system->akas; - }); + $so = Config::get('setup'); + $so->loadMissing([ + 'system:id,name', + 'system.akas:addresses.id,addresses.zone_id,region_id,host_id,node_id,point_id,addresses.system_id,addresses.active,role', + 'system.akas.zone:id,domain_id,zone_id', + 'system.akas.zone.domain:id,name', + ]); // If we dont have any addresses - if ($our->count() === 0) + if ($so->system->akas->count() === 0) return NULL; // We havent asked for an address/domain, so we'll return them all. if (is_null($o)) - return $our; + return $so->system->akas; // We are requesting a list of addresses for a Domain, or a specific Address, and we have more than 1 switch (get_class($o)) { case Address::class: - $filter = $our + $filter = $so->system->akas ->filter(fn($item)=>$item->zone->domain_id === $o->zone->domain_id) ->sortBy('role_id'); @@ -130,7 +126,7 @@ function our_address(Domain|Address $o=NULL): Collection|Address|NULL return $filter->count() ? $filter->last()->unsetRelation('nodes_hub') : NULL; case Domain::class: - return $our + return $so->system->akas ->filter(fn($item)=>$item->zone->domain_id === $o->id) ->sortBy('role_id');