Refactor peer synchronization and resolution logic.

This commit is contained in:
netkas 2025-01-15 15:21:44 -05:00
parent f9a268058f
commit ab03439e04
2 changed files with 29 additions and 14 deletions

View file

@ -66,21 +66,11 @@
// Otherwise, resolve the peer from the remote server // Otherwise, resolve the peer from the remote server
try try
{ {
$client = Socialbox::getExternalSession($peerAddress->getDomain()); return $rpcRequest->produceResponse(Socialbox::resolveExternalPeer($peerAddress));
} }
catch(Exception $e) catch(Exception $e)
{ {
throw new StandardException(sprintf('There was an error while trying to connect to %s: %s', $peerAddress->getDomain(), $e->getMessage()), StandardError::RESOLUTION_FAILED, $e); throw new StandardException(sprintf('There was an error while trying to resolve the peer %s: %s', $peerAddress, $e->getMessage()), StandardError::RESOLUTION_FAILED, $e);
}
// Return the result/error of the resolution
try
{
return $rpcRequest->produceResponse($client->resolvePeer($peerAddress));
}
catch(RpcException $e)
{
throw new StandardException($e->getMessage(), StandardError::tryFrom($e->getCode()) ?? StandardError::UNKNOWN, $e);
} }
} }
} }

View file

@ -28,6 +28,7 @@
use Socialbox\Managers\SessionManager; use Socialbox\Managers\SessionManager;
use Socialbox\Objects\ClientRequest; use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\PeerAddress; use Socialbox\Objects\PeerAddress;
use Socialbox\Objects\Standard\Peer;
use Socialbox\Objects\Standard\ServerInformation; use Socialbox\Objects\Standard\ServerInformation;
use Throwable; use Throwable;
@ -734,15 +735,21 @@
/** /**
* Synchronizes an external peer by resolving and integrating its information into the system. * Synchronizes an external peer by resolving and integrating its information into the system.
* *
* @param PeerAddress|string $externalPeer The external peer to synchronize, provided as a PeerAddress instance or a string. * @param PeerAddress|Peer|string $externalPeer The external peer to synchronize, provided as a PeerAddress instance or a string.
* @return void * @return void
* @throws CryptographyException If there is an error in the cryptography * @throws CryptographyException If there is an error in the cryptography
* @throws DatabaseOperationException If there is an error while processing the peer against the database * @throws DatabaseOperationException If there is an error while processing the peer against the database
* @throws ResolutionException If the synchronization process fails due to unresolved peer information or other errors. * @throws ResolutionException If the synchronization process fails due to unresolved peer information or other errors.
* @throws RpcException If there is an RPC exception while connecting to the remote server * @throws RpcException If there is an RPC exception while connecting to the remote server
*/ */
public static function synchronizeExternalPeer(PeerAddress|string $externalPeer): void public static function synchronizeExternalPeer(PeerAddress|Peer|string $externalPeer): void
{ {
if($externalPeer instanceof Peer)
{
RegisteredPeerManager::synchronizeExternalPeer($externalPeer);
return;
}
if($externalPeer instanceof PeerAddress) if($externalPeer instanceof PeerAddress)
{ {
$externalPeer = $externalPeer->getAddress(); $externalPeer = $externalPeer->getAddress();
@ -752,6 +759,24 @@
RegisteredPeerManager::synchronizeExternalPeer($client->resolvePeer($externalPeer)); RegisteredPeerManager::synchronizeExternalPeer($client->resolvePeer($externalPeer));
} }
/**
* Resolves an external peer based on the given peer address or string identifier.
*
* @param PeerAddress|string $externalPeer The external peer address or string identifier to be resolved.
* @return Peer The resolved external peer after synchronization.
*/
public static function resolveExternalPeer(PeerAddress|string $externalPeer): Peer
{
if($externalPeer instanceof PeerAddress)
{
$externalPeer = $externalPeer->getAddress();
}
$resolvedPeer = self::getExternalSession($externalPeer->getDomain())->resolvePeer($externalPeer);
self::synchronizeExternalPeer($resolvedPeer);
return $resolvedPeer;
}
/** /**
* Retrieves the server information by assembling data from the configuration settings. * Retrieves the server information by assembling data from the configuration settings.
* *