text:
package org.onosproject.store.flow.impl;

import java.security.SecureRandom;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.collect.Streams;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.onlab.util.KryoNamespace;
import org.onlab.util.Tools;
import org.onosproject.cluster.ClusterService;
import org.onosproject.cluster.NodeId;
import org.onosproject.core.CoreService;
import org.onosproject.core.IdGenerator;
import org.onosproject.mastership.MastershipService;
import org.onosproject.net.DeviceId;
import org.onosproject.net.device.DeviceService;
import org.onosproject.net.flow.CompletedBatchOperation;
import org.onosproject.net.flow.DefaultFlowEntry;
import org.onosproject.net.flow.FlowEntry;
import org.onosproject.net.flow.FlowEntry.FlowEntryState;
import org.onosproject.net.flow.FlowId;
import org.onosproject.net.flow.FlowRule;
import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry;
import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry.FlowRuleOperation;
import org.onosproject.net.flow.oldbatch.FlowRuleBatchEvent;
import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation;
import org.onosproject.net.flow.oldbatch.FlowRuleBatchRequest;
import org.onosproject.net.flow.FlowRuleEvent;
import org.onosproject.net.flow.FlowRuleEvent.Type;
import org.onosproject.net.flow.FlowRuleService;
import org.onosproject.net.flow.FlowRuleStore;
import org.onosproject.net.flow.FlowRuleStoreDelegate;
import org.onosproject.net.flow.StoredFlowEntry;
import org.onosproject.net.flow.TableStatisticsEntry;
import org.onosproject.store.AbstractStore;
import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
import org.onosproject.store.cluster.messaging.MessageSubject;
import org.onosproject.store.impl.MastershipBasedTimestamp;
import org.onosproject.store.serializers.KryoNamespaces;
import org.onosproject.store.service.AsyncDocumentTree;
import org.onosproject.store.service.DocumentPath;
import org.onosproject.store.service.DocumentTree;
import org.onosproject.store.service.EventuallyConsistentMap;
import org.onosproject.store.service.EventuallyConsistentMapEvent;
import org.onosproject.store.service.EventuallyConsistentMapListener;
import org.onosproject.store.service.IllegalDocumentModificationException;
import org.onosproject.store.service.NoSuchDocumentPathException;
import org.onosproject.store.service.Serializer;
import org.onosproject.store.service.StorageException;
import org.onosproject.store.service.StorageService;
import org.onosproject.store.service.Versioned;
import org.onosproject.store.service.WallClockTimestamp;
import org.slf4j.Logger;

import static org.onlab.util.Tools.groupedThreads;
import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_UPDATED;
import static org.slf4j.LoggerFactory.getLogger;

/**
 * Manages inventory of flow rules using a distributed state management protocol.
 */
@Component(immediate = true)
@Service
public class DistributedFlowRuleStore
        extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate>
        implements FlowRuleStore {

    private final Logger log = getLogger(getClass());

    // Constant exception used to indicate an atomic read-modify-write operation needs to be retried.
    // We don't want to populate a stack trace every time an optimistic lock is retried.
    private static final StorageException.ConcurrentModification RETRY;

    // Initialize retry exception with an empty stack trace.
    static {
        RETRY = new StorageException.ConcurrentModification();
        RETRY.setStackTrace(new StackTraceElement[0]);
    }

    private static final int SCHEDULED_THREAD_POOL_SIZE = 8;
    private static final int MESSAGE_HANDLER_THREAD_POOL_SIZE = 8;
    private static final int MAX_RETRY_DELAY_MILLIS = 50;

    private static final String FLOW_TABLE = "onos-flow-table";

    private static final MessageSubject APPLY_BATCH_FLOWS = new MessageSubject("onos-flow-apply");
    private static final MessageSubject COMPLETE_BATCH = new MessageSubject("onos-flow-batch-complete");

    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
    protected DeviceService deviceService;

    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
    protected CoreService coreService;

    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
    protected MastershipService mastershipService;

    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
    protected ClusterCommunicationService clusterCommunicator;

    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
    protected ClusterService clusterService;

    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
    protected StorageService storageService;

    protected final Serializer serializer = Serializer.using(KryoNamespaces.API);

    protected final KryoNamespace.Builder serializerBuilder = KryoNamespace.newBuilder()
            .register(KryoNamespaces.API)
            .register(MastershipBasedTimestamp.class);

    private EventuallyConsistentMap<DeviceId, List<TableStatisticsEntry>> deviceTableStats;
    private final EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>> tableStatsListener =
            new InternalTableStatsListener();

    private Set<Long> pendingBatches = Sets.newConcurrentHashSet();
    private ScheduledExecutorService scheduledExecutor;
    private ExecutorService messageHandlingExecutor;
    private final Random random = new SecureRandom();

    private AsyncDocumentTree<Map<StoredFlowEntry, StoredFlowEntry>> asyncFlows;
    private DocumentTree<Map<StoredFlowEntry, StoredFlowEntry>> flows;
    private IdGenerator idGenerator;
    private NodeId local;

    @Activate
    public void activate() {
        idGenerator = coreService.getIdGenerator(FlowRuleService.FLOW_OP_TOPIC);

        local = clusterService.getLocalNode().id();

        scheduledExecutor = Executors.newScheduledThreadPool(
                SCHEDULED_THREAD_POOL_SIZE,
                groupedThreads("onos/store/flow", "schedulers", log));

        messageHandlingExecutor = Executors.newFixedThreadPool(
                MESSAGE_HANDLER_THREAD_POOL_SIZE,
                groupedThreads("onos/store/flow", "message-handlers", log));

        deviceTableStats = storageService.<DeviceId, List<TableStatisticsEntry>>eventuallyConsistentMapBuilder()
                .withName("onos-flow-table-stats")
                .withSerializer(serializerBuilder)
                .withAntiEntropyPeriod(5, TimeUnit.SECONDS)
                .withTimestampProvider((k, v) -> new WallClockTimestamp())
                .withTombstonesDisabled()
                .build();
        deviceTableStats.addListener(tableStatsListener);

        asyncFlows = storageService.<Map<StoredFlowEntry, StoredFlowEntry>>documentTreeBuilder()
                .withName(FLOW_TABLE)
                .withSerializer(serializer)
                .buildDocumentTree();
        flows = asyncFlows.asDocumentTree();

        clusterCommunicator.addSubscriber(
                APPLY_BATCH_FLOWS,
                serializer::decode,
                this::applyBatchFlows,
                messageHandlingExecutor);
        clusterCommunicator.addSubscriber(
                COMPLETE_BATCH,
                serializer::decode,
                this::completeBatch,
                messageHandlingExecutor);

        log.info("Started");
    }

    @Deactivate
    public void deactivate() {
        deviceTableStats.removeListener(tableStatsListener);
        deviceTableStats.destroy();
        clusterCommunicator.removeSubscriber(APPLY_BATCH_FLOWS);
        clusterCommunicator.removeSubscriber(COMPLETE_BATCH);
        messageHandlingExecutor.shutdownNow();
        scheduledExecutor.shutdownNow();
        log.info("Stopped");
    }

    /**
     * Retries the given supplier until successful.
     *

     * This method retries the given supplier until no {@code ConcurrentModification} exceptions are thrown. In
     * between retries, it waits a semi-random interval to attempt to avoid transaction conflicts with other processes.
     *
     * @param supplier the supplier to retry
     * @param <T> the return type
     * @return the return value of the given supplier once it runs successfully
     */
    private <T> T retryUntilSuccess(Supplier<T> supplier) {
        return Tools.retryable(
                supplier,
                StorageException.ConcurrentModification.class,
                Integer.MAX_VALUE,
                MAX_RETRY_DELAY_MILLIS)
                .get();
    }

    /**
     * Retries the given asynchronous supplier until successful.
     *

     * This method retries the given supplier until no {@code ConcurrentModification} exceptions are thrown. In
     * between retries, it waits a semi-random interval to attempt to avoid transaction conflicts with other processes.
     *
     * @param supplier the supplier to retry
     * @param <T> the return type
     * @return the return value of the given supplier once it runs successfully
     */
    private <T> CompletableFuture<T> retryAsyncUntilSuccess(Supplier<CompletableFuture<T>> supplier) {
        return retryAsyncUntilSuccess(supplier, new CompletableFuture<>());
    }

    /**
     * Retries the given asynchronous supplier until successful.
     *

     * This method retries the given supplier until no {@code ConcurrentModification} exceptions are thrown. In
     * between retries, it waits a semi-random interval to attempt to avoid transaction conflicts with other processes.
     *
     * @param supplier the supplier to retry
     * @param future future to be completed once the operation has been successful
     * @param <T> the return type
     * @return the return value of the given supplier once it runs successfully
     */
    private <T> CompletableFuture<T> retryAsyncUntilSuccess(
            Supplier<CompletableFuture<T>> supplier,
            CompletableFuture<T> future) {
        supplier.get().whenComplete((result, error) -> {
            if (error == null) {
                future.complete(result);
            } else {
                Throwable cause = error.getCause() != null ? error.getCause() : error;
                if (cause instanceof StorageException.ConcurrentModification) {
                    scheduledExecutor.schedule(
                            () -> retryAsyncUntilSuccess(supplier, future),
                            random.nextInt(50),
                            TimeUnit.MILLISECONDS);
                } else {
                    future.completeExceptionally(error);
                }
            }
        });
        return future;
    }

    /**
     * Return method for {@link #retryUntilSuccess(Supplier)} callbacks to indicate that the callback needs to be
     * retried after a randomized delay.
     *
     * @param <T> the return type
     * @return nothing
     * @throws StorageException.ConcurrentModification to force a retry of the callback
     */
    private <T> T retry() {
        throw RETRY;
    }

    /**
     * Handles a completed batch event received from the master node.
     *

     * If this node is the source of the batch, notifies event listeners to complete the operations.
     *
     * @param event the event to handle
     */
    private void completeBatch(FlowRuleBatchEvent event) {
        if (pendingBatches.remove(event.subject().batchId())) {
            notifyDelegate(event);
        }
    }

    // This is not a efficient operation on a distributed sharded
    // flow store. We need to revisit the need for this operation or at least
    // make it device specific.
    @Override
    public int getFlowRuleCount() {
        return Streams.stream(deviceService.getDevices()).parallel()
                .mapToInt(device -> Iterables.size(getFlowEntries(device.id())))
                .sum();
    }

    /**
     * Returns the {@link DocumentPath} for the given {@link DeviceId}.
     *
     * @param deviceId the device identifier for which to return a path
     * @return the path for the given device
     */
    private DocumentPath getPathFor(DeviceId deviceId) {
        return DocumentPath.from("root", deviceId.toString());
    }

    /**
     * Returns the {@link DocumentPath} for the given {@link DeviceId} and {@link FlowId}.
     *
     * @param deviceId the device identifier for which to return the path
     * @param flowId the flow identifier for which to return the path
     * @return the path for the given device/flow
     */
    private DocumentPath getPathFor(DeviceId deviceId, FlowId flowId) {
        return DocumentPath.from("root", deviceId.toString(), flowId.toString());
    }

    @Override
    @SuppressWarnings("unchecked")
    public FlowEntry getFlowEntry(FlowRule rule) {
        DeviceId deviceId = rule.deviceId();
        if (mastershipService.getMasterFor(deviceId) != null) {
            DocumentPath path = getPathFor(deviceId, rule.id());
            Versioned<Map<StoredFlowEntry, StoredFlowEntry>> flowEntries = flows.get(path);
            return flowEntries != null ? flowEntries.value().get(rule) : null;
        } else {
            log.debug("Failed to getFlowEntries: No master for {}", deviceId);
            return null;
        }

    }

    @Override
    public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
        if (mastershipService.getMasterFor(deviceId) != null) {
            DocumentPath path = getPathFor(deviceId);
            try {
                return getFlowEntries(path);
            } catch (NoSuchDocumentPathException e) {
                return Collections.emptyList();
            }
        } else {
            log.debug("Failed to getFlowEntries: No master for {}", deviceId);
            return Collections.emptyList();
        }

    }

    @SuppressWarnings("unchecked")
    private Iterable<FlowEntry> getFlowEntries(DocumentPath path) {
        return flows.getChildren(path)
                .values()
                .stream()
                .flatMap(v -> v.value().values().stream())
                .collect(Collectors.toList());
    }

    @Override
    @SuppressWarnings("unchecked")
    public void storeFlowRule(FlowRule rule) {
        storeBatch(new FlowRuleBatchOperation(
                Collections.singletonList(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)),
                rule.deviceId(), idGenerator.getNewId()));
    }

    @Override
    public void storeBatch(FlowRuleBatchOperation operation) {
        if (operation.getOperations().isEmpty()) {
            notifyDelegate(FlowRuleBatchEvent.completed(
                    new FlowRuleBatchRequest(operation.id(), Collections.emptySet()),
                    new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId())));
            return;
        }

        DeviceId deviceId = operation.deviceId();
        NodeId master = mastershipService.getMasterFor(deviceId);

        if (master == null) {
            log.warn("No master for {} ", deviceId);

            updateStoreInternal(operation).whenComplete((result, error) -> {
                notifyDelegate(FlowRuleBatchEvent.completed(
                        new FlowRuleBatchRequest(operation.id(), Collections.emptySet()),
                        new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId())));
            });
            return;
        }

        pendingBatches.add(operation.id());

        // If the local node is the master, apply the flows. Otherwise, send them to the master.
        if (Objects.equals(local, master)) {
            applyBatchFlows(operation);
        } else {
            log.trace("Forwarding storeBatch to {}, which is the primary (master) for device {}", master, deviceId);
            clusterCommunicator.unicast(
                    operation,
                    APPLY_BATCH_FLOWS,
                    serializer::encode,
                    master);
        }
    }

    /**
     * Asynchronously applies a batch of flows to the store.
     *

     * This operation is performed on the master node to ensure that events occur after flows have been stored
     * and are visible to the master node. If a non-master node stores flows and then triggers events on the master,
     * the flows may not yet be visible to the master node due to the nature of sequentially consistent reads on the
     * underlying {@code DocumentTree} primitive.
     */
    private void applyBatchFlows(FlowRuleBatchOperation operation) {
        updateStoreInternal(operation).whenComplete((operations, error) -> {
            if (error == null) {
                if (operations.isEmpty()) {
                    batchOperationComplete(FlowRuleBatchEvent.completed(
                            new FlowRuleBatchRequest(operation.id(), Collections.emptySet()),
                            new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId())));
                } else {
                    notifyDelegate(FlowRuleBatchEvent.requested(
                            new FlowRuleBatchRequest(operation.id(), operations),
                            operation.deviceId()));
                }
            }
        });
    }

    private CompletableFuture<Set<FlowRuleBatchEntry>> updateStoreInternal(FlowRuleBatchOperation operation) {
        return Tools.allOf(operation.getOperations().stream().map(op -> {
            switch (op.operator()) {
                case ADD:
                case MODIFY:
                    return addBatchEntry(op).thenApply(succeeded -> succeeded ? op : null);
                case REMOVE:
                    return removeBatchEntry(op).thenApply(succeeded -> succeeded ? op : null);
                default:
                    log.warn("Unknown flow operation operator: {}", op.operator());
                    return CompletableFuture.<FlowRuleBatchEntry>completedFuture(null);
            }
        }).collect(Collectors.toList()))
                .thenApply(results -> results.stream()
                        .filter(Objects::nonNull)
                        .collect(Collectors.toSet()));
    }

    @SuppressWarnings("unchecked")
    private CompletableFuture<Boolean> addBatchEntry(FlowRuleBatchEntry batchEntry) {
        StoredFlowEntry entry = new DefaultFlowEntry(batchEntry.target());
        DocumentPath path = getPathFor(entry.deviceId(), entry.id());
        return retryAsyncUntilSuccess(() -> {
            CompletableFuture<Boolean> future = new CompletableFuture<>();
            asyncFlows.get(path).whenComplete((value, getError) -> {
                if (getError == null) {
                    if (value != null) {
                        Map<StoredFlowEntry, StoredFlowEntry> entries = Maps.newHashMap(value.value());
                        entries.put(entry, entry);
                        asyncFlows.replace(path, entries, value.version()).whenComplete((succeeded, replaceError) -> {
                            if (replaceError == null) {
                                if (succeeded) {
                                    log.trace("Stored new flow rule: {}", entry);
                                    future.complete(true);
                                } else {
                                    log.trace("Failed to store new flow rule: {}", entry);
                                    future.completeExceptionally(RETRY);
                                }
                            } else {
                                future.completeExceptionally(replaceError);
                            }
                        });
                    } else {
                        // If there are no entries stored for the device, initialize the device's flows.
                        Map<StoredFlowEntry, StoredFlowEntry> map = Maps.newHashMap();
                        map.put(entry, entry);
                        asyncFlows.createRecursive(path, map).whenComplete((succeeded, createError) -> {
                            if (createError == null) {
                                if (succeeded) {
                                    log.trace("Stored new flow rule: {}", entry);
                                    future.complete(true);
                                } else {
                                    log.trace("Failed to store new flow rule: {}", entry);
                                    future.completeExceptionally(RETRY);
                                }
                            } else {
                                future.completeExceptionally(createError);
                            }
                        });
                    }
                } else {
                    future.completeExceptionally(getError);
                }
            });
            return future;
        });
    }

    @SuppressWarnings("unchecked")
    private CompletableFuture<Boolean> removeBatchEntry(FlowRuleBatchEntry batchEntry) {
        FlowRule rule = batchEntry.target();
        DocumentPath path = getPathFor(rule.deviceId(), rule.id());
        return retryAsyncUntilSuccess(() -> {
            CompletableFuture<Boolean> future = new CompletableFuture<>();
            asyncFlows.get(path).whenComplete((value, getError) -> {
                if (getError == null) {
                    if (value != null) {
                        Map<StoredFlowEntry, StoredFlowEntry> entries = Maps.newHashMap(value.value());
                        StoredFlowEntry entry = entries.get(rule);
                        if (entry != null) {
                            entry.setState(FlowEntryState.PENDING_REMOVE);
                            asyncFlows.replace(path, entries, value.version()).whenComplete((succeeded, error) -> {
                                if (error == null) {
                                    if (succeeded) {
                                        log.trace("Updated flow rule state to PENDING_REMOVE: {}", entry);
                                        future.complete(true);
                                    } else {
                                        log.trace("Failed to update flow rule state to PENDING_REMOVE: {}", entry);
                                        future.completeExceptionally(RETRY);
                                    }
                                } else {
                                    future.completeExceptionally(error);
                                }
                            });
                        } else {
                            future.complete(false);
                        }
                    } else {
                        future.complete(false);
                    }
                } else {
                    future.completeExceptionally(getError);
                }
            });
            return future;
        });
    }

    @Override
    public void batchOperationComplete(FlowRuleBatchEvent event) {
        if (pendingBatches.remove(event.subject().batchId())) {
            notifyDelegate(event);
        } else {
            clusterCommunicator.broadcast(event, COMPLETE_BATCH, serializer::encode);
        }
    }

    @Override
    public void deleteFlowRule(FlowRule rule) {
        storeBatch(
                new FlowRuleBatchOperation(
                        Collections.singletonList(
                                new FlowRuleBatchEntry(
                                        FlowRuleOperation.REMOVE,
                                        rule)), rule.deviceId(), idGenerator.getNewId()));
    }

    @Override
    public FlowRuleEvent pendingFlowRule(FlowEntry rule) {
        DocumentPath path = getPathFor(rule.deviceId(), rule.id());
        return retryUntilSuccess(() -> {
            Versioned<Map<StoredFlowEntry, StoredFlowEntry>> value = flows.get(path);
            if (value != null) {
                Map<StoredFlowEntry, StoredFlowEntry> entries = Maps.newHashMap(value.value());
                StoredFlowEntry entry = entries.get(rule);
                if (entry != null && entry.state() != FlowEntryState.PENDING_ADD) {
                    entry.setState(FlowEntryState.PENDING_ADD);
                    if (flows.replace(path, entries, value.version())) {
                        log.trace("Updated flow rule state to PENDING_ADD: {}", entry);
                        return new FlowRuleEvent(RULE_UPDATED, rule);
                    } else {
                        log.trace("Failed to update flow rule state to PENDING_ADD: {}", entry);
                        return retry();
                    }
                } else {
                    return null;
                }
            } else {
                return null;
            }
        });
    }

    @Override
    @SuppressWarnings("unchecked")
    public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
        DocumentPath path = getPathFor(rule.deviceId(), rule.id());
        return retryUntilSuccess(() -> {
            Versioned<Map<StoredFlowEntry, StoredFlowEntry>> value = flows.get(path);
            if (value != null) {
                Map<StoredFlowEntry, StoredFlowEntry> entries = Maps.newHashMap(value.value());
                StoredFlowEntry entry = entries.get(rule);
                if (entry != null) {
                    FlowRuleEvent event;
                    String message;

                    entry.setBytes(rule.bytes());
                    entry.setLife(rule.life(TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS);
                    entry.setLiveType(rule.liveType());
                    entry.setPackets(rule.packets());
                    entry.setLastSeen();

                    // If the entry state is PENDING_ADD, set it to ADDED. Otherwise, just update the rule.
                    if (entry.state() == FlowEntryState.PENDING_ADD) {
                        entry.setState(FlowEntryState.ADDED);
                        event = new FlowRuleEvent(Type.RULE_ADDED, rule);
                        message = "Updated flow rule state to ADDED: {}";
                    } else {
                        event = new FlowRuleEvent(Type.RULE_UPDATED, rule);
                        message = "Updated flow rule: {}";
                    }

                    if (flows.replace(path, entries, value.version())) {
                        log.trace(message, entry);
                        return event;
                    } else {
                        log.trace("Failed to update flow rule: {}", entry);
                        return retry();
                    }
                } else {
                    // If the rule does not exist, return null. Inserting the rule risks race conditions
                    // that can result in removed rules being retained.
                    return null;
                }
            } else {
                return null;
            }
        });
    }

    @Override
    @SuppressWarnings("unchecked")
    public FlowRuleEvent removeFlowRule(FlowEntry rule) {
        DocumentPath path = getPathFor(rule.deviceId(), rule.id());
        return retryUntilSuccess(() -> {
            Versioned<Map<StoredFlowEntry, StoredFlowEntry>> value = flows.get(path);
            if (value != null) {
                Map<StoredFlowEntry, StoredFlowEntry> entries = Maps.newHashMap(value.value());
                StoredFlowEntry entry = entries.remove(rule);
                if (entry != null) {
                    if (flows.replace(path, entries, value.version())) {
                        log.trace("Removed flow rule: {}", entry);
                        return new FlowRuleEvent(RULE_REMOVED, entry);
                    } else {
                        log.trace("Failed to remove flow rule: {}", entry);
                        return retry();
                    }
                } else {
                    return null;
                }
            } else {
                return null;
            }
        });
    }

    @Override
    public void purgeFlowRule(DeviceId deviceId) {
        DocumentPath path = getPathFor(deviceId);
        retryUntilSuccess(() -> {
            try {
                for (String flowId : flows.getChildren(path).keySet()) {
                    flows.removeNode(DocumentPath.from("root", deviceId.toString(), flowId));
                }
            } catch (NoSuchDocumentPathException e) {
                // Do nothing. There are no flows for the device.
            }

            // New children may have been created since they were removed above. Catch
            // IllegalDocumentModificationException and retry if necessary.
            try {
                flows.removeNode(path);
            } catch (NoSuchDocumentPathException e) {
                return null;
            } catch (IllegalDocumentModificationException e) {
                return retry();
            }
            return null;
        });
    }

    @Override
    public void purgeFlowRules() {
        try {
            for (String deviceId : flows.getChildren(flows.root()).keySet()) {
                purgeFlowRule(DeviceId.deviceId(deviceId));
            }
        } catch (NoSuchDocumentPathException e) {
            // Do nothing if no children exist.
        }
    }

    @Override
    public FlowRuleEvent updateTableStatistics(DeviceId deviceId,
            List<TableStatisticsEntry> tableStats) {
        deviceTableStats.put(deviceId, tableStats);
        return null;
    }

    @Override
    public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) {
        if (mastershipService.getMasterFor(deviceId) != null) {
            List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId);
            if (tableStats == null) {
                return Collections.emptyList();
            }
            return ImmutableList.copyOf(tableStats);
        } else {
            log.debug("Failed to getTableStatistics: No master for {}", deviceId);
            return Collections.emptyList();
        }

    }

    @Override
    public long getActiveFlowRuleCount(DeviceId deviceId) {
        if (mastershipService.getMasterFor(deviceId) != null) {
            return Streams.stream(getTableStatistics(deviceId))
                    .mapToLong(TableStatisticsEntry::activeFlowEntries)
                    .sum();
        } else {
            log.debug("Failed to getActiveFlowRuleCount: No master for {}", deviceId);
            return 0;
        }
    }

    private class InternalTableStatsListener
            implements EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>> {
        @Override
        public void event(EventuallyConsistentMapEvent<DeviceId,
                List<TableStatisticsEntry>> event) {
            //TODO: Generate an event to listeners (do we need?)
        }
    }
}


text:
FactoryGirl.define do
  factory :tag, class: Wordpress::PostTag do
    transient do
      name "NAME"
    end
    description { "Describes #{name}" }
    term_id { create(:term, name: name).id }
  end
end

redpajama_set_name: RedPajamaGithub


text:
<?php

namespace Twitter;

interface TwitterObject extends TwitterSerializable
{
}


text: This is a project to show how the boku-framework by Karim Kawambwa is used


text:
1. Fork this repository;
2. Clone the forked version of create-graphql:
  ```sh
  git clone git@github.com:<your_username>/create-graphql.git
  ```

3. Install [lerna/lerna](https://github.com/lerna/lerna)
  ```sh
  yarn global add lerna@prerelease # With NPM: `npm install --global lerna@prerelease`
  ```

4. Install the main package dependencies
  ```sh
  yarn # With NPM: `npm install`
  ```

5. Bootstrap all packages
  ```sh
  lerna bootstrap
  ```
  This will install all dependencies of all `subpackages` and link them properly

6. Link the `generator` package
  ```sh
  cd packages/generator && yarn link # With NPM: `npm link`
  ```

7. Watch all packages (create-graphql and generator)
  ```sh
  yarn watch # With NPM: `npm run watch`
  ```

8. Create a new branch
  ```sh
  git checkout -b feature/more_awesomeness
  ```

9. Make your changes
10. Run the CLI with your changes
  ```sh
  node packages/create-graphql/dist --help
  ```

11. Commit your changes and push your branch
  ```sh
  git add .
  git commit -m 'more awesome for create-graphql'
  git push origin feature/more_awesomeness
  ```

12. Open your Pull Request
13. Have your Pull Request merged! 😎


text:
<?php

namespace NilPortugues\SchemaOrg\Properties;

use NilPortugues\SchemaOrg\SchemaProperty;

/**
 * The base salary of the job or of an employee in an EmployeeRole.
 */
class BaseSalaryProperty extends SchemaProperty
{
    const SCHEMA_URL = 'http://schema.org/baseSalary';
    const PROPERTY_NAME = 'baseSalary';

    /**
     * A list of schemas allowed to use this property.
     *
     * @var array
     */
    protected static $allowedSchemas = [
        'http://schema.org/JobPosting',
        'http://schema.org/EmployeeRole',
    ];
}


text:
<?php

$session = session();

$_SESSION['item'];
$session->get('item');
$session->item;
session('item');


text:
<?php

/**
 * Class common\components\CurlRequest
 *
 */

namespace common\components;

class CurlRequest {

    /**
     * The Socket Timout limit in seconds
     *
     * @var     int
     * @access  protected
     *
     **/
    protected $_timeout = 30;

    /**
     * The User Agent to send
     *
     * @var     string
     * @access  protected
     *
     **/
    protected $_user_agent = 'RPC Client';

    /**
     * Should the request Fail if page gets a 400 or above status code?
     *
     * @var     bool
     * @access  protected
     *
     **/
    protected $_fail_on_error = true;

    /**
     * Should the request follow "Location" header redirects?
     *
     * @var     bool
     * @access  protected
     *
     **/
    protected $_follow_location_headers = true;

    /**
     * The last connection based error message
     *
     * @var     string
     * @access  protected
     *
     **/
    protected $_last_error = '';

    /**
     * The last connection stats
     *
     * @var     array
     * @access  protected
     *
     **/
    protected $_last_stats = null;

    /**
     * Constructor
     *
     * @access public
     * @return \Curl_Request
     */
    public function __construct() {

    }

    /**
     * getTimeout
     *
     * @access public
     * @return int
     */
    public function getTimeout() {
        return $this->_timeout;
    }

    /**
     * setTimeout
     *
     * @access public
     * @param  int    $timeout
     * @return void
     */
    public function setTimeout($timeout) {
        $this->_timeout = intval($timeout);
    }

    /**
     * getUserAgent
     *
     * @access public
     * @return string
     */
    public function getUserAgent() {
        return $this->_user_agent;
    }

    /**
     * setUserAgent
     *
     * @access public
     * @param  string  $user_agent
     * @return void
     */
    public function setUserAgent($user_agent) {
        $this->_user_agent = $user_agent;
    }

    /**
     * getFailOnError
     *
     * @access public
     * @return bool
     */
    public function getFailOnError() {
        return $this->_fail_on_error;
    }

    /**
     * setFailOnError
     *
     * @access public
     * @param  bool     $bool
     * @return void
     */
    public function setFailOnError($bool) {
        $this->_fail_on_error = (bool) $bool;
    }

    /**
     * getFollowLocationHeaders
     *
     * @access public
     * @return bool
     */
    public function getFollowLocationHeaders() {
        return $this->_follow_location_headers;
    }

    /**
     * setFollowLocationHeaders
     *
     * @access public
     * @param  bool     $bool
     * @return void
     */
    public function setFollowLocationHeaders($bool) {
        $this->_follow_location_headers = (bool) $bool;
    }

    /**
     * getLastError
     *
     * @access public
     * @return string
     */
    public function getLastError() {
        return $this->_last_error;
    }

    /**
     * getLastStats
     *
     * @access public
     * @return array
     */
    public function getLastStats() {
        return $this->_last_stats;
    }

    /**
     * head
     *
     * @access public
     * @param  string  $url
     * @return mixed
     */
    public function head($url) {
        $curl_opt_array = array(
            // Set Url
            CURLOPT_URL            => $url,
            // Return a String
            CURLOPT_RETURNTRANSFER => true,
            // Timeouts
            CURLOPT_CONNECTTIMEOUT => $this->getTimeout(),
            CURLOPT_TIMEOUT        => $this->getTimeout(),
            // UA
            CURLOPT_USERAGENT      => $this->getUserAgent(),
            // Fail for 400+
            CURLOPT_FAILONERROR    => $this->getFailOnError(),
            // Follow "Location" headers
            CURLOPT_FOLLOWLOCATION => $this->getFollowLocationHeaders(),
            // Only do a HEAD request
            CURLOPT_NOBODY         => true,
        );

        return $this->_request($curl_opt_array);
    }

    /**
     * get
     *
     * @access public
     * @param  string  $url
     * @return mixed
     */
    public function get($url) {
        $curl_opt_array = array(
            // Set Url
            CURLOPT_URL            => $url,
            // Return a String
            CURLOPT_RETURNTRANSFER => true,
            // Timeouts
            CURLOPT_CONNECTTIMEOUT => $this->getTimeout(),
            CURLOPT_TIMEOUT        => $this->getTimeout(),
            // UA
            CURLOPT_USERAGENT      => $this->getUserAgent(),
            // Fail for 400+
            CURLOPT_FAILONERROR    => $this->getFailOnError(),
            // Follow "Location" headers
            CURLOPT_FOLLOWLOCATION => $this->getFollowLocationHeaders(),
        );

        return $this->_request($curl_opt_array);
    }

    /**
     * post
     *
     * @access public
     * @param  string  $url
     * @param  array   $post_data_array
     * @return mixed
     */
    public function post($url, $post_data_array) {
        $curl_opt_array = array(
            // Set Url
            CURLOPT_URL            => $url,
            // Return a String
            CURLOPT_RETURNTRANSFER => true,
            // Timeouts
            CURLOPT_CONNECTTIMEOUT => $this->getTimeout(),
            CURLOPT_TIMEOUT        => $this->getTimeout(),
            // UA
            CURLOPT_USERAGENT      => $this->getUserAgent(),
            // Fail for 400+
            CURLOPT_FAILONERROR    => $this->getFailOnError(),
            // Follow "Location" headers
            CURLOPT_FOLLOWLOCATION => $this->getFollowLocationHeaders(),
            // This is POST
            CURLOPT_POST           => true,
            // Post Fields
            CURLOPT_POSTFIELDS     => $post_data_array,
        );

        return $this->_request($curl_opt_array);
    }

    /**
     * delete
     *
     * @access public
     * @param  string        $url
     * @param  string|array  $body_data
     * @return mixed
     */
    public function delete($url, $body_data = '') {
        $curl_opt_array = array(
            // Set Url
            CURLOPT_URL            => $url,
            // Return a String
            CURLOPT_RETURNTRANSFER => true,
            // Timeouts
            CURLOPT_CONNECTTIMEOUT => $this->getTimeout(),
            CURLOPT_TIMEOUT        => $this->getTimeout(),
            // UA
            CURLOPT_USERAGENT      => $this->getUserAgent(),
            // Fail for 400+
            CURLOPT_FAILONERROR    => $this->getFailOnError(),
            // Follow "Location" headers
            CURLOPT_FOLLOWLOCATION => $this->getFollowLocationHeaders(),
            // This is DELETE
            CURLOPT_HEADER         => 0,
            CURLOPT_CUSTOMREQUEST  => 'DELETE',
            // Post Fields
            CURLOPT_POSTFIELDS     => $body_data,
        );

        return $this->_request($curl_opt_array);
    }

    /**
     * _request
     *
     * @access protected
     * @param  array   $curl_opt_array
     * @return mixed
     */
    protected function _request($curl_opt_array) {
        $this->_last_error = '';
        $this->_last_stats = null;

        $resource = curl_init();
        curl_setopt_array($resource, $curl_opt_array);

        // Send!
        $response = curl_exec($resource);

        // Stats
        $this->_last_stats = curl_getinfo($resource);

        // Errors and redirect failures
        if (!$response && !$this->getFollowLocationHeaders() && !curl_errno($resource)) {
            $response = null;
        } else if (!$response) {
            $response = false;
            $this->_last_error =  curl_errno($resource).' - '.curl_error($resource);
        }

        curl_close($resource);
        return $response;
    }

}


text:
package jakiro.datasource.factory;

import java.sql.SQLException;
import java.util.Map;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

public class DruidDataSourceManagerTool extends IDataSourceManagerTool {

    @Override
    public DataSource createAinitDataSource(Map<String, String> properties) {
        DataSource ds = null;
        try {
            ds = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            log.error("create druid datasource error", e);
            try {
                ds = DruidDataSourceFactory.createDataSource(properties);
            } catch (Exception ex) {
                log.error("create druid datasource error", ex);
                throw new IllegalArgumentException("Cannot Create Datasource", ex);
            }
        }
        try {
            ((DruidDataSource) ds).init();
        } catch (SQLException e) {
            log.error("Init druid datasource errror", e);
            throw new IllegalArgumentException("Init Datasource Failure", e);
        }
        return ds;
    }

    @Override
    public boolean destroyDataSource(DataSource datasource) {
        if (datasource != null && datasource instanceof DruidDataSource) {
            try {
                ((DruidDataSource) datasource).close();
                return true;
            } catch (Throwable ex) {
                log.error("close datasource error : ", ex);
            }
        }
        return false;
    }
}


text:
import os
from setuptools import setup, find_packages

with open('requirements.txt') as file_requirements:
    requirements = file_requirements.read().splitlines()

setup(
    name='aws_eis',
    version='1.0.3',
    author='John Paul P. Doria',
    author_email='jp@lazyadm.in',
    description=('Register snapshot directory and take and restore ' +
                 'snapshots of Elasticsearch Service indices.'),
    license='MIT',
    keywords='aws elasticsearch index snapshot',
    url='https://github.com/jpdoria/aws_eis',
    packages=find_packages(),
    entry_points={
        'console_scripts': [
            'aws_eis = aws_eis.aws_eis:main'
        ]
    },
    install_requires=requirements,
    classifiers=[
        'Development Status :: 5 - Production/Stable',
        'Environment :: Console',
        'Intended Audience :: Developers',
        'Intended Audience :: System Administrators',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Programming Language :: Python :: 3.6',
        'Topic :: System :: Systems Administration',
        'Topic :: Utilities'
    ],
)


text:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical"
              android:background="@color/activity_background">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/activity_background">

        <LinearLayout
            android:id="@+id/profile_view"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:padding="10dip"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/mash_profile_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dip"
                android:orientation="vertical"
                android:background="@drawable/rounded_bg_white">

                <TextView
                    android:id="@+id/mash_profile_title"
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:gravity="center"
                    android:background="@drawable/profile_title_bg"
                    android:text="@string/mash_profile"
                    android:textColor="@color/title_color"/>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/fermentation_profile_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dip"
                android:orientation="vertical"
                android:background="@drawable/rounded_bg_white">

                <TextView
                    android:id="@+id/fermentation_profile_title"
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:gravity="center"
                    android:background="@drawable/profile_title_bg"
                    android:text="@string/fermentation_profile"
                    android:textColor="@color/title_color"/>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/style_profile_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dip"
                android:orientation="vertical"
                android:background="@drawable/rounded_bg_white">

                <TextView
                    android:id="@+id/profile_title"
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:gravity="center"
                    android:background="@drawable/profile_title_bg"
                    android:text="@string/style_profile"
                    android:textColor="@color/title_color"/>

                <TextView
                    android:id="@+id/style_profile_name"
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:gravity="center"
                    android:background="@color/transparent"
                    android:text="Other"
                    android:textColor="@color/text"/>

                <Button
                    android:id="@+id/display_style_button"
                    android:layout_width="match_parent"
                    android:layout_height="48dp"
                    style="@style/button"
                    android:onClick="displayStyle"
                    android:text="Details"/>

            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>


text:
package net.sourceforge.safr.core.integration.sample;

import net.sourceforge.safr.core.annotation.Inherit;
import net.sourceforge.safr.core.annotation.Secure;
import net.sourceforge.safr.core.annotation.SecureAction;
import net.sourceforge.safr.core.annotation.SecureObject;

/**
 * @author Martin Krasser
 */
@SecureObject
public class DomainObjectB extends DomainObjectA {

    // -------------------------------------------------------------------
    //  parameter annotation
    // -------------------------------------------------------------------

    // ...

    // -------------------------------------------------------------------
    //  parameter annotation inheritance
    // -------------------------------------------------------------------

    @Override @Inherit
    public void m02a( // inherit annotations
            String p1,
            String p2) {}
    @Override
    public void m02b( // override annotations
            @Secure(SecureAction.CREATE) String p1,
            String p2) {}
    @Override
    public void m02c( // define annotations
            @Secure(SecureAction.UPDATE) String p1,
            String p2) {}
    @Override
    public void m02d( // override annotations (different location)
            String p1,
            @Secure(SecureAction.DELETE) String p2) {}

    // -------------------------------------------------------------------
    //  method annotation inheritance (@Secure)
    // -------------------------------------------------------------------

    @Override @Secure(SecureAction.UPDATE)
    public void m03a() {}

    @Override @Inherit
    public void m03b() {}

    // -------------------------------------------------------------------
    //  method annotation inheritance (@Filter)
    // -------------------------------------------------------------------

    // -------------------------------------------------------------------
    //  combination of annotation inheritance
    // -------------------------------------------------------------------

    @Override @Secure
    public String m09a(String p1) {
        return "x";
    }

    // -------------------------------------------------------------------
    //  method annotation (@Secure)
    // -------------------------------------------------------------------

    // -------------------------------------------------------------------
    //  method annotation (@Filter)
    // -------------------------------------------------------------------

}


text:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Model{
	function __construct() {
		$this->tableName = 'users';
		$this->primaryKey = 'id';
		$this->load->model('user_model');
	}
	public function checkUser($data = array()){
		$this->db->select($this->primaryKey);
		$this->db->from($this->tableName);
		$this->db->where(array('oauth_provider'=>$data['oauth_provider'],'oauth_uid'=>$data['oauth_uid']));
		$prevQuery = $this->db->get();
		$prevCheck = $prevQuery->num_rows();

		if($prevCheck > 0){
			$prevResult = $prevQuery->row_array();
			$data['modified'] = date("Y-m-d H:i:s");
			$update = $this->db->update($this->tableName,$data,array('id'=>$prevResult['id']));
			$userID = $prevResult['id'];
			$this->updateMainUsrTable($data);
		}else{
			$data['created'] = date("Y-m-d H:i:s");
			$data['modified'] = date("Y-m-d H:i:s");var_dump($data, $this->tableName);
			$insert = $this->db->insert($this->tableName,$data);
			$userID = $this->db->insert_id();
			$this->updateMainUsrTable($data);
		}

		return $userID?$userID:FALSE;
    }

    public function updateMainUsrTable($userdata = array()){
    	$data = array();

    	$data['contact_name'] = $userdata['first_name'].' '.$userdata['last_name'];
    	$data['email'] = $userdata['email'];
    	$data['gender'] = $userdata['gender'];
    	//$userData['locale'] = $userdata['locale'];
    	//$userData['profile_url'] = $userdata['link'];
    	$data['profile_img'] = $userdata['picture_url'];
    	if($this->user_model->isUserEmailExists($userdata['email'])){
    		$data['edited_date'] = date("Y-m-d H:i:s");
    		$update = $this->db->update('user_detail',$data,array('email'=>$data['email']));
    	}else{
    		$data['created_date'] = date("Y-m-d H:i:s");
    		$data['edited_date'] = date("Y-m-d H:i:s");
    		$data['user_type'] = 'User';
    		$insert = $this->db->insert('user_detail',$data);
    	}
    }
}


text:
# jwt-policy

[![npm version](https://badge.fury.io/js/jwt-policy.svg)](https://badge.fury.io/js/jwt-policy)

JSON Web Token middleware friendly with Express and Sails.js

Validates `token` from HTTP request header authorization and sets `req.user`, token is expected to be found at `Authorization: Bearer <token>`.

This module verifies tokens generated with [node-jsonwebtoken](https://github.com/auth0/node-jsonwebtoken)

## Install

```
$ npm install jwt-policy --save
```

## Usage

### jwtPolicy(options, [callback])

`options` :

* `secret` : is a string containing the secret for decoding token.
* `extractToken` : function to extract token instead of default (HTTP Authorization Header).
* `attachTo` : allows the user to override the default path where the decoded token will be attached to, default is `user`.

**Note: You can pass all available options for [`jwt.verify`](https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback) such as `audience`, `issuer`, etc.**

Specify callback if you wish to do something with `req.user` or check for possible errors, if callback is not supplied then default behavior will take effect.

For default, `jwt-policy` extracts token using [extractor-token](https://github.com/joshuamarquez/node-token-extractor/) (HTTP Authorization Header) but in case you are passing the token by any other method you can use `extractToken` option.

### Usage in Sails.js

Default behavior

```javascript
// Will return 401 HTTP status code if any errors occurred.
// policies/jwtAuth.js
module.exports = require('jwt-policy')({ secret: 'my_secret_key' });
```

Override default behavior

```javascript
// policies/jwtAuth.js
module.exports = require('jwt-policy')({
  secret: 'my_secret_key'
}, function(err, req, res, next) {
  if (!err) {
    // user can be found at 'req.user'

    return next();
  }

  return res.status(401).json(err);
});
```

Override the way the token is extracted using `extractToken` option.

```javascript
// policies/jwtAuth.js
module.exports = require('jwt-policy')({
  secret: 'my_secret_key',
  extractToken: function(req) {
    return req.param('token');
  }
});
```

### Usage in Express

Default behavior

```javascript
const jwtPolicy = require('jwt-policy');

app.get('/', jwtPolicy({ secret: 'my_secret_key' }), function(req, res) {
  res.send(req.user);
});
```

Override default behavior

```javascript
const jwtPolicy = require('jwt-policy');

app.use(jwtPolicy({ secret: 'my_secret_key' }, function(err, req, res, next) {
  if (!err) {
    return res.next();
  }

  return res.status(401).json(err);
}));

app.get('/', function(req, res) {
  res.send(req.user);
});
```

Override the way the token is extracted using `extractToken` option.

```javascript
app.use(jwtPolicy({
  secret: 'my_secret_key',
  extractToken: function(req) {
    return req.query.token;
  }
}));
```

### Attach to

`attachTo` option usage example:

```javascript
const jwtPolicy = require('jwt-policy');

app.use(jwtPolicy({
  secret: 'my_secret_key',
  attachTo: 'auth'
}));

app.get('/', function(req, res) {
  // decoded token can now
  // be found at `req.auth`
  res.send(req.auth);
});
```

## Error handling

Possible thrown errors

### TokenExtractorError

| message                                         | code                                 |
| ----------------------------------------------- |:------------------------------------:|
| No Authorization header is present              | `E_AUTHORIZATION_REQUIRED`           |
| Format is :: Authorization: Bearer <token>      | `E_AUTHORIZATION_INVALID_FORMAT`     |
| Authorization token was not found               | `E_AUTHORIZATION_TOKEN_NOT_FOUND`    |

### JWTError

| message                                         | code                                 |
| ----------------------------------------------- |:------------------------------------:|
| JSON Web Token provided has expired             | `E_TOKEN_EXPIRED`                    |
| Invalid JSON Web Token provided                 | `E_TOKEN_INVALID`                    |

Suppose `E_TOKEN_EXPIRED` error was thrown

```javascript
app.use(jwtPolicy({ secret: 'my_secret_key' }, function(err, req, res, next) {
  if (err) {
    console.log(err.toJSON());
    /*
      {
        status: 401,
        message: 'JSON Web Token provided has expired',
        code: 'E_TOKEN_EXPIRED'
      }
    */

    console.log(err.toString());
    /*
      [JWTError (E_TOKEN_EXPIRED) JSON Web Token provided has expired]
    */

    console.trace(err);
    /*
      prints Error Stack since err instanceof Error
    */
  }
}));
```

## Test

```
$ npm test
```


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VRage;
using VRage.Library.Collections;
using VRage.Network;

namespace Torch.API.Managers
{
    /// <summary>
    /// API for the network intercept.
    /// </summary>
    public interface INetworkManager : IManager
    {
        /// <summary>
        /// Register a network handler.
        /// </summary>
        void RegisterNetworkHandler(INetworkHandler handler);

        /// <summary>
        /// Unregister a network handler.
        /// </summary>
        /// <returns>true if the handler was unregistered, false if it wasn't registered to begin with</returns>
        bool UnregisterNetworkHandler(INetworkHandler handler);
    }

    /// <summary>
    /// Handler for multiplayer network messages.
    /// </summary>
    public interface INetworkHandler
    {
        /// <summary>
        /// Returns if the handler can process the call site.
        /// </summary>
        bool CanHandle(CallSite callSite);

        /// <summary>
        /// Processes a network message.
        /// </summary>
        /// <returns>true if the message should be discarded</returns>
        bool Handle(ulong remoteUserId, CallSite site, BitStream stream, object obj, MyPacket packet);
    }
}


text:
class AllowedUserController < TestController
  allow :user => :nancy
end


text:
Create users from the command line

### Requirements

- PHP => 5.6.4
- Laravel >= 5.4

### Installation

**Composer**

Run the following to include this via Composer

```shell
composer require mahimahi/auth-command
```

Then add `Nexmo\Laravel\NexmoServiceProvider` to the `providers` array in your `config/app.php`:

```php
MahiMahi\AuthCommand\AuthCommandServiceProvider::class,
```

Usage
-----

Create users from the cli :

```
php artisan auth:create-user "John Doe" john@doe.com
```


text:
This library contains shared types, such as error classes, for the google-cloud
project. Please see the [GitHub
repository](https://github.com/googleapis/google-cloud-ruby) for more
information about the individual google-cloud gems.

- [google-cloud-core API documentation](https://googleapis.github.io/google-cloud-ruby/docs/google-cloud-core/latest)

## Supported Ruby Versions

This library is supported on Ruby 2.3+.

Google provides official support for Ruby versions that are actively supported
by Ruby Core—that is, Ruby versions that are either in normal maintenance or in
security maintenance, and not end of life. Currently, this means Ruby 2.3 and
later. Older versions of Ruby _may_ still work, but are unsupported and not
recommended. See https://www.ruby-lang.org/en/downloads/branches/ for details
about the Ruby support schedule.

## Versioning

This library follows [Semantic Versioning](http://semver.org/).

It is currently in major version zero (0.y.z), which means that anything may
change at any time and the public API should not be considered stable.

## Contributing

Contributions to this library are always welcome and highly encouraged.

See the [Contributing
Guide](https://googleapis.github.io/google-cloud-ruby/docs/google-cloud-core/latest/file.CONTRIBUTING)
for more information on how to get started.

Please note that this project is released with a Contributor Code of Conduct. By
participating in this project you agree to abide by its terms. See [Code of
Conduct](https://googleapis.github.io/google-cloud-ruby/docs/google-cloud-core/latest/file.CODE_OF_CONDUCT)
for more information.

## License

This library is licensed under Apache 2.0. Full license text is available in
[LICENSE](https://googleapis.github.io/google-cloud-ruby/docs/google-cloud-core/latest/file.LICENSE).

## Support

Please [report bugs at the project on
Github](https://github.com/googleapis/google-cloud-ruby/issues). Don't
hesitate to [ask
questions](http://stackoverflow.com/questions/tagged/google-cloud-platform+ruby)
about the client or APIs on [StackOverflow](http://stackoverflow.com).


text:
using System;
using System.Collections.Concurrent;
using System.Reflection;

namespace Rabbit.WeiXin.Tests.Utility
{
    public static class ApiTestHelper
    {
        private static readonly ConcurrentDictionary<string, byte[]> Dictionary = new ConcurrentDictionary<string, byte[]>();

        public const string MusicUrl = "http://www.chunsun.cc/星辰泪.mp3";
        public const string HqMusicUrl = "http://www.chunsun.cc/星辰泪.mp3";

        public static byte[] GetJpgBytes()
        {
            return GetByte("1.jpg");
        }

        public static byte[] GetAmrBytes()
        {
            return GetByte("1.amr");
        }

        public static byte[] GetMp3Bytes()
        {
            return GetByte("1.mp3");
        }

        public static byte[] GetMp4Bytes()
        {
            return GetByte("1.mp4");
        }

        #region Private Method

        private static byte[] GetByte(string name)
        {
            var assembly = Assembly.Load(new AssemblyName(typeof(ApiTestHelper).AssemblyQualifiedName));
            using (var stream = assembly.GetManifestResourceStream("Rabbit.WeiXin.Tests.Resoueces." + name))
            {
                if (stream == null)
                    throw new Exception("找不到资源:" + name);
                var bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                return bytes;
            }
        }

        #endregion Private Method
    }
}


text:
package com.mlwork.classification.test;

import java.io.FileNotFoundException;

import com.mlwork.classification.model.ClassificationModel;
import com.mlwork.classification.model.CsvModelSource;

public class CsvModelTest {

	private static final String CSV_FILE_RELATIVE_LOC = "/MLWorkMath/test/com/mlwork/math/testdata/csvtestfile.csv";

	private static final String MLWORKLOCATION = System.getenv("MLWORK");

	public static ClassificationModel loadCsvModelTest() throws FileNotFoundException {
		CsvModelSource src = new CsvModelSource(MLWORKLOCATION + CSV_FILE_RELATIVE_LOC);
		ClassificationModel<String> model = src.loadModel();
		return model;
	}
}


text:
<!DOCTYPE HTML>

Clicky


text:
package com.amazonaws.services.servicecatalog.model.transform;

import javax.annotation.Generated;

import com.amazonaws.SdkClientException;
import com.amazonaws.services.servicecatalog.model.*;

import com.amazonaws.protocol.*;
import com.amazonaws.annotation.SdkInternalApi;

/**
 * AssociateTagOptionWithResourceRequestMarshaller
 */
@Generated("com.amazonaws:aws-java-sdk-code-generator")
@SdkInternalApi
public class AssociateTagOptionWithResourceRequestMarshaller {

    private static final MarshallingInfo<String> RESOURCEID_BINDING = MarshallingInfo.builder(MarshallingType.STRING)
            .marshallLocation(MarshallLocation.PAYLOAD).marshallLocationName("ResourceId").build();
    private static final MarshallingInfo<String> TAGOPTIONID_BINDING = MarshallingInfo.builder(MarshallingType.STRING)
            .marshallLocation(MarshallLocation.PAYLOAD).marshallLocationName("TagOptionId").build();

    private static final AssociateTagOptionWithResourceRequestMarshaller instance = new AssociateTagOptionWithResourceRequestMarshaller();

    public static AssociateTagOptionWithResourceRequestMarshaller getInstance() {
        return instance;
    }

    /**
     * Marshall the given parameter object.
     */
    public void marshall(AssociateTagOptionWithResourceRequest associateTagOptionWithResourceRequest, ProtocolMarshaller protocolMarshaller) {

        if (associateTagOptionWithResourceRequest == null) {
            throw new SdkClientException("Invalid argument passed to marshall(...)");
        }

        try {
            protocolMarshaller.marshall(associateTagOptionWithResourceRequest.getResourceId(), RESOURCEID_BINDING);
            protocolMarshaller.marshall(associateTagOptionWithResourceRequest.getTagOptionId(), TAGOPTIONID_BINDING);
        } catch (Exception e) {
            throw new SdkClientException("Unable to marshall request to JSON: " + e.getMessage(), e);
        }
    }

}


text:
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Org.Apache.REEF.Tang.Examples;
using Org.Apache.REEF.Tang.Implementations.Tang;
using Org.Apache.REEF.Tang.Interface;
using Org.Apache.REEF.Tang.Protobuf;
using Xunit;

namespace Org.Apache.REEF.Tang.Tests.ClassHierarchy
{
    public class TestAnonymousType
    {
        const string ClassHierarchyBinFileName = "example.bin";

        [Fact]
        public void TestAnonymousTypeWithDictionary()
        {
            List<string> appDlls = new List<string>();
            appDlls.Add(typeof(AnonymousType).GetTypeInfo().Assembly.GetName().Name);
            var c = TangFactory.GetTang().GetClassHierarchy(appDlls.ToArray());
            c.GetNode(typeof(AnonymousType).AssemblyQualifiedName);

            IConfiguration conf = TangFactory.GetTang().NewConfigurationBuilder(c).Build();
            IInjector injector = TangFactory.GetTang().NewInjector(conf);
            var obj = injector.GetInstance<AnonymousType>();
            Assert.NotNull(obj);

            var cd = Directory.GetCurrentDirectory();
            Console.WriteLine(cd);

            ProtocolBufferClassHierarchy.Serialize(ClassHierarchyBinFileName, c);
            IClassHierarchy ch = ProtocolBufferClassHierarchy.DeSerialize(ClassHierarchyBinFileName);
            ch.GetNode(typeof(AnonymousType).AssemblyQualifiedName);
        }
    }
}


text:
package com.itracker.android.data;

/**
 * Listen for application fully initialized.
 *
 * @author alexander.ivanov
 */
public interface OnInitializedListener extends BaseManagerInterface {

    /**
     * Called once on service start and all data were loaded.
     * <p/>
     * Called from UI thread.
     */
    void onInitialized();

}


text:
<DOCTYPE! html>

    <meta charset="UTF-8">
    <link type="text/css" rel="stylesheet" href="stylesheets/cheat-sheet.css"/>
   <link href='http://fonts.googleapis.com/css?family=Black+Ops+One|Khula|Catamaran' rel='stylesheet' type='text/css'>
    <title>Cheat Sheet</title>

New HTML 5 Tags Cheat Sheet

<article>
self-contained composition that is independently distributable

<aside>
section of page that consists of content
 tangentially related to the content around it

<audio>
sound content

<bdi>
span of page to be isolated form surroundings
for bidirectional formating purposes

<canvas>
area that can be used to draw graphics via JavaScript

<command>
user invokable command

<datalist>
dropdown list

<datatemplate>
data template

<details>
details of element

<embed>
embedded content

<figcaption>
caption of figure element

<figure>
group of media content

<footer>
footer section or page

<header>
header section or page

<hgroup>
group of headings for section

<keygen>
generated key in a form

<main>
specifies the main content area of an HTML document

<menuitem>
specifies a command that a user can evoke from a popup menu

<mark>
marked text

<meter>
measurement in range

<nav>
navigation links

<output>
represents results of calculation

<progress>
progress of any kind of task

<rb>
marks the base text component of a ruby annotation

<rp>
parenthesized ruby text

<rt>
ruby text

<rtc>
marks ruby text container for ruby text components in a ruby annotation

<ruby>
ruby annotations

<section>
sections in a document

<source>
media resources

<summary>
header of a detail element

<template>
declares HTML fragments that can be cloned and inserted in the document by script

<time>
date/time

<track>
specifies a text track for media such as video and audio

<video>
video

<wbr>
possible line break

Made in NYC ♥ | © 2015


text:
package me.bramhaag.discordselfbot.util;

import net.dv8tion.jda.core.entities.User;
import org.jetbrains.annotations.NotNull;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class ImageUtil {

    /**
     * Get {@code user}'s avatar
     * @param user user whom's avatar to get
     * @return {@code user}'s avatar
     * @throws IOException if an error occurs during reading or when not
     * able to create required ImageInputStream.
     */
    @NotNull
    public static BufferedImage getAvatar(@NotNull User user) throws IOException {
        String url = user.getAvatarUrl() == null ? user.getDefaultAvatarUrl() : user.getAvatarUrl();
        HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
        connection.setRequestProperty("User-Agent", Constants.USER_AGENT);

        return ImageIO.read(connection.getInputStream());
    }

    /**
     * Resize picture
     * @param source picture to be resized
     * @param type type of image
     * @param width width of image
     * @param height height of image
     * @return resized image with {@code width}x{@code height} as dimensions
     */
    @NotNull
    public static BufferedImage resize(@NotNull BufferedImage source, int type, int width, int height) {
        Image scaled = source.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        BufferedImage resized = new BufferedImage(width, height, type);

        Graphics2D g = resized.createGraphics();
        g.drawImage(scaled, 0, 0,null);
        g.dispose();

        return resized;
    }

    /**
     * Create circle from an image
     * @param image image to create circle of
     * @param type type of image
     * @param size radius of circle
     * @return circular image of {@code image}
     */
    @NotNull
    public static BufferedImage makeCircular(@NotNull BufferedImage image, int type, int size) {
        BufferedImage circle = new BufferedImage(size, size, type);

        Graphics2D g = circle.createGraphics();
        g.clip(new Ellipse2D.Float(0, 0, size, size));
        g.drawImage(image, 0, 0, null);

        return circle;
    }
}


text:
package com.jlj.service;

import java.util.List;

import com.jlj.model.Pagearticle;

public interface IPagearticleService {

	public abstract void add(Pagearticle pagearticle) throws Exception;

	public abstract void delete(Pagearticle pagearticle);

	public abstract void deleteById(int id);

	public abstract void update(Pagearticle pagearticle);

	public abstract List<Pagearticle> getPagearticles();

	public abstract Pagearticle loadById(int id);

	public abstract int getTotalCount(int con, String convalue, int status,
			String publicaccount);

	public abstract List<Pagearticle> queryList(int con, String convalue,
			int status, String publicaccount, int page, int size);

	public abstract int getFrontPageCount(int stid, int size);

	public abstract List<Pagearticle> queryFrontList(int stid, int page,
			int size);

	public abstract int getFrontTotalCount(int stid);

	public abstract List<Pagearticle> queryFrontIndexList(String frontpa,int isshow, int page,
			int psize);

	public abstract int getPageCount(int totalCount, int size);

	public abstract List<Pagearticle> queryPagearticlesByEndBigtypeId(
			int bigtypeid);

}


text:
package com.remobile.cordova;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.Nullable;

import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import org.json.JSONArray;
import org.json.JSONException;

public abstract class CordovaPlugin extends ReactContextBaseJavaModule implements ActivityEventListener, LifecycleEventListener {
    public static final int RESULT_STEP = 1000000;
    public static int CURRENT_RESULT = 0;

    public CordovaInterface cordova;
    public int requestCodeStart = 0;
    public CordovaWebView webView;

    public CordovaPlugin(ReactApplicationContext reactContext) {
        super(reactContext);
        cordova = new CordovaInterface(this);
        webView = new CordovaWebView(reactContext);
    }

    public CordovaPlugin(ReactApplicationContext reactContext, boolean needActivityResult) {
        this(reactContext);
        CURRENT_RESULT += RESULT_STEP;
        requestCodeStart = CURRENT_RESULT;
    }

    @Override
    public void initialize() {
        super.initialize();
        getReactApplicationContext().addActivityEventListener(this);
        getReactApplicationContext().addLifecycleEventListener(this);
    }

    public Activity getActivity() {
        return this.getCurrentActivity();
    }

    public void sendJSEvent(String eventName, @Nullable WritableMap params) {
        getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
    }

    public void onRequestPermissionResult(int requestCode, String[] permissions,
                                          int[] grantResults) throws JSONException {
    }

    public void executeReactMethod(String action, ReadableArray args, Callback success, Callback error) {
        try {
            this.execute(action, JsonConvert.reactToJSON(args), new CallbackContext(success, error));
        } catch (Exception ex) {
            FLog.e(getName(), "Unexpected error:" + ex.getMessage());
        }
    }

    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        return true;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {}

    public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
        if (requestCode >= requestCodeStart && requestCode < requestCodeStart+RESULT_STEP) {
            onActivityResult(requestCode-requestCodeStart, resultCode, data);
        }
    }

    public void onNewIntent(Intent intent) {
    }

    public void onResume() {
    }

    public void onHostResume() {
        onResume();
    }

    public void onPause() {
    }

    public void onHostPause() {
        onPause();
    }

    public void onDestroy() {
    }

    public void onHostDestroy() {
        onDestroy();
    }
}


text:
<title>
"Bush's embrace of Sharon"
</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<?php include "../../legacy-includes/Script.htmlf" ?>

<?php include "../../legacy-includes/TopLogo.htmlf" ?>

<?php include "../../legacy-includes/TopAd.htmlf" ?>

<?php include "../../legacy-includes/LeftButtons.htmlf" ?>

<?php include "../../legacy-includes/BodyInsert.htmlf" ?>

Naseer Aruri on U.S. support for Israel's war:

"Bush's embrace of Sharon"

June 21, 2002 | Page 5

NASEER ARURI has written and edited numerous books about the Palestinian struggle, the latest being Palestinian Refugees: The Right of Return. He talked to Socialist Worker about the U.S. government's role in backing Israel's war against Palestinians.

- - - - - - - - - - - - - - - -

THERE WAS always a question about the role of the United States--as the catalyst for peace, the conciliator. This assumption has been challenged more widely than before.

I think the new theme is that after Sharon's sixth visit to the White House, there seems to be an end to the pretense that the U.S. is an "honest broker."

Here is Bush saying about Sharon, "Every time he comes here, I learn something. He's a 'good teacher.'" That comes on the heels of another statement during the previous visit--number five--when Bush called Sharon "a man of peace."

Concretely, what did Bush gain from the teaching? First of all, when Sharon came on his last visit on June 10, he invited himself because he knew that his "student" had a long meeting with the Saudi crown prince.

Sharon came in order to veto the idea of talks, and Bush obliged when he made a statement--with Sharon at his side--that he didn't think conditions were right for talks.

He was asked about the regional conference that Secretary of State Colin Powell floated a couple months ago as the administration was facing a lot of criticism after the massacres in Jenin, Nablus and Ramallah.

Bush acknowledged that there's urgency, but that it's not time yet. What this really means is that even the nuances have been abandoned--that is to say that the American embrace of Sharon's position seems to be complete.

Both Sharon and Bush have run out of conditions to place on Arafat. First, there was cease-fire. Then, he had to destroy the network of terror. And lastly, reform and reorganization of the government.

So we have moved from Bush not wanting to be involved at all in the Middle East conflict when he came to power in January 2001 to being involved wholly on the Israeli side.

I find this unprecedented, having studied U.S. presidential policies towards Palestine since Nixon. American policy for the past 35 years has been extremely deceptive--claiming to be the peacemaker while acting as a co-belligerent.

And yet every American president and secretary of state found it necessary to couch this support in diplomatic language that was intended to be deceptive. Now that's been abandoned.

So Sharon went back having accomplished what he wanted.

<?php include "../../legacy-includes/BottomNavLinks.htmlf" ?>

<?php include "../../legacy-includes/RightAdFolder.htmlf" ?>


text:
<?php

declare(strict_types=1);

namespace Sentry\State;

use Sentry\Breadcrumb;
use Sentry\ClientInterface;
use Sentry\Event;
use Sentry\EventHint;
use Sentry\EventId;
use Sentry\Integration\IntegrationInterface;
use Sentry\Severity;
use Sentry\Tracing\SamplingContext;
use Sentry\Tracing\Span;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;

/**
 * This interface represent the class which is responsible for maintaining a
 * stack of pairs of clients and scopes. It is the main entry point to talk
 * with the Sentry client.
 */
interface HubInterface
{
    /**
     * Gets the client bound to the top of the stack.
     */
    public function getClient(): ?ClientInterface;

    /**
     * Gets the ID of the last captured event.
     */
    public function getLastEventId(): ?EventId;

    /**
     * Creates a new scope to store context information that will be layered on
     * top of the current one. It is isolated, i.e. all breadcrumbs and context
     * information added to this scope will be removed once the scope ends. Be
     * sure to always remove this scope with {@see Hub::popScope} when the
     * operation finishes or throws.
     */
    public function pushScope(): Scope;

    /**
     * Removes a previously pushed scope from the stack. This restores the state
     * before the scope was pushed. All breadcrumbs and context information added
     * since the last call to {@see Hub::pushScope} are discarded.
     */
    public function popScope(): bool;

    /**
     * Creates a new scope with and executes the given operation within. The scope
     * is automatically removed once the operation finishes or throws.
     *
     * @param callable $callback The callback to be executed
     *
     * @return mixed|void The callback's return value, upon successful execution
     *
     * @psalm-template T
     *
     * @psalm-param callable(Scope): T $callback
     *
     * @psalm-return T
     */
    public function withScope(callable $callback);

    /**
     * Calls the given callback passing to it the current scope so that any
     * operation can be run within its context.
     *
     * @param callable $callback The callback to be executed
     */
    public function configureScope(callable $callback): void;

    /**
     * Binds the given client to the current scope.
     *
     * @param ClientInterface $client The client
     */
    public function bindClient(ClientInterface $client): void;

    /**
     * Captures a message event and sends it to Sentry.
     *
     * @param string         $message The message
     * @param Severity|null  $level   The severity level of the message
     * @param EventHint|null $hint    Object that can contain additional information about the event
     */
    public function captureMessage(string $message, ?Severity $level = null/*, ?EventHint $hint = null*/): ?EventId;

    /**
     * Captures an exception event and sends it to Sentry.
     *
     * @param \Throwable     $exception The exception
     * @param EventHint|null $hint      Object that can contain additional information about the event
     */
    public function captureException(\Throwable $exception/*, ?EventHint $hint = null*/): ?EventId;

    /**
     * Captures a new event using the provided data.
     *
     * @param Event          $event The event being captured
     * @param EventHint|null $hint  May contain additional information about the event
     */
    public function captureEvent(Event $event, ?EventHint $hint = null): ?EventId;

    /**
     * Captures an event that logs the last occurred error.
     *
     * @param EventHint|null $hint Object that can contain additional information about the event
     */
    public function captureLastError(/*?EventHint $hint = null*/): ?EventId;

    /**
     * Records a new breadcrumb which will be attached to future events. They
     * will be added to subsequent events to provide more context on user's
     * actions prior to an error or crash.
     *
     * @param Breadcrumb $breadcrumb The breadcrumb to record
     *
     * @return bool Whether the breadcrumb was actually added to the current scope
     */
    public function addBreadcrumb(Breadcrumb $breadcrumb): bool;

    /**
     * Gets the integration whose FQCN matches the given one if it's available on the current client.
     *
     * @param string $className The FQCN of the integration
     *
     * @psalm-template T of IntegrationInterface
     *
     * @psalm-param class-string<T> $className
     *
     * @psalm-return T|null
     */
    public function getIntegration(string $className): ?IntegrationInterface;

    /**
     * Starts a new `Transaction` and returns it. This is the entry point to manual
     * tracing instrumentation.
     *
     * A tree structure can be built by adding child spans to the transaction, and
     * child spans to other spans. To start a new child span within the transaction
     * or any span, call the respective `startChild()` method.
     *
     * Every child span must be finished before the transaction is finished,
     * otherwise the unfinished spans are discarded.
     *
     * The transaction must be finished with a call to its `finish()` method, at
     * which point the transaction with all its finished child spans will be sent to
     * Sentry.
     *
     * @param TransactionContext   $context               Properties of the new transaction
     * @param array<string, mixed> $customSamplingContext Additional context that will be passed to the {@see SamplingContext}
     */
    public function startTransaction(TransactionContext $context/*, array $customSamplingContext = []*/): Transaction;

    /**
     * Returns the transaction that is on the Hub.
     */
    public function getTransaction(): ?Transaction;

    /**
     * Returns the span that is on the Hub.
     */
    public function getSpan(): ?Span;

    /**
     * Sets the span on the Hub.
     *
     * @param Span|null $span The span
     */
    public function setSpan(?Span $span): HubInterface;
}


text:
require 'taste_tester/logging'
require 'between_meals/util'
require 'taste_tester/ssh_util'

module TasteTester
  # Thin ssh tunnel wrapper
  class Tunnel
    include TasteTester::Logging
    include BetweenMeals::Util
    include TasteTester::SSH::Util

    attr_reader :port

    def initialize(host, server)
      @host = host
      @server = server
    end

    def run
      @port = TasteTester::Config.tunnel_port
      logger.info("Setting up tunnel on port #{@port}")
      exec!(cmd, logger)
    rescue StandardError => e
      logger.error "Failed bringing up ssh tunnel: #{e}"
      error!
    end

    def cmd
      if TasteTester::Config.windows_target
        cmds = windows_tunnel_cmd
      else
        cmds = sane_os_tunnel_cmd
      end

      # As great as it would be to have ExitOnForwardFailure=yes,
      # we had multiple cases of tunnels dying
      # if -f and ExitOnForwardFailure are used together.
      # In most cases the first request from chef was "breaking" the tunnel,
      # in a way that port was still open, but subsequent requests were hanging.
      # This is reproducible and should be looked into.
      cmd = "#{ssh_base_cmd} -o ServerAliveInterval=10 " +
        "-o ServerAliveCountMax=6 -f -R #{@port}:localhost:#{@server.port} "
      build_ssh_cmd(cmd, [cmds])
    end

    def self.kill(name)
      ssh = TasteTester::SSH.new(name)
      # Since commands are &&'d together, and we're using &&, we need to
      # surround this in paryns, and make sure as a whole it evaluates
      # to true so it doesn't mess up other things... even though this is
      # the only thing we're currently executing in this SSH.
      if TasteTester::Config.windows_target
        cmd = <<~EOPS
          if (Test-Path "#{TasteTester::Config.timestamp_file}") {
            $x = cat "#{TasteTester::Config.timestamp_file}"
            if ($x -ne $null) {
              kill -Force $x 2>$null
            }
          }
          $LASTEXITCODE = 0
        EOPS
      else
        cmd = "( [ -s #{TasteTester::Config.timestamp_file} ]" +
              ' && kill -9 -- ' +
              "-\$(cat #{TasteTester::Config.timestamp_file}) 2>/dev/null; " +
              ' true )'
      end
      ssh << cmd
      ssh.run!
    end

    private

    def windows_tunnel_cmd
      # We are powershell. If you walk up you get:
      #   ppid - ssh
      #   pppid - ssh
      #   ppppid - ssh
      #   pppppid - services
      #
      # Unlike in Linux you don't need to walk up the tree, however. In fact,
      # killing pppid or ppid didn't actually terminate the session. Only
      # killing our actual powershell instance did.
      #
      # Moreover, it doesn't seem like re-parenting works the same way. So
      # this is pretty simple.
      #
      # For the record, if you want to play with this, you do so with:
      #   (gwmi win32_process | ? processid -eq $PID).parentprocessid
      #
      # Also note that backtick is a line-continuation marker in powershell.
      <<~EOS
      $ts = "#{TasteTester::Config.timestamp_file}"
      echo $PID | Out-File -Encoding ASCII "$ts"
      # TODO: pull this from Host.touchcmd
      (Get-Item "$ts").LastWriteTime=("#{TasteTester::Config.testing_end_time}")

      while (1 -eq 1) {
        if (-Not (Test-Path $ts)) {
          # if we are here, we know we've created our source
          Write-EventLog -LogName "Application" -Source "taste-tester" `
            -EventID 5 -EntryType Information `
            -Message "Ending tunnel: timestamp file disappeared"
        }
        sleep 60
      }
      done
      EOS
    end

    def sane_os_tunnel_cmd
      @ts = TasteTester::Config.testing_end_time.strftime('%y%m%d%H%M.%S')
      # Tie the life of our SSH tunnel with the life of timestamp file.
      # taste-testing can be renewed, so we'll wait until:
      # 1. the timestamp file is entirely gone
      # 2. our parent sshd process dies
      # 3. new taste-tester instance is running (file contains different PGID)
      <<~EOS
      log() {
        [ -e /usr/bin/logger ] || return
        logger -t taste-tester "$*"
      }
      # sets $current_pgid
      # This is important, this should just be called ald let it set the
      # variable. Do NOT call in a subshell like foo=$(get_current_pgid)
      # as then you end up even further down the list of children
      get_current_pgid() {

        # if TT user is non-root, then it breaks down like this:
        #   we are 'bash'
        #   our parent is 'sudo'
        #   our parent's parent is 'bash "echo ..." | sudo bash -x'
        #   our parent's parent's parent is ssh
        #   - we want the progress-group ID of *that*
        #
        # EXCEPT... sometimes sudo forks itself one more time so it's
        #   we are 'bash'
        #   our parent is 'sudo'
        #   our parent's parent 'sudo'
        #   our parent's parent's parent is 'bash "echo ..." | sudo bash -x'
        #   our parent's parent's parent's parent is ssh
        #   - we want the progress-group ID of *that*
        #
        # BUT if the TT user is root, no sudo at all...
        #   we are 'bash'
        #   our parent is 'bash "echo ..." | bash -c
        #   our parent's parent is ssh
        #   - we want the progress-group ID of *that*
        #
        # We can make all sorts of assumptions, but the most reliable way
        # to do this that's always correct is to is simply to walk parents until
        # we hit something with SSH in the name. Start with PPID and go from
        # there.
        #
        # There's a few commented out 'log's here that are too verbose
        # for operation (since this function runs every minute)  but are useful
        # for debugging.

        relevant_pid=''
        current_pid=$PPID
        while true; do
          name=$(ps -o command= -p $current_pid)
          if [[ "$name" =~ sshd ]]; then
            # Uncomment the following for debugging...
            #log "$current_pid is ssh, that's us!"
            relevant_pid=$current_pid
            break
          fi
          # Uncomment the following for debugging...
          #log "$current_pid is $name, finding parent..."
          current_pid=$(ps -o ppid= -p $current_pid)
        done
        if [ -z "$relevant_pid" ];then
          log "Cannot determine relevant PGID"
          exit 42
        fi
        current_pgid="$(ps -o pgid= -p $relevant_pid | sed "s| ||g")"
        # Uncomment the following for debugging...
        #log "PGID of ssh ($relevant_pid) is $current_pgid"
      }
      get_current_pgid
      SSH_PGID=$current_pgid

      echo $SSH_PGID > #{TasteTester::Config.timestamp_file} && \
      # TODO: pull this from Host.touchcmd
      touch -t #{@ts} #{TasteTester::Config.timestamp_file} && \
      while true; do
        if ! [ -f "#{TasteTester::Config.timestamp_file}" ]; then
          log "Ending tunnel: timestamp file disappeared"
          break
        fi
        current_pid="$(cat #{TasteTester::Config.timestamp_file})"
        if ! [ "$current_pid" = "$SSH_PGID" ]; then
          log "Ending tunnel: timestamp PGID changed"
          break
        fi
        get_current_pgid
        if ! [ "$current_pgid" = "$SSH_PGID" ]; then
          log "Ending tunnel: timestamp PGID isn't ours"
          break
        fi
        sleep 60
      done
      EOS
    end
  end
end


text:
layout: post
title: Introducing molva.spb.ru
---

## How does one gets to know what's going on?

### A word on how do we interact with each other

Imagine a day without any word heard from other people.
Quite impossible and creepy, isn't it?

We talk to each other, we write emails, do texting by phone,
send messages, write blog posts, read blog posts, and comment them.

Every bit of information coming through our brain produces some reaction.
Most of the time it is *rejection*, but from time to time we *respond*.

There begins association between words heard and words replied.
These words are chained together, they form a new one species — **a conversation**.

### Ah, Twitter again

Yes, we do talk a lot at Twitter. It allows not only share your current thinking,
but also comment on someone else's current thinking. Twitter is tremendous source
of public conversations.

There question arises:

> # How can we summarize all the talking over social network and present the most valuable topics in user-friendly form?

Here at [molva.spb.ru](http://molva.spb.ru/), we are trying to answer this question for Russian segment of Twitter network.

### Why Twitter?

Twitter is rather popular, has nice API's, and is **simple**.

### Why Russian segment?

Whilst there exist tons of Twitter trend monitors for English, for Russian they are few and poor.
And Russian is both tricky and beatiful!

## What's the model

The big picture of [molva.spb.ru](http://molva.spb.ru/) is:

- reads [Twitter streaming API](https://dev.twitter.com/docs/api) 24/7
- greps only Russian posts
- finds replys and create conversation chains (post and reply pairs, actually)
- parses tweets with tremendous [Yandex Tomita Parser](http://api.yandex.ru/tomita/) and stores each tweet's stems
- selects top **N** most popular stems
- builds post-reply frequence distribution (which words are used to answer on which) and weights it, so most common reply-words get lowered and more specific ones get praised
- runs each-to-each *cosine similarity* for word profiles
- clusters word profiles into **M** clusters with *k-means algorithm*
- rank several trendy clusters and display them

The trend analysis is based on *"moving average"*:

- three time frames are taken: now, now - 3h, now - 6h
- frame size is 24h
- trend is fraction of actual word popularity versus expected

## Evaluation

Information presented by [molva.spb.ru](http://molva.spb.ru/) appears to have following broad topics:

- news
- weather
- time of day
- leasures
- general synonymy

The most appealing is ability to observe the *public opinion*, i.e. what does the group of people think about critical news, persons and so on.
The evaluation therefore is to find out how well is current molva.spb.ru main page correlated to top of news, available for example, at [Yandex News](http://news.yandex.ru/).
Can human pick one words group displayed at [molva.spb.ru](http://molva.spb.ru/) and find corresponding news article?

While evaluation is still in progress, and we don't have good numbers, graphs and tables to show, it's current results seem promising.

-----

Ждем вас в гости на сайте [molva.spb.ru](http://molva.spb.ru/)!


text:
export BASE_PATH=`pwd`

set -e -x

if ! findmnt /sys/fs/cgroup
then
  mount -t tmpfs none /sys/fs/cgroup
  mkdir /sys/fs/cgroup/blkio \
        /sys/fs/cgroup/cpu \
        /sys/fs/cgroup/cpuacct \
        /sys/fs/cgroup/devices \
        /sys/fs/cgroup/memory
  mount -t cgroup none /sys/fs/cgroup/blkio -o blkio
  mount -t cgroup none /sys/fs/cgroup/cpu -o cpu
  mount -t cgroup none /sys/fs/cgroup/cpuacct -o cpuacct
  mount -t cgroup none /sys/fs/cgroup/devices -o devices
  mount -t cgroup none /sys/fs/cgroup/memory -o memory
fi

go version

mkdir -p go/src/github.com/apcera
ln -s $BASE_PATH/kurma-source go/src/github.com/apcera/kurma
export GOPATH="$BASE_PATH/go"

cd go/src/github.com/apcera/kurma
novendor=$(go list ./... | grep -v /vendor/)
go test -i $novendor
go test -v $novendor


text:
require 'test_helper'

class StarredEntriesExportTest < ActiveSupport::TestCase
  test "should send email" do
    user = users(:ben)
    entry = create_entry(user.feeds.first)
    StarredEntry.create_from_owners(user, entry)

    assert_difference "ActionMailer::Base.deliveries.count", +1 do
      StarredEntriesExport.new().tap do |job|
        def job.upload_file(file)
          JSON.parse(File.open(file).read)
          Faker::Internet.url
        end
        job.perform(user.id)
      end
    end

  end
end


text:
using Orchard;
using OShop.PayPal.Models;

namespace OShop.PayPal.Services {
    public interface IPaypalSettingsService : IDependency {
        PaypalSettings GetSettings();
        void SetSettings(PaypalSettings Settings);
    }
}