From f0f2d74a14da5f8c81781d6163f53cad4a652ea9 Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 8 Nov 2024 23:13:04 +1100 Subject: [PATCH] Reduce the need for Mailer::class in protocols --- app/Classes/Protocol.php | 56 ++++++++++++++++++++------------- app/Classes/Protocol/Binkp.php | 4 +-- app/Classes/Protocol/EMSI.php | 4 +-- app/Classes/Protocol/Zmodem.php | 4 +-- app/Jobs/AddressPoll.php | 4 +-- 5 files changed, 42 insertions(+), 30 deletions(-) diff --git a/app/Classes/Protocol.php b/app/Classes/Protocol.php index 6136c63..f579297 100644 --- a/app/Classes/Protocol.php +++ b/app/Classes/Protocol.php @@ -7,7 +7,7 @@ use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Log; use App\Classes\File\{Receive,Send}; -use App\Classes\Protocol\EMSI; +use App\Classes\Protocol\{Binkp,DNS,EMSI,Zmodem}; use App\Classes\Sock\Exception\SocketException; use App\Classes\Sock\SocketClient; use App\Models\{Address,Mailer,Setup,System,SystemLog}; @@ -98,10 +98,10 @@ abstract class Protocol // File transfer status - public const FOP_OK = 0; - public const FOP_CONT = 1; - public const FOP_SKIP = 2; - public const FOP_ERROR = 3; + public const FOP_OK = 0; + public const FOP_CONT = 1; + public const FOP_SKIP = 2; + public const FOP_ERROR = 3; public const FOP_SUSPEND = 4; public const FOP_GOT = 5; @@ -123,10 +123,13 @@ abstract class Protocol protected array $capability; // @todo make private /** @var bool Are we originating a connection */ protected bool $originate; - /** Our comms details */ + /** @var bool Is the application down for maintenance */ protected bool $down = FALSE; + /** @var int Our mailer ID for logging purposes */ + private int $mailer_id; + private array $comms; protected bool $force_queue = FALSE; @@ -138,6 +141,24 @@ abstract class Protocol public function __construct() { $this->setup = Config::get('setup',Setup::findOrFail(config('app.id'))); + + // Some initialisation details + switch (get_class($this)) { + case Binkp::class: + $this->mailer_id = Mailer::where('name','BINKP')->sole()->id; + break; + + case DNS::class: + case Zmodem::class: + break; + + case EMSI::class: + $this->mailer_id = Mailer::where('name','EMSI')->sole()->id; + break; + + default: + throw new \Exception('not handled'.get_class($this)); + } } /** @@ -302,6 +323,7 @@ abstract class Protocol * Our addresses to send to the remote * * @return Collection + * @throws \Exception */ protected function our_addresses(): Collection { @@ -327,13 +349,12 @@ abstract class Protocol /** * Initialise our Session * - * @param Mailer $mo * @param SocketClient $client * @param Address|null $o * @return int * @throws \Exception */ - public function session(Mailer $mo,SocketClient $client,Address $o=NULL): int + public function session(SocketClient $client,Address $o=NULL): int { if ($o->exists) Log::withContext(['ftn'=>$o->ftn]); @@ -370,8 +391,8 @@ abstract class Protocol app()->isDownForMaintenance(); $this->down = app()->isDownForMaintenance(); - switch ($mo->name) { - case 'EMSI': + switch (get_class($this)) { + case EMSI::class: Log::debug(sprintf('%s:- Starting EMSI',self::LOGKEY)); $rc = $this->protocol_init(); @@ -385,7 +406,7 @@ abstract class Protocol break; - case 'BINKP': + case Binkp::class: Log::debug(sprintf('%s:- Starting BINKP',self::LOGKEY)); $rc = $this->protocol_session($this->originate); @@ -393,13 +414,11 @@ abstract class Protocol break; default: - Log::error(sprintf('%s:! Unsupported session type [%d]',self::LOGKEY,$mo->id)); + Log::error(sprintf('%s:! Unsupported session type [%s]',self::LOGKEY,get_class($this))); return self::S_FAILURE; } - // @todo Unlock outbounds - // @todo These flags determine when we connect to the remote. // If the remote indicated that they dont support file requests (NRQ) or temporarily hold them (HRQ) if (($this->node->optionGet(self::O_NRQ) && (! $this->setup->optionGet(EMSI::F_IGNORE_NRQ,'emsi_options'))) || $this->node->optionGet(self::O_HRQ)) @@ -430,6 +449,7 @@ abstract class Protocol if ($so && $so->exists) { foreach ($this->node->aka_other as $aka) // @todo For disabled zones, we shouldnt refuse to record the address + // @todo If the system hasnt presented an address for a configured period (eg: 30 days) assume it no longer carries it if ((! Address::findFTN($aka)) && ($oo=Address::createFTN($aka,$so))) { $oo->validated = TRUE; $oo->save(); @@ -441,7 +461,7 @@ abstract class Protocol $slo->items_sent_size = $this->send->total_sent_bytes; $slo->items_recv = $this->recv->total_recv; $slo->items_recv_size = $this->recv->total_recv_bytes; - $slo->mailer_id = $mo->id; + $slo->mailer_id = $this->mailer_id; $slo->sessiontime = $this->node->session_time; $slo->result = ($rc & self::S_MASK); $slo->originate = $this->originate; @@ -455,12 +475,6 @@ abstract class Protocol } } - // @todo Optional after session execution event - // if ($this->node->start_time && $this->setup->cfg('CFG_AFTERSESSION')) {} - - // @todo Optional after session includes mail event - // if ($this->node->start_time && $this->setup->cfg('CFG_AFTERMAIL')) {} - return $rc; } diff --git a/app/Classes/Protocol/Binkp.php b/app/Classes/Protocol/Binkp.php index 9de93ad..8c43d25 100644 --- a/app/Classes/Protocol/Binkp.php +++ b/app/Classes/Protocol/Binkp.php @@ -14,7 +14,7 @@ use App\Classes\Protocol as BaseProtocol; use App\Classes\Sock\Exception\SocketException; use App\Classes\Sock\SocketClient; use App\Exceptions\{FileGrewException,InvalidFTNException}; -use App\Models\{Address,Mailer,Setup}; +use App\Models\{Address,Setup}; final class Binkp extends BaseProtocol { @@ -155,7 +155,7 @@ final class Binkp extends BaseProtocol if (! parent::onConnect($client)) { Log::withContext(['pid'=>getmypid()]); - $this->session(Mailer::where('name','BINKP')->singleOrFail(),$client,(new Address)); + $this->session($client,(new Address)); $this->client->close(); exit(0); } diff --git a/app/Classes/Protocol/EMSI.php b/app/Classes/Protocol/EMSI.php index f2ca62e..136382e 100644 --- a/app/Classes/Protocol/EMSI.php +++ b/app/Classes/Protocol/EMSI.php @@ -11,7 +11,7 @@ use App\Classes\Sock\SocketClient; use App\Exceptions\InvalidFTNException; use App\Interfaces\CRC as CRCInterface; use App\Interfaces\Zmodem as ZmodemInterface; -use App\Models\{Address,Mailer,Setup}; +use App\Models\{Address,Setup}; use App\Traits\CRC as CRCTrait; // http://ftsc.org/docs/fsc-0056.001 @@ -95,7 +95,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface if (! parent::onConnect($client)) { Log::withContext(['pid'=>getmypid()]); - $this->session(Mailer::where('name','EMSI')->singleOrFail(),$client,(new Address)); + $this->session($client,(new Address)); $this->client->close(); exit(0); } diff --git a/app/Classes/Protocol/Zmodem.php b/app/Classes/Protocol/Zmodem.php index fb2bc91..b84a3fd 100644 --- a/app/Classes/Protocol/Zmodem.php +++ b/app/Classes/Protocol/Zmodem.php @@ -10,7 +10,7 @@ use App\Classes\Sock\Exception\SocketException; use App\Classes\Sock\SocketClient; use App\Interfaces\CRC as CRCInterface; use App\Interfaces\Zmodem as ZmodemInterface; -use App\Models\{Address,Mailer}; +use App\Models\Address; use App\Traits\CRC as CRCTrait; /** @@ -213,7 +213,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface if (! parent::onConnect($client)) { Log::withContext(['pid'=>getmypid()]); - $this->session(Mailer::where('name','ZMODEM')->singleOrFail(),$client); + $this->session($client); $this->client->close(); Log::info(sprintf('%s:= onConnect - Connection closed [%s]',self::LOGKEY,$client->address_remote)); diff --git a/app/Jobs/AddressPoll.php b/app/Jobs/AddressPoll.php index 1ae9b94..0744dca 100644 --- a/app/Jobs/AddressPoll.php +++ b/app/Jobs/AddressPoll.php @@ -94,13 +94,11 @@ class AddressPoll implements ShouldQueue, ShouldBeUnique switch ($o->name) { case 'BINKP': $s = new Binkp; - $mo = Mailer::where('name','BINKP')->singleOrFail(); break; case 'EMSI': $s = new EMSI; - $mo = Mailer::where('name','EMSI')->singleOrFail(); break; @@ -116,7 +114,7 @@ class AddressPoll implements ShouldQueue, ShouldBeUnique try { $client = SocketClient::create($this->ao->system->address,$o->pivot->port); - if (($s->session($mo,$client,$this->ao) & Protocol::S_MASK) === Protocol::S_OK) { + if (($s->session($client,$this->ao) & Protocol::S_MASK) === Protocol::S_OK) { Log::info(sprintf('%s:= Connection ended successfully with [%s] (%s)',self::LOGKEY,$client->address_remote,$this->ao->ftn)); return;