diff --git a/app/Classes/Sock/SocketClient.php b/app/Classes/Sock/SocketClient.php index e543eda..b7d15f2 100644 --- a/app/Classes/Sock/SocketClient.php +++ b/app/Classes/Sock/SocketClient.php @@ -241,7 +241,13 @@ final class SocketClient { if ($socket === FALSE) throw new SocketException(SocketException::CANT_CREATE_SOCKET,socket_strerror(socket_last_error($socket))); - $result = socket_connect($socket,$address,$port); + try { + $result = socket_connect($socket,$address,$port); + + } catch (\ErrorException $e) { + throw new SocketException(SocketException::CANT_CONNECT,socket_strerror(socket_last_error($socket))); + } + if ($result === FALSE) throw new SocketException(SocketException::CANT_CONNECT,socket_strerror(socket_last_error($socket))); diff --git a/app/Console/Commands/BinkpSend.php b/app/Console/Commands/BinkpSend.php index b984c6c..c79d587 100644 --- a/app/Console/Commands/BinkpSend.php +++ b/app/Console/Commands/BinkpSend.php @@ -6,9 +6,8 @@ use Illuminate\Console\Command; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Facades\Log; -use App\Classes\Protocol\Binkp; -use App\Classes\Sock\SocketClient; -use App\Models\{Address,Setup}; +use App\Models\Address; +use App\Jobs\AddressPoll as Job; class BinkpSend extends Command { @@ -29,29 +28,16 @@ class BinkpSend extends Command /** * Execute the console command. * - * @return mixed - * @throws \App\Classes\Sock\SocketException * @throws \Exception */ - public function handle() + public function handle(): void { Log::info('Call BINKP send'); - $no = Address::findFTN($this->argument('ftn')); - if (! $no) + $ao = Address::findFTN($this->argument('ftn')); + if (! $ao) throw new ModelNotFoundException('Unknown node: '.$this->argument('ftn')); - if ($no->system->mailer_type != Setup::O_BINKP) - 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__]); + Job::dispatchSync($ao); } } diff --git a/app/Console/Commands/EMSISend.php b/app/Console/Commands/EMSISend.php index 710b6e3..b075463 100644 --- a/app/Console/Commands/EMSISend.php +++ b/app/Console/Commands/EMSISend.php @@ -6,9 +6,8 @@ use Illuminate\Console\Command; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Facades\Log; -use App\Classes\Protocol\EMSI; -use App\Classes\Sock\SocketClient; -use App\Models\{Address,Setup}; +use App\Models\Address; +use App\Jobs\AddressPoll as Job; class EMSISend extends Command { @@ -29,28 +28,16 @@ class EMSISend extends Command /** * Execute the console command. * - * @return mixed - * @throws \App\Classes\Sock\SocketException + * @throws \Exception */ - public function handle() + public function handle(): void { Log::info('Call EMSI send'); - $no = Address::findFTN($this->argument('ftn')); - if (! $no) + $ao = Address::findFTN($this->argument('ftn')); + if (! $ao) throw new ModelNotFoundException('Unknown node: '.$this->argument('ftn')); - if ($no->system->mailer_type != Setup::O_EMSI) - 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__]); + Job::dispatchSync($ao); } } diff --git a/app/Jobs/AddressPoll.php b/app/Jobs/AddressPoll.php new file mode 100644 index 0000000..125cb0e --- /dev/null +++ b/app/Jobs/AddressPoll.php @@ -0,0 +1,66 @@ +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)); + } +} \ No newline at end of file