From 54a935284c12dbc2b493a56b081722e75f3519f1 Mon Sep 17 00:00:00 2001 From: Shinichiro Oba Date: Wed, 11 Nov 2015 21:12:52 +0900 Subject: [PATCH 1/3] add RequestIdentifierGenerator --- JSONRPCKit.xcodeproj/project.pbxproj | 4 +++ JSONRPCKit/RequestIdentifierGenerator.swift | 31 +++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 JSONRPCKit/RequestIdentifierGenerator.swift diff --git a/JSONRPCKit.xcodeproj/project.pbxproj b/JSONRPCKit.xcodeproj/project.pbxproj index 0406f8c..1adacdf 100644 --- a/JSONRPCKit.xcodeproj/project.pbxproj +++ b/JSONRPCKit.xcodeproj/project.pbxproj @@ -28,6 +28,7 @@ CBB0C35A1BF0B2D3006A7D41 /* RequestType.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBB0C3591BF0B2D3006A7D41 /* RequestType.swift */; }; CBB0C35C1BF0B6FD006A7D41 /* JSONRPC.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBB0C35B1BF0B6FD006A7D41 /* JSONRPC.swift */; }; CBB4161A1BF3541F00B4DB0E /* BatchRequestViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBB416181BF3534A00B4DB0E /* BatchRequestViewController.swift */; }; + CBB4161C1BF3599D00B4DB0E /* RequestIdentifierGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBB4161B1BF3599D00B4DB0E /* RequestIdentifierGenerator.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -77,6 +78,7 @@ CBB0C3591BF0B2D3006A7D41 /* RequestType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequestType.swift; sourceTree = ""; }; CBB0C35B1BF0B6FD006A7D41 /* JSONRPC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONRPC.swift; sourceTree = ""; }; CBB416181BF3534A00B4DB0E /* BatchRequestViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BatchRequestViewController.swift; sourceTree = ""; }; + CBB4161B1BF3599D00B4DB0E /* RequestIdentifierGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequestIdentifierGenerator.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -154,6 +156,7 @@ CB36EB211BF24F1E003A4BCA /* AuthRequestType.swift */, CBB0C35B1BF0B6FD006A7D41 /* JSONRPC.swift */, CB82B9E11BF0C5CE00756CB1 /* JSONRPCError.swift */, + CBB4161B1BF3599D00B4DB0E /* RequestIdentifierGenerator.swift */, CBB0C3531BF0B1D7006A7D41 /* Info.plist */, ); path = JSONRPCKit; @@ -286,6 +289,7 @@ buildActionMask = 2147483647; files = ( CB82B9E21BF0C5CE00756CB1 /* JSONRPCError.swift in Sources */, + CBB4161C1BF3599D00B4DB0E /* RequestIdentifierGenerator.swift in Sources */, CB36EB221BF24F1E003A4BCA /* AuthRequestType.swift in Sources */, CBB0C35A1BF0B2D3006A7D41 /* RequestType.swift in Sources */, CBB0C35C1BF0B6FD006A7D41 /* JSONRPC.swift in Sources */, diff --git a/JSONRPCKit/RequestIdentifierGenerator.swift b/JSONRPCKit/RequestIdentifierGenerator.swift new file mode 100644 index 0000000..7ae56c3 --- /dev/null +++ b/JSONRPCKit/RequestIdentifierGenerator.swift @@ -0,0 +1,31 @@ +// +// RequestIdentifierGenerator.swift +// JSONRPCKit +// +// Created by Shinichiro Oba on 2015/11/11. +// Copyright © 2015年 Shinichiro Oba. All rights reserved. +// + +import Foundation + +public protocol RequestIdentifierGenerator { + typealias IdentifierType: Hashable + + func next() -> IdentifierType +} + +public class NumberIdentifierGenerator: RequestIdentifierGenerator { + + private var identifier = 1 + + public func next() -> Int { + return self.identifier++ + } +} + +public class GUIDGenerator: RequestIdentifierGenerator { + + public func next() -> String { + return NSUUID().UUIDString + } +} From e5bab7e88542e36ef5c518fb3feea01444f924e4 Mon Sep 17 00:00:00 2001 From: Shinichiro Oba Date: Wed, 11 Nov 2015 21:53:27 +0900 Subject: [PATCH 2/3] add NumberIdentifierGenerator and a Box for RequestIdentifierGenerator --- JSONRPCKit.xcodeproj/project.pbxproj | 4 ++++ JSONRPCKit/NumberIdentifierGenerator.swift | 18 ++++++++++++++++++ JSONRPCKit/RequestIdentifierGenerator.swift | 16 ++++++---------- 3 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 JSONRPCKit/NumberIdentifierGenerator.swift diff --git a/JSONRPCKit.xcodeproj/project.pbxproj b/JSONRPCKit.xcodeproj/project.pbxproj index 1adacdf..a2dcb86 100644 --- a/JSONRPCKit.xcodeproj/project.pbxproj +++ b/JSONRPCKit.xcodeproj/project.pbxproj @@ -29,6 +29,7 @@ CBB0C35C1BF0B6FD006A7D41 /* JSONRPC.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBB0C35B1BF0B6FD006A7D41 /* JSONRPC.swift */; }; CBB4161A1BF3541F00B4DB0E /* BatchRequestViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBB416181BF3534A00B4DB0E /* BatchRequestViewController.swift */; }; CBB4161C1BF3599D00B4DB0E /* RequestIdentifierGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBB4161B1BF3599D00B4DB0E /* RequestIdentifierGenerator.swift */; }; + CBB4161E1BF370EA00B4DB0E /* NumberIdentifierGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBB4161D1BF370EA00B4DB0E /* NumberIdentifierGenerator.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -79,6 +80,7 @@ CBB0C35B1BF0B6FD006A7D41 /* JSONRPC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONRPC.swift; sourceTree = ""; }; CBB416181BF3534A00B4DB0E /* BatchRequestViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BatchRequestViewController.swift; sourceTree = ""; }; CBB4161B1BF3599D00B4DB0E /* RequestIdentifierGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequestIdentifierGenerator.swift; sourceTree = ""; }; + CBB4161D1BF370EA00B4DB0E /* NumberIdentifierGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NumberIdentifierGenerator.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -157,6 +159,7 @@ CBB0C35B1BF0B6FD006A7D41 /* JSONRPC.swift */, CB82B9E11BF0C5CE00756CB1 /* JSONRPCError.swift */, CBB4161B1BF3599D00B4DB0E /* RequestIdentifierGenerator.swift */, + CBB4161D1BF370EA00B4DB0E /* NumberIdentifierGenerator.swift */, CBB0C3531BF0B1D7006A7D41 /* Info.plist */, ); path = JSONRPCKit; @@ -289,6 +292,7 @@ buildActionMask = 2147483647; files = ( CB82B9E21BF0C5CE00756CB1 /* JSONRPCError.swift in Sources */, + CBB4161E1BF370EA00B4DB0E /* NumberIdentifierGenerator.swift in Sources */, CBB4161C1BF3599D00B4DB0E /* RequestIdentifierGenerator.swift in Sources */, CB36EB221BF24F1E003A4BCA /* AuthRequestType.swift in Sources */, CBB0C35A1BF0B2D3006A7D41 /* RequestType.swift in Sources */, diff --git a/JSONRPCKit/NumberIdentifierGenerator.swift b/JSONRPCKit/NumberIdentifierGenerator.swift new file mode 100644 index 0000000..3e940b2 --- /dev/null +++ b/JSONRPCKit/NumberIdentifierGenerator.swift @@ -0,0 +1,18 @@ +// +// NumberIdentifierGenerator.swift +// JSONRPCKit +// +// Created by Shinichiro Oba on 2015/11/11. +// Copyright © 2015年 Shinichiro Oba. All rights reserved. +// + +import Foundation + +public class NumberIdentifierGenerator: RequestIdentifierGenerator { + + private var currentIdentifier = 1 + + public func next() -> Int { + return self.currentIdentifier++ + } +} diff --git a/JSONRPCKit/RequestIdentifierGenerator.swift b/JSONRPCKit/RequestIdentifierGenerator.swift index 7ae56c3..f587452 100644 --- a/JSONRPCKit/RequestIdentifierGenerator.swift +++ b/JSONRPCKit/RequestIdentifierGenerator.swift @@ -14,18 +14,14 @@ public protocol RequestIdentifierGenerator { func next() -> IdentifierType } -public class NumberIdentifierGenerator: RequestIdentifierGenerator { +public struct RequestIdentifierGeneratorBox: RequestIdentifierGenerator { + private let _next: () -> T - private var identifier = 1 - - public func next() -> Int { - return self.identifier++ + public init(_ generator: P) { + _next = generator.next } -} - -public class GUIDGenerator: RequestIdentifierGenerator { - public func next() -> String { - return NSUUID().UUIDString + public func next() -> T { + return _next() } } From 96f294eb6f732bc9b3db8c8352e3fa5302e3bcfe Mon Sep 17 00:00:00 2001 From: Shinichiro Oba Date: Thu, 12 Nov 2015 00:19:35 +0900 Subject: [PATCH 3/3] introduce RequestIdentifier --- Example/SingleRequestViewController.swift | 12 ++++++- JSONRPCKit.xcodeproj/project.pbxproj | 4 +++ JSONRPCKit/JSONRPC.swift | 29 ++++++++++++----- JSONRPCKit/NumberIdentifierGenerator.swift | 4 +-- JSONRPCKit/RequestIdentifier.swift | 35 +++++++++++++++++++++ JSONRPCKit/RequestIdentifierGenerator.swift | 16 +--------- 6 files changed, 75 insertions(+), 25 deletions(-) create mode 100644 JSONRPCKit/RequestIdentifier.swift diff --git a/Example/SingleRequestViewController.swift b/Example/SingleRequestViewController.swift index bf1066b..2b908ff 100644 --- a/Example/SingleRequestViewController.swift +++ b/Example/SingleRequestViewController.swift @@ -9,6 +9,16 @@ import UIKit import JSONRPCKit +public class StringIdentifierGenerator: RequestIdentifierGenerator { + + private var currentIdentifier = 1 + + public func next() -> RequestIdentifier { + return .StringType("\(self.currentIdentifier++)") + } +} + + class SingleRequestViewController: UIViewController { @IBOutlet weak var firstTextField: UITextField! @@ -16,7 +26,7 @@ class SingleRequestViewController: UIViewController { @IBOutlet weak var subtractAnswerLabel: UILabel! func subtract(first: Int, _ second: Int) { - let jsonrpc = JSONRPC() + let jsonrpc = JSONRPC(identifierGenerator: StringIdentifierGenerator()) let subtractRequest = Subtract( userName: MathServiceAPI.userName, diff --git a/JSONRPCKit.xcodeproj/project.pbxproj b/JSONRPCKit.xcodeproj/project.pbxproj index a2dcb86..f5af25d 100644 --- a/JSONRPCKit.xcodeproj/project.pbxproj +++ b/JSONRPCKit.xcodeproj/project.pbxproj @@ -23,6 +23,7 @@ CB36EB221BF24F1E003A4BCA /* AuthRequestType.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB36EB211BF24F1E003A4BCA /* AuthRequestType.swift */; }; CB36EB231BF25F45003A4BCA /* JSONRPCKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CBB0C34E1BF0B1D6006A7D41 /* JSONRPCKit.framework */; }; CB36EB241BF25F45003A4BCA /* JSONRPCKit.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = CBB0C34E1BF0B1D6006A7D41 /* JSONRPCKit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; + CB36EB281BF39028003A4BCA /* RequestIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB36EB271BF39028003A4BCA /* RequestIdentifier.swift */; }; CB82B9E21BF0C5CE00756CB1 /* JSONRPCError.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB82B9E11BF0C5CE00756CB1 /* JSONRPCError.swift */; }; CBB0C3521BF0B1D6006A7D41 /* JSONRPCKit.h in Headers */ = {isa = PBXBuildFile; fileRef = CBB0C3511BF0B1D6006A7D41 /* JSONRPCKit.h */; settings = {ATTRIBUTES = (Public, ); }; }; CBB0C35A1BF0B2D3006A7D41 /* RequestType.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBB0C3591BF0B2D3006A7D41 /* RequestType.swift */; }; @@ -72,6 +73,7 @@ CB36EB181BF243C7003A4BCA /* Result.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Result.framework; path = Carthage/Build/iOS/Result.framework; sourceTree = ""; }; CB36EB1F1BF24497003A4BCA /* MathService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MathService.swift; sourceTree = ""; }; CB36EB211BF24F1E003A4BCA /* AuthRequestType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AuthRequestType.swift; sourceTree = ""; }; + CB36EB271BF39028003A4BCA /* RequestIdentifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequestIdentifier.swift; sourceTree = ""; }; CB82B9E11BF0C5CE00756CB1 /* JSONRPCError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONRPCError.swift; sourceTree = ""; }; CBB0C34E1BF0B1D6006A7D41 /* JSONRPCKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = JSONRPCKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; CBB0C3511BF0B1D6006A7D41 /* JSONRPCKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JSONRPCKit.h; sourceTree = ""; }; @@ -158,6 +160,7 @@ CB36EB211BF24F1E003A4BCA /* AuthRequestType.swift */, CBB0C35B1BF0B6FD006A7D41 /* JSONRPC.swift */, CB82B9E11BF0C5CE00756CB1 /* JSONRPCError.swift */, + CB36EB271BF39028003A4BCA /* RequestIdentifier.swift */, CBB4161B1BF3599D00B4DB0E /* RequestIdentifierGenerator.swift */, CBB4161D1BF370EA00B4DB0E /* NumberIdentifierGenerator.swift */, CBB0C3531BF0B1D7006A7D41 /* Info.plist */, @@ -293,6 +296,7 @@ files = ( CB82B9E21BF0C5CE00756CB1 /* JSONRPCError.swift in Sources */, CBB4161E1BF370EA00B4DB0E /* NumberIdentifierGenerator.swift in Sources */, + CB36EB281BF39028003A4BCA /* RequestIdentifier.swift in Sources */, CBB4161C1BF3599D00B4DB0E /* RequestIdentifierGenerator.swift in Sources */, CB36EB221BF24F1E003A4BCA /* AuthRequestType.swift in Sources */, CBB0C35A1BF0B2D3006A7D41 /* RequestType.swift in Sources */, diff --git a/JSONRPCKit/JSONRPC.swift b/JSONRPCKit/JSONRPC.swift index b5d8444..97bd572 100644 --- a/JSONRPCKit/JSONRPC.swift +++ b/JSONRPCKit/JSONRPC.swift @@ -13,14 +13,21 @@ public class JSONRPC { public let version = "2.0" + private let identifierGenerator: RequestIdentifierGenerator + private var requests: [[String: AnyObject]] = [] - private var handlers: [String: ([String: AnyObject]) -> Void] = [:] - - public init() { + private var handlers: [RequestIdentifier: ([String: AnyObject]) -> Void] = [:] + + public init(identifierGenerator: RequestIdentifierGenerator) { + self.identifierGenerator = identifierGenerator } + public convenience init() { + self.init(identifierGenerator: NumberIdentifierGenerator()) + } + public func addRequest(request: T, handler: (Result) -> Void) { - let id = NSUUID().UUIDString + let id = self.identifierGenerator.next() self.requests.append(self.buildJSONFromRequest(request, id: id)) @@ -43,13 +50,18 @@ public class JSONRPC { self.requests.append(self.buildJSONFromRequest(request)) } - private func buildJSONFromRequest(request: T, id: String? = nil) -> [String: AnyObject] { + private func buildJSONFromRequest(request: T, id: RequestIdentifier? = nil) -> [String: AnyObject] { var json: [String: AnyObject] = request.buildJSON() json["jsonrpc"] = self.version if let id = id { - json["id"] = id + switch id { + case .StringType(let string): + json["id"] = string + case .NumberType(let number): + json["id"] = number + } } return json @@ -78,7 +90,10 @@ public class JSONRPC { throw JSONRPCError.UnsupportedVersion(version) } - if let id = json["id"] as? String, handle = self.handlers[id] { + if let id = json["id"] as? String, handle = self.handlers[.StringType(id)] { + handle(json) + } + if let id = json["id"] as? Int, handle = self.handlers[.NumberType(id)] { handle(json) } } diff --git a/JSONRPCKit/NumberIdentifierGenerator.swift b/JSONRPCKit/NumberIdentifierGenerator.swift index 3e940b2..5be220f 100644 --- a/JSONRPCKit/NumberIdentifierGenerator.swift +++ b/JSONRPCKit/NumberIdentifierGenerator.swift @@ -12,7 +12,7 @@ public class NumberIdentifierGenerator: RequestIdentifierGenerator { private var currentIdentifier = 1 - public func next() -> Int { - return self.currentIdentifier++ + public func next() -> RequestIdentifier { + return .NumberType(self.currentIdentifier++) } } diff --git a/JSONRPCKit/RequestIdentifier.swift b/JSONRPCKit/RequestIdentifier.swift new file mode 100644 index 0000000..47ff919 --- /dev/null +++ b/JSONRPCKit/RequestIdentifier.swift @@ -0,0 +1,35 @@ +// +// RequestIdentifier.swift +// JSONRPCKit +// +// Created by Shinichiro Oba on 11/12/15. +// Copyright © 2015 Shinichiro Oba. All rights reserved. +// + +import Foundation + +public enum RequestIdentifier: Hashable { + case NumberType(Int) + case StringType(String) + + public var hashValue: Int { + switch self { + case NumberType(let number): + return number + case StringType(let string): + return string.hashValue + } + } +} + +public func ==(lhs: RequestIdentifier, rhs: RequestIdentifier) -> Bool { + if case let (.NumberType(lnumber), .NumberType(rnumber)) = (lhs, rhs) { + return lnumber == rnumber + } + + if case let (.StringType(lstring), .StringType(rstring)) = (lhs, rhs) { + return lstring == rstring + } + + return false +} diff --git a/JSONRPCKit/RequestIdentifierGenerator.swift b/JSONRPCKit/RequestIdentifierGenerator.swift index f587452..5788dae 100644 --- a/JSONRPCKit/RequestIdentifierGenerator.swift +++ b/JSONRPCKit/RequestIdentifierGenerator.swift @@ -9,19 +9,5 @@ import Foundation public protocol RequestIdentifierGenerator { - typealias IdentifierType: Hashable - - func next() -> IdentifierType -} - -public struct RequestIdentifierGeneratorBox: RequestIdentifierGenerator { - private let _next: () -> T - - public init(_ generator: P) { - _next = generator.next - } - - public func next() -> T { - return _next() - } + func next() -> RequestIdentifier }