text:
!+-----------------------------------------------------------------------+
!| Program       : Direct.f                                              |
!| Last modified : 07-16-2001                                            |
!| Written by    : Joerg Gablonsky (jmgablon@unity.ncsu.edu)             |
!|                 North Carolina State University                       |
!|                 Dept. of Mathematics                                  |
!| DIRECT is a method to solve problems of the form:                     |
!|              min f: Q --> R,                                          |
!| where f is the function to be minimized and Q is an n-dimensional     |
!| hyperrectangle given by the the following equation:                   |
!|                                                                       |
!|       Q={ x : l(i) <= x(i) <= u(i), i = 1,...,n }.                    |
!| Note: This version of DIRECT can also handle hidden constraints. By   |
!|       this we mean that the function may not be defined over the whole|
!|       hyperrectangle Q, but only over a subset, which is not given    |
!|       analytically.                                                   |
!|                                                                       |
!| We now give a brief outline of the algorithm:                         |
!|                                                                       |
!|   The algorithm starts with mapping the hyperrectangle Q to the       |
!|   n-dimensional unit hypercube. DIRECT then samples the function at   |
!|   the center of this hypercube and at 2n more points, 2 in each       |
!|   coordinate direction. Uisng these function values, DIRECT then      |
!|   divides the domain into hyperrectangles, each having exactly one of |
!|   the sampling points as its center. In each iteration, DIRECT chooses|
!|   some of the existing hyperrectangles to be further divided.         |
!|   We provide two different strategies of how to decide which          |
!|   hyperrectangles DIRECT divides and several different convergence    |
!|   criteria.                                                           |
!|                                                                       |
!|   DIRECT was designed to solve problems where the function f is       |
!|   Lipschitz continues. However, DIRECT has proven to be effective on  |
!|   more complex problems than these.                                   |
!+-----------------------------------------------------------------------+

      SUBROUTINE Direct(fcn, x, n, eps, maxf, maxT, fmin, l, u,&
                       algmethod, Ierror, logfile, &
                       fglobal, fglper, volper, sigmaper,&
                       iidata, iisize, ddata, idsize, cdata, icsize,&
                       verbose)

!+-----------------------------------------------------------------------+
!|    SUBROUTINE Direct                                                  |
!| On entry                                                              |
!|     fcn -- The argument containing the name of the user-supplied      |
!|            SUBROUTINE that returns values for the function to be      |
!|            minimized.                                                 |
!|       n -- The dimension of the problem.                              |
!|     eps -- Exceeding value. If eps > 0, we use the same epsilon for   |
!|            all iterations. If eps < 0, we use the update formula from |
!|            Jones:                                                     |
!|               eps = max(1.D-4*abs(fmin),epsfix),                      |
!|            where epsfix = abs(eps), the absolute value of eps which is|
!|            passed to the function.                                    |
!|    maxf -- The maximum number of function evaluations.                |
!|    maxT -- The maximum number of iterations.                          |
!|            Direct stops when either the maximum number of iterations  |
!|            is reached or more than maxf function-evalutions were made.|
!|       l -- The lower bounds of the hyperbox.                          |
!|       u -- The upper bounds of the hyperbox.                          |
!|algmethod-- Choose the method, that is either use the original method  |
!|            as described by Jones et.al. (0) or use our modification(1)|
!| logfile -- File-Handle for the logfile. DIRECT expects this file to be|
!|            opened and closed by the user outside of DIRECT. We moved  |
!|            this to the outside so the user can add extra informations |
!|            to this file before and after the call to DIRECT.          |
!| fglobal -- Function value of the global optimum. If this value is not |
!|            known (that is, we solve a real problem, not a testproblem)|
!|            set this value to -1.D100 and fglper (see below) to 0.D0.  |
!|  fglper -- Terminate the optimization when the percent error          |
!|                100(f_min - fglobal)/max(1,abs(fglobal)) < fglper.     |
!|  volper -- Terminate the optimization when the volume of the          |
!|            hyperrectangle S with f(c(S)) = fmin is less then volper   |
!|            percent of the volume of the original hyperrectangle.      |
!|sigmaper -- Terminate the optimization when the measure of the         |
!|            hyperrectangle S with f(c(S)) = fmin is less then sigmaper.|
!|                                                                       |
!| User data that is passed through without being changed:               |
!|  iidata -- Integer Array of user data. This array of size iisize is   |
!|            passed to the function to be optimized and can be used to  |
!|            transfer data to this function. The contents are not       |
!|            changed by DIRECT.                                         |
!|  iisize -- Size of array iidata.                                      |
!|   ddata -- DOUBLE PRECISION array of user data. See iidata.           |
!|  idsize -- Size of array ddata.                                       |
!|   cdata -- Character array. See iidata.                               |
!|  icsize -- Size of array ddata.                                       |
!|                                                                       |
!| On return                                                             |
!|                                                                       |
!|       x -- The final point obtained in the optimization process.      |
!|            X should be a good approximation to the global minimum     |
!|            for the function within the hyper-box.                     |
!|                                                                       |
!|    fmin -- The value of the function at x.                            |
!|  Ierror -- Error flag. If Ierror is lower 0, an error has occured. The|
!|            values of Ierror mean                                      |
!|            Fatal errors :                                             |
!|             -1   u(i) <= l(i) for some i.                             |
!|             -2   maxf is too large.                                   |
!|             -3   Initialization in DIRpreprc failed.                  |
!|             -4   Error in DIRSamplepoints, that is there was an error |
!|                  in the creation of the sample points.                |
!|             -5   Error in DIRSamplef, that is an error occured while  |
!|                  the function was sampled.                            |
!|             -6   Error in DIRDoubleInsert, that is an error occured   |
!|                  DIRECT tried to add all hyperrectangles with the same|
!|                  size and function value at the center. Either        |
!|                  increase maxdiv or use our modification (Jones = 1). |
!|            Termination values :                                       |
!|              1   Number of function evaluations done is larger then   |
!|                  maxf.                                                |
!|              2   Number of iterations is equal to maxT.               |
!|              3   The best function value found is within fglper of    |
!|                  the (known) global optimum, that is                  |
!|                   100(fmin - fglobal/max(1,|fglobal|))  < fglper.     |
!|                  Note that this termination signal only occurs when   |
!|                  the global optimal value is known, that is, a test   |
!|                  function is optimized.                               |
!|              4   The volume of the hyperrectangle with fmin at its    |
!|                  center is less than volper percent of the volume of  |
!|                  the original hyperrectangle.                         |
!|              5   The measure of the hyperrectangle with fmin at its   |
!|                  center is less than sigmaper.                        |
!|                                                                       |
!| SUBROUTINEs used :                                                    |
!|                                                                       |
!| DIRheader, DIRInitSpecific, DIRInitList, DIRpreprc, DIRInit, DIRChoose|
!| DIRDoubleInsert, DIRGet_I, DIRSamplepoints, DIRSamplef, DIRDivide     |
!| DIRInsertList, DIRreplaceInf, DIRWritehistbox, DIRsummary, Findareas  |
!|                                                                       |
!| Functions used :                                                      |
!|                                                                       |
!| DIRgetMaxdeep, DIRgetlevel                                            |
!+-----------------------------------------------------------------------+

      IMPLICIT NONE
!+-----------------------------------------------------------------------+
!| Parameters                                                            |
!+-----------------------------------------------------------------------+

!+-----------------------------------------------------------------------+
!| The maximum of function evaluations allowed.                          |
!| The maximum dept of the algorithm.                                    |
!| The maximum number of divisions allowed.                              |
!| The maximal dimension of the problem.                                 |
!+-----------------------------------------------------------------------+
      INTEGER maxfunc, maxdeep, maxdiv, MaxDim, mdeep
      PARAMETER (Maxfunc = 2000)
      PARAMETER (maxdeep = 600)
      PARAMETER (maxdiv = 3000)
      PARAMETER (MaxDim = 64)

!+-----------------------------------------------------------------------+
!| Global Variables.                                                     |
!+-----------------------------------------------------------------------+
      INTEGER JONES
      COMMON /directcontrol/ JONES

!+-----------------------------------------------------------------------+
!| EXTERNAL Variables.                                                   |
!+-----------------------------------------------------------------------+
      EXTERNAL fcn
      INTEGER n, maxf, maxT, algmethod, Ierror, logfile, dwrit, verbose
      DOUBLE PRECISION  x(n),fmin,eps,l(n),u(n)
      DOUBLE PRECISION fglobal, fglper, volper, sigmaper

!+-----------------------------------------------------------------------+
!| User Variables.                                                       |
!| These can be used to pass user defined data to the function to be     |
!| optimized.                                                            |
!+-----------------------------------------------------------------------+
      INTEGER iisize, idsize, icsize
      INTEGER iidata(iisize)
      DOUBLE PRECISION ddata(idsize)
      Character*40 cdata(icsize)

!+-----------------------------------------------------------------------+
!| Place to define, if needed, some application-specific variables.      |
!| Note: You should try to use the arrays defined above for this.        |
!+-----------------------------------------------------------------------+

!+-----------------------------------------------------------------------+
!| End of application - specific variables !                             |
!+-----------------------------------------------------------------------+

!+-----------------------------------------------------------------------+
!| Internal variables :                                                  |
!|       f -- values of functions.                                       |
!|divfactor-- Factor used for termination with known global minimum.     |
!|  anchor -- anchors of lists with deepness i, -1 is anchor for list of |
!|            NaN - values.                                              |
!|       S -- List of potentially optimal points.                        |
!|   point -- lists                                                      |
!|    free -- first free position                                        |
!|       c -- midpoints of arrays                                        |
!|  thirds -- Precalculated values of 1/3^i.                             |
!|  levels -- Length of intervals.                                       |
!|  length -- Length of intervall (index)                                |
!|       t -- actual iteration                                           |
!|       j -- loop-variable                                              |
!| actdeep -- the actual minimal interval-length index                   |
!|  Minpos -- position of the actual minimum                             |
!|    file -- The filehandle for a datafile.                             |
!|  maxpos -- The number of intervalls, which are truncated.             |
!|    help -- A help variable.                                           |
!| numfunc -- The actual number of function evaluations.                 |
!|   file2 -- The filehandle for an other datafile.                      |
!|  ArrayI -- Array with the indexes of the sides with maximum length.   |
!|    maxi -- Number of directions with maximal side length.             |
!|    oops -- Flag which shows if anything went wrong in the             |
!|            initialisation.                                            |
!|   cheat -- Obsolete. If equal 1, we don't allow Ktilde > kmax.        |
!|  writed -- If writed=1, store final division to plot with Matlab.     |
!|   List2 -- List of indicies of intervalls, which are to be truncated. |
!|       i -- Another loop-variable.                                     |
!|actmaxdeep-- The actual maximum (minimum) of possible Interval length. |
!|  oldpos -- The old index of the minimum. Used to print only, if there |
!|            is a new minimum found.                                    |
!|  tstart -- The start of the outer loop.                               |
!|   start -- The postion of the starting point in the inner loop.       |
!| Newtosample -- The total number of points to sample in the inner loop.|
!|       w -- Array used to divide the intervalls                        |
!|    kmax -- Obsolete. If cheat = 1, Ktilde was not allowed to be larger|
!|            than kmax. If Ktilde > kmax, we set ktilde = kmax.         |
!|   delta -- The distance to new points from center of old hyperrec.    |
!|    pos1 -- Help variable used as an index.                            |
!| version -- Store the version number of DIRECT.                        |
!| oldmaxf -- Store the original function budget.                        |
!|increase -- Flag used to keep track if function budget was increased   |
!|            because no feasible point was found.                       |
!| freeold -- Keep track which index was free before. Used with          |
!|            SUBROUTINE DIRReplaceInf.                                  |
!|actdeep_div-- Keep track of the current depths for divisions.          |
!|    oldl -- Array used to store the original bounds of the domain.     |
!|    oldu -- Array used to store the original bounds of the domain.     |
!|  epsfix -- If eps < 0, we use Jones update formula. epsfix stores the |
!|            absolute value of epsilon.                                 |
!|iepschange-- flag iepschange to store if epsilon stays fixed or is     |
!|             changed.                                                  |
!|DIRgetMaxdeep-- Function to calculate the level of a hyperrectangle.   |
!|DIRgetlevel-- Function to calculate the level and stage of a hyperrec. |
!|    fmax -- Keep track of the maximum value of the function found.     |
!|Ifeasiblef-- Keep track if a feasible point has  been found so far.    |
!|             Ifeasiblef = 0 means a feasible point has been found,     |
!|             Ifeasiblef = 1 no feasible point has been found.          |
!|   dwrit -- Controls the level of output. So far not used a lot, set to|
!|            0. If its value is set to 2, more output, exspecially from |
!|            Subroutine DIRChoose.                                      |
!+-----------------------------------------------------------------------+
      DOUBLE PRECISION  f(maxfunc,2), divfactor
      INTEGER anchor(-1:maxdeep),S(maxdiv,2)
      INTEGER point(maxfunc), free
      DOUBLE PRECISION  c(maxfunc,MaxDim)
      DOUBLE PRECISION  thirds(0:maxdeep),levels(0:maxdeep)
      INTEGER length(maxfunc,MaxDim),t,j,actdeep
      INTEGER Minpos,file,maxpos,help,numfunc,file2
      INTEGER ArrayI(MaxDim),maxi,oops,cheat,writed
      INTEGER List2(MaxDim,2),i,actmaxdeep,oldpos
      INTEGER tstart,start,Newtosample
      DOUBLE PRECISION  w(MaxDim),kmax, delta
      INTEGER pos1
!+-----------------------------------------------------------------------+
!| JG 09/25/00 Version counter.                                          |
!+-----------------------------------------------------------------------+
      INTEGER version
      INTEGER oldmaxf,increase, freeold
!+-----------------------------------------------------------------------+
!| JG 09/24/00 Add another actdeep to keep track of the current depths   |
!|             for divisions.                                            |
!+-----------------------------------------------------------------------+
      INTEGER actdeep_div
      DOUBLE PRECISION  oldl(MaxDim), oldu(MaxDim)
!+-----------------------------------------------------------------------+
!|JG 01/13/01 Added epsfix for epsilon update. If eps < 0, we use Jones  |
!|            update formula. epsfix stores the absolute value of epsilon|
!|            then. Also added flag iepschange to store if epsilon stays |
!|            fixed or is changed.                                       |
!+-----------------------------------------------------------------------+
      DOUBLE PRECISION epsfix
      INTEGER iepschange

      INTEGER DIRGetMaxdeep, DIRgetlevel
!+-----------------------------------------------------------------------+
!| JG 01/22/01 fmax is used to keep track of the maximum value found.    |
!+-----------------------------------------------------------------------+
      DOUBLE PRECISION fmax
!+-----------------------------------------------------------------------+
!| JG 01/22/01 Ifeasiblef is used to keep track if a feasible point has  |
!|             been found so far. Ifeasiblef = 0 means a feasible point  |
!|             has been found, Ifeasiblef = 1 if not.                    |
!| JG 03/09/01 IInfeasible is used to keep track if an infeasible point  |
!|             has been found. IInfeasible > 0 means a infeasible point  |
!|             has been found, IInfeasible = 0 if not.                   |
!+-----------------------------------------------------------------------+
      INTEGER Ifeasiblef, IInfesiblef

!+-----------------------------------------------------------------------+
!+-----------------------------------------------------------------------+
!|                            Start of code.                             |
!+-----------------------------------------------------------------------+
!+-----------------------------------------------------------------------+
      writed = 0
      dwrit  = 0
      JONES  = algmethod
!+-----------------------------------------------------------------------+
!| Save the upper and lower bounds.                                      |
!+-----------------------------------------------------------------------+
      DO 150,i=1,n
        oldu(i) = u(i)
        oldl(i) = l(i)
150   CONTINUE

!+-----------------------------------------------------------------------+
!| Set version.                                                          |
!+-----------------------------------------------------------------------+
      version = 204
!+-----------------------------------------------------------------------+
!| Set parameters.                                                       |
!|    If cheat > 0, we do not allow \tilde{K} to be larger than kmax, and|
!|    set \tilde{K} to set value if necessary. Not used anymore.         |
!+-----------------------------------------------------------------------+
      cheat = 0
      kmax  = 1.D10
      mdeep = maxdeep
!+-----------------------------------------------------------------------+
!| Write the header of the logfile.                                      |
!+-----------------------------------------------------------------------+
      CALL DIRheader(logfile, version, x, n, eps, maxf, maxT, l, u,&
                    algmethod, maxfunc, maxdeep, fglobal, fglper,&
                    Ierror, epsfix, iepschange, volper, sigmaper,&
                    iidata, iisize, ddata, idsize, cdata,&
                    icsize, verbose)
!+-----------------------------------------------------------------------+
!| If an error has occured while writing the header (we do some checking |
!| of variables there), return to the main program.                      |
!+-----------------------------------------------------------------------+
      IF (Ierror .lt. 0) then
         RETURN
      END IF
!+-----------------------------------------------------------------------+
!| If the known global minimum is equal 0, we cannot divide by it.       |
!| Therefore we set it to 1. If not, we set the divisionfactor to the    |
!| absolute value of the global minimum.                                 |
!+-----------------------------------------------------------------------+
      IF (fglobal .EQ. 0.D0) then
         divfactor = 1.D0
      ELSE
         divfactor = abs(fglobal)
      END IF

!+-----------------------------------------------------------------------+
!| Start of application-specific initialisation.                         |
!+-----------------------------------------------------------------------+
      CALL DIRInitSpecific(x,n,&
        iidata, iisize, ddata, idsize, cdata, icsize)
!+-----------------------------------------------------------------------+
!| End of application-specific initialisation.                           |
!+-----------------------------------------------------------------------+

!+-----------------------------------------------------------------------+
!| Save the budget given by the user. The variable maxf will be changed  |
!| if in the beginning no feasible points are found.                     |
!+-----------------------------------------------------------------------+
      oldmaxf = maxf
      increase = 0
!+-----------------------------------------------------------------------+
!| Initialiase the lists.                                                |
!+-----------------------------------------------------------------------+
      CALL DIRInitList(anchor,free,point,f,maxfunc,maxdeep)
!+-----------------------------------------------------------------------+
!| Call the routine to initialise the mapping of x from the n-dimensional|
!| unit cube to the hypercube given by u and l. If an error occured,     |
!| give out a error message and return to the main program with the error|
!| flag set.                                                             |
!| JG 07/16/01 Changed call to remove unused data.                       |
!+-----------------------------------------------------------------------+
      CALL DIRpreprc(u,l,n,l,u,oops)
      IF (oops .GT. 0) THEN
        if (verbose == 1) Write(*,10005)
        if (verbose == 1) Write(logfile,10005)
        IError = -3
        Return
      END IF
      tstart = 2
!+-----------------------------------------------------------------------+
!| Initialise the algorithm DIRECT.                                      |
!+-----------------------------------------------------------------------+
!+-----------------------------------------------------------------------+
!| Added variable to keep track of the maximum value found.              |
!+-----------------------------------------------------------------------+
      CALL DIRInit(f,fcn,c,length,actdeep,point,anchor,free,&
        dwrit,logfile,ArrayI,maxI,List2,w,x,l,u,fmin,minpos,&
        thirds,levels,maxfunc,maxdeep,n,MaxDim,fmax,Ifeasiblef,&
        IInfesiblef, Ierror,&
        iidata, iisize, ddata, idsize, cdata, icsize)
!+-----------------------------------------------------------------------+
!| Added error checking.                                                 |
!+-----------------------------------------------------------------------+
      IF (Ierror .lt. 0) then
         IF (Ierror .eq. -4) THEN
            if (verbose == 1) Write(*,10006)
            if (verbose == 1) Write(logfile,10006)
            return
         END IF
         IF (Ierror .eq. -5) THEN
            if (verbose == 1) Write(*,10007)
            if (verbose == 1) Write(logfile,10007)
            return
         END IF
      END IF

      numfunc = 1 + maxI + maxI
      actmaxdeep = 1
      oldpos = 0
      tstart = 2
!+-----------------------------------------------------------------------+
!| If no feasible point has been found, give out the iteration, the      |
!| number of function evaluations and a warning. Otherwise, give out     |
!| the iteration, the number of function evaluations done and fmin.      |
!+-----------------------------------------------------------------------+
      IF (Ifeasiblef .gt. 0) then
        if (verbose == 1) write(*,10012) tstart-1,numfunc
        if (verbose == 1) write(logfile,10012) t,numfunc
      ELSE
        if (verbose == 1) Write(*,10002) numfunc, fmin, fmax
        if (verbose == 1) Write(logfile,10003) tstart-1,numfunc,fmin
      END IF
!+-----------------------------------------------------------------------+
!+-----------------------------------------------------------------------+
!| Main loop!                                                            |
!+-----------------------------------------------------------------------+
!+-----------------------------------------------------------------------+
      DO 10,t=tstart,MaxT
!+-----------------------------------------------------------------------+
!| Choose the sample points. The indices of the sample points are stored |
!| in the list S.                                                        |
!+-----------------------------------------------------------------------+
        actdeep = actmaxdeep
        CALL DIRChoose(anchor,S,maxdeep,f,fmin,eps,levels,maxpos,&
          length,maxfunc,maxdeep,maxdiv,n,logfile,dwrit,cheat,kmax,&
          Ifeasiblef)
!+-----------------------------------------------------------------------+
!| Add other hyperrectangles to S, which have the same level and the same|
!| function value at the center as the ones found above (that are stored |
!| in S). This is only done if we use the original DIRECT algorithm.     |
!| JG 07/16/01 Added Errorflag.                                          |
!+-----------------------------------------------------------------------+
        IF (algmethod .EQ. 0) THEN
          CALL DIRDoubleInsert(anchor, S, maxpos, point, f,&
                maxdeep, maxfunc, maxdiv, Ierror)
          IF (Ierror .eq. -6) THEN
              if (verbose == 1) Write(*,10020)
              if (verbose == 1) Write(*,10021)
              if (verbose == 1) Write(*,10022)
              if (verbose == 1) Write(*,10023)
              if (verbose == 1) Write(logfile,10020)
              if (verbose == 1) Write(logfile,10021)
              if (verbose == 1) Write(logfile,10022)
              if (verbose == 1) Write(logfile,10023)
              return
          END IF
        ENDIF
        oldpos = minpos
!+-----------------------------------------------------------------------+
!| Initialise the number of sample points in this outer loop.            |
!+-----------------------------------------------------------------------+
        Newtosample = 0
        DO 20,j=1,maxpos
           actdeep = S(j,2)
!+-----------------------------------------------------------------------+
!| If the actual index is a point to sample, do it.                      |
!+-----------------------------------------------------------------------+
           IF (S(j,1) .GT. 0) THEN
!+-----------------------------------------------------------------------+
!| JG 09/24/00 Calculate the value delta used for sampling points.       |
!+-----------------------------------------------------------------------+
              actdeep_div = DIRGetmaxdeep(S(j,1),length,maxfunc,n)
              delta = thirds(actdeep_div+1)
              actdeep = S(j,2)
!+-----------------------------------------------------------------------+
!| If the current dept of division is only one under the maximal allowed |
!| dept, stop the computation.                                           |
!+-----------------------------------------------------------------------+
              IF (actdeep+1 .GE. mdeep) THEN
                 if (verbose == 1) Write(*,10004)
                 if (verbose == 1) write(logfile,10004)
                 Ierror = -6
                 GOTO 100
              END IF
              actmaxdeep = max(actdeep,actmaxdeep)
              help = S(j,1)
              IF (.NOT. (anchor(actdeep) .EQ. help)) THEN
                pos1 = anchor(actdeep)
                DO WHILE (.NOT. (point(pos1) .EQ. help))
                  pos1 = point(pos1)
                END DO
                point(pos1) = point(help)
              ELSE
                anchor(actdeep) = point(help)
              END IF
              IF (actdeep .lt. 0) then
                actdeep = f(help,1)
              END IF
!+-----------------------------------------------------------------------+
!| Get the Directions in which to decrease the intervall-length.         |
!+-----------------------------------------------------------------------+
              CALL DIRGet_I(length,help,ArrayI,maxI,n,maxfunc)
!+-----------------------------------------------------------------------+
!| Sample the function. To do this, we first calculate the points where  |
!| we need to sample the function. After checking for errors, we then do |
!| the actual evaluation of the function, again followed by checking for |
!| errors.                                                               |
!+-----------------------------------------------------------------------+
              CALL DIRSamplepoints(c,ArrayI,delta,help,&
                  start,length,dwrit,logfile,f,free,maxI,point,&
                  fcn,x,l,fmin,minpos,u,n,maxfunc,maxdeep,oops)
              IF (oops .GT. 0) THEN
                if (verbose == 1) Write(*,10006)
                if (verbose == 1) Write(logfile,10006)
                IError = -4
                return
              END IF
              Newtosample = newtosample + maxI
!+-----------------------------------------------------------------------+
!| JG 01/22/01 Added variable to keep track of the maximum value found.  |
!+-----------------------------------------------------------------------+
              CALL DIRSamplef(c,ArrayI,delta,help,start,&
                 length,dwrit,logfile,f,free,maxI,point,fcn,x,l,&
                 fmin,minpos,u,n,maxfunc,maxdeep,oops,fmax,&
                 Ifeasiblef,IInfesiblef,&
                 iidata, iisize, ddata, idsize, cdata, icsize)
              IF (oops .GT. 0) THEN
                if (verbose == 1) Write(*,10007)
                if (verbose == 1) Write(logfile,10007)
                IError = -5
                return
              END IF
!+-----------------------------------------------------------------------+
!| Divide the intervalls.                                                |
!+-----------------------------------------------------------------------+
              CALL DIRDivide(start,actdeep_div,length,point,&
                  ArrayI,help,List2,w,maxI,f,maxfunc,maxdeep,n)
!+-----------------------------------------------------------------------+
!| Insert the new intervalls into the list (sorted).                     |
!+-----------------------------------------------------------------------+
              CALL DIRInsertList(start,anchor,point,f,maxI,length,&
                         maxfunc,maxdeep,n,help)
!+-----------------------------------------------------------------------+
!| Increase the number of function evaluations.                          |
!+-----------------------------------------------------------------------+
              numfunc = numfunc + maxI + maxI
           END IF
!+-----------------------------------------------------------------------+
!| End of main loop.                                                     |
!+-----------------------------------------------------------------------+
20      CONTINUE
!+-----------------------------------------------------------------------+
!| If there is a new minimum, show the actual iteration, the number of   |
!| function evaluations, the minimum value of f (so far) and the position|
!| in the array.                                                         |
!+-----------------------------------------------------------------------+
        IF (oldpos .LT. minpos) THEN
          if (verbose == 1) Write(*,10002) numfunc,fmin, fmax
          if (verbose == 1) Write(logfile,10003) t,numfunc,fmin
        END IF
!+-----------------------------------------------------------------------+
!| If no feasible point has been found, give out the iteration, the      |
!| number of function evaluations and a warning.                         |
!+-----------------------------------------------------------------------+
        IF (Ifeasiblef .gt. 0) then
          if (verbose == 1) write(*,10012) t,numfunc
          if (verbose == 1) write(logfile,10012) t,numfunc
        END IF
!+-----------------------------------------------------------------------+
!+-----------------------------------------------------------------------+
!|                       Termination Checks                              |
!+-----------------------------------------------------------------------+

!+-----------------------------------------------------------------------+
!| JG 01/22/01 Calculate the index for the hyperrectangle at which       |
!|             fmin is assumed. We then calculate the volume of this     |
!|             hyperrectangle and store it in delta. This delta can be   |
!|             used to stop DIRECT once the volume is below a certain    |
!|             percentage of the original volume. Since the original     |
!|             is 1 (scaled), we can stop once delta is below a certain  |
!|             percentage, given by volper.                              |
!+-----------------------------------------------------------------------+
        Ierror = Jones
        Jones = 0
        actdeep_div = DIRGetlevel(minpos,length,maxfunc,n)
        Jones = Ierror
!+-----------------------------------------------------------------------+
!| JG 07/16/01 Use precalculated values to calculate volume.             |
!+-----------------------------------------------------------------------+
        delta = thirds(actdeep_div)*100
        IF (delta .LE. volper) THEN
           Ierror = 4
           if (verbose == 1) Write(*,10011) delta, volper
           if (verbose == 1) Write(logfile,10011) delta, volper
           GOTO 100
        END IF
!+-----------------------------------------------------------------------+
!| JG 01/23/01 Calculate the measure for the hyperrectangle at which     |
!|             fmin is assumed. If this measure is smaller then sigmaper,|
!|             we stop DIRECT.                                           |
!+-----------------------------------------------------------------------+
        actdeep_div = DIRGetlevel(minpos,length,maxfunc,n)
        delta = levels(actdeep_div)
        IF (delta .LE. sigmaper) THEN
           Ierror = 5
           if (verbose == 1) Write(*,10013) delta, sigmaper
           if (verbose == 1) Write(logfile,10013) delta, sigmaper
           GOTO 100
        END IF
!+-----------------------------------------------------------------------+
!| If the best found function value is within fglper of the (known)      |
!| global minimum value, terminate. This only makes sense if this optimal|
!| value is known, that is, in test problems.                            |
!+-----------------------------------------------------------------------+
        IF ((100*(fmin - fglobal)/divfactor) .LE. fglper) THEN
           Ierror = 3
           if (verbose == 1) Write(*,10010)
           if (verbose == 1) Write(logfile,10010)
           GOTO 100
        END IF
!+-----------------------------------------------------------------------+
!| Find out if there are infeasible points which are near feasible ones. |
!| If this is the case, replace the function value at the center of the  |
!| hyper rectangle by the lowest function value of a nearby function.    |
!| If no infeasible points exist (IInfesiblef = 0), skip this.           |
!+-----------------------------------------------------------------------+
        IF (IInfesiblef .gt. 0) THEN
           CALL DIRreplaceInf(free,freeold,f,c,thirds,length,anchor,&
            point,u,l,maxfunc,maxdeep,maxdim,n,logfile, fmax)
        ENDIF
        freeold = free
!+-----------------------------------------------------------------------+
!| If iepschange = 1, we use the epsilon change formula from Jones.      |
!+-----------------------------------------------------------------------+
        IF (iepschange .eq. 1) then
           eps = max(1.D-4*abs(fmin),epsfix)
        END IF
!+-----------------------------------------------------------------------+
!| If no feasible point has been found yet, set the maximum number of    |
!| function evaluations to the number of evaluations already done plus   |
!| the budget given by the user.                                         |
!| If the budget has already be increased, increase it again. If a       |
!| feasible point has been found, remark that and reset flag. No further |
!| increase is needed.                                                   |
!+-----------------------------------------------------------------------+
        IF (increase .eq. 1) then
           maxf = numfunc + oldmaxf
           IF (Ifeasiblef .eq. 0) then
           if (verbose == 1) write(logfile,10031) maxf
             increase = 0
           END IF
        END IF
!+-----------------------------------------------------------------------+
!| Check if the number of function evaluations done is larger than the   |
!| allocated budget. If this is the case, check if a feasible point was  |
!| found. If this is a case, terminate. If no feasible point was found,  |
!| increase the budget and set flag increase.                            |
!+-----------------------------------------------------------------------+
        IF (numfunc .GT. maxf) THEN
           IF (Ifeasiblef .eq. 0) then
              Ierror = 1
              if (verbose == 1) Write(*,10008)
              if (verbose == 1) Write(logfile,10008)
              GOTO 100
           ELSE
              increase = 1
              if (verbose == 1) write(logfile,10030) numfunc
              maxf = numfunc+ oldmaxf
           END IF
        END IF
10    CONTINUE
!+-----------------------------------------------------------------------+
!+-----------------------------------------------------------------------+
!| End of main loop.                                                     |
!+-----------------------------------------------------------------------+
!+-----------------------------------------------------------------------+

!+-----------------------------------------------------------------------+
!| The algorithm stopped after maxT iterations.                          |
!+-----------------------------------------------------------------------+
      Ierror = 2
      if (verbose == 1) Write(*,10009)
      if (verbose == 1) Write(logfile,10009)

100   CONTINUE
!+-----------------------------------------------------------------------+
!| Store the position of the minimum in x.                               |
!+-----------------------------------------------------------------------+
      DO 50,i=1,n
         x(i) = c(Minpos,i)*l(i)+l(i)*u(i)
         u(i) = oldu(i)
         l(i) = oldl(i)
50    CONTINUE
!+-----------------------------------------------------------------------+
!| Store the number of function evaluations in maxf.                     |
!+-----------------------------------------------------------------------+
      maxf = numfunc

!+-----------------------------------------------------------------------+
!| If needed, save the final division in a file for use with Matlab.     |
!+-----------------------------------------------------------------------+
      writed = 0
      IF (writed .EQ. 1) THEN
        file  = 12
        file2 =  0
        CALL DIRWritehistbox(point,f,thirds,c,anchor,actdeep,file,&
                    l,u,file2,maxfunc,maxdeep,n,MaxDim,length)
      END IF

!+-----------------------------------------------------------------------+
!| Give out a summary of the run.                                        |
!+-----------------------------------------------------------------------+
      CALL DIRsummary(logfile,x,l,u,n,fmin,fglobal, numfunc, Ierror,&
      verbose)

!+-----------------------------------------------------------------------+
!| Format statements.                                                    |
!+-----------------------------------------------------------------------+
10002  FORMAT(i5," & ",f18.10," & ",f18.10," \\\\ ")
10003  FORMAT(i5,'       ', i5,'     ',f18.10)
10004  FORMAT('WARNING : Maximum number of levels reached. Increase maxdeep.')
10005  FORMAT('WARNING : Initialisation in DIRpreprc failed.')
10006  FORMAT('WARNING : Error occured in routine DIRsamplepoints.')
10007  FORMAT('WARNING : Error occured in routine DIRsamplef.')
10008  FORMAT('DIRECT stopped: numfunc >= maxf.')
10009  FORMAT('DIRECT stopped: maxT iterations.')
10010  FORMAT('DIRECT stopped: fmin within fglper of global minimum.')
10011  FORMAT('DIRECT stopped: Volume of S_min is ',d9.2, '% < ',d9.2,'% of the original volume.')
10012  FORMAT('No feasible point found in ',I4,' iterations ', 'and ',I5,' function evaluations.')
10013  FORMAT('DIRECT stopped: Measure of S_min = ',d9.2,' < ' ,d9.2,'.')
10020  FORMAT('WARNING : Capacity of array S in DIRDoubleInsert reached. Increase maxdiv.')
10021  FORMAT('This means that there are a lot of hyperrectangles')
10022  FORMAT('with the same function value at the center. We')
10023  FORMAT('suggest to use our modification instead (Jones = 1)')

10030 FORMAT('DIRECT could not find a feasible ', 'point after ', I5, ' function evaluations. ', 'DIRECT continues until a feasible point is found.')
10031 FORMAT('DIRECT found a feasible point. ', 'The adjusted budget is now set to ',I5,'.')
      END


text:
using GitVersion.Core.Tests.Helpers;
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersion.OutputVariables;
using Microsoft.Extensions.DependencyInjection;

namespace GitVersion.App.Tests;

public sealed class ProgramFixture
{
    private readonly IEnvironment environment;
    private List<Action<IServiceCollection>> Overrides { get; } = new();
    private readonly Lazy<string> logger;
    private readonly Lazy<string?> output;

    private readonly string workingDirectory;

    public ProgramFixture(string workingDirectory = "")
    {
        this.workingDirectory = workingDirectory;
        var logBuilder = new StringBuilder();
        var logAppender = new TestLogAppender(m => logBuilder.AppendLine(m));
        ILog log = new Log(logAppender);

        var consoleBuilder = new StringBuilder();
        IConsole consoleAdapter = new TestConsoleAdapter(consoleBuilder);

        this.environment = new TestEnvironment();

        Overrides.Add(services =>
        {
            services.AddSingleton(log);
            services.AddSingleton(consoleAdapter);
            services.AddSingleton(this.environment);
        });

        this.logger = new Lazy<string>(() => logBuilder.ToString());
        this.output = new Lazy<string?>(() => consoleAdapter.ToString());
    }

    public void WithEnv(params KeyValuePair<string, string>[] envs)
    {
        foreach (var (key, value) in envs)
        {
            this.environment.SetEnvironmentVariable(key, value);
        }
    }

    public Task<ProgramFixtureResult> Run(string arg)
    {
        var args = arg.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
        return Run(args);
    }

    public async Task<ProgramFixtureResult> Run(params string[] args)
    {
        // Create the application and override registrations.
        var program = new Program(builder => Overrides.ForEach(action => action(builder)));

        if (!this.workingDirectory.IsNullOrWhiteSpace())
        {
            args = new[] { "-targetpath", this.workingDirectory }.Concat(args).ToArray();
        }
        await program.RunAsync(args);

        return new ProgramFixtureResult
        {
            ExitCode = System.Environment.ExitCode,
            Output = this.output.Value,
            Log = this.logger.Value
        };
    }
}

public class ProgramFixtureResult
{
    public int ExitCode { get; set; }
    public string? Output { get; set; }
    public string Log { get; set; }

    public VersionVariables? OutputVariables
    {
        get
        {
            if (Output.IsNullOrWhiteSpace()) return null;

            var jsonStartIndex = Output.IndexOf("{", StringComparison.Ordinal);
            var jsonEndIndex = Output.IndexOf("}", StringComparison.Ordinal);
            var json = Output.Substring(jsonStartIndex, jsonEndIndex - jsonStartIndex + 1);

            return VersionVariables.FromJson(json);
        }
    }

}

redpajama_set_name: RedPajamaGithub


text:
from django.test import TestCase
from .templatetags.version_info import *

class Version_Info_Test(TestCase):
    def test_version_info(self):
        self.assertTrue( ' at ' in site_version_info() )


text:
module Spree
  module Adjustable
    module Adjuster
      class Promotion < Spree::Adjustable::Adjuster::Base
        def update
          promo_adjustments = adjustments.competing_promos.reload.map { |a| a.update!(adjustable) }
          promos_total = promo_adjustments.compact.sum
          choose_best_promo_adjustment unless promos_total == 0
          promo_total = best_promo_adjustment.try(:amount).to_f if best_promo_adjustment.try(:promotion?)

          update_totals(promo_total)
        end

        private

        # Picks one (and only one) competing discount to be eligible for
        # this order. This adjustment provides the most discount, and if
        # two adjustments have the same amount, then it will pick the
        # latest one.
        def choose_best_promo_adjustment
          if best_promo_adjustment
            other_promotions = adjustments.competing_promos.where.not(id: best_promo_adjustment.id)
            other_promotions.update_all(eligible: false)
          end
        end

        def best_promo_adjustment
          @best_promo_adjustment ||= begin
            adjustments.competing_promos.eligible.reorder("amount ASC, created_at DESC, id DESC").first
          end
        end

        def update_totals(promo_total)
          promo_total ||= 0.0
          @totals[:promo_total] = promo_total
          @totals[:taxable_adjustment_total] += promo_total
        end
      end
    end
  end
end


text:
BSON Serializing a PHP resource should throw exception
--FILE--
<?php

require_once __DIR__ . '/../utils/basic.inc';

throws(function() {
    $a = array("stderr" => STDERR);

    $b = fromPHP($a);
}, "MongoDB\Driver\Exception\UnexpectedValueException");

throws(function() {
    $a = array("stderr" => STDERR, "stdout" => STDOUT);

    $b = fromPHP($a);
}, "MongoDB\Driver\Exception\UnexpectedValueException");

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
===DONE===


text:
package com.rollbar.api.payload.data;

import com.rollbar.api.json.JsonSerializable;

import java.util.HashMap;
import java.util.Map;

/**
 * Represents the server object sent to Rollbar.
 */
public class Server implements JsonSerializable {

  private final String host;

  private final String root;

  private final String branch;

  private final String codeVersion;

  private Server(Builder builder) {
    this.host = builder.host;
    this.root = builder.root;
    this.branch = builder.branch;
    this.codeVersion = builder.codeVersion;
  }

  /**
   * Getter.
   * @return the host.
   */
  public String getHost() {
    return host;
  }

  /**
   * Getter.
   * @return the root.
   */
  public String getRoot() {
    return root;
  }

  /**
   * Getter.
   * @return the branch.
   */
  public String getBranch() {
    return branch;
  }

  /**
   * Getter.
   * @return the code version.
   */
  public String getCodeVersion() {
    return codeVersion;
  }

  @Override
  public Map<String, Object> asJson() {
    Map<String, Object> values = new HashMap<>();

    if (host != null) {
      values.put("host", host);
    }
    if (root != null) {
      values.put("root", root);
    }
    if (branch != null) {
      values.put("branch", branch);
    }
    if (codeVersion != null) {
      values.put("code_version", codeVersion);
    }

    return values;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    Server server = (Server) o;

    if (host != null ? !host.equals(server.host) : server.host != null) {
      return false;
    }
    if (root != null ? !root.equals(server.root) : server.root != null) {
      return false;
    }
    if (branch != null ? !branch.equals(server.branch) : server.branch != null) {
      return false;
    }
    return codeVersion != null ? codeVersion.equals(server.codeVersion)
        : server.codeVersion == null;
  }

  @Override
  public int hashCode() {
    int result = host != null ? host.hashCode() : 0;
    result = 31 * result + (root != null ? root.hashCode() : 0);
    result = 31 * result + (branch != null ? branch.hashCode() : 0);
    result = 31 * result + (codeVersion != null ? codeVersion.hashCode() : 0);
    return result;
  }

  @Override
  public String toString() {
    return "Server{"
        + "host='" + host + '\''
        + ", root='" + root + '\''
        + ", branch='" + branch + '\''
        + ", codeVersion='" + codeVersion + '\''
        + '}';
  }

  /**
   * Builder class for {@link Notifier notifier}.
   */
  public static final class Builder {

    private String host;

    private String root;

    private String branch;

    private String codeVersion;

    /**
     * Constructor.
     */
    public Builder() {

    }

    /**
     * Constructor.
     *
     * @param server the {@link Server server} to initialize a new builder instance.
     */
    public Builder(Server server) {
      this.host = server.host;
      this.root = server.root;
      this.branch = server.branch;
      this.codeVersion = server.codeVersion;
    }

    /**
     * The host.
     *
     * @param host the host.
     * @return the builder instance.
     */
    public Builder host(String host) {
      this.host = host;
      return this;
    }

    /**
     * The file system root.
     *
     * @param root the root.
     * @return the builder instance.
     */
    public Builder root(String root) {
      this.root = root;
      return this;
    }

    /**
     * The current source control branch.
     *
     * @param branch the branch.
     * @return the builder instance.
     */
    public Builder branch(String branch) {
      this.branch = branch;
      return this;
    }

    /**
     * The current source control version (SHA, or name).
     *
     * @param codeVersion the code version.
     * @return the builder instance.
     */
    public Builder codeVersion(String codeVersion) {
      this.codeVersion = codeVersion;
      return this;
    }

    /**
     * Builds the {@link Server server}.
     *
     * @return the server.
     */
    public Server build() {
      return new Server(this);
    }
  }
}


text:
//go:build test_integration && test_local

package test

import (
	"context"
	"fmt"
	"testing"
	"time"

	"github.com/nuclio/nuclio/pkg/dockerclient"
	"github.com/nuclio/nuclio/pkg/functionconfig"
	pubsubtrigger "github.com/nuclio/nuclio/pkg/processor/trigger/pubsub"
	"github.com/nuclio/nuclio/pkg/processor/trigger/test"

	"cloud.google.com/go/pubsub"
	"github.com/stretchr/testify/suite"
	"google.golang.org/api/option"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"k8s.io/api/core/v1"
)

type testSuite struct {
	*triggertest.AbstractBrokerSuite

	// messaging
	client      *pubsub.Client
	topic       *pubsub.Topic
	projectID   string
	numMessages int

	// pubsub broker
	brokerPort          int
	brokerURL           string
	brokerContainerName string
}

func (suite *testSuite) SetupSuite() {
	var err error

	suite.projectID = "nuclio-test"

	// pubsub cluster
	suite.brokerPort = 9200
	suite.brokerContainerName = "nuclio-pubsub-server"
	suite.brokerURL = fmt.Sprintf("%s:%d", suite.BrokerHost, suite.brokerPort)
	suite.BrokerContainerNetworkName = "nuclio-pubsub-test"
	suite.LoggerName = "pubsub-test"

	suite.AbstractBrokerSuite.SetupSuite()
	suite.Logger.InfoWith("Creating pubsub broker resources",
		"brokerURL", suite.brokerURL)

	time.Sleep(10 * time.Second)

	// local address, insecure
	conn, err := grpc.Dial(suite.brokerURL, grpc.WithTransportCredentials(insecure.NewCredentials()))
	suite.Require().NoError(err)

	// pubsub client
	ctx := context.TODO()
	suite.client, err = pubsub.NewClient(ctx,
		suite.projectID,
		option.WithGRPCConn(conn),
		option.WithTelemetryDisabled(),
		option.WithoutAuthentication(),
	)
	suite.Require().NoError(err)

	// create topic
	suite.topic, err = suite.client.CreateTopic(ctx, "nuclio-test-topic")
	suite.Require().NoError(err, "Failed to create topic")

	suite.Logger.InfoWith("Created topic",
		"topic", suite.topic)

	suite.numMessages = 10

}

func (suite *testSuite) TestReceiveRecords() {
	pubsubContainerBrokerURL := fmt.Sprintf("%s:%d", suite.brokerContainerName, suite.brokerPort)
	createFunctionOptions := suite.GetDeployOptions("pubsub-event-recorder", suite.FunctionPaths["python"])
	createFunctionOptions.FunctionConfig.Spec.Platform = functionconfig.Platform{
		Attributes: map[string]interface{}{
			"network": suite.BrokerContainerNetworkName,
		},
	}
	createFunctionOptions.FunctionConfig.Spec.Env = []v1.EnvVar{
		{
			Name:  "PUBSUB_EMULATOR_HOST",
			Value: pubsubContainerBrokerURL,
		},
	}

	createFunctionOptions.FunctionConfig.Spec.Triggers = map[string]functionconfig.Trigger{
		"test-pubsub": {
			Kind: "pubsub",
			URL:  pubsubContainerBrokerURL,
			Attributes: map[string]interface{}{
				"subscriptions": []pubsubtrigger.Subscription{
					{
						Topic: suite.topic.ID(),
					},
				},
				"projectID":     suite.projectID,
				"NoCredentials": true,
			},
		},
	}

	triggertest.InvokeEventRecorder(&suite.AbstractBrokerSuite.TestSuite,
		suite.BrokerHost,
		createFunctionOptions,
		map[string]triggertest.TopicMessages{
			suite.topic.ID(): {
				NumMessages: suite.numMessages,
			},
		},
		nil,
		suite.publishMessageToTopic)
}

// GetContainerRunInfo returns information about the broker container
func (suite *testSuite) GetContainerRunInfo() (string, *dockerclient.RunOptions) {
	return "gcr.io/iguazio/nuclio/simple-pubsub-emulator:v1", &dockerclient.RunOptions{
		Network: suite.BrokerContainerNetworkName,
		Ports: map[int]int{
			suite.brokerPort: suite.brokerPort,
		},
		ContainerName: suite.brokerContainerName,
		Env: map[string]string{
			"PROJECT": suite.projectID,
		},
	}
}

func (suite *testSuite) publishMessageToTopic(topic string, body string) error {
	suite.Logger.DebugWith("Publishing message to topic", "topic", topic, "body", body)
	suite.topic.Publish(context.TODO(), &pubsub.Message{
		Data: []byte(body),
	})
	suite.Logger.Debug("Successfully published a message")
	return nil
}

func TestIntegrationSuite(t *testing.T) {
	if testing.Short() {
		return
	}

	testSuiteInstance := &testSuite{}
	testSuiteInstance.AbstractBrokerSuite = triggertest.NewAbstractBrokerSuite(testSuiteInstance)
	suite.Run(t, testSuiteInstance)
}


text:
/*
 * Licensed 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.
 */

package org.apache.zeppelin.hbase;

import org.apache.zeppelin.interpreter.*;
import org.apache.zeppelin.scheduler.Scheduler;
import org.apache.zeppelin.scheduler.SchedulerFactory;
import org.jruby.embed.LocalContextScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.jruby.embed.ScriptingContainer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * Support for Hbase Shell. All the commands documented here
 * http://hbase.apache.org/book.html#shell is supported.
 *
 * Requirements:
 * Hbase Shell should be installed on the same machine. To be more specific, the following dir.
 * should be available: https://github.com/apache/hbase/tree/master/hbase-shell/src/main/ruby
 * Hbase Shell should be able to connect to the Hbase cluster from terminal. This makes sure
 * that the client is configured properly.
 *
 * The interpreter takes 3 config parameters:
 * hbase.home: Root dir. where hbase is installed. Default is /usr/lib/hbase/
 * hbase.ruby.sources: Dir where shell ruby code is installed.
 *                          Path is relative to hbase.home. Default: lib/ruby
 * hbase.irb.load: (Testing only) Default is true.
 *                      Whether to load irb in the interpreter.
 */
public class HbaseInterpreter extends Interpreter {
  private Logger logger = LoggerFactory.getLogger(HbaseInterpreter.class);
  private ScriptingContainer scriptingContainer;

  private StringWriter writer;

  static {
    Interpreter.register("hbase", "hbase", HbaseInterpreter.class.getName(),
        new InterpreterPropertyBuilder()
            .add("hbase.home", "/usr/lib/hbase/", "Installation dir. of Hbase")
            .add("hbase.ruby.sources", "lib/ruby",
                "Path to Ruby scripts relative to 'hbase.home'")
            .add("hbase.test.mode", "false", "Disable checks for unit and manual tests")
            .build());
  }

  public HbaseInterpreter(Properties property) {
    super(property);
  }

  @Override
  public void open() {
    this.scriptingContainer  = new ScriptingContainer(LocalContextScope.SINGLETON);
    this.writer = new StringWriter();
    scriptingContainer.setOutput(this.writer);

    if (!Boolean.parseBoolean(getProperty("hbase.test.mode"))) {
      String hbase_home = getProperty("hbase.home");
      String ruby_src = getProperty("hbase.ruby.sources");
      Path abs_ruby_src = Paths.get(hbase_home, ruby_src).toAbsolutePath();

      logger.info("Home:" + hbase_home);
      logger.info("Ruby Src:" + ruby_src);

      File f = abs_ruby_src.toFile();
      if (!f.exists() || !f.isDirectory()) {
        throw new InterpreterException("hbase ruby sources is not available at '" + abs_ruby_src
            + "'");
      }

      logger.info("Absolute Ruby Source:" + abs_ruby_src.toString());
      // hirb.rb:41 requires the following system property to be set.
      Properties sysProps = System.getProperties();
      sysProps.setProperty("hbase.ruby.sources", abs_ruby_src.toString());

      Path abs_hirb_path = Paths.get(hbase_home, "bin/hirb.rb");
      try {
        FileInputStream fis = new FileInputStream(abs_hirb_path.toFile());
        this.scriptingContainer.runScriptlet(fis, "hirb.rb");
        fis.close();
      } catch (IOException e) {
        throw new InterpreterException(e.getCause());
      }
    }
  }

  @Override
  public void close() {
    if (this.scriptingContainer != null) {
      this.scriptingContainer.terminate();
    }
  }

  @Override
  public InterpreterResult interpret(String cmd, InterpreterContext interpreterContext) {
    try {
      logger.info(cmd);
      this.writer.getBuffer().setLength(0);
      this.scriptingContainer.runScriptlet(cmd);
      this.writer.flush();
      logger.debug(writer.toString());
      return new InterpreterResult(InterpreterResult.Code.SUCCESS, writer.getBuffer().toString());
    } catch (Throwable t) {
      logger.error("Can not run '" + cmd + "'", t);
      return new InterpreterResult(InterpreterResult.Code.ERROR, t.getMessage());
    }
  }

  @Override
  public void cancel(InterpreterContext context) {}

  @Override
  public FormType getFormType() {
    return FormType.SIMPLE;
  }

  @Override
  public int getProgress(InterpreterContext context) {
    return 0;
  }

  @Override
  public Scheduler getScheduler() {
    return SchedulerFactory.singleton().createOrGetFIFOScheduler(
        HbaseInterpreter.class.getName() + this.hashCode());
  }

  @Override
  public List<String> completion(String buf, int cursor) {
    return null;
  }

}


text:
layout: post
title: Nginx et Apache
description: "Serveur Web"
tags: [réseau, serveur, web, apache, nginx]
---

Coupler Nginx (front-end) à Apache 2.4 (back-end)

> Prérequis :

> - Debian Jessie
> - Apache2

Modification du port d'écoute de Apache :

```lua
sudo vim /etc/apache2/ports.conf

Listen 8080

<IfModule ssl_module>
        Listen 443
</IfModule>

<IfModule mod_gnutls.c>
        Listen 443
</IfModule>
```

Modifier les VirtualHost :
```lua
sudo vim /etc/apache2/sites-available/000-default.conf
```

Téléchargemebt de Nginx :

```lua
sudo vim /etc/apt/sources.list
deb http://packages.dotdeb.org jessie all
deb http://nginx.org/packages/mainline/debian/ jessie nginx

wget http://www.dotdeb.org/dotdeb.gpg
sudo apt-key add dotdeb.gpg

wget http://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key

sudo apt-get update

sudo apt-get install nginx
```

Configuration de Nginx
Fichier proxi :

```lua
sudo vim /etc/nginx/conf.d/proxy.conf

proxy_redirect          off;
proxy_set_header        Host            $host;
proxy_set_header        X-Real-IP       $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffer_size   16k;
proxy_buffers       32   16k;
proxy_busy_buffers_size 64k;
```

EN COURS

<!-- more -->


text:
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.34209
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Zaggoware.BugTracker.Locale {
    using System;

    /// <summary>
    ///   A strongly-typed resource class, for looking up localized strings, etc.
    /// </summary>
    // This class was auto-generated by the StronglyTypedResourceBuilder
    // class via a tool like ResGen or Visual Studio.
    // To add or remove a member, edit your .ResX file then rerun ResGen
    // with the /str option, or rebuild your VS project.
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    public class Titles {

        private static global::System.Resources.ResourceManager resourceMan;

        private static global::System.Globalization.CultureInfo resourceCulture;

        [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        internal Titles() {
        }

        /// <summary>
        ///   Returns the cached ResourceManager instance used by this class.
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        public static global::System.Resources.ResourceManager ResourceManager {
            get {
                if (object.ReferenceEquals(resourceMan, null)) {
                    global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Zaggoware.BugTracker.Locale.Titles", typeof(Titles).Assembly);
                    resourceMan = temp;
                }
                return resourceMan;
            }
        }

        /// <summary>
        ///   Overrides the current thread's CurrentUICulture property for all
        ///   resource lookups using this strongly typed resource class.
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        public static global::System.Globalization.CultureInfo Culture {
            get {
                return resourceCulture;
            }
            set {
                resourceCulture = value;
            }
        }

        /// <summary>
        ///   Looks up a localized string similar to About.
        /// </summary>
        public static string About {
            get {
                return ResourceManager.GetString("About", resourceCulture);
            }
        }

        /// <summary>
        ///   Looks up a localized string similar to BugTracker.
        /// </summary>
        public static string ApplicationName {
            get {
                return ResourceManager.GetString("ApplicationName", resourceCulture);
            }
        }

        /// <summary>
        ///   Looks up a localized string similar to Bug archive.
        /// </summary>
        public static string BugArchive {
            get {
                return ResourceManager.GetString("BugArchive", resourceCulture);
            }
        }

        /// <summary>
        ///   Looks up a localized string similar to Bugs.
        /// </summary>
        public static string Bugs {
            get {
                return ResourceManager.GetString("Bugs", resourceCulture);
            }
        }

        /// <summary>
        ///   Looks up a localized string similar to Change password.
        /// </summary>
        public static string ChangePassword {
            get {
                return ResourceManager.GetString("ChangePassword", resourceCulture);
            }
        }

        /// <summary>
        ///   Looks up a localized string similar to Dashboard.
        /// </summary>
        public static string Dashboard {
            get {
                return ResourceManager.GetString("Dashboard", resourceCulture);
            }
        }

        /// <summary>
        ///   Looks up a localized string similar to My account.
        /// </summary>
        public static string EditAccount {
            get {
                return ResourceManager.GetString("EditAccount", resourceCulture);
            }
        }

        /// <summary>
        ///   Looks up a localized string similar to Login.
        /// </summary>
        public static string Login {
            get {
                return ResourceManager.GetString("Login", resourceCulture);
            }
        }

        /// <summary>
        ///   Looks up a localized string similar to OrganizationArchive.
        /// </summary>
        public static string OrganizationArchive {
            get {
                return ResourceManager.GetString("OrganizationArchive", resourceCulture);
            }
        }

        /// <summary>
        ///   Looks up a localized string similar to Organizations.
        /// </summary>
        public static string Organizations {
            get {
                return ResourceManager.GetString("Organizations", resourceCulture);
            }
        }

        /// <summary>
        ///   Looks up a localized string similar to Project archive.
        /// </summary>
        public static string ProjectArchive {
            get {
                return ResourceManager.GetString("ProjectArchive", resourceCulture);
            }
        }

        /// <summary>
        ///   Looks up a localized string similar to Projects.
        /// </summary>
        public static string Projects {
            get {
                return ResourceManager.GetString("Projects", resourceCulture);
            }
        }

        /// <summary>
        ///   Looks up a localized string similar to User manager.
        /// </summary>
        public static string Users {
            get {
                return ResourceManager.GetString("Users", resourceCulture);
            }
        }
    }
}


text:
package helpers

import "errors"

type errReader bool

func (e *errReader) Read([]byte) (n int, err error) {
	return 0, errors.New("bad things")
}


text:
package gov.nih.nci.ispy.web.struts.form;

import gov.nih.nci.ispy.service.common.TimepointType;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.struts.util.LabelValueBean;

public class P53QueryForm extends BaseForm implements Serializable {

    private String analysisResultName = "";
    private String[] timepoints;
    private Collection timepointCollection = new ArrayList();
    private String[] mutationStatus;
    private Collection mutationStatusCollection = new ArrayList();
    private String[] mutationType;
    private Collection mutationTypeCollection = new ArrayList();

    public P53QueryForm(){
        for (TimepointType timepointType : TimepointType.values()){
            timepointCollection.add(new LabelValueBean(timepointType.toString(),timepointType.name()));
        }

    }

	public String getAnalysisResultName() {
		return analysisResultName;
	}
	public void setAnalysisResultName(String analysisResultName) {
		this.analysisResultName = analysisResultName;
	}
	public String[] getTimepoints() {
		return timepoints;
	}
	public void setTimepoints(String[] timepoints) {
		this.timepoints = timepoints;
	}
	public Collection getTimepointCollection() {
		return timepointCollection;
	}
	public void setTimepointCollection(Collection timepointCollection) {
		this.timepointCollection = timepointCollection;
	}
	public String[] getMutationStatus() {
		return mutationStatus;
	}
	public void setMutationStatus(String[] mutationStatus) {
		this.mutationStatus = mutationStatus;
	}
	public Collection getMutationStatusCollection() {
		return mutationStatusCollection;
	}
	public void setMutationStatusCollection(Collection mutationStatusCollection) {
		this.mutationStatusCollection = mutationStatusCollection;
	}
	public String[] getMutationType() {
		return mutationType;
	}
	public void setMutationType(String[] mutationType) {
		this.mutationType = mutationType;
	}
	public Collection getMutationTypeCollection() {
		return mutationTypeCollection;
	}
	public void setMutationTypeCollection(Collection mutationTypeCollection) {
		this.mutationTypeCollection = mutationTypeCollection;
	}

}


text:
<?php declare(strict_types=1);

namespace ApiClients\Tests\Client\Travis\Resource\Async;

use ApiClients\Client\Travis\ApiSettings;
use ApiClients\Client\Travis\Resource\Build;
use ApiClients\Tools\ResourceTestUtilities\AbstractResourceTest;

class BuildTest extends AbstractResourceTest
{
    public function getSyncAsync(): string
    {
        return 'Async';
    }

    public function getClass(): string
    {
        return Build::class;
    }

    public function getNamespace(): string
    {
        return Apisettings::NAMESPACE;
    }
}


text:
/* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */
/*
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MIT
 *
 * Portions created by Alan Antonuk are Copyright (c) 2012-2013
 * Alan Antonuk. All Rights Reserved.
 *
 * Portions created by Mike Steinert are Copyright (c) 2012-2013
 * Mike Steinert. All Rights Reserved.
 *
 * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
 * All Rights Reserved.
 *
 * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
 * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * ***** END LICENSE BLOCK *****
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <stdint.h>
#include <amqp_ssl_socket.h>
#include <amqp_framing.h>

#include "utils.h"

int main(int argc, char const *const *argv)
{
  char const *hostname;
  int port, status;
  char const *exchange;
  char const *bindingkey;
  char const *queue;
  amqp_socket_t *socket;
  amqp_connection_state_t conn;

  if (argc < 6) {
    fprintf(stderr, "Usage: amqps_unbind host port exchange bindingkey queue "
            "[cacert.pem [verifypeer] [verifyhostname] [key.pem cert.pem]]\n");
    return 1;
  }

  hostname = argv[1];
  port = atoi(argv[2]);
  exchange = argv[3];
  bindingkey = argv[4];
  queue = argv[5];

  conn = amqp_new_connection();

  socket = amqp_ssl_socket_new(conn);
  if (!socket) {
    die("creating SSL/TLS socket");
  }

  amqp_ssl_socket_set_verify_peer(socket, 0);
  amqp_ssl_socket_set_verify_hostname(socket, 0);

  if (argc > 6) {
    int nextarg = 7;
    status = amqp_ssl_socket_set_cacert(socket, argv[6]);
    if (status) {
      die("setting CA certificate");
    }
    if (argc > nextarg && !strcmp("verifypeer", argv[nextarg])) {
      amqp_ssl_socket_set_verify_peer(socket, 1);
      nextarg++;
    }
    if (argc > nextarg && !strcmp("verifyhostname", argv[nextarg])) {
      amqp_ssl_socket_set_verify_hostname(socket, 1);
      nextarg++;
    }
    if (argc > nextarg + 1) {
      status =
          amqp_ssl_socket_set_key(socket, argv[nextarg + 1], argv[nextarg]);
      if (status) {
        die("setting client cert");
      }
    }
  }

  status = amqp_socket_open(socket, hostname, port);
  if (status) {
    die("opening SSL/TLS connection");
  }

  die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"),
                    "Logging in");
  amqp_channel_open(conn, 1);
  die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel");

  amqp_queue_unbind(conn, 1,
                    amqp_cstring_bytes(queue),
                    amqp_cstring_bytes(exchange),
                    amqp_cstring_bytes(bindingkey),
                    amqp_empty_table);
  die_on_amqp_error(amqp_get_rpc_reply(conn), "Unbinding");

  die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "Closing channel");
  die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection");
  die_on_error(amqp_destroy_connection(conn), "Ending connection");
  return 0;
}


text:
module Ebay # :nodoc:
  module Types # :nodoc:
    class ProductStateCode
      extend Enumerable
      extend Enumeration
      Update = 'Update'
      UpdateMajor = 'UpdateMajor'
      UpdateNoDetails = 'UpdateNoDetails'
      Merge = 'Merge'
      Delete = 'Delete'
    end
  end
end


text:
var port = 3000;

var io = require("socket.io")();
var crypto = require("crypto");

var connections = 0;

io.on("connection", function(socket) {
	connections++;
	log(connections + " connections.", "connect");
	socket.on("chat", function(message) {
		log(message.id.substr(0, 6) + ": " + message.content, "message");
		var sha1 = crypto.createHash("sha1");
		sha1.update(message.id);
		message.content = htmlspecialchars(message.content);
		message.id = sha1.digest("hex").substr(0, 6);
		socket.broadcast.emit("chat", message);
	});
	socket.on("disconnect", function() {
		connections--;
		log(connections + " connections.", "disconn");
	});
});

io.listen(port);
log("Welcome to Kinggai Server.");
log("Listening on *:" + port + ".");

function htmlspecialchars(s) {
    s = s.replace(/&/g, "&");
    s = s.replace(/</g, "<");
    s = s.replace(/>/g, ">");
    s = s.replace(/"/g, """);
    s = s.replace(/'/g, "'");
    return s;
};

function log(message, type) {
	console.log("[" + new Date().toISOString() + "] (" + (type || "systlog") + ") " + message);
}


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<TITLE>
edu.cmu.ri.createlab.rss.readers (Finch API)
</TITLE>

<META NAME="date" CONTENT="2011-05-31">

<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../../../../stylesheet.css" TITLE="Style">

      Overview
    Package
      Class
      Deprecated
      Help

 PREV PACKAGE
 NEXT PACKAGE

  FRAMES
 NO FRAMES

  All Classes

Package edu.cmu.ri.createlab.rss.readers

Class Summary

EarthquakeReader

 EarthquakeReader accesses USGS recent earthquake data.

MovieReader

 MovieReader accesses rottentomatoes movie listings.

NewsReader

 NewsReader accesses yahoo top stories.

StockReader

 StockReader accesses stock quotes.

TuneReader

 TuneReader accesses the iTunes top 100 songs data.

WeatherReader

 WeatherReader accesses current weather conditions for north american cities.

WikipediaReader

 WikipediaReader accesses wikipedia articles.

WOTDReader

 WOTDReader accesses the dictionary.com word of the day.

      Overview
    Package
      Class
      Deprecated
      Help

 PREV PACKAGE
 NEXT PACKAGE

  FRAMES
 NO FRAMES

  All Classes


text:
require 'spec_helper'

describe Etherlite::Connection do
  let(:connection) { described_class.new uri }

  context "when uri uses http scheme" do
    let(:uri) { URI('http://www.etherlite.com') }
    let(:status_code) { 200 }
    let(:response) { { 'result' => 'foo' }.to_json }

    before do
      stub_request(:any, /^www\.etherlite\.com(.*)?/)
        .to_return(body: response, status: status_code)
    end

    describe "#ipc_call" do
      it "performs a json-rpc call with the given method and parameters" do
        connection.ipc_call('method', 'param1', 'param2')

        assert_requested(
          :post,
          'http://www.etherlite.com/',
          body: hash_including(jsonrpc: "2.0", method: 'method', params: ['param1', 'param2']),
          times: 1
        )
      end

      it "returns the content of the response's 'result' attribute" do
        expect(connection.ipc_call('method')).to eq 'foo'
      end
    end

    context "and response includes an 'error' attribute" do
      let(:response) { { error: { code: 1, message: 'notgood' } }.to_json }

      it "fails with a NodeError exception" do
        expect { connection.ipc_call('method') }.to raise_error Etherlite::NodeError
      end
    end

    context "and response has a non-200 status code" do
      let(:status_code) { 500 }

      it "fails with a RPCError exception" do
        expect { connection.ipc_call('method') }.to raise_error Etherlite::RPCError
      end
    end
  end
end


text:
namespace MassTransit.Caching
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Internals;

    public class GreenCache<TValue> :
        ICache<TValue>
        where TValue : class
    {
        readonly IDictionary<string, ICacheIndex<TValue>> _indices;
        readonly INodeTracker<TValue> _nodeTracker;

        /// <summary>
        /// Create a cache using the specified cache settings
        /// </summary>
        /// <param name="settings">The cache settings</param>
        public GreenCache(CacheSettings settings = null)
        {
            _indices = new Dictionary<string, ICacheIndex<TValue>>();

            _nodeTracker = new NodeTracker<TValue>(settings ?? new CacheSettings());
        }

        public CacheStatistics Statistics => _nodeTracker.Statistics;

        public IIndex<TKey, TValue> AddIndex<TKey>(string name, KeyProvider<TKey, TValue> keyProvider,
            MissingValueFactory<TKey, TValue> missingValueFactory = null)
        {
            lock (_indices)
            {
                if (_indices.ContainsKey(name))
                    throw new ArgumentException($"An index with the same name was already added: {name}", nameof(name));

                var index = new Index<TKey, TValue>(_nodeTracker, keyProvider);

                _indices[name] = index;

                return index;
            }
        }

        public IIndex<TKey, TValue> GetIndex<TKey>(string name)
        {
            lock (_indices)
            {
                if (!_indices.TryGetValue(name, out ICacheIndex<TValue> cacheIndex))
                    throw new ArgumentException($"An index named {name} was not found", nameof(name));

                if (cacheIndex is IIndex<TKey, TValue> index)
                    return index;

                throw new ArgumentException(
                    $"The index named {name} key type {TypeCache.GetShortName(cacheIndex.KeyType)} did not match the specified key type {TypeCache<TKey>.ShortName}",
                    nameof(TKey));
            }
        }

        public void Add(TValue value)
        {
            _nodeTracker.Add(value);
        }

        public void Clear()
        {
            _nodeTracker.Clear();
        }

        public IEnumerable<Task<TValue>> GetAll()
        {
            return _nodeTracker.GetAll().Select(x => x.Value);
        }

        public ConnectHandle Connect(ICacheValueObserver<TValue> observer)
        {
            return _nodeTracker.Connect(observer);
        }
    }
}


text:
'use strict';

var _       = require('lodash');
var SvgPath = require('svgpath');
var ucs2 = require('./lib/ucs2');
var svg     = require('./lib/svg');
var sfnt    = require('./lib/sfnt');

function svg2ttf(svgString, options) {
  var font = new sfnt.Font();
  var svgFont = svg.load(svgString);

  options = options || {};

  font.id = options.id || svgFont.id;
  font.familyName = options.familyname || svgFont.familyName || svgFont.id;
  font.copyright = options.copyright || svgFont.metadata;
  font.sfntNames.push({ id: 2, value: options.subfamilyname || 'Regular' }); // subfamily name
  font.sfntNames.push({ id: 4, value: options.fullname || svgFont.id }); // full name
  font.sfntNames.push({ id: 5, value: 'Version 1.0' }); // version ID for TTF name table
  font.sfntNames.push({ id: 6, value: options.fullname || svgFont.id }); // Postscript name for the font, required for OSX Font Book

  if (typeof options.ts !== 'undefined') {
    font.createdDate = font.modifiedDate = new Date(parseInt(options.ts, 10) * 1000);
  }

  // Try to fill font metrics or guess defaults
  //
  font.unitsPerEm   = svgFont.unitsPerEm || 1000;
  font.horizOriginX = svgFont.horizOriginX || 0;
  font.horizOriginY = svgFont.horizOriginY || 0;
  font.vertOriginX  = svgFont.vertOriginX || 0;
  font.vertOriginY  = svgFont.vertOriginY || 0;
  // need to correctly convert text values, use default (400) until compleete
  //font.weightClass = svgFont.weightClass;
  font.width    = svgFont.width || svgFont.unitsPerEm;
  font.height   = svgFont.height || svgFont.unitsPerEm;
  font.descent  = !isNaN(svgFont.descent) ? svgFont.descent : -font.vertOriginY;
  font.ascent   = svgFont.ascent || (font.unitsPerEm - font.vertOriginY);

  var glyphs = font.glyphs;
  var codePoints = font.codePoints;
  var ligatures = font.ligatures;

  function addCodePoint(codePoint, glyph) {
    if(codePoints[codePoint]) {
      // Ignore code points already defined
      return false;
    }
    codePoints[codePoint] = glyph;
    return true;
  }

  // add SVG glyphs to SFNT font
  _.forEach(svgFont.glyphs, function (svgGlyph) {
    var glyph = new sfnt.Glyph();

    glyph.name = svgGlyph.name;
    glyph.d = svgGlyph.d;
    glyph.height = !isNaN(svgGlyph.height) ? svgGlyph.height : font.height;
    glyph.width = !isNaN(svgGlyph.width) ? svgGlyph.width : font.width;
    glyphs.push(glyph);

    svgGlyph.sfntGlyph = glyph;

    _.forEach(svgGlyph.unicode, function(codePoint) {
      addCodePoint(codePoint, glyph);
    });
  });

  var missingGlyph;

  // add missing glyph to SFNT font
  // also, check missing glyph existance and single instance
  if (svgFont.missingGlyph) {
    missingGlyph = new sfnt.Glyph();
    missingGlyph.d = svgFont.missingGlyph.d;
    missingGlyph.height = !isNaN(svgFont.missingGlyph.height) ? svgFont.missingGlyph.height : font.height;
    missingGlyph.width = !isNaN(svgFont.missingGlyph.width) ? svgFont.missingGlyph.width : font.width;
  } else {
    missingGlyph = _.find(glyphs, function(glyph) {
      return glyph.name === '.notdef';
    });
  }
  if(!missingGlyph) { // no missing glyph and .notdef glyph, we need to create missing glyph
    missingGlyph = new sfnt.Glyph();
  }

  // Create glyphs for all characters used in ligatures
  _.forEach(svgFont.ligatures, function(svgLigature) {
    var ligature = {
        ligature: svgLigature.ligature,
        unicode: svgLigature.unicode,
        glyph: svgLigature.glyph.sfntGlyph
    };
    _.forEach(ligature.unicode, function(charPoint) {
      // We need to have a distinct glyph for each code point so we can reference it in GSUB
      var glyph = new sfnt.Glyph();
      var added = addCodePoint(charPoint, glyph);
      if(added) {
        glyph.name = ucs2.encode([charPoint]);
        glyphs.push(glyph);
      }
    });
    ligatures.push(ligature);
  });

  // Missing Glyph needs to have index 0
  if(glyphs.indexOf(missingGlyph) !== -1) {
    glyphs.splice(glyphs.indexOf(missingGlyph), 1);
  }
  glyphs.unshift(missingGlyph);

  var nextID = 0;
  //add IDs
  _.forEach(glyphs, function(glyph) {
    glyph.id = nextID;
    nextID++;
  });

  _.forEach(glyphs, function (glyph) {

    //SVG transformations
    var svgPath = new SvgPath(glyph.d)
      .abs()
      .unshort()
      .unarc()
      .iterate(svg.cubicToQuad);
    var sfntContours = svg.toSfntCoutours(svgPath);

    // Add contours to SFNT font
    glyph.contours = _.map(sfntContours, function (sfntContour) {
      var contour = new sfnt.Contour();

      contour.points = _.map(sfntContour, function (sfntPoint) {
        var point = new sfnt.Point();
        point.x = sfntPoint.x;
        point.y = sfntPoint.y;
        point.onCurve = sfntPoint.onCurve;
        return point;
      });

      return contour;
    });
  });

  var ttf = sfnt.toTTF(font);
  return ttf;
}

module.exports = svg2ttf;


text:
title: "Crypt"
description: "Encryption overlay remote"
date: "2016-07-28"
---

<i class="fa fa-lock"></i>Crypt
----------------------------------------

The `crypt` remote encrypts and decrypts another remote.

To use it first set up the underlying remote following the config
instructions for that remote.  You can also use a local pathname
instead of a remote which will encrypt and decrypt from that directory
which might be useful for encrypting onto a USB stick for example.

First check your chosen remote is working - we'll call it
`remote:path` in these docs.  Note that anything inside `remote:path`
will be encrypted and anything outside won't.  This means that if you
are using a bucket based remote (eg S3, B2, swift) then you should
probably put the bucket in the remote `s3:bucket`. If you just use
`s3:` then rclone will make encrypted bucket names too (if using file
name encryption) which may or may not be what you want.

Now configure `crypt` using `rclone config`. We will call this one
`secret` to differentiate it from the `remote`.

```
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n
name> secret
Type of storage to configure.
Choose a number from below, or type in your own value
 1 / Amazon Drive
   \ "amazon cloud drive"
 2 / Amazon S3 (also Dreamhost, Ceph, Minio)
   \ "s3"
 3 / Backblaze B2
   \ "b2"
 4 / Dropbox
   \ "dropbox"
 5 / Encrypt/Decrypt a remote
   \ "crypt"
 6 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
 7 / Google Drive
   \ "drive"
 8 / Hubic
   \ "hubic"
 9 / Local Disk
   \ "local"
10 / Microsoft OneDrive
   \ "onedrive"
11 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
12 / Yandex Disk
   \ "yandex"
Storage> 5
Remote to encrypt/decrypt.
Normally should contain a ':' and a path, eg "myremote:path/to/dir",
"myremote:bucket" or "myremote:"
remote> remote:path
How to encrypt the filenames.
Choose a number from below, or type in your own value
 1 / Don't encrypt the file names.  Adds a ".bin" extension only.
   \ "off"
 2 / Encrypt the filenames see the docs for the details.
   \ "standard"
filename_encryption> 2
Password or pass phrase for encryption.
y) Yes type in my own password
g) Generate random password
y/g> y
Enter the password:
password:
Confirm the password:
password:
Password or pass phrase for salt. Optional but recommended.
Should be different to the previous password.
y) Yes type in my own password
g) Generate random password
n) No leave this optional password blank
y/g/n> g
Password strength in bits.
64 is just about memorable
128 is secure
1024 is the maximum
Bits> 128
Your password is: JAsJvRcgR-_veXNfy_sGmQ
Use this password?
y) Yes
n) No
y/n> y
Remote config
--------------------
[secret]
remote = remote:path
filename_encryption = standard
password = *** ENCRYPTED ***
password2 = *** ENCRYPTED ***
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y
```

**Important** The password is stored in the config file is lightly
obscured so it isn't immediately obvious what it is.  It is in no way
secure unless you use config file encryption.

A long passphrase is recommended, or you can use a random one.  Note
that if you reconfigure rclone with the same passwords/passphrases
elsewhere it will be compatible - all the secrets used are derived
from those two passwords/passphrases.

Note that rclone does not encrypt
  * file length - this can be calcuated within 16 bytes
  * modification time - used for syncing

## Specifying the remote ##

In normal use, make sure the remote has a `:` in. If you specify the
remote without a `:` then rclone will use a local directory of that
name.  So if you use a remote of `/path/to/secret/files` then rclone
will encrypt stuff to that directory.  If you use a remote of `name`
then rclone will put files in a directory called `name` in the current
directory.

If you specify the remote as `remote:path/to/dir` then rclone will
store encrypted files in `path/to/dir` on the remote. If you are using
file name encryption, then when you save files to
`secret:subdir/subfile` this will store them in the unencrypted path
`path/to/dir` but the `subdir/subpath` bit will be encrypted.

Note that unless you want encrypted bucket names (which are difficult
to manage because you won't know what directory they represent in web
interfaces etc), you should probably specify a bucket, eg
`remote:secretbucket` when using bucket based remotes such as S3,
Swift, Hubic, B2, GCS.

## Example ##

To test I made a little directory of files using "standard" file name
encryption.

```
plaintext/
├── file0.txt
├── file1.txt
└── subdir
    ├── file2.txt
    ├── file3.txt
    └── subsubdir
        └── file4.txt
```

Copy these to the remote and list them back

```
$ rclone -q copy plaintext secret:
$ rclone -q ls secret:
        7 file1.txt
        6 file0.txt
        8 subdir/file2.txt
       10 subdir/subsubdir/file4.txt
        9 subdir/file3.txt
```

Now see what that looked like when encrypted

```
$ rclone -q ls remote:path
       55 hagjclgavj2mbiqm6u6cnjjqcg
       54 v05749mltvv1tf4onltun46gls
       57 86vhrsv86mpbtd3a0akjuqslj8/dlj7fkq4kdq72emafg7a7s41uo
       58 86vhrsv86mpbtd3a0akjuqslj8/7uu829995du6o42n32otfhjqp4/b9pausrfansjth5ob3jkdqd4lc
       56 86vhrsv86mpbtd3a0akjuqslj8/8njh1sk437gttmep3p70g81aps
```

Note that this retains the directory structure which means you can do this

```
$ rclone -q ls secret:subdir
        8 file2.txt
        9 file3.txt
       10 subsubdir/file4.txt
```

If don't use file name encryption then the remote will look like this
- note the `.bin` extensions added to prevent the cloud provider
attempting to interpret the data.

```
$ rclone -q ls remote:path
       54 file0.txt.bin
       57 subdir/file3.txt.bin
       56 subdir/file2.txt.bin
       58 subdir/subsubdir/file4.txt.bin
       55 file1.txt.bin
```

### File name encryption modes ###

Here are some of the features of the file name encryption modes

Off
  * doesn't hide file names or directory structure
  * allows for longer file names (~246 characters)
  * can use sub paths and copy single files

Standard
  * file names encrypted
  * file names can't be as long (~156 characters)
  * can use sub paths and copy single files
  * directory structure visibile
  * identical files names will have identical uploaded names
  * can use shortcuts to shorten the directory recursion

Cloud storage systems have various limits on file name length and
total path length which you are more likely to hit using "Standard"
file name encryption.  If you keep your file names to below 156
characters in length then you should be OK on all providers.

There may be an even more secure file name encryption mode in the
future which will address the long file name problem.

### Modified time and hashes ###

Crypt stores modification times using the underlying remote so support
depends on that.

Hashes are not stored for crypt.  However the data integrity is
protected by an extremely strong crypto authenticator.

## File formats ##

### File encryption ###

Files are encrypted 1:1 source file to destination object.  The file
has a header and is divided into chunks.

#### Header ####

  * 8 bytes magic string `RCLONE\x00\x00`
  * 24 bytes Nonce (IV)

The initial nonce is generated from the operating systems crypto
strong random number genrator.  The nonce is incremented for each
chunk read making sure each nonce is unique for each block written.
The chance of a nonce being re-used is miniscule.  If you wrote an
exabyte of data (10¹⁸ bytes) you would have a probability of
approximately 2×10⁻³² of re-using a nonce.

#### Chunk ####

Each chunk will contain 64kB of data, except for the last one which
may have less data.  The data chunk is in standard NACL secretbox
format. Secretbox uses XSalsa20 and Poly1305 to encrypt and
authenticate messages.

Each chunk contains:

  * 16 Bytes of Poly1305 authenticator
  * 1 - 65536 bytes XSalsa20 encrypted data

64k chunk size was chosen as the best performing chunk size (the
authenticator takes too much time below this and the performance drops
off due to cache effects above this).  Note that these chunks are
buffered in memory so they can't be too big.

This uses a 32 byte (256 bit key) key derived from the user password.

#### Examples ####

1 byte file will encrypt to

  * 32 bytes header
  * 17 bytes data chunk

49 bytes total

1MB (1048576 bytes) file will encrypt to

  * 32 bytes header
  * 16 chunks of 65568 bytes

1049120 bytes total (a 0.05% overhead).  This is the overhead for big
files.

### Name encryption ###

File names are encrypted segment by segment - the path is broken up
into `/` separated strings and these are encrypted individually.

File segments are padded using using PKCS#7 to a multiple of 16 bytes
before encryption.

They are then encrypted with EME using AES with 256 bit key. EME
(ECB-Mix-ECB) is a wide-block encryption mode presented in the 2003
paper "A Parallelizable Enciphering Mode" by Halevi and Rogaway.

This makes for determinstic encryption which is what we want - the
same filename must encrypt to the same thing otherwise we can't find
it on the cloud storage system.

This means that

  * filenames with the same name will encrypt the same
  * filenames which start the same won't have a common prefix

This uses a 32 byte key (256 bits) and a 16 byte (128 bits) IV both of
which are derived from the user password.

After encryption they are written out using a modified version of
standard `base32` encoding as described in RFC4648.  The standard
encoding is modified in two ways:

  * it becomes lower case (no-one likes upper case filenames!)
  * we strip the padding character `=`

`base32` is used rather than the more efficient `base64` so rclone can be
used on case insensitive remotes (eg Windows, Amazon Drive).

### Key derivation ###

Rclone uses `scrypt` with parameters `N=16384, r=8, p=1` with a an
optional user supplied salt (password2) to derive the 32+32+16 = 80
bytes of key material required.  If the user doesn't supply a salt
then rclone uses an internal one.

`scrypt` makes it impractical to mount a dictionary attack on rclone
encrypted data.  For full protection agains this you should always use
a salt.