Move address polling to a Job, better catch Socket connection refused errors

This commit is contained in:
Deon George 2021-09-20 20:39:03 +10:00
parent 8b8b513ed1
commit 82e3283d6d
4 changed files with 86 additions and 41 deletions

View File

@ -241,7 +241,13 @@ final class SocketClient {
if ($socket === FALSE) if ($socket === FALSE)
throw new SocketException(SocketException::CANT_CREATE_SOCKET,socket_strerror(socket_last_error($socket))); throw new SocketException(SocketException::CANT_CREATE_SOCKET,socket_strerror(socket_last_error($socket)));
try {
$result = socket_connect($socket,$address,$port); $result = socket_connect($socket,$address,$port);
} catch (\ErrorException $e) {
throw new SocketException(SocketException::CANT_CONNECT,socket_strerror(socket_last_error($socket)));
}
if ($result === FALSE) if ($result === FALSE)
throw new SocketException(SocketException::CANT_CONNECT,socket_strerror(socket_last_error($socket))); throw new SocketException(SocketException::CANT_CONNECT,socket_strerror(socket_last_error($socket)));

View File

@ -6,9 +6,8 @@ use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use App\Classes\Protocol\Binkp; use App\Models\Address;
use App\Classes\Sock\SocketClient; use App\Jobs\AddressPoll as Job;
use App\Models\{Address,Setup};
class BinkpSend extends Command class BinkpSend extends Command
{ {
@ -29,29 +28,16 @@ class BinkpSend extends Command
/** /**
* Execute the console command. * Execute the console command.
* *
* @return mixed
* @throws \App\Classes\Sock\SocketException
* @throws \Exception * @throws \Exception
*/ */
public function handle() public function handle(): void
{ {
Log::info('Call BINKP send'); Log::info('Call BINKP send');
$no = Address::findFTN($this->argument('ftn')); $ao = Address::findFTN($this->argument('ftn'));
if (! $no) if (! $ao)
throw new ModelNotFoundException('Unknown node: '.$this->argument('ftn')); throw new ModelNotFoundException('Unknown node: '.$this->argument('ftn'));
if ($no->system->mailer_type != Setup::O_BINKP) Job::dispatchSync($ao);
throw new \Exception(sprintf('Node [%s] doesnt support BINKD',$this->argument('ftn')));
if ((! $no->system->mailer_address) || (! $no->system->mailer_port))
throw new \Exception(sprintf('Unable to poll [%s] missing mailer details',$this->argument('ftn')));
$client = SocketClient::create($no->system->mailer_address,$no->system->mailer_port);
$o = new Binkp(Setup::findOrFail(config('app.id')));
$o->session(Binkp::SESSION_BINKP,$client,$no);
Log::info(sprintf('Connection ended: %s',$client->address_remote),['m'=>__METHOD__]);
} }
} }

View File

@ -6,9 +6,8 @@ use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use App\Classes\Protocol\EMSI; use App\Models\Address;
use App\Classes\Sock\SocketClient; use App\Jobs\AddressPoll as Job;
use App\Models\{Address,Setup};
class EMSISend extends Command class EMSISend extends Command
{ {
@ -29,28 +28,16 @@ class EMSISend extends Command
/** /**
* Execute the console command. * Execute the console command.
* *
* @return mixed * @throws \Exception
* @throws \App\Classes\Sock\SocketException
*/ */
public function handle() public function handle(): void
{ {
Log::info('Call EMSI send'); Log::info('Call EMSI send');
$no = Address::findFTN($this->argument('ftn')); $ao = Address::findFTN($this->argument('ftn'));
if (! $no) if (! $ao)
throw new ModelNotFoundException('Unknown node: '.$this->argument('ftn')); throw new ModelNotFoundException('Unknown node: '.$this->argument('ftn'));
if ($no->system->mailer_type != Setup::O_EMSI) Job::dispatchSync($ao);
throw new \Exception(sprintf('Node [%s] doesnt support EMSI',$this->argument('ftn')));
if ((! $no->system->mailer_address) || (! $no->system->mailer_port))
throw new \Exception(sprintf('Unable to poll [%s] missing mailer details',$this->argument('ftn')));
$client = SocketClient::create($no->system->mailer_address,$no->system->mailer_port,38400);
$o = new EMSI(Setup::findOrFail(config('app.id')));
$o->session(EMSI::SESSION_AUTO,$client,$no);
Log::info(sprintf('Connection ended: %s',$client->address_remote),['m'=>__METHOD__]);
} }
} }

66
app/Jobs/AddressPoll.php Normal file
View File

@ -0,0 +1,66 @@
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use App\Classes\Protocol\{Binkp,EMSI};
use App\Classes\Sock\SocketClient;
use App\Classes\Sock\SocketException;
use App\Models\{Address,Setup};
class AddressPoll implements ShouldQueue
{
private const LOGKEY = 'JAP';
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private Address $ao;
public function __construct(Address $ao)
{
// Some checks
$this->ao = $ao;
}
/**
* When calling MessageProcess - we assume that the packet is from a valid source
*/
public function handle()
{
if ((! $this->ao->system->mailer_address) || (! $this->ao->system->mailer_port))
throw new \Exception(sprintf('Unable to poll [%s] missing mailer details',$this->argument('ftn')));
try {
$client = SocketClient::create($this->ao->system->mailer_address,$this->ao->system->mailer_port);
} catch (SocketException $e) {
Log::error(sprintf('%s:! Unable to connect to [%s]: %s',self::LOGKEY,$this->ao->ftn,$e->getMessage()));
abort(500);
}
switch ($this->ao->system->mailer_type) {
case Setup::O_BINKP:
$o = new Binkp(Setup::findOrFail(config('app.id')));
$o->session(Binkp::SESSION_BINKP,$client,$this->ao);
break;
case Setup::O_EMSI:
$o = new EMSI(Setup::findOrFail(config('app.id')));
$o->session(EMSI::SESSION_AUTO,$client,$this->ao);
break;
default:
throw new \Exception(sprintf('Node [%s] has a mailer type that is unhandled',$this->ao->ftn));
}
Log::info(sprintf('%s:Connection ended: %s',self::LOGKEY,$client->address_remote));
}
}