aboutsummaryrefslogtreecommitdiff
path: root/Sources/swiftGopherClient/gopherClient.swift
diff options
context:
space:
mode:
authorNavan Chauhan <navanchauhan@gmail.com>2023-12-17 00:28:48 -0700
committerNavan Chauhan <navanchauhan@gmail.com>2023-12-17 00:31:01 -0700
commitc7f509feb7b8d290469178e7d4c940f973c658eb (patch)
treefe8bacd63e5e0b68b51ea8fa0017fb271c51f53d /Sources/swiftGopherClient/gopherClient.swift
parent6ddc93125e8a47217901a2daea645ae62ce02ddd (diff)
use swift-format
use swift-format
Diffstat (limited to 'Sources/swiftGopherClient/gopherClient.swift')
-rw-r--r--Sources/swiftGopherClient/gopherClient.swift129
1 files changed, 76 insertions, 53 deletions
diff --git a/Sources/swiftGopherClient/gopherClient.swift b/Sources/swiftGopherClient/gopherClient.swift
index eabbbc4..c7542ee 100644
--- a/Sources/swiftGopherClient/gopherClient.swift
+++ b/Sources/swiftGopherClient/gopherClient.swift
@@ -6,67 +6,90 @@
//
import Foundation
+import GopherHelpers
import NIO
import NIOTransportServices
-import GopherHelpers
+/// `GopherClient` is a class for handling network connections and requests to Gopher servers.
+///
+/// It utilizes `NIOTSEventLoopGroup` on iOS/macOS (Not sure why you would run this on watchOS/tvOS but it supports that as well) for network operations, falling back to `MultiThreadedEventLoopGroup` otherwise.
public class GopherClient {
- private let group: EventLoopGroup
+ private let group: EventLoopGroup
- public init() {
- if #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
- self.group = NIOTSEventLoopGroup()
- } else {
- self.group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
- }
+ /// Initializes a new instance of `GopherClient`.
+ ///
+ /// It automatically chooses the appropriate `EventLoopGroup` based on the running platform.
+ public init() {
+ if #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
+ self.group = NIOTSEventLoopGroup()
+ } else {
+ self.group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
}
+ }
- deinit {
- try? group.syncShutdownGracefully()
- }
+ deinit {
+ try? group.syncShutdownGracefully()
+ }
- public func sendRequest(to host: String, port: Int = 70, message: String, completion: @escaping (Result<[gopherItem], Error>) -> Void) {
- if #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
- let bootstrap = NIOTSConnectionBootstrap(group: group)
- .channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
- .channelInitializer { channel in
- channel.pipeline.addHandler(GopherRequestResponseHandler(message: message, completion: completion))
- }
- bootstrap.connect(host: host, port: port).whenComplete { result in
- switch result {
- case .success(let channel):
- channel.closeFuture.whenComplete { _ in
- print("Connection closed")
- }
- case .failure(let error):
- completion(.failure(error))
- }
- }
- } else {
- let bootstrap = ClientBootstrap(group: group)
- .channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
- .channelInitializer { channel in
- channel.pipeline.addHandler(GopherRequestResponseHandler(message: message, completion: completion))
- }
- bootstrap.connect(host: host, port: port).whenComplete { result in
- switch result {
- case .success(let channel):
- channel.closeFuture.whenComplete { _ in
- print("Connection closed")
- }
- case .failure(let error):
- completion(.failure(error))
- }
- }
+ /// Sends a request to a Gopher server.
+ ///
+ /// - Parameters:
+ /// - host: The host address of the Gopher server.
+ /// - port: The port of the Gopher server. Defaults to 70.
+ /// - message: The message to be sent to the server.
+ /// - completion: A closure that handles the result of the request.
+ ///
+ /// The method asynchronously establishes a connection, sends the request, and calls the completion handler with the result.
+ public func sendRequest(
+ to host: String, port: Int = 70, message: String,
+ completion: @escaping (Result<[gopherItem], Error>) -> Void
+ ) {
+ if #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
+ let bootstrap = NIOTSConnectionBootstrap(group: group)
+ .channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
+ .channelInitializer { channel in
+ channel.pipeline.addHandler(
+ GopherRequestResponseHandler(message: message, completion: completion))
}
-
- }
-
- private func shutdownEventLoopGroup() {
- do {
- try group.syncShutdownGracefully()
- } catch {
- print("Error shutting down event loop group: \(error)")
- }
+ bootstrap.connect(host: host, port: port).whenComplete { result in
+ switch result {
+ case .success(let channel):
+ channel.closeFuture.whenComplete { _ in
+ print("Connection closed")
+ }
+ case .failure(let error):
+ completion(.failure(error))
+ }
+ }
+ } else {
+ let bootstrap = ClientBootstrap(group: group)
+ .channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
+ .channelInitializer { channel in
+ channel.pipeline.addHandler(
+ GopherRequestResponseHandler(message: message, completion: completion))
}
+ bootstrap.connect(host: host, port: port).whenComplete { result in
+ switch result {
+ case .success(let channel):
+ channel.closeFuture.whenComplete { _ in
+ print("Connection closed")
+ }
+ case .failure(let error):
+ completion(.failure(error))
+ }
+ }
+ }
+
+ }
+
+ /// Shuts down the event loop group, releasing any resources.
+ ///
+ /// This method is called during deinitialization to ensure clean shutdown of network resources.
+ private func shutdownEventLoopGroup() {
+ do {
+ try group.syncShutdownGracefully()
+ } catch {
+ print("Error shutting down event loop group: \(error)")
+ }
+ }
}