From 0526500ff0598786d58fea687f06dd053a7e6f06 Mon Sep 17 00:00:00 2001 From: Deon George Date: Sun, 10 Dec 2023 19:07:53 +1100 Subject: [PATCH] Integrate Mailer::class into System_Log::class, removed Zmodem Server/Client --- app/Classes/Protocol.php | 39 ++++---------- app/Classes/Protocol/Binkp.php | 6 +-- app/Classes/Protocol/EMSI.php | 6 +-- app/Classes/Protocol/Zmodem.php | 4 +- app/Console/Commands/CommZmodemReceive.php | 51 ------------------- app/Console/Commands/CommZmodemSend.php | 45 ---------------- app/Jobs/AddressPoll.php | 6 +-- app/Models/SystemLog.php | 5 ++ .../2023_12_10_143252_fix_mailer_id.php | 46 +++++++++++++++++ .../views/system/widget/form-system.blade.php | 2 +- 10 files changed, 71 insertions(+), 139 deletions(-) delete mode 100644 app/Console/Commands/CommZmodemReceive.php delete mode 100644 app/Console/Commands/CommZmodemSend.php create mode 100644 database/migrations/2023_12_10_143252_fix_mailer_id.php diff --git a/app/Classes/Protocol.php b/app/Classes/Protocol.php index df99049..f683fc5 100644 --- a/app/Classes/Protocol.php +++ b/app/Classes/Protocol.php @@ -9,7 +9,7 @@ use App\Classes\File\{Receive,Send}; use App\Classes\Protocol\EMSI; use App\Classes\Sock\SocketClient; use App\Classes\Sock\SocketException; -use App\Models\{Address,Setup,System,SystemLog}; +use App\Models\{Address,Mailer,Setup,System,SystemLog}; abstract class Protocol { @@ -26,16 +26,6 @@ abstract class Protocol protected const RCDO = -3; protected const ERROR = -5; - // Our sessions Types - /** @deprecated use Mailer::class */ - public const SESSION_AUTO = 0; - /** @deprecated Use mailers:class */ - public const SESSION_EMSI = 1; - /** @deprecated Use mailers:class */ - public const SESSION_BINKP = 2; - /** @deprecated Use mailers:class */ - public const SESSION_ZMODEM = 3; - protected const MAX_PATH = 1024; /* O_ options - [First 9 bits are protocol P_* (Interfaces/ZModem)] */ @@ -331,13 +321,13 @@ abstract class Protocol /** * Initialise our Session * - * @param int $type + * @param Mailer $mo * @param SocketClient $client * @param Address|null $o * @return int * @throws \Exception */ - public function session(int $type,SocketClient $client,Address $o=NULL): int + public function session(Mailer $mo,SocketClient $client,Address $o=NULL): int { if ($o->exists) Log::withContext(['ftn'=>$o->ftn]); @@ -371,10 +361,9 @@ abstract class Protocol $this->optionSet(self::O_TCP); $this->setClient($client); - switch ($type) { - /** @noinspection PhpMissingBreakStatementInspection */ - case self::SESSION_AUTO: - Log::debug(sprintf('%s:- Trying EMSI',self::LOGKEY)); + switch ($mo->name) { + case 'EMSI': + Log::debug(sprintf('%s:- Starting EMSI',self::LOGKEY)); $rc = $this->protocol_init(); if ($rc < 0) { @@ -383,27 +372,19 @@ abstract class Protocol return self::S_FAILURE; } - case self::SESSION_EMSI: - Log::debug(sprintf('%s:- Starting EMSI',self::LOGKEY)); $rc = $this->protocol_session($this->originate); break; - case self::SESSION_BINKP: + case 'BINKP': Log::debug(sprintf('%s:- Starting BINKP',self::LOGKEY)); + $rc = $this->protocol_session($this->originate); break; - case self::SESSION_ZMODEM: - Log::debug(sprintf('%s:- Starting ZMODEM',self::LOGKEY)); - $this->client->speed = self::TCP_SPEED; - $this->originate = FALSE; - - return $this->protocol_session($this->originate); - default: - Log::error(sprintf('%s:! Unsupported session type [%d]',self::LOGKEY,$type)); + Log::error(sprintf('%s:! Unsupported session type [%d]',self::LOGKEY,$mo->id)); return self::S_FAILURE; } @@ -448,7 +429,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->sessiontype = $type; + $slo->mailer_id = $mo->id; $slo->sessiontime = $this->node->session_time; $slo->result = ($rc & self::S_MASK); $slo->originate = $this->originate; diff --git a/app/Classes/Protocol/Binkp.php b/app/Classes/Protocol/Binkp.php index 7c28501..aa026c7 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\SocketClient; use App\Classes\Sock\SocketException; use App\Exceptions\{FileGrewException,InvalidFTNException}; -use App\Models\Address; +use App\Models\{Address,Mailer}; final class Binkp extends BaseProtocol { @@ -148,7 +148,6 @@ final class Binkp extends BaseProtocol * @param SocketClient $client * @return int|null * @throws SocketException - * @throws \Exception */ public function onConnect(SocketClient $client): ?int { @@ -156,8 +155,7 @@ final class Binkp extends BaseProtocol if (! parent::onConnect($client)) { Log::withContext(['pid'=>getmypid()]); - // @todo If we can use SESSION_EMSI set an object class value that in BINKP of SESSION_BINKP, and move this method to the parent class - $this->session(self::SESSION_BINKP,$client,(new Address)); + $this->session(Mailer::where('name','BINKP')->singleOrFail(),$client,(new Address)); $this->client->close(); exit(0); } diff --git a/app/Classes/Protocol/EMSI.php b/app/Classes/Protocol/EMSI.php index b0115e4..1cf3bd9 100644 --- a/app/Classes/Protocol/EMSI.php +++ b/app/Classes/Protocol/EMSI.php @@ -9,7 +9,7 @@ use App\Classes\Protocol as BaseProtocol; use App\Classes\Sock\SocketClient; use App\Classes\Sock\SocketException; use App\Exceptions\InvalidFTNException; -use App\Models\{Address,Setup}; +use App\Models\{Address,Mailer,Setup}; use App\Interfaces\CRC as CRCInterface; use App\Interfaces\Zmodem as ZmodemInterface; use App\Traits\CRC as CRCTrait; @@ -88,7 +88,6 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface * @param SocketClient $client * @return int|null * @throws SocketException - * @throws \Exception */ public function onConnect(SocketClient $client): ?int { @@ -96,8 +95,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface if (! parent::onConnect($client)) { Log::withContext(['pid'=>getmypid()]); - // @todo Can this be SESSION_EMSI? if so, set an object class value that in EMSI of SESSION_EMSI, and move this method to the parent class - $this->session(self::SESSION_AUTO,$client,(new Address)); + $this->session(Mailer::where('name','EMSI')->singleOrFail(),$client,(new Address)); $this->client->close(); exit(0); } diff --git a/app/Classes/Protocol/Zmodem.php b/app/Classes/Protocol/Zmodem.php index 2222293..8f178b2 100644 --- a/app/Classes/Protocol/Zmodem.php +++ b/app/Classes/Protocol/Zmodem.php @@ -10,7 +10,7 @@ use App\Classes\File\{Receive,Send}; use App\Classes\Sock\{SocketClient,SocketException}; use App\Interfaces\CRC as CRCInterface; use App\Interfaces\Zmodem as ZmodemInterface; -use App\Models\Address; +use App\Models\{Address,Mailer}; 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(self::SESSION_ZMODEM,$client); + $this->session(Mailer::where('name','ZMODEM')->singleOrFail(),$client); $this->client->close(); Log::info(sprintf('%s:= onConnect - Connection closed [%s]',self::LOGKEY,$client->address_remote)); diff --git a/app/Console/Commands/CommZmodemReceive.php b/app/Console/Commands/CommZmodemReceive.php deleted file mode 100644 index 83885ce..0000000 --- a/app/Console/Commands/CommZmodemReceive.php +++ /dev/null @@ -1,51 +0,0 @@ -handler = [new ZmodemClass,'onConnect']; - - try { - $server->listen(); - - } catch (SocketException $e) { - if ($e->getMessage() === 'Can\'t accept connections: "Success"') - Log::debug('Server Terminated'); - else - Log::emergency('Uncaught Message: '.$e->getMessage()); - } - } -} diff --git a/app/Console/Commands/CommZmodemSend.php b/app/Console/Commands/CommZmodemSend.php deleted file mode 100644 index a9782a8..0000000 --- a/app/Console/Commands/CommZmodemSend.php +++ /dev/null @@ -1,45 +0,0 @@ -argument('ip'),2); - $client = SocketClient::create($address,$service_port); - - $o = new ZmodemClass; - $o->session(Protocol::SESSION_ZMODEM,$client); - - Log::info(sprintf('Connection ended: %s',$client->address_remote),['m'=>__METHOD__]); - } -} \ No newline at end of file diff --git a/app/Jobs/AddressPoll.php b/app/Jobs/AddressPoll.php index 6a16f39..66eca44 100644 --- a/app/Jobs/AddressPoll.php +++ b/app/Jobs/AddressPoll.php @@ -94,13 +94,13 @@ class AddressPoll implements ShouldQueue, ShouldBeUnique switch ($o->name) { case 'BINKP': $s = new Binkp(Setup::findOrFail(config('app.id'))); - $session = Binkp::SESSION_BINKP; + $mo = Mailer::where('name','BINKP')->singleOrFail(); break; case 'EMSI': $s = new EMSI(Setup::findOrFail(config('app.id'))); - $session = EMSI::SESSION_AUTO; + $mo = Mailer::where('name','EMSI')->singleOrFail(); break; @@ -116,7 +116,7 @@ class AddressPoll implements ShouldQueue, ShouldBeUnique try { $client = SocketClient::create($this->ao->system->address,$o->pivot->port); - if (($s->session($session,$client,$this->ao) & Protocol::S_MASK) === Protocol::S_OK) { + if (($s->session($mo,$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; diff --git a/app/Models/SystemLog.php b/app/Models/SystemLog.php index 71022bc..1b6640d 100644 --- a/app/Models/SystemLog.php +++ b/app/Models/SystemLog.php @@ -8,6 +8,11 @@ class SystemLog extends Model { /* RELATIONS */ + public function mailer() + { + return $this->belongsTo(Mailer::class); + } + public function system() { return $this->belongsTo(System::class); diff --git a/database/migrations/2023_12_10_143252_fix_mailer_id.php b/database/migrations/2023_12_10_143252_fix_mailer_id.php new file mode 100644 index 0000000..611bf21 --- /dev/null +++ b/database/migrations/2023_12_10_143252_fix_mailer_id.php @@ -0,0 +1,46 @@ +bigInteger('mailer_id')->nullable()->after('system_id'); + $table->foreign('mailer_id')->references('id')->on('mailers'); + }); + + DB::update('UPDATE system_logs SET mailer_id=? WHERE sessiontype=?',[1,2]); + DB::update('UPDATE system_logs SET mailer_id=? WHERE sessiontype=?',[2,0]); + DB::update('ALTER TABLE system_logs ALTER COLUMN mailer_id SET NOT NULL'); + + Schema::table('system_logs',function (Blueprint $table) { + $table->dropColumn(['sessiontype']); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('system_logs',function (Blueprint $table) { + $table->integer('sessiontype')->nullable(); + }); + + DB::update('UPDATE system_logs SET sessiontype=? WHERE mailer_id=?',[0,2]); + DB::update('UPDATE system_logs SET sessiontype=? WHERE mailer_id=?',[2,1]); + DB::update('ALTER TABLE system_logs ALTER COLUMN sessiontype SET NOT NULL'); + + Schema::table('system_logs',function (Blueprint $table) { + $table->dropForeign(['mailer_id']); + $table->dropColumn(['mailer_id']); + }); + } +}; diff --git a/resources/views/system/widget/form-system.blade.php b/resources/views/system/widget/form-system.blade.php index 07282c9..b3c1432 100644 --- a/resources/views/system/widget/form-system.blade.php +++ b/resources/views/system/widget/form-system.blade.php @@ -267,7 +267,7 @@ Method:
- {{ $x?->sessiontype ? \App\Models\Mailer::findOrFail($x->sessiontype)->name : '-' }} + {{ $x ? $x->mailer->name : '-' }}