Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 4 Classes for Fiber-UDP Fiber-DNS and Fiber-Executors #216

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.proxy4o.utils;

/**
* Created by linkerlin on 7/30/16.
*/
public interface FiberCompletion<T extends Object, E extends Exception> {
void success(T result);

void failure(E exception);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.proxy4o.utils;

import co.paralleluniverse.fibers.FiberAsync;
import co.paralleluniverse.fibers.SuspendExecution;
import co.paralleluniverse.fibers.Suspendable;

import java.net.UnknownHostException;
import java.util.concurrent.*;

/**
* Created by linkerlin on 7/30/16.
*/
public class FiberExecutors<T extends Object, E extends Exception> extends FiberAsync<T, E> implements FiberCompletion<T, E> {

private static ExecutorService es = Executors.newCachedThreadPool();

private final Callable<T> callable;

public FiberExecutors(Callable<T> callable) {
this.callable = callable;
}

@Suspendable // without a checked Exception
public static <T extends Object, E extends Exception> T fiberSubmit(Callable<T> callable) throws E, InterruptedException, SuspendExecution {
return (new FiberExecutors<T,E>(callable)).run();
}

@Suspendable // with a checked Exception
public static <T extends Object, E extends Exception> T fiberSubmit(Callable<T> callable, Class<E> throwE) throws E, InterruptedException, SuspendExecution {
return (new FiberExecutors<T,E>(callable)).run();
}

@Suspendable
public static <T extends Object, E extends Exception> T fiberSubmit(final Callable<T> callable, Class<E> throwE, final long timeout, final TimeUnit unit)
throws E, ExecutionException, TimeoutException, InterruptedException, SuspendExecution {
return (new FiberExecutors<T,E>(new Callable<T>() {
@Override
public T call() throws E, ExecutionException, TimeoutException, ExecutionException, InterruptedException {
return es.submit(new Callable<T>() {
@Override
public T call() throws Exception {
return callable.call();
}
}).get(timeout,unit);
}
})).run();
}

@Suspendable
@Override
protected void requestAsync() {
es.submit(new Callable<Object>() {
@Override
public Object call() throws E {
try {
success(callable.call());
} catch (Exception e) {
failure((E) e);
}
return null;
}
});
}

@Suspendable
@Override
public void success(T result) {
asyncCompleted(result);
}

@Suspendable
@Override
public void failure(E exception) {
asyncFailed(exception);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.proxy4o.fiber.dns;

import co.paralleluniverse.fibers.FiberAsync;
import co.paralleluniverse.fibers.SuspendExecution;
import co.paralleluniverse.fibers.Suspendable;
import com.proxy4o.utils.FiberExecutors;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;

/**
* Created by linkerlin on 6/1/16.
*/
public class FiberDNS {

@Suspendable
public static InetAddress doResolve(final String domain) throws SuspendExecution, InterruptedException, UnknownHostException {
return FiberExecutors.fiberSubmit(new Callable<InetAddress>() {
@Override
public InetAddress call() throws UnknownHostException {

InetAddress ret = InetAddress.getByName(domain);
return ret;
}
}, UnknownHostException.class);
}



}