use of rx.functions.Action1 in project xDrip-plus by jamorham.
the class ReadDataShare method readSerialNumber.
public void readSerialNumber(final Action1<String> serialNumberListener) {
final Action1<byte[]> manufacturingDataListener = new Action1<byte[]>() {
@Override
public void call(byte[] s) {
Element el = ParsePage(s, Constants.RECORD_TYPES.MANUFACTURING_DATA.ordinal());
Observable.just(el.getAttribute("SerialNumber")).subscribe(serialNumberListener);
}
};
readDataBasePage(Constants.RECORD_TYPES.MANUFACTURING_DATA.ordinal(), 0, manufacturingDataListener);
}
use of rx.functions.Action1 in project xDrip-plus by jamorham.
the class ReadDataShare method readDataBasePage.
private <T> T readDataBasePage(final int recordType, int page, final Action1<byte[]> fullPageListener) {
byte numOfPages = 1;
if (page < 0) {
throw new IllegalArgumentException("Invalid page requested:" + page);
}
ArrayList<Byte> payload = new ArrayList<Byte>();
payload.add((byte) recordType);
byte[] pageInt = ByteBuffer.allocate(4).putInt(page).array();
payload.add(pageInt[3]);
payload.add(pageInt[2]);
payload.add(pageInt[1]);
payload.add(pageInt[0]);
payload.add(numOfPages);
accumulatedResponse = null;
final Action1<byte[]> databasePageReader = new Action1<byte[]>() {
@Override
public void call(byte[] s) {
Log.d("ReadDataShare", "Database Page Reader received SIZE: " + s.length);
byte[] temp = s;
if (accumulatedResponse == null) {
accumulatedResponse = s;
} else {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(accumulatedResponse);
outputStream.write(temp);
accumulatedResponse = outputStream.toByteArray();
Log.d("ReadDataShare", "Combined Response length: " + accumulatedResponse.length);
} catch (Exception e) {
e.printStackTrace();
}
}
if (temp.length < 20) {
Observable.just(accumulatedResponse).subscribe(fullPageListener).unsubscribe();
}
}
};
writeCommand(Constants.READ_DATABASE_PAGES, payload, databasePageReader);
return null;
}
use of rx.functions.Action1 in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method processCreateUpdateNicRequest.
/**
* Processes request for creating and updating Network interface resources.
*/
private void processCreateUpdateNicRequest(NetworkInterfaceState nic, NetworkInterfaceInner remoteNic, EnumerationContext ctx, List<DeferredResult<NetworkInterfaceState>> ops, Map<String, String> subnetPerNicId, boolean isCreate) {
nic.name = remoteNic.name();
nic.subnetLink = subnetPerNicId.get(remoteNic.id());
NicMetadata nicMeta = new NicMetadata();
nicMeta.state = nic;
nicMeta.macAddress = remoteNic.macAddress();
// else will default to original ID for PATCH requests
if (isCreate) {
nic.id = remoteNic.id();
nic.endpointLink = ctx.request.endpointLink;
AdapterUtils.addToEndpointLinks(nic, ctx.request.endpointLink);
nic.tenantLinks = ctx.parentCompute.tenantLinks;
nic.regionId = remoteNic.location();
nic.computeHostLink = ctx.parentCompute.documentSelfLink;
} else {
if (StringUtils.isEmpty(nic.endpointLink)) {
nic.endpointLink = ctx.request.endpointLink;
}
nic.endpointLinks.add(ctx.request.endpointLink);
}
List<NetworkInterfaceIPConfigurationInner> ipConfigurations = remoteNic.ipConfigurations();
if (ipConfigurations == null || ipConfigurations.isEmpty()) {
executeNicCreateUpdateRequest(nic, remoteNic, ctx, ops, nicMeta, isCreate);
return;
}
NetworkInterfaceIPConfigurationInner nicIPConf = ipConfigurations.get(0);
nic.address = nicIPConf.privateIPAddress();
if (nicIPConf.publicIPAddress() == null) {
executeNicCreateUpdateRequest(nic, remoteNic, ctx, ops, nicMeta, isCreate);
return;
}
// IP address is not directly available in NetworkInterfaceIPConfigurationInner.
// It is available as a SubResource, We use the SubResource ID of IP address from
// NetworkInterfaceIPConfigurationInner to obtain the IP address.
Consumer<Throwable> failure = e -> {
logWarning("Error getting public IP address from Azure [endpointLink:%s], [Exception:%s]", ctx.request.endpointLink, e.getMessage());
handleError(ctx, e);
};
PhotonModelUtils.runInExecutor(this.executorService, () -> {
Azure azure = ctx.azureSdkClients.getAzureClient();
azure.publicIPAddresses().getByIdAsync(nicIPConf.publicIPAddress().id()).subscribe(injectOperationContext(new Action1<PublicIPAddress>() {
@Override
public void call(PublicIPAddress publicIPAddress) {
nicMeta.publicIp = publicIPAddress.ipAddress();
if (publicIPAddress.inner().dnsSettings() != null) {
nicMeta.publicDnsName = publicIPAddress.inner().dnsSettings().fqdn();
}
executeNicCreateUpdateRequest(nic, remoteNic, ctx, ops, nicMeta, isCreate);
}
}));
}, failure);
}
use of rx.functions.Action1 in project jocean-http by isdom.
the class MessageUtil method interact.
public static Interact interact(final HttpClient client) {
final InitiatorBuilder _initiatorBuilder = client.initiator();
final AtomicBoolean _isSSLEnabled = new AtomicBoolean(false);
final AtomicReference<Observable<Object>> _obsreqRef = new AtomicReference<>(fullRequestWithoutBody(HttpVersion.HTTP_1_1, HttpMethod.GET));
final List<String> _nvs = new ArrayList<>();
final AtomicReference<URI> _uriRef = new AtomicReference<>();
return new Interact() {
private void updateObsRequest(final Action1<Object> action) {
_obsreqRef.set(_obsreqRef.get().doOnNext(action));
}
private void addQueryParams() {
if (!_nvs.isEmpty()) {
updateObsRequest(MessageUtil.addQueryParam(_nvs.toArray(new String[0])));
}
}
private void extractUriWithHost(final Object... reqbeans) {
if (null == _uriRef.get()) {
for (Object bean : reqbeans) {
try {
final Path path = bean.getClass().getAnnotation(Path.class);
if (null != path) {
final URI uri = new URI(path.value());
if (null != uri.getHost()) {
uri(path.value());
return;
}
}
} catch (Exception e) {
LOG.warn("exception when extract uri from bean {}, detail: {}", bean, ExceptionUtils.exception2detail(e));
}
}
}
}
private void checkAddr() {
if (null == _uriRef.get()) {
throw new RuntimeException("remote address not set.");
}
}
private InitiatorBuilder addSSLFeatureIfNeed(final InitiatorBuilder builder) {
if (_isSSLEnabled.get()) {
return builder;
} else if ("https".equals(_uriRef.get().getScheme())) {
return builder.feature(F_SSL);
} else {
return builder;
}
}
@Override
public Interact method(final HttpMethod method) {
updateObsRequest(MessageUtil.setMethod(method));
return this;
}
@Override
public Interact uri(final String uriAsString) {
try {
final URI uri = new URI(uriAsString);
_uriRef.set(uri);
_initiatorBuilder.remoteAddress(uri2addr(uri));
updateObsRequest(MessageUtil.setHost(uri));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
return this;
}
@Override
public Interact path(final String path) {
updateObsRequest(MessageUtil.setPath(path));
return this;
}
@Override
public Interact paramAsQuery(final String name, final String value) {
_nvs.add(name);
_nvs.add(value);
return this;
}
@Override
public Interact reqbean(final Object... reqbeans) {
updateObsRequest(MessageUtil.toRequest(reqbeans));
extractUriWithHost(reqbeans);
return this;
}
@Override
public Interact body(final Observable<? extends MessageBody> body) {
_obsreqRef.set(_obsreqRef.get().compose(MessageUtil.addBody(body)));
return this;
}
@Override
public Interact body(final Object bean, final ContentEncoder contentEncoder) {
return body(toBody(bean, contentEncoder.contentType(), contentEncoder.encoder()));
}
// @Override
// public Interact disposeBodyOnTerminate(final boolean doDispose) {
// _doDisposeBody.set(doDispose);
// return this;
// }
@Override
public Interact onrequest(final Action1<Object> action) {
updateObsRequest(action);
return this;
}
@Override
public Interact feature(final Feature... features) {
_initiatorBuilder.feature(features);
if (isSSLEnabled(features)) {
_isSSLEnabled.set(true);
}
return this;
}
private boolean isSSLEnabled(final Feature... features) {
for (Feature f : features) {
if (f instanceof Feature.ENABLE_SSL) {
return true;
}
}
return false;
}
private Observable<? extends Object> hookDisposeBody(final Observable<Object> obsreq, final HttpInitiator initiator) {
return obsreq.doOnNext(DisposableWrapperUtil.disposeOnForAny(initiator));
}
private Observable<? extends DisposableWrapper<HttpObject>> defineInteraction(final HttpInitiator initiator) {
return initiator.defineInteraction(hookDisposeBody(_obsreqRef.get(), initiator));
}
@Override
public Observable<? extends Interaction> execution() {
checkAddr();
addQueryParams();
return addSSLFeatureIfNeed(_initiatorBuilder).build().map(new Func1<HttpInitiator, Interaction>() {
@Override
public Interaction call(final HttpInitiator initiator) {
final Observable<? extends DisposableWrapper<HttpObject>> interaction = defineInteraction(initiator);
return new Interaction() {
@Override
public HttpInitiator initiator() {
return initiator;
}
@Override
public Observable<? extends DisposableWrapper<HttpObject>> execute() {
return interaction;
}
};
}
});
}
};
}
use of rx.functions.Action1 in project jocean-http by isdom.
the class MessageUtil method addQueryParam.
public static Action1<Object> addQueryParam(final String... nvs) {
return new Action1<Object>() {
@Override
public void call(final Object obj) {
if (obj instanceof HttpRequest) {
final HttpRequest request = (HttpRequest) obj;
if (nvs.length > 0) {
final QueryStringEncoder encoder = new QueryStringEncoder(request.uri());
int idx = 0;
while (idx + 1 < nvs.length) {
encoder.addParam(nvs[idx], nvs[idx + 1]);
idx += 2;
}
request.setUri(encoder.toString());
}
}
}
};
}
Aggregations