All Projects → kevin0571 → Stnettaskqueue

kevin0571 / Stnettaskqueue

Licence: mit
STNetTaskQueue is a networking queue library for iOS and OS X. It's abstract and can be implemented in different protocols.

Projects that are alternatives of or similar to Stnettaskqueue

Metta
An information security preparedness tool to do adversarial simulation.
Stars: ✭ 867 (+863.33%)
Mutual labels:  network, networking
Dratini
Dratini is a neat network abstraction layer.
Stars: ✭ 38 (-57.78%)
Mutual labels:  network, networking
Bash Toolkit
Este proyecto esá destinado a ayudar a los sysadmin
Stars: ✭ 13 (-85.56%)
Mutual labels:  network, networking
P2p
Practice project to demonstrate p2p file sharing.
Stars: ✭ 16 (-82.22%)
Mutual labels:  network, networking
React Native Netinfo
React Native Network Info API for Android & iOS
Stars: ✭ 1,049 (+1065.56%)
Mutual labels:  network, networking
Diffios
Cisco IOS diff tool
Stars: ✭ 23 (-74.44%)
Mutual labels:  network, networking
Xdp
Package xdp allows one to use XDP sockets from the Go programming language.
Stars: ✭ 36 (-60%)
Mutual labels:  network, networking
Fast Android Networking
🚀 A Complete Fast Android Networking Library that also supports HTTP/2 🚀
Stars: ✭ 5,346 (+5840%)
Mutual labels:  network, networking
Pnet
High level Java network library
Stars: ✭ 49 (-45.56%)
Mutual labels:  network, networking
Dknetworking
基于 AFNetworking + YYCache 的二次封装,支持缓存策略的网络请求框架
Stars: ✭ 41 (-54.44%)
Mutual labels:  network, networking
Nexer
Content based network multiplexer or redirector made with love and Go
Stars: ✭ 7 (-92.22%)
Mutual labels:  network, networking
Avenue
Wrapper around URLSession and URLSessionTask to enable seamless integration with Operation / OperationQueue.
Stars: ✭ 58 (-35.56%)
Mutual labels:  network, networking
Bmon
bandwidth monitor and rate estimator
Stars: ✭ 787 (+774.44%)
Mutual labels:  network, networking
Librg
🚀 Making multi-player gamedev simpler since 2017
Stars: ✭ 813 (+803.33%)
Mutual labels:  network, networking
Grassmarlin
Provides situational awareness of Industrial Control Systems (ICS) and Supervisory Control and Data Acquisition (SCADA) networks in support of network security assessments. #nsacyber
Stars: ✭ 621 (+590%)
Mutual labels:  network, networking
Libzmq
ZeroMQ core engine in C++, implements ZMTP/3.1
Stars: ✭ 7,418 (+8142.22%)
Mutual labels:  network, networking
Hp Socket
High Performance TCP/UDP/HTTP Communication Component
Stars: ✭ 4,420 (+4811.11%)
Mutual labels:  network, networking
Gns3 Server
GNS3 server
Stars: ✭ 477 (+430%)
Mutual labels:  network, networking
Ineter
Fast Java library for working with IP addresses, ranges, and subnets
Stars: ✭ 39 (-56.67%)
Mutual labels:  network, networking
Macfinder
An iOS Library that helps you find the MAC Address of a specific IP
Stars: ✭ 57 (-36.67%)
Mutual labels:  network, networking

STNetTaskQueue CI Status Version License

STNetTaskQueue is a networking queue library for iOS and OS X. It's abstract and can be implemented in different protocols.

STNetTaskQueue avoid you from directly dealing with "url", "request packing" and "response parsing". All networking tasks are described and processed by subclassing STNetTask, which provides you a clean code style in UI layer when handling networking.

Features

  • Auto packing parameters for HTTP net task.
  • Max concurrent tasks count in each STNetTaskQueue.
  • Max retry count for each STNetTask.
  • Net task is cancelable after added to STNetTaskQueue.
  • Multiple delegates for same net task.
  • Works with ReactiveCocoa, subscribeCompleted for net task result.

STHTTPNetTaskQueueHandler

STHTTPNetTaskQueueHandler is a HTTP based implementation of STNetTaskQueueHandler. It provides different ways to pack request and parse response, e.g. STHTTPNetTaskRequestJSON is for JSON format request body, STHTTPNetTaskResponseJSON is for JSON format response data and STHTTPNetTaskRequestFormData is for form data format request body which is mostly used for uploading file.

STNetTask

STNetTask is abstract, it provides basic properties and callbacks for subclassing.

STNetTaskDelegate

STNetTaskDelegate is the delegate protocol for observing result of STNetTask, mostly it is used in view controller.

STNetTaskChain (Deprecated. Use STNetTaskGroup instead)

STNetTaskChain is a chain which processes an array of STNetTask serially. A net task chain is considered as successful only if all net tasks in the chain are end without error.

STNetTaskGroup

A net task group for executing net tasks serially or concurrently.

Get Started

Podfile

platform :ios, '7.0'
pod 'STNetTaskQueue'

Carthage

github "kevin0571/STNetTaskQueue"

Use STNetTaskQueue in your project

Step 1: Setup STNetTaskQueue after your app launch

NSURL *baseUrl = [NSURL URLWithString:@"http://jsonplaceholder.typicode.com"];
STHTTPNetTaskQueueHandler *httpHandler = [[STHTTPNetTaskQueueHandler alloc] initWithBaseURL:baseUrl];
[STNetTaskQueue sharedQueue].handler = httpHandler;

Step 2: Create your net task

@interface STTestPostNetTask : STHTTPNetTask

@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *body;
@property (nonatomic, strong) NSDate *date;
@property (nonatomic, assign) int userId;
@property (nonatomic, strong) NSString<STIgnore> *ignored; // This property is ignored when packing the request.
@property (nonatomic, strong, readonly) NSDictionary *post;

@end
@implementation STTestPostNetTask

- (STHTTPNetTaskMethod)method
{
    return STHTTPNetTaskPost;
}

- (NSString *)uri
{
    return @"posts";
}

// Optional. Retry 3 times after error occurs.
- (NSUInteger)maxRetryCount
{
    return 3;
}

// Optional. Retry for all types of errors
- (BOOL)shouldRetryForError:(NSError *)error
{
    return YES;
}

// Optional. Retry after 5 seconds.
- (NSTimeInterval)retryInterval
{
    return 5;
}

// Optional. Custom headers.
- (NSDictionary *)headers
{
    return @{ @"custom_header": @"value" };
}

// Optional. Add parameters which are not inclued in requestObject and net task properties.
- (NSDictionary *)parameters
{
    return @{ @"other_parameter": @"value" };
}

// Optional. Transform value to a format you want.
- (id)transformValue:(id)value
{
    if ([value isKindOfClass:[NSDate class]]) {
        return @([value timeIntervalSince1970]);
    }
    return value;
}

- (void)didResponseDictionary:(NSDictionary *)dictionary
{
    _post = dictionary;
}

@end

Step 3: Send net task and delegate for the result

STTestPostNetTask *testPostTask = [STTestPostNetTask new];
testPostTask.title = @"Test Post Net Task Title";
testPostTask.body = @"Test Post Net Task Body";
testPostTask.userId = 1;
testPostTask.date = [NSDate new];
testPostTask.ignored = @"test";
[[STNetTaskQueue sharedQueue] addTaskDelegate:self uri:testPostTask.uri];
[[STNetTaskQueue sharedQueue] addTask:testPostTask];

// The net task will be sent as described below.
/*
    URI: posts
    Method: POST
    Request Type: Key-Value String
    Response Type: JSON
    Custom Headers:
    {
        "custom_header" = value;
    }
    Parameters:
    {
        body = "Test Post Net Task Body";
        date = "1452239110.829915";
        "other_parameter" = value;
        title = "Test Post Net Task Title";
        "user_id" = 1;
    }
 */

Use subscription block

[testPostTask subscribeState:STNetTaskStateFinished usingBlock:^{
    if (testPostTask.error) {
        // Handle error cases
        return;
    }
    // Access result from net task
}];

Use STNetTaskDelegate

- (void)netTaskDidEnd:(STNetTask *)task
{
    if (task.error) {
        // Handle error cases
        return;
    }
    // Access result from net task
}

Work with ReactiveCocoa for getting net task result

[STNetTaskObserve(testPostTask) subscribeCompleted:^(
    if (testPostTask.error) {
        // Handle error cases
        return;
    }
    // Access result from net task
}];

For more details, check out unit tests.

Set max concurrent tasks count of STNetTaskQueue

Sometimes we need to set the concurrent image download tasks to avoid too much data coming at the same time.

STNetTaskQueue *downloadQueue = [STNetTaskQueue new];
downloadQueue.handler = [[STHTTPNetTaskQueueHandler alloc] initWithBaseURL:[NSURL URLWithString:@"http://example.com"]];
downloadQueue.maxConcurrentTasksCount = 2;
/*
[downloadQueue addTask:task1];
[downloadQueue addTask:task2];
[downloadQueue addTask:task3]; // task3 will be sent after task1 or task2 is finished.
*/

Use STNetTaskGroup to execute multiple net tasks

STNetTaskGroup supports two modes: STNetTaskGroupModeSerial and STNetTaskGroupConcurrent. STNetTaskGroupModeSerial will execute a net task after the previous net task is finished. STNetTaskGroupModeConcurrent will execute all net tasks concurrently.

STTestGetNetTask *task1 = [STTestGetNetTask new];
task1.id = 1;
    
STTestGetNetTask *task2 = [STTestGetNetTask new];
task2.id = 2;
    
STNetTaskGroup *group = [[STNetTaskGroup alloc] initWithTasks:@[ task1, task2 ] mode:STNetTaskGroupModeSerial];
[group subscribeState:STNetTaskGroupStateFinished usingBlock:^(STNetTaskGroup *group, NSError *error) {
    if (error) {
        // One of the net task is failed.
        return;
    }
    // All net tasks are finished without error.
}];
[group start];

Or a handy way:

STTestGetNetTask *task1 = [STTestGetNetTask new];
task1.id = 1;
    
STTestGetNetTask *task2 = [STTestGetNetTask new];
task2.id = 2;

[[[@[ task1, task2 ] serialNetTaskGroup] subscribeState:STNetTaskGroupStateFinished usingBlock:^(STNetTaskGroup *group, NSError *error) {
    if (error) {
        // One of the net task is failed.
        return;
    }
    // All net tasks are finished without error.
}] start];
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].