Network framework Analysis-all the routines

Source: Internet
Author: User

Objective

These days take time to chew the volley and Picasso source, harvest a lot, so here to share with you.

For the Web request framework or the picture loading framework, our ideal type should be broadly like this:

    • Simple: the appearance of the framework is, of course, to improve our development efficiency, so that our development is simple, so in the case of quality assurance is the first place
    • Configurable: There are no identical two leaves in the sky, nor exactly the same two items, so some differences should be configurable, such as cache location, cache size, cache policy, etc.
    • Ease of expansion: The framework is designed to take into account changes and encapsulate them. For example, with a better HTTP client, we should be able to easily modify it and not have too much impact on our previous code.

But original aim, these frameworks are basically the same skeleton, and today we're going to talk about the routines in these frameworks.

Basic modules

Since we're saying that these frameworks are basically the same structure, let's look at a similar module structure.

The whole process is probably this:

Client request, generate framework encapsulated request type, scheduler start processing task, call data acquisition module, processing of acquired data, callback to client

Producer Consumer Model

The request management and task scheduling module in the framework typically uses the producer consumer model.

Why there is a producer consumer model

In the world of threads, the producer is the thread of production data, and the consumer is the thread of consumption data. in multithreaded development, producers have to wait for the consumer to continue producing data if the producer is processing fast and the consumer processing is slow. Similarly, consumers must wait for producers if their processing power is greater than that of producers. To solve this problem, the producer and consumer models were introduced.

What is the producer consumer model

The producer-consumer model solves the problem of strong coupling between producers and consumers through a container. producers and consumers do not communicate with each other directly, and through the blocking queue to communicate, so producers do not have to wait for consumer processing after the production of data, directly to the blocking queue, consumers do not find producers to data, but directly from the blocking queue, blocking the queue is equivalent to a buffers , balancing the processing power of producers and consumers.

Usage scenarios for producer consumer models

the thread pool class in Java is actually a way of implementing producer and consumer patterns, but the implementation is more sophisticated. The producer throws the task to the thread pool, the thread pool creates the threads and processes the task, and if the number of tasks to be run is greater than the number of basic threads in the thread pool, throwing the task into the block queue is obviously a lot smarter than using only one blocking queue to implement the producer and consumer models . Because consumers are able to deal with it directly, it is faster, and producers first, and the consumer is obviously slower to take this way.

Applications in the framework

For the above usage scenarios, we can find the implementation in the framework respectively.

The implementation of volley source code is to implement the producer consumer model with a priority blocking queue. The producer is the thread that adds data to the queue, and the consumer is a default 4-element thread array (not including the thread that handles the cache) to keep the message processing out.

and Picssso is a typical producer consumer model for threading pool implementations , so there's not much to be introduced here.

The data structures used by both frameworks are Priorityblockingqueue (priority blocking queues), which are intended to be sorted to ensure that high-priority requests are processed first.

By the way, the message processing mechanism of Android is actually a producer consumer model.

A small problem

The blogger then came up with a small problem: what was the order of awakening when the consumer was awakened?

Here comes a concept called the Fair Access Queue , the so-called Fair access queue refers to all blocked producer threads or consumer threads, when the queue is available, you can access the queue in order of blocking , that is, the first blocked producer thread, you can first insert elements into the queue, The first blocking consumer thread, you can get the elements from the queue first. in general, the throughput is reduced to ensure fairness.

Cache

The android cache is divided into memory cache and file cache (disk cache).

The General network framework does not need to handle the memory cache, but the picture loading framework is required. after Android3.1, Android introduced LruCache , the memory cache class, where objects in LruCache are strongly referenced. The Picasso memory cache is implemented using the LRUCache. For disk caching, one solution that Google offers is to use Disklrucache(Disklrucache is not integrated into the Android source code, as explained in the Android Doc example). The disk cache for Picasso is okhttp based and uses Disklrucache. The volley disk cache is implemented in Diskbasedcache and is based on the LRU algorithm.

For other concepts such as cache algorithm, cache hit ratio, etc. I won't do much about it here.

Asynchronous processing

We know that Android is a single-threaded model, and we should avoid time-consuming operations in the UI thread, which is a more typical time-consuming operation, so the network-related framework will encapsulate some of the asynchronous operations.

In fact, there is nothing complicated here, is simply the use of handler for inter-thread communication , and then with the callback mechanism , the results are returned to the main thread. Here you can refer to my previous article Android Handler message mechanism (in doubt) and when the Observer mode and callback mechanism encounter Android source code.

We take volley as an example to see briefly that the Executordelivery class is responsible for distributing responses data or error messages generated by the child threads. Initialization is in the Requestqueue class.

publicRequestQueueint threadPoolSize) {        this(cache, network, threadPoolSize,                new ExecutorDelivery(new Handler(Looper.getMainLooper())));    }

Here is the main thread of the handler object, and this Executordelivery object will be passed into Networkdispatcher and Cachedispatcher, these two classes are inherited from thread, is responsible for processing requests in the queue. So the operation that handles the request is occurring on the child thread.

Then we look at the construction of the Executordelivery class.

publicExecutorDelivery(final Handler handler) {        // Make an Executor that just wraps the handler.        new Executor() {            @Override            publicvoidexecute(Runnable command) {                handler.post(command);            }        };    }

Here, a layer of handler is packaged with executor. The responses data or error messages in the volley are sent through executor, so that the message is in the main thread.

Picasso is a little more complicated than volley, which is a time-consuming operation for images to be transformed by Picasso, so the distribution of requests in Picasso and the processing of results are placed in a single thread. This thread is a thread with Message Queuing that performs a cyclic task , which is to process the acquired data. When the results are processed, the results are sent back to the main thread for display, handler the main thread.

Design Patterns

A good framework makes use of design patterns to make code easy to extend and later to maintain. Here are some design patterns that appear to be relatively high frequency.

    • Static Factory Method: A factory object determines which instance of the product class is created
    • Singleton mode: Ensure that only one object is created
    • Builder mode: Separates the construction of a complex object from its representation so that the same build process can create different representations
    • Appearance mode: Simplifies the interface of a group of classes
    • Command mode: Encapsulates a request to become an object
    • Policy mode: Encapsulates the behavior that can be mutually selectable and uses a delegate to decide which one to use
Frame entry

The general framework, in order to invoke brevity, does not allow the client to instantiate a portal object directly from new. The creation mode is needed here.

Volley's entrance uses a static factory approach , similar to bitmap in Android source code, which can be used to refer to the static factory method in Android source code.

/**  * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.  *  * @param context A {@link Context} to use for creating the cache dir.  * @return A started {@link RequestQueue} instance.  */publicstaticnewRequestQueue(Context context) {  returnnull);}

Picasso's entry method uses a double-lock single- case mode .

staticvolatilenull;publicstaticwith(Context context) {    ifnull) {      synchronized (Picasso.class) {        ifnull) {          new Builder(context).build();        }      }    }    return singleton;  }

The Picasso also uses the builder mode because there are too many configurable items.

At the same time, some frameworks, in order to provide a concise API to the client, will use the appearance pattern to define a high-level interface, making each module in the framework easier to use. The appearance mode is a structural pattern .

The appearance mode can refer to the appearance mode in the Android source code

Command mode

The definition of a command pattern is to encapsulate a request as an object, allowing you to parameterize the customer with different requests, queue up requests or log requests, and support revocable operations. In the framework of the network request, the request is packaged into an object for easy delivery and use. For example, the request and action in Request,picasso in volley.

Command mode can refer to the command mode in Android source code

Policy mode

The strategy pattern is also a pattern that most frameworks use to encapsulate the behavior that can be mutually selectable and use a delegate to decide which one to use.

The programming idea of interface- oriented programming is used extensively in volley. Here we look at the entrance method of volley

 Public StaticRequestqueueNewrequestqueue(context context, Httpstack stack,intMaxdiskcachebytes) {//~ Omit part-independent code ~  if(Stack = =NULL) {if(Build.VERSION.SDK_INT >=9) {stack =NewHurlstack (); }Else{//Prior to Gingerbread, HttpURLConnection was unreliable.      //See:http://android-developers.blogspot.com/2011/09/androids-http-clients.htmlstack =NewHttpclientstack (Androidhttpclient.newinstance (useragent)); }} Network Network =NewBasicnetwork (stack);//~ Omit part-independent code ~}

Different HTTP clients are selected based on the API version, which implements a common interface

/** * An HTTP stack abstraction. * * Public  interface httpstack {  /** * Performs an HTTP request with the given parameters. * * <p>a GET request is sent if request.getpostbody () = = null.     A POST request is sent otherwise, * and the Content-type header is set to Request.getpostbodycontenttype () .</p> * * @param request the request to perform * @param additionalheaders additional headers to B E sent together with * {@link request#getheaders ()} * @return The HTTP response */   PublicHttpResponseperformrequest(Request<?> Request, map<string, string> additionalheaders)throwsIOException, Authfailureerror;}

Of course we can implement this interface ourselves, and then replace the HTTP client with Okhttp.

Postscript

Network-related framework routines are basically these, specific details we can go to their own view of the relevant source code. If there is any imperfect or wrong place also ask you to give more advice.

Network framework Analysis-all the routines

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.