Intersection of Two Rectangles with paintComponent

Question: I am writing a program that allows a user to paint rectangles onto a JLabel, and show the intersection and union of these rectangles.  I have setup the GUI for the class but am struggling to find a way to integrate the intersection and union methods from the rectangle class.  I know these methods work when used separately from the GUI.
When I try to run the program I keep getting an IndexOutOfBoundsException and the drawn rectangles are cleared from the gui.  The intersections of each rectangle should show up in a different color on the JLabel.  I tried to debug the program and for some reason when I create two rectangles and store them in my array list many many rectangle objects of the same characteristics are being created.
For the union method, a new rectangle should be create that contains all of the rectangles on the inside.
RectangleFrame1:
public class RectangleFrame1 extends JFrame implements ActionListener
{

    JPanel buttonPanel;
    JButton saveImage;
    JButton clearImage;
    JCheckBox intersections;
    JCheckBox union;
    RectangleLabel drawingArea;
    boolean intersect = false;
    boolean uni = false;

    public RectangleFrame1()
    {
        super();
        setTitle("Rectangles");
        setSize(600,600);
        setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        buttonPanel = new JPanel();
        buttonPanel.setBorder(BorderFactory.createLineBorder(Color.black));
        this.add(buttonPanel, BorderLayout.SOUTH);

        intersections = new JCheckBox("Draw Intersections");
        buttonPanel.add(intersections);
        intersections.addActionListener(this);
        intersections.setActionCommand("Intersections");

        union = new JCheckBox("Draw Union");
        buttonPanel.add(union);
        union.addActionListener(this);
        union.setActionCommand("Union");

        saveImage = new JButton("Save Image");
        saveImage.setMargin(new Insets(0,0,0,0));
        buttonPanel.add(saveImage);
        saveImage.addActionListener(this);
        saveImage.setActionCommand("Save Image");

        clearImage = new JButton("Clear Image");
        clearImage.setMargin(new Insets(0,0,0,0));
        buttonPanel.add(clearImage);
        clearImage.addActionListener(this);
        clearImage.setActionCommand("Clear Image");

        drawingArea = new RectangleLabel();
        drawingArea.setBorder(BorderFactory.createLineBorder(Color.blue));
        this.add(drawingArea, BorderLayout.CENTER);
        drawingArea.addMouseListener((MouseListener) drawingArea);
        drawingArea.addMouseMotionListener((MouseMotionListener) drawingArea);

    }

    @Override
    public void actionPerformed(ActionEvent arg0)
    {
        switch(arg0.getActionCommand())
        {
            case "Intersections":
            if(intersections.isSelected())
            {
                intersect = true;
            }
            else
            {
                intersect = false;
            }
            case "Union":
            if(union.isSelected())
            {
                uni = true;
            }
            else
            {
                uni = false;
            }
            case "Clear Image": drawingArea.clearImage();
            case "Save Image": drawingArea.saveImage();
        }

    }

    class RectangleLabel extends JLabel implements MouseListener, MouseMotionListener
    {

        Rectangle rectangle = null;
        int x, y, x2, y2;
        ArrayList<Rectangle> a = new ArrayList();
        int counter;
        public RectangleLabel()
        {
            super();
        }
        @Override
        public void mousePressed(MouseEvent arg0)
        {
            x = arg0.getX();
            y = arg0.getY();

            rectangle = new Rectangle(x, y, 0, 0);
        }

        @Override
        public void mouseDragged(MouseEvent arg0)
        {
            // TODO Auto-generated method stub
            x2 = arg0.getX();
            y2 = arg0.getY();

            if(rectangle.getX() > x2)
            {
                rectangle.setWidth(x-x2);
            }
            if(x2 > rectangle.getX())
            {
                rectangle.setWidth(x2-x);
            }
            if(y > y2)
            {
                rectangle.setHeight(y-y2);
            }
            if(y2 > y)
            {
                rectangle.setHeight(y2-y);
            }

            a.add(rectangle);
            counter++;
            repaint();

        }

        @Override
        public void mouseReleased(MouseEvent arg0)
        {
            repaint();
        }

        @Override
        public void mouseMoved(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseClicked(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            if(rectangle != null)
            {
                for(int i = 0; i < a.size(); i++)
                {
                    float thickness = 2;
                    ((Graphics2D) g).setStroke(new BasicStroke(thickness));
                    g.setColor(Color.BLUE);
                    g.drawRect(a.get(i).getX(), a.get(i).getY(), a.get(i).getWidth(), a.get(i).getHeight());
                    g.setColor(Color.gray);
                    g.fillRect(a.get(i).getX(), a.get(i).getY(), a.get(i).getWidth(), a.get(i).getHeight());
                }
            }

            if(intersect == true && counter > 0)
            {
                for(int h = 0; h < counter-1; h++)
                {
                    for(int j = 1; j < counter; j++)
                    {   if(a.get(h).overlaps(a.get(j)))
                        {
                            Rectangle rect = a.get(h).intersect(a.get(j));
                            g.setColor(Color.RED);
                            g.fillRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
                        }
                    }
                }
            }

            if(uni == true && counter > 0)
            {
                for(int h = 0; h < counter - 1; h++)
                {
                    for(int j = 1; j < counter; j++)
                    {
                        Rectangle rect = a.get(h).union(a.get(j));
                        g.setColor(Color.WHITE);
                        g.drawRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());

                    }
                }
            }
        }

        public void saveImage()
        {
            BufferedImage image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics2D aaaaa = image.createGraphics();
            aaaaa.setBackground(Color.WHITE);
            aaaaa.clearRect(0, 0, this.getWidth(), this.getHeight());
            this.paintAll(aaaaa);
            try
            {
                File output = new File("rectangle.png");
                ImageIO.write(image, "Rectangles", output);
            }
            catch(IOException ie)
            {

            }
        }
        public void clearImage()
        {
            a.clear();
            repaint();
        }

    }

}

Rectangle:
public class Rectangle
{

    private int x,y,width,height;

    public Rectangle(int x,int y,int width,int height)
    {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    public Rectangle(Rectangle a)
    {
        this.x = a.x;
        this.y = a.y;
        this.width = a.width;
        this.height = a.height;
    }

    public String toString()
    {
        return "Start: ("+x+","+y+"), Width: "+width+", Height: "+height+"\n";
    }

    public int getX()
    {
        return x;
    }

    public int getY()
    {
        return y;
    }

    public int getWidth()
    {
        return width;
    }

    public int getHeight()
    {
        return height;
    }

    public void setX(int x)
    {
        this.x = x;
    }

    public void setY(int y)
    {
        this.y = y;
    }

    public void setWidth(int width)
    {
        this.width = width;
    }

    public void setHeight(int height)
    {
        this.height = height;
    }

    public int area()
    {
        return width*height;
    }

    public boolean overlaps(Rectangle a)
    {
        if ((x>a.x+a.width) || (a.x>x+width) || (y>a.y+a.height) || (a.y>y+height))
        {
            return false;
        }
        return true;
    }

    public Rectangle intersect(Rectangle a)
    {
        if (!overlaps(a))
            return null;

        int left,right,top,bottom;

        if (x<a.x)
            left = a.x;
        else
            left = x;

        if (y<a.y)
            bottom = a.y;
        else
            bottom = y;

        if ((x+width)<(a.x+a.width))
            right = x+width;
        else
            right = a.x+a.width;

        if ((y+height)<(a.y+a.height))
            top = y+height;
        else
            top = a.y+a.height;

        return new Rectangle(left,bottom,right-left,top-bottom);
    }

    public Rectangle union(Rectangle a)
    {
        int left,right,top,bottom;

        if (x<a.x)
            left = x;
        else
            left = a.x;

        if (y<a.y)
            bottom = y;
        else
            bottom = a.y;

        if ((x+width)<(a.x+a.width))
            right = a.x+a.width;
        else
            right = x+width;

        if ((y+height)<(a.y+a.height))
            top = a.y+a.height;
        else
            top = y+height;

        return new Rectangle(left,bottom,right-left,top-bottom);
    }
}

Comment: What `Rectangle` class are you using?  As `java.awt.Rectangle` doesn't have a `setWidth` method
Comment: It is a rectangle class that I created.
Comment: You might want to provide that as well, otherwise we can't run the code...
Comment: Yeah.  The full stack trace would be good too, and indicate which lines of what you've posted are mentioned in it.  Otherwise, there's a lot to read through to look for possible exceptions.
Comment: Other than the union() method, was there some compelling reason to write your own Rectangle class? I've come up with my own implementations of common objects from time to time, just to get an idea of how things worked, but in practice, I've always gone back to the tried and true.
Comment: Where is the part where you're storing things in the array?
Comment: The only place I add a rectangle to the array is in the mouseDragged method.
Comment: @user2932450 This is a lot of code. Can you please post your stack trace as well as clearly indicate the line that is causing the exception?
Comment: What is the `counter` variable that you're using in `paintComponent`?  It seems to me that you'd get your exception if that were set to the wrong value.
Answer: There are a number of problems...
You're mouseDragged event handler is adding the same Rectangle to the a ArrayList, over and over again, increasing the counter value along with it.
This is not required.  All you need to do is add the Rectangle on the mousePressed event.  When mouseReleased is called, you could evaluate the Rectangle to check if the size is greater than 0x0 and remove it if it's not (ie remove any Rectangle whose size is 0x0)
Don't rely on the counter variable, it's too easy for the value to become misaligned with the size of the a List.  Instead, rely on a.size instead.
The switch statement in your ActionListener is evaluating the matching case, but also all the following cases below it.  This is a feature of switch.  If you don't want to evaluate the following cases, you need to add a break at the end of each case (or where ever you want to "break" the processing).
For example, when you click Draw Intersections, it is also evaluating Union, Clear Image and Save Image cases...
You could try using something like...
switch (arg0.getActionCommand()) {
    case "Intersections":
        intersect = intersections.isSelected();
        break;
    case "Union":
        uni = union.isSelected();
        break;
    case "Clear Image":
        drawingArea.clearImage();
        break;
    case "Save Image":
        drawingArea.saveImage();
        break;
}

instead...
Logic Errors in Painting Code...
There's also a logic area with your intersect paint code, where it is comparing a rectangle with itself, which ends up painting the whole rectangle.
Instead of
if (a.get(h).overlaps(a.get(j))) {

You might consider using
if (!a.get(h).equals(a.get(j)) && a.get(h).overlaps(a.get(j))) {

This is possibly occurring in you union painting logic as well, but I didn't really check that
Works fine for me...

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class RectangleFrame1 extends JFrame implements ActionListener {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                RectangleFrame1 frame = new RectangleFrame1();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    JPanel buttonPanel;
    JButton saveImage;
    JButton clearImage;
    JCheckBox intersections;
    JCheckBox union;
    RectangleLabel drawingArea;
    boolean intersect = false;
    boolean uni = false;

    public RectangleFrame1() {
        super();
        setTitle("Rectangles");
        setSize(600, 600);
//        setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        buttonPanel = new JPanel();
        buttonPanel.setBorder(BorderFactory.createLineBorder(Color.black));
        this.add(buttonPanel, BorderLayout.SOUTH);

        intersections = new JCheckBox("Draw Intersections");
        buttonPanel.add(intersections);
        intersections.addActionListener(this);
        intersections.setActionCommand("Intersections");

        union = new JCheckBox("Draw Union");
        buttonPanel.add(union);
        union.addActionListener(this);
        union.setActionCommand("Union");

        saveImage = new JButton("Save Image");
        saveImage.setMargin(new Insets(0, 0, 0, 0));
        buttonPanel.add(saveImage);
        saveImage.addActionListener(this);
        saveImage.setActionCommand("Save Image");

        clearImage = new JButton("Clear Image");
        clearImage.setMargin(new Insets(0, 0, 0, 0));
        buttonPanel.add(clearImage);
        clearImage.addActionListener(this);
        clearImage.setActionCommand("Clear Image");

        drawingArea = new RectangleLabel();
        drawingArea.setBorder(BorderFactory.createLineBorder(Color.blue));
        this.add(drawingArea, BorderLayout.CENTER);
        drawingArea.addMouseListener((MouseListener) drawingArea);
        drawingArea.addMouseMotionListener((MouseMotionListener) drawingArea);

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        switch (arg0.getActionCommand()) {
            case "Intersections":
                intersect = intersections.isSelected();
                break;
            case "Union":
                uni = union.isSelected();
                break;
            case "Clear Image":
                drawingArea.clearImage();
                break;
            case "Save Image":
                drawingArea.saveImage();
                break;
        }
        repaint();
    }

    class RectangleLabel extends JLabel implements MouseListener, MouseMotionListener {

        Rectangle rectangle = null;
        int x, y, x2, y2;
        ArrayList<Rectangle> a = new ArrayList();

        public RectangleLabel() {
            super();
        }

        @Override
        public void mousePressed(MouseEvent arg0) {
            x = arg0.getX();
            y = arg0.getY();

            rectangle = new Rectangle(x, y, 0, 0);
            a.add(rectangle);
        }

        @Override
        public void mouseDragged(MouseEvent arg0) {
            // TODO Auto-generated method stub
            x2 = arg0.getX();
            y2 = arg0.getY();

            if (rectangle.getX() > x2) {
                rectangle.setWidth(x - x2);
            }
            if (x2 > rectangle.getX()) {
                rectangle.setWidth(x2 - x);
            }
            if (y > y2) {
                rectangle.setHeight(y - y2);
            }
            if (y2 > y) {
                rectangle.setHeight(y2 - y);
            }

            repaint();

        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            rectangle = null;
            repaint();
        }

        @Override
        public void mouseMoved(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseClicked(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            for (Rectangle rect : a) {
                float thickness = 2;
                ((Graphics2D) g).setStroke(new BasicStroke(thickness));
                g.setColor(Color.BLUE);
                g.drawRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
                g.setColor(Color.gray);
                g.fillRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
            }

            if (intersect) {
                for (int h = 0; h < a.size() - 1; h++) {
                    for (int j = 1; j < a.size(); j++) {
                        if (!a.get(h).equals(a.get(j)) && a.get(h).overlaps(a.get(j))) {
                            Rectangle rect = a.get(h).intersect(a.get(j));
                            g.setColor(Color.RED);
                            g.fillRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
                        }
                    }
                }
            }

            if (uni) {
                for (int h = 0; h < a.size() - 1; h++) {
                    for (int j = 1; j < a.size(); j++) {
                        if (!a.get(h).equals(a.get(j)) && a.get(h).overlaps(a.get(j))) {
                            Rectangle rect = a.get(h).union(a.get(j));
                            g.setColor(Color.WHITE);
                            g.drawRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());

                        }
                    }
                }
            }
        }

        public void saveImage() {
            BufferedImage image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics2D aaaaa = image.createGraphics();
            aaaaa.setBackground(Color.WHITE);
            aaaaa.clearRect(0, 0, this.getWidth(), this.getHeight());
            this.paintAll(aaaaa);
            try {
                File output = new File("rectangle.png");
                ImageIO.write(image, "Rectangles", output);
            } catch (IOException ie) {

            }
        }

        public void clearImage() {
            a.clear();
            repaint();
        }

    }

    public class Rectangle {

        private int x, y, width, height;

        public Rectangle(int x, int y, int width, int height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }

        public Rectangle(Rectangle a) {
            this.x = a.x;
            this.y = a.y;
            this.width = a.width;
            this.height = a.height;
        }

        public String toString() {
            return "Start: (" + x + "," + y + "), Width: " + width + ", Height: " + height + "\n";
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        public int getWidth() {
            return width;
        }

        public int getHeight() {
            return height;
        }

        public void setX(int x) {
            this.x = x;
        }

        public void setY(int y) {
            this.y = y;
        }

        public void setWidth(int width) {
            this.width = width;
        }

        public void setHeight(int height) {
            this.height = height;
        }

        public int area() {
            return width * height;
        }

        public boolean overlaps(Rectangle a) {
            if ((x > a.x + a.width) || (a.x > x + width) || (y > a.y + a.height) || (a.y > y + height)) {
                return false;
            }
            return true;
        }

        public Rectangle intersect(Rectangle a) {
            if (!overlaps(a)) {
                return null;
            }

            int left, right, top, bottom;

            if (x < a.x) {
                left = a.x;
            } else {
                left = x;
            }

            if (y < a.y) {
                bottom = a.y;
            } else {
                bottom = y;
            }

            if ((x + width) < (a.x + a.width)) {
                right = x + width;
            } else {
                right = a.x + a.width;
            }

            if ((y + height) < (a.y + a.height)) {
                top = y + height;
            } else {
                top = a.y + a.height;
            }

            return new Rectangle(left, bottom, right - left, top - bottom);
        }

        public Rectangle union(Rectangle a) {
            int left, right, top, bottom;

            if (x < a.x) {
                left = x;
            } else {
                left = a.x;
            }

            if (y < a.y) {
                bottom = y;
            } else {
                bottom = a.y;
            }

            if ((x + width) < (a.x + a.width)) {
                right = a.x + a.width;
            } else {
                right = x + width;
            }

            if ((y + height) < (a.y + a.height)) {
                top = a.y + a.height;
            } else {
                top = y + height;
            }

            return new Rectangle(left, bottom, right - left, top - bottom);
        }
    }
}

Comment: I made all of these changes, but when I activate the intersection check box the intersections do not show.
Comment: That's because you forgot to call `repaint` in the `actionPerformed` method
Comment: This turns everything red. Not just the specific rectangle that is formed by the intersection.
Comment: Yes, see the last section of the answer which discuss a possible logic error in you intersection and union painting code...
Comment: I added that portion before I commented the first time.
Comment: I figured it out. Thanks


Elegantly initializing openmp threads in parallel for loop

Question: I have a for loop that uses a (somewhat complicated) counter object sp_ct to initialize an array.  The serial code looks like
sp_ct.depos(0);
for(int p=0;p<size; p++, sp_ct.increment() ) {
  in[p]=sp_ct.parable_at_basis();
}

My counter supports parallelization because it can be initialized to the state after p increments, leading to the following working code-fragment:
  int firstloop=-1;
#pragma omp parallel for \
       default(none) shared(size,in) firstprivate(sp_ct,firstloop)
  for(int p=0;p<size;p++) {
    if( firstloop == -1 ) {
      sp_ct.depos(p); firstloop=0;
    } else {
      sp_ct.increment();
    }
    in[p]=sp_ct.parable_at_basis();
  } // end omp paralell for

I dislike this because of the clutter that obscures what is really going on, and because it has an unnecessary branch inside the loop (Yes, I know that this is likely to not have a measurable influence on running time because it is so predictable...).
I would prefer to write something like
#pragma omp parallel for default(none) shared(size,in) firstprivate(sp_ct,firstloop)
  for(int p=0;p<size;p++) {
#prgma omp initialize // or something
    {  sp_ct.depos(p); }
    in[p]=sp_ct.parable_at_basis();
    sp_ct.increment();
    }
  } // end omp paralell for

Is this possible?
Comment: Yes, the initialization depends on the first value of `p` that is assigned to the thread.  This is not known outside the loop.
Comment: Is there a reason you can't do the initialization outside the loop?
Answer: If I generalize you problem, the question is "How to execute some intialization code for each thread of a parallel section ?", is that right ? You may use a property of the firstprivate clause : "the initialization or construction of the given variable happens as if it were done once per thread, prior to the thread's execution of the construct".
struct thread_initializer
{
  explicit thread_initializer(
    int size /*initialization params*/) : size_(size) {}

  //Copy constructor that does the init
  thread_initializer(thread_initializer& _it) : size_(_it.size)
  {
    //Here goes once per thread initialization
    for(int p=0;p<size;p++)
      sp_ct.depos(p);
  }

  int size_;
  scp_type sp_ct;
};

Then the loop may be written :
thread_initializer init(size);
#pragma omp parallel for \
       default(none) shared(size,in) firstprivate(init)
for(int p=0;p<size;p++) {
  init.sp_ct.increment();
}
in[p]=init.sp_ct.parable_at_basis();

The bad things are that you have to write this extra initializer and some code is moved away from its actual execution point. The good thing is that you can reuse it as well as the cleaner loop syntaxe.
Answer: From what I can tell you can do this by manually defining the chunks.  This looks somewhat like something I was trying to do with induction in OpenMP Induction with OpenMP: getting range values for a parallized for loop in OpenMP
So you probably want something like this:
#pragma omp parallel
{
    const int nthreads = omp_get_num_threads();
    const int ithread = omp_get_thread_num();
    const int start = ithread*size/nthreads;
    const int finish = (ithread+1)*size/nthreads;
    Counter_class_name sp_ct;

    sp_ct.depos(start);
    for(int p=start; p<finish; p++, sp_ct.increment()) {
        in[p]=sp_ct.parable_at_basis();
    }
}

Notice that except for some declarations and changing the range values this code is almost identical to the serial code.
Also you don't have to declare anything shared or private.  Everything declared inside the parallel block is private and everything declared outside is shared.  You don't need firstprivate either.  This makes the code cleaner and more clear (IMHO).
Answer: I see what you're trying to do, and I don't think it is possible. I'm just going to write some code that I believe would achieve the same thing, and is somewhat clean, and if you like it, sweet!
sp_ct.depos(0);
in[0]=sp_ct.parable_at_basis();
#pragma omp parallel for \
       default(none) shared(size,in) firstprivate(sp_ct,firstloop)
  for(int p = 1; p < size; p++) {
    sp_ct.increment();
    in[p]=sp_ct.parable_at_basis();
  } // end omp paralell for

Comment: You would have to edit the #pragma statement I'm sure. (I don't really know openmp, I'm sorry)
Comment: Yes, this is precisely my question: Can you put the initialization somehow in the `#pragma`?
Comment: I don't know, sorry I can't answer your question. Might I ask why do you need to do it like that?
Comment: I am not sure if this is what you ask. Let my try this answer: The `sp_ct.increment()` changes the state of `sp_ct`.  Without anything else, this would make parallelization impossible. But, I do have the `sp_ct.depos(p)` which allows me to bring `sp_ct` precisely in the state after `p` calls to `sp_ct.increment()`.  Now the code in the question works by initializing once in every thread, then setting `firstloop=0` and not redoing the initialization but using `sp_ct.increment()` to continue.
Answer: Riko, implement sp_ct.depos(), so it will invoke .increment() only as often as necessary to bring the counter to the passed parameter. Then you can use this code:
sp_ct.depos(0);
#pragma omp parallel for \
       default(none) shared(size,in) firstprivate(sp_ct)
for(int p=0;p<size;p++) {
  sp_ct.depos(p);
  in[p]=sp_ct.parable_at_basis();
} // end omp paralell for

This solution has one additional benefit: Your implementation only works if each thread receives only one chunk out of 0 - size. Which is the case when specifying schedule(static) omitting the chunk size (OpenMP 4.0 Specification, chapter 2.7.1, page 57). But since you did not specify a schedule the used schedule will be implementation dependent (OpenMP 4.0 Specification, chapter 2.3.2). If the implementation chooses to use dynamic or guided, threads will receive multiple chunks with gaps between them. So one thread could receive chunk 0-20 and then chunk 70-90 which will make p and sp_ct out of sync on the second chunk. The solution above is compatible to all schedules.


New Vanity: Sink Won't Drain Completely

Question: My wife and I just replaced the vanity in our bathroom. To fit the new sink, we had to cut some new PVC. Here's a picture.

All the plumbing works, without leaks, except that it will not drain completely out of the sink. After the sink stops draining, there is still about 1/2 inch of water standing in the sink.
All of the pipes that look horizontal are almost exactly horizontal. Did we not include enough of a downward slope? If so, how would we do that?
Comment: There should be a compression fitting that screws onto the threaded pipe to secure the non-threaded pipe.  It should be similar to the one below that where the pipe connects to the top of the trap.
Comment: What's with the large amount of plumbers putty, is there a junction there being held with putty?
Comment: Where does the drain go inside the wall?  E.g. how far from the stack is it, and does it go uphill at any point?  Also, do you have an adjustable stopper in the sink, and if so, does the problem still happen if you remove it?
Comment: The old drain went into the wall and I don't know where it goes -- we just used what was there and was working. No, there is not an adjustable stopper. It's just a straight drain.
Comment: Is this new vanity shorter than the old one?  Or is it deaper than the old one?
Comment: It is slightly deeper than the old one, about 2-3 inches. It's also taller. The top of the P-trap was level with the pipe in the wall.
Comment: The sink drain had some drips where that threaded area connected to the non-threaded area. That wasn't anything we put together but I threw some putty on it to stop the drip.
Comment: When you took apart the old trap did a lot of water come out, or only a small amount?  Typically you should only see the amount of water the lower U bend of the trap can hold, If you find more comes out it's likely the drain goes up to meet the main drain.
Comment: Only the amount of water that should have been in the P-trap came out.
Comment: Does the sink drain slowly, or does it drain normally (just not all the way)?  I would have to guess your looking at a clog.  If the drain went up before meeting the main drain there could be a problem, but since the normal amount of water drained out when you disconnected it I would doubt this is the case (also I couldn't see how it would pass inspection if it did).  Try some drain cleaner, or plunging. If that does not help look for a nearby clean-out or disconnect the trap and try snaking it out.
Comment: Would you be able to post a picture of the water sitting in the sink and the drain at the bottom?
Comment: Did you ever get this resolved? If so, please give a check-mark to the answer that helped you the most, or write up your own answer explaining what you did to get it fixed and give yourself a check mark. That will help others with this kind of problem know that this has a resolution and is a good place to look for their answer
Comment: The way the pipes are shown in the photo, it is an s-trap, not allowed by code. Even if there is a vent in the wall, there is an s-trap before the vent.
Answer: This might not be the answer, but for reference here is a problem that can occur if the plumbing is not installed properly.
If you look at Fig. 3, this is what a proper drain looks like.  You'll notice the orange line represents the water level in the system, the water levels out in the trap preventing sewer gases from entering the sink drain.
In Fig. 1, you can see what will happen if the drain line has to go up before meeting the main drain line.  Again the orange line represents the water level in the system, and as you can see the water level is much higher in the drain.
Fig. 2 shows what could happen if the sink was installed lower than the rise in the drain line. The water would never fully drain from the sink, since it would require extra pressure in the system to push the water up the drain.

I wouldn't guess this is the issue in this case; since as you can see, if you opened the trap in this case you would have more water drain than what is typically in the trap.
In a perfect world Fig.1 and 2 would never pass inspection, but as we all know not all plumbing is properly installed and/or inspected. So this could possibly be the case in this situation (given the plumbing was installed by an amateur plumber), but it is doubtful. It would be more likely caused by a clog in the line.
Comment: @michaelkoss: I figured from your comments that the plumbing was probably correct, that's why I suspect the problem is a clog.  Also for future reference, problems like this is why I never glue trap assemblies. I usually use compression fittings and threaded connectors, so the whole thing can be disassembled if there is a problem (or a ring or something falls down the drain).
Comment: @michaelkoss: Also, please get a proper fitting so you can remove that plumbers putty. That's a "*works for now*" solution, not a proper installation technique ;)
Comment: Wow, this is a really detailed answer. Thank you for taking the time to write it out and draw those diagrams. My setup is like figure 3, so theoretically, it should work.
Comment: I assume Home Depot or Lowe's would have something to thread on there? How would I add it post-glue? I'm not opposed to cutting this out and doing it again if I need to. I'd rather it be right.
Comment: +1 for the pics @Tester.  I was thinking the same thing, hence my original comment.  If it was a clog, I'd expect it to always be slow.  The only thing left I can think of is an air bubble in the drain, hence my question about the adjustable stopper.
Comment: @michaelkoss: you should be able to add the new compression fitting by disconnecting the threaded connector in the middle of the trap assembly, then unscrewing the threaded connector that connects to the metal pipe. the lower U of the trap and the extension should be able to drop out, allowing you access to install the new fitting.
Comment: There are threaded connectors on there that will let the OP check/clean the trap. There is one between the chrome and trap and one just after the trap. Unthread those and check for hairballs. Run a snake down the main drain while you've got it apart anyway.
Comment: In figure 1, would the trap really be serving any purpose? And with regard to figure 3, I think most P-traps I've seen would not have the pipe go down before the horizontal run.
Comment: @supercat They're crappy inaccurate drawings done in MS paint used to explain a concept. Not to mention the post is 3 years old.  Give me a break.
Comment: @Tester101: I wasn't trying to belittle the drawings, but was wondering if they had been intended to be illustrative of more issues than what were described in the text.  For example, if there were a rise between the trap and the wall, might that create a pressure-lock condition between the two traps.
Answer: I see one issue that doesn't meet the plumbing code. Before you drain down in the vertical there should be a vent. An air admittance valve will take care of that and probably your dining issue too
Comment: The vent pipe may be in the wall.

New Vanity: Sink Won't Drain Completely


Construct an open set containing all rational points in interval with given measure.

Question: In a recent exam I was asked to construct an open set $M \subseteq [0,1]$ mit $\mathbb{Q} \cap [0,1] \subseteq M$ with $\lambda[M] \in (0, \frac{1}{2})$ where $\lambda$ is the uniform distribution on $[0,1]$. Since every rational number $q$ is assumed to be in $M$ and $M$ is open there exists $\epsilon > 0$ with $B_\epsilon(q) \subseteq M$. Doesn't this already imply $M = [0,1]$? What am I missing?
Answer: Enumerate the rationals $q_0, \ldots, q_n, \ldots$. Then consider the balls $B_{2^{-n-3}}(q_n)$. Then $\lambda(\bigcup_{n \in \mathbb{N}} B_{2^{-n-3}}(q_n)) \leq \sum_{n \in \mathbb{N}} \lambda(B_{2^{-n-3}}(q_n))) = \sum_{n \in \mathbb{N}} 2^{-n-3} = \frac{1}{4}$.
$M = \bigcup_{n \in \mathbb{N}} B_{2^{-n-3}}(q_n)$ is one of the sets you are searching.
Comment: Ah, we use the subspace topology and since the open ball is open in $\mathbb{R}$ it is also open in $[0,1]$. Thank you really much.
Comment: Do I have to worry about the end points $0,1$, since I can't find a bill contained in $[0,1]$ or what is the definition of open in this context?
Comment: If they mean an open set in $\mathbb{R}$, then such a set doesn't exist (you need a ball also around $0$ and $1$). If they mean an open set in the inducted topology in $[0,1]$, then the intervals of the kind $[0,a)$ and $(b,1]$ are open.


Override AWS SDK API Call

Question: I want to implement caching mechanism for AWS SDK calls like describeReservations() in EC2 and so on.
I can create a wrapper around SDK and create custom SDK but that's a long process. So, I was looking for something like overriding API calls in AWS SDK itself and using some custom function to do so.
How can I modify AWS SDK code for java available on github in such a way to implement this mechanism ?
Answer: Not sure of this can help
ResponseMetadataCache
<code>public ResponseMetadataCache(int maxEntries)
</code>
Creates a new cache that will contain, at most the specified number of entries.
Parameters:
maxEntries - The maximum size of this cache.
https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/util/ResponseMetadataCache.html#ResponseMetadataCache-int-
Comment: I want to intercent the HTTP requests and implement caching of my own using redis or whatever. They use their internal cache and also this is for diagnostic purpose

Override AWS SDK API Call


Junction box cover required for a non-light ceiling device, and if yes, which one?

Question: I am installing one of these infrared heating panels hardwired on my ceiling.
My plan is to put a junction box into the ceiling (same size as a ceiling light), located underneath the panel. The wire to the panel will need to exit from the box somehow.
Can this box be left open behind the panel? In contrast to a light fixture, the panel is not fixed to then junction box but attached with separate screws.
If it should be closed, how?
Here is how this would look from the side:

Note that the (metal) junction box will not be flush with the ceiling because I need to fix it to the beam (there will be 1-2 drywell thickness in between). Also the cable that connects to the heating panel is not Romex. It will be a flexible cable, similar as the ones for lighting fixutures.
Answer: You've got two options:
Cord/plug
I don't recommend this. But the linked unit comes with a cord/plug. You can install a junction box flush to the ceiling and put a standard receptacle in it. The receptacle and matching faceplate form a cover for the box. I did not originally recommend this method, but now that I know the hardwiring method is not by replacing the cord/plug with appropriate cable (NM cable or a wire whip) but rather by chopping the plug off and wiring the end of the cord into a box, I would recommend cord/plug installation.
Plug in the heater and attach it to the ceiling. as long as the cord is not pinched, looped (too many loops will cause heating in the cord) or otherwise messed up you should be fine.
Hardwired
The factory installed cord connects to some sort of junction box on the back on the unit. I suspect (but couldn't easily find instructions) that hardwiring is done by:

Open the integral junction box
Disconnect the cord
Connect a piece of NM cable (could be wire nuts or screw connections)
Close the junction box
Install a junction box on the ceiling
Connect this NM cable to the wires in the junction box using standard methods
The NM cable should be installed through an appropriate clamp in each junction box
Install a blank cover on the ceiling box
Install the device on the ceiling
Comment: Yes, a switched receptacle is perfectly fine. The usual concern is that the receptacle might get used for other things that shouldn't be switched, but not an issue at all with a ceiling receptacle.
Comment: Yes, I want to hardwire it and it is officially supported. I could not yet find a way to open then integrated junction box so I think I may need to cut the cord but I asked. Regardless, my question is about the OTHER junction box. If this box is in the ceiling, how would I attach a clamp? How would I route the wire out if there is a cover on it? Note, the jbox would look same as for a ceiling light. I hope my question is clear
Comment: It depends on the type of box. For example, with a metal box you can use [one of these covers](https://www.lowes.com/pd/RACO-1-Gang-Square-Metal-Electrical-Box-Cover/3128079) which has a 1/2" knockout and you put a clamp in the knockout. You are *not* allowed to cut the cord for a permanent installation because cords can't be used where NM is supposed to be used, and vice versa. You need to get instructions from the manufacturer about how to properly hardwire.
Comment: Oh I see. Ok I'll get a metal box and this cover then. Good to know. But why not? I thought, the proper way is to cut the cord, strip it (=use as a pigtail) and then use wire nuts in the external junction box.
Comment: Is there likely to be a problem by cutting the cord and stripping it, etc.? No. But it is not to code. Code recognizes different types of cables for different situations. Cordage is designed to be flexible and also to be used in open air (which dissipates heat). Non-metallic cable (a.k.a., Romex) is not designed to be very flexible (bend it and it keeps its shape) but is designed to be run inside walls. One critical example is that cordage can, depending on the product, use thinner wire than standard for cables run inside walls. For a temporary fix cut cord/etc. would be fine. Not permanent.
Comment: I checked and I do not see a way to open the integrated junction box. I am sure I am supposed to cut the wire and use it as a pigtail. I do not think/believe I need to use Romex for this. Light fixtures don't have Romex all the way to to the fixture either. It's a flexible cable (similar as the one that's currently attached to the heating panel) which is then splied in the junction box. Regardless, I will wait for the vendor reply and post their response. I highly doubt they would offer a non code compliant hardwired option.
Comment: Also, things are still not 100% clear to me (even if I cut the existing wire). I added a schematic to my posting to be more clear. Are you saying I would still screw your suggested cover on it (possibly with longer mechanical screws) and use a clamp? Aren't these clamps only for Romex? (I am fairly positive one would not run Romex all the way to the heating panel)
Comment: I highly doubt they would offer a non-code compliant hardwired option, which is why I **don't** think they will tell you to chop the cord. It makes no sense. Yes, NM cable (Romex) is 100% fine in a situation like this. The issues with Romex are that it can't be used with something that moves around (this doesn't, you are mounting it to the ceiling) and that it needs to be protected from damage (a - not an issue on the ceiling anyway, b - the fixture blocks it anyway).
Comment: I got the following answer from vendor: " To hardwire the units your electrician will cut the plug and will hardwire the heater by using the cable, but not the connection box. The connection box is not to be opened. Please note the hardwiring must be done by a certified electrician.". As I expected they want to cut the cord. Do you have a NEC reference that shows that this is not code compliant?
Comment: Interesting. The usual rationale for it *not* being OK to do that is based on the type of cord. But if the vendor says so **and the item is UL or ETL listed** then I would get that in writing (if you already have it in an email that is enough - *save it*). If the item is not UL or ETL listed then together with this problem I would steer clear. Hardwiring a non-listed item, particularly in a usually inappropriate manner, is not generally a good idea, because if something were to go wrong you'd be in big trouble.
Comment: I am afraid it's not UL or ETL ... could not find anything in the manual or the website. I wrote Wexstar again. I am very disappointed by the product/company (it also doesn't look great, contrary as they advertise). Would not have expected this from a US/Canadian company. Regardless, as a backup, would it be "ok" to put an outlet in the junction box under the panel and just plug it in?
Comment: I got another answer from Wexstar and they said they are fully certified in North America which complies with UL. They provided this link: https://www.certipedia.com/certificates/72227479?locale=en and it states "UL 1278:2014 R2.22". Does this confirm it is UL listed and I can follow the manufacturer's instructions to cut wire?
Comment: Interesting. TUV is a European thing, roughly equivalent to UL and ETL. So that seems reasonable. That being said, the vendor's instructions mostly make sense (not 100%) but I caution you about one piece: *Please note the hardwiring must be done by a certified electrician*. That may be CYA for installation in general. But it may also be a hidden way of denying warranty claims. Admittedly, a device like this if it works on day one (which you can test by plugging it in) will probably last a long time. But they *could* deny a warranty claim if you send back a broken unit with a chopped cord and
Comment: are not able to show that an electrician did the work. Personally, I think I would go with: Install a receptacle in the ceiling box and plug this in with the original cord/plug.
Comment: Awesome, thanks! Sorry, one last question before closing this: I want to turn on/off the device with a wall switch. Is it permissible to wire the outlet to a wall switch? I know this is allowed for lights (and often done for normal outlets) but I want to double check.
Answer: It looks from your diagram that the panel will be spaced away from the ceiling surface.
There's no reason in the world to attach the junction box to the joists, or to have it buried inside the ceiling cavity. The only reason you need a junction box is to provide protection for the wire connections, which can be done with a surface-mounted metal pancake box, 1/2" or so deep. It can be mounted by using screw anchors in the ceiling drywall. The feed cable can emerge from a small hole in the ceiling and connect to the knockout hole which the pancake box will have in its back. It does not need a cover if the panel surface under it is non-combustible, which would astonish me if it was not. Light fixtures are installed all the time with no covers (other than the fixture itself) over the box.
FYI, it is not code-compliant to cut the plug off a flexible cord and use it to hard-wire a device.
Comment: Ok, I can use a surface mounted pancake box. However, I was thinking a larger and more rigid metal box gives me more flexibility and it makes me feel better if it's solidly attached to the beam rather than drywall. Further: There will actually be multiple of these heating panels and a larger and robust metal box would give me the option to splice them flexibly at multiple locations or install a relay for remote control. Is there a big reason not to "bury" the box (and attach to beam)? "Ceiling" on the picture is just normal drywall thickness.
Comment: "The feed cable can emerge from a small hole in the ceiling". Do I understand correctly that you'd drill a hole next to the junction box to feed the cable to the panel out?
Comment: I got the following answer from vendor: " To hardwire the units your electrician will cut the plug and will hardwire the heater by using the cable, but not the connection box. The connection box is not to be opened. Please note the hardwiring must be done by a certified electrician.". As I expected they want to cut the cord. Do you have a reference why cutting the cord would not be code compliant?


Check if user is active before allowing user to sign in with devise (rails)

Question: I am using devise and created a User field called :active which is either true or false.  I have to manually make the user active (true) before the user is allowed to log in. At least that is the intent.  I have tried this...
class SessionsController < Devise::SessionsController
  # POST /resource/sign_in
  def create
    "resource/signin CREATE"
    self.resource = warden.authenticate!(auth_options)
    unless resource.active?
      sign_out
      redirect_to :sorry_not_active_url
      return
    end
    set_flash_message(:notice, :signed_in) if is_navigational_format?
    sign_in(resource_name, resource)
    respond_with resource, :location => after_sign_in_path_for(resource)
  end
end

However this does not catch all the places where a user can log in, for example, when a user changes their password, the site automatically logs them in automatically after. However, if the user is not active, I do not want them to be allowed to log in, but rather be redirected to a sorry_not_active_url.
What would be the best way to prevent the user from signing in if the user is not active?
Thank you.
Answer: Add these two methods to your user model, devise should pick them up automatically - you should NOT need to extend Devise::SessionsController
def active_for_authentication?
  super && self.your_method_for_checking_active # i.e. super && self.is_active
end

def inactive_message
  "Sorry, this account has been deactivated."
end

Comment: after thinking about it, I assume I could make def inactive_message reference a field, and the field could have a custom message per user.  Does this seem correct?
Comment: This great! is there a way to check for different states, like, active, not active, pending, rejected by creating a field with 4 different values and deliver a different message for each state?
Comment: For Devise 4.7 these methods instead need to be added to the model you use for authentication, usually User, i.e. not to the SessionsController
Comment: without explanation this is feels like a hack and magic because there is no doc that mention about this. but devise has the code in [here](https://github.com/heartcombo/devise/blob/8593801130f2df94a50863b5db535c272b00efe1/lib/devise/models/authenticatable.rb) and [here](https://github.com/heartcombo/devise/blob/8593801130f2df94a50863b5db535c272b00efe1/lib/devise/hooks/activatable.rb)
Answer: Devise (If you have devise 3.2+) now support block parameter in (session) create
# assuming this is your session controller

class SessionsController < Devise::SessionsController

def create
  super do |resource|
     unless resource.active?
      sign_out
      # you can set flash message as well.
      redirect_to :sorry_not_active_url
      return
    end
  end
end

Comment: @Marklar This might be beneficial when you support multiple authentication strategies and need to disable the regular authentication flow, but not for all. While the OP's use case is for checking if a user is active, I have needed to disable regular session creation if a user has authenticated via another method (OAuth) for example. The model based approach using `active_for_authentication?` would have been more difficult.
Comment: Hi Viren, would you be able to list a couple of benefits to doing it this way as opposed to the implementation in the accepted answer? I'm looking to implement this now myself and am wondering what's the current best practice. Thanks.


Pass a variable to other function in javascript

Question: $('.header_menu_item').on('mouseenter',function(){
    var default_img = $(this).find('.header_menu_item_icon_img').attr('src');
    var hover_img = $(this).find('.header_menu_item_icon_img').attr('data-hover');
    $(this).find('.header_menu_item_icon_img').prop('src',hover_img);
}).on('mouseleave',function(){
    $(this).find('.header_menu_item_icon_img').prop('src',default_img);
});

How can i pass default_img var to following mouseleave function?
Thanks for any help :)
Answer: One option will be declare a variable outside of the 2 functions.
var default_img = '';
$('.header_menu_item').on('mouseenter',function(){
default_img = $(this).find('.header_menu_item_icon_img').attr('src');
   var hover_img = $(this).find('.header_menu_item_icon_img').attr('data-hover');
   $(this).find('.header_menu_item_icon_img').prop('src',hover_img);
}).on('mouseleave',function(){
   $(this).find('.header_menu_item_icon_img').prop('src',default_img);
});

Answer: Set it as data on the element itself and then retrieve it later
//setting
$(this).data('default_img',default_img);

//retrieving
var img = $(this).data('default_img');

Through native js
//setting
this.dataset['default_img'] = default_img;

//retrieving
var img = this.dataset['default_img'];

You could also before hand set up a data-* attribute on the html itself so no need to use JS to set the initial data

data-* attributes reference
dataset reference
jQuery's data() method
Answer: You need to use global variables. A function can only pass variables directly to functions that it calls, not functions that are called outside.
var default_img;
$('.header_menu_item').on('mouseenter',function(){
    default_img = $(this).find('.header_menu_item_icon_img').attr('src');
    var hover_img = $(this).find('.header_menu_item_icon_img').attr('data-hover');
    $(this).find('.header_menu_item_icon_img').prop('src',hover_img);
}).on('mouseleave',function(){
    $(this).find('.header_menu_item_icon_img').prop('src',default_img);
});

Comment: default_img can differ if you enter two $('.header_menu_item') but only leave one of them, you have to store default_img by each header_menu_item
Comment: @ThomasFellinger Unless they're nested, I doubt that can happen.
Comment: i think they just need to be positioned somehow overlapping to get failures
Comment: We need to see the HTML to know whether this is important.
Answer: you can not pass it directly - you can store it in an outer variable - but you have to do it by each item:
$('.header_menu_item').each(function(){

  var $header_menu_item_icon_img = $(this).find('.header_menu_item_icon_img'),
      default_img = $header_menu_item_icon_img.attr('src'),
      hover_img = $header_menu_item_icon_img.attr('data-hover');

  $(this).on('mouseenter',function(){
    $header_menu_item_icon_img.prop('src',hover_img);
  }).on('mouseleave',function(){
    $header_menu_item_icon_img.prop('src',default_img);
  });
});

Comment: Only user wanted to pass `default_img` variable in `mouseleave` function.. why `each` function is needed.. ??
Comment: because default_img can differ per menu_item and can and will be overwritten in a global variable


Civ 5 won't let me found a new city, why?

Question: Playing Brave New world, I started a new game on my new comp I'm 56 turns into the game and now I can't build cities. I've been playing Civ 5 a long time and never had this issue. Is there a mod that is causing this? I'm Polynesian race.
Comment: Did you accidentally turn on the One City Challenge game option? Start a new game with advanced setup and check the options. I would think that blocks building settlers.
Comment: 1) Are you using any mods. 2) Is it any desert-tile or a specific one (or group)? 3) What is your happiness, is it below -10?
Comment: Screenshot and mod list would help.
Comment: Do you have settlers? Are they at least 5 tiles away from the next city?
Comment: Yes to both questions. It's only happening on desert tiles
Comment: Can you try to make another game, then try to use your settler on the desert tile and see if it works. Report back.
Comment: I can't remember the exact rules but I think there are situations where you can't build on deserts. Can you post a screenshot?
Answer: Each city can't be 4 tiles from another.
Comment: According to the comments, this is not the problem.


all websites on webservers at 100%

Question: So we have VM environment using VMWare consisting of 5 servers.
Database server,

Database Server
File Server (all site content is on here)
3 Webservers in a load balancing configuration

Other notes:

Application website uses .NET 3.5
Webservers running windows server 2008 R2 Standard with IIS 7.5
Each Virtual webserver is running Xeon 2680 with 4 cores and 16gb ram
Database server is fine

There are around 30 websites on each webserver which is directed by a physical load balancer via round robin.
Each seperate website has a virtual directory called SharedContent which is outside the main website folder - on a separate content server.
that folder is shared and mapped as a virtual folder for all the websites
So all websites pull images, trailers from that same folder.
Currently there is around 1400000 files in the root folder and couple thousand in 7 sub folders in the shared content folder.
The issue is that on several occasions all webservers goes to 100% CPU and cause all websites to not respond as more requests are coming through. The application is based on ASP.NET 3.5, the servers starts returning 503
the websites are configured to use ASP.NET File based Caching
We have suspected it to be something to do with the SharedContent folder and all the files in it. We did a test of moving around 30 files to the shared folder (mapped as virtual directory in all sites) manually - at first the File Server peaked to 100% CPU (probably re-indexing the files) then this effect cascaded to all 3 webservers, all webservers hit 100% CPU and stayed there
The only way to bring the webservers back to functional is killing processes with high cpu usage from the resource manager.
we don't know why this happens - we are working on a patch to distribute files in that folder which will eventually create sub-folders and move files in there
what we don't know is what's the reason behind it ? - if we add a few files (copying 20-30 files) to that folder quickly, the CPU first shoots up on the file content server, then one by one all web servers also shoot upto 100% CPU usage
during these outages:

traffic was normal
operations were normal - no huge files suddenly being added to that folder

if anyone has any idea why this happens ? or reason behind this ?
Have anyone experienced anything like this before?
Update: 20/1/17
All websites have its own app pool on each webserver.
The only errors that were added in the event viewer at that time were Request timeouts.
The infrastructure was migrated from physical environment to VMs last August and never had an issue.
End of November 2016, 3 websites were upgraded to .net 4.5 which the rest was still on .net 3.5.
First instance of this issue happened at the end of December.
We attempted to add another permission role to the SharedContent folder which shoot it up to 100% CPU at peak period. We had to cancel the operation.
Traffic was normal.
SharedContent is configured as a mapped drive.
Comment: You got a memory leak somewhere in the app.
Comment: Is it one particular worker process that hits the 100% CPU? Also while technically possible I have seen issues when stored large amounts of files in a single folder, is it possible to breakdown the 1.4M files into different folders. Also I can imagine as all content is coming from 1 content server this could be placing a lot of stress on that box and the "network" between the content boxes and the web servers themselves?
Comment: It happens on all website processes. It seems to cascade to all 3 webservers and causes all of them to hit 100%. Yes. all content is stored on the file server and webservers just map to it. The Sharedcontent folder is outside the main websites folder. What i don't understand is how it cascades to all 3 webservers? The internal link between VMs is 1gigabit.
Answer: Here are the things I would check;
Have all websites got their own app pool, or is it shared between all 30 site on each server.
Have you isolated the exact process that is utilising 100%?
Do any errors get added to the event viewer?
Are you receiving any spikes in external traffic / internal traffic.
Are any error being generated on the vm host?
Are physical cpus being over allocated to vms?
Are the machines in a domain controller? Is that triggering any kind of update activity on the boxes?
How is the shared content done, mapped drive or MS distributed file system? If using DFS what topology is it?
Comment: Added additional info in the original post

all websites on webservers at 100%


Can the action-value function be "expanded"?

Question: I know the basic language of probability, but not fluent in it, and the text books on reinforcement learning usually assumes one is fluent in it, so they don't define everything rigorously, here is my attempt to define every symbol appears in the action value function:
$$q_\pi{(s, a)}=E_\pi[G_t|S_t=s,A_t=a]$$
where:
$s$ and $a$ are concrete state and action (no problems here)
$S_t$ is a random variable represents the state of the system at time step $t$ (a little philosophical problem, but let's not get into it, so let's say no problem here)
$A_t$ is a random variable represents the action the agent take at time step $t$ (more on this later)
$G_t$ is the return at time step $t$, defined to be $\sum_{i>=t+1}^T{\gamma^{i-t-1}R_i}$, where $T$ is the terminal step, and $\gamma \in [0,1]$ is the discount factor (just a sum of $R_\_$s, so if I understand $R_\_$, I understand this, so no problem here)
$R_{i+1}$ is a random variable represents the reward the environment gives to the agent due to its action $A_i$ in state $S_i$ (more on this later)
$\pi(a|s)=P[A_t=a|S_t=s]$ is the probability of taking action $a$ at time $t$ given state $s$ (just a conditional probability, if I can understand $A_\_$, I can understand this)
$E_\pi[\_]$ expectation of some kind, more on this later
Questions:

It seems to me that, $R_{t+1}$ depends on the current state and action, so, is it technically correct that if I write $R_{t+1}=r(S_t,A_t)$, if it's correct, what is $r$? is it a function whose domain is $\{S_i\}_{i=1}^T \times \{A_i\}_{i=1}^T$? (no, I don't mean $StateSpace \times ActionSpace$, that would be $r(s, a)$), is this (r.v. valued function of r.v.s) studied in math?
About the notation $E_\pi[\_]$, there is where everything really starts to fall apart, (I undersdand expectation and conditional expectation), the text book says, and I quote: we define the value of taking action $a$ in state $s$ under a policy $\pi$, as the expected return starting from $s$, taking the action $a$, and thereafter following policy $\pi$. Especially on the "thereafter following policy $\pi$" part, how can one just put a $\pi$ at the subscript of the expectation willy-nilly? I know this is how mathematician do things, they define things for a living ^_^ but still, at least symbols in the definition should related to each other more obviously, not just by words-behind-the-scenes.

(2 continued) When you don't understand something, it sometimes can be hard to know that it is what exactly that you don't understand. I guess that the main source confusion is that, there are too many "free random variables"/"free moving parts" and they are "verbally defined" (contrast to formally defined, where you can follow things more or less mechanically), and all held together by the sentence in 2. For example, there seems to be a relation between $A_t$ and $\pi$, can this relation be used to eliminate $A_t$? (probably cannot, since this is a MDP, but still), Can the definition be expanded to something like $E_{sth \sim \pi(sth)}[sth(sth)|S_t=s,sth]$ where $sth$ meanings "something", and one $sth$ don't have to be equal to another $sth$, if it could, then I think I will have a easier time understanding it, since all "moving parts" are somewhat "bounded"/"qualified".
(this 2 is too long) I guess that at this point, the best I can say is that, is there a formulation of this expectation, that reflects the sentence: we define the value of taking action $a$ in state $s$ under a policy $\pi$, as the expected return starting from $s$, taking the action $a$, and thereafter following policy $\pi$ in a more obvious way? and not like "Oh, this is the state of art, just think this sentence when you look at the definition equation, sometime later, you will grew used to it" - I want to understand it, not treat it "axiomatically" - that's what "grew used to it" means.
I know that this is a really really long question, and a badly-stated one, you probably don't know what I'm talking about, that's because I really don't know it is what exactly I don't understand. So I put it here, Preying to Aristotle and all the souls of the great mathematicians, hoping they would send me an experienced teacher/mathematician with a kind heart to spot my problem and save me from frustration. QED.
Thank you for even considering reading this question.
Comment: Not much clear to me what you don't understand. Seems to me that you mostly got it. However, think about this. You choose a policy. Then, you  play the game many times and each time receive a total reward which might be different from other runs, both because you don't choose your actions deterministically and because the rewards are random. The expectation you are talking about is just the average of your reward playing the game and following that policy. You see that different policies might produce different expected total rewards.
Comment: Thanks for reading the whole thing :) I tried to convince myself like you said before I ask the question, yeah, it seems everything follows, but there always seems to be some thing hidden that I don't get. Thanks anyway.
Answer: It's a long question, so sorry if I misunderstand something.
You have the action-value function:
$$ Q_\pi(s,a) = \mathbb{E}_\pi\left[G_t|S_t = s,A_t=a\right] $$
Notice how this relates to the value function:
$$ V_\pi(s) = \mathbb{E}_\pi\left[ G_t| S_t=s\right] = \mathbb{E}_{a\sim\pi(a|s)}\left[ Q_\pi(s,a) \right] $$
In other words, if $V$ tells the agent the value of a given state $s$ (under $\pi$), then $Q$ tells them the value of taking a particular action when in $s$. Thus, $V$ is given by "integrating out" the actions in $Q$, i.e. it is the value of $s$ averaged over the possible actions we could take in $s$.
I thought that might give some intuition. Anyway, for your questions:

It depends on the situation. Sometimes the reward depends on both the state and the action; other times, it depends only on the state. It can also be stochastic, i.e. $r_t\sim P_R(s_t,a_t)$ is a random variable that depends on the current state and action, or a deterministic function. Usually, we consider some kind of Markov decision process, so we don't need to consider the older states and actions.
I think it is written that way because it is hard to fully write out. In many cases, we have $a_t\sim \pi(a|s_t)$. Thus, the expected reward for a single step is given by $\mathbb{E}_{a\sim\pi}[r_t]$, where $r_t$ depends (stochastically) on $s_t$ and $a_t$. Also remember that state transitions can be stochastic too, i.e. $s_{t+1}\sim P_E(s_{t},a_t)$, which can be more important for model-based RL. So then the expected reward in the next step depends on which action was (randomly) chosen from the policy, and so on. The notation just encapsulates the idea that the expectation is over all time steps, all distributions over the environmental dynamics, and over the stochasticity of the policy itself. Only $\pi$ is really under the agent's control, so the subscript only includes it.
Overall you seem to get this though, so I'm not sure what your question is beyond clarifying notation.


importing struct with trait from lib

Question: Just wondering if there is a difference between importing from a module and importing from a library.
Can someone run the following code and tell me what I am missing
pub trait PersonDetails {
    fn name(&self) -> String;
}

#[derive(Debug)]
pub struct Person {
    pub first_name: String,
    pub last_name: String,
    pub age: u32
}

impl PersonDetails for Person {
    fn name(&self) -> String {
        format!("{} {}", self.first_name, self.last_name)
    }
}

putting the above in a normal file works perfectly.
The problem is when I put it into a lib and try importing Person and then running name function as so
let person = Person{ first_name: "fname".to_string(), last_name: "lname".to_string(), age: 32};
println!("{:?}", person.name());

I created a lib called broker and inside lib.rs I put the trait and struct, imported it into my main project and tried to use it.
When I import it into the main project, I get the following error

Any help and explanation is greatly appreciated.
Comment: Please provide a [complete example](https://stackoverflow.com/help/minimal-reproducible-example). "I imported it" is unnecessarily vague, when the exact code you used could be provided just as easily.
Comment: You guys are pretty awesome. Works well now
Answer: If you read the error message carefully you will notice that it says "items from traits can only be used if the trait is in scope". That means that to use name(&self) you must import PersonDetails into the module. Your code works on your original crate because you declared the trait in that scope.

importing struct with trait from lib


In interactive Python, how to unambiguously import a module

Question: In interactive python I'd like to import a module that is in, say,
C:\Modules\Module1\module.py
What I've been able to do is to create an empty
C:\Modules\Module1\__init__.py

and then do:
>>> import sys
>>> sys.path.append(r'C:\Modules\Module1')
>>> import module

And that works, but I'm having to append to sys.path, and if there was another file called module.py that is in the sys.path as well, how to unambiguously resolve to the one that I really want to import?
Is there another way to import that doesn't involve appending to sys.path?
Comment: See [How to import module from file name](http://stackoverflow.com/questions/67631/how-to-import-module-from-file-name)
Comment: Thanks for your responses.  I do like the following approach:
>>> import imp
>>> m = imp.find_module('module',r'c:\modules\module1')
>>> imp.load_module('module',m[0],m[1],m[2])
Comment: Possible duplicate of [How to import a module given the full path?](https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path)
Answer: EDIT: Here's something I'd forgotten about: Is this correct way to import python scripts residing in arbitrary folders? I'll leave the rest of my answer here for reference.

There is, but you'd basically wind up writing your own importer which manually creates a new module object and uses execfile to run the module's code in that object's "namespace". If you want to do that, take a look at the mod_python importer for an example.
For a simpler solution, you could just add the directory of the file you want to import to the beginning of sys.path, not the end, like so:
>>> import sys
>>> sys.path.insert(0, r'C:\Modules\Module1')
>>> import module

You shouldn't need to create the __init__.py file, not unless you're importing from within a package (so, if you were doing import package.module then you'd need __init__.py).
Answer: inserting in sys.path (at the very first place) works better:
>>> import sys
>>> sys.path.insert(0, 'C:/Modules/Module1')
>>> import module
>>> del sys.path[0]  # if you don't want that directory in the path

append to a list puts the item in the last place, so it's quite possible that other previous entries in the path take precedence; putting the directory in the first place is therefore a sounder approach.


How to check if I have touched inside or outside of a modal Activity?

Question: I use the following style and defined a modal activity:
<code><style name="Theme.TransparentD0" parent="android:Theme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@color/semi_transparentD0</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>
</code>
How to check if user has touched inside or outside of my Activity?
Answer: <code>@Override
public boolean onTouchEvent(MotionEvent event) {

    final int width = getWindow().getDecorView().getWidth();
    final int height = getWindow().getDecorView().getHeight();
    final int x = (int) event.getX();
    final int y = (int) event.getY();

    if (x > 0 && y > 0 && x < width && y < height)
    {
        Log.i("onTouchEvent", "Inside");
    }
    else
    {
        Log.i("onTouchEvent", "Outside");
    }
    return super.onTouchEvent(event);
}
</code>


How do I transition between tile and drywall in a tub surround?

Question: I'm working on a tub surround, which is about to be tiled. And the last piece I need to figure out, is how to transition between finished tile and adjacent wall.
This is how my wall looks right now.

Backerboard is 1/4", and drywall is 1/2". With thinset, tile, etc, tile qill be ~1/8" thicker than drywall. I'll be using this edge where the tile ends:

What is the best way to facilitate the transition so that I don't have to re-do all the walls in the bathroom? (e.g. something that will not just have a gap there, and will not require reapplying the wallpaper or repainting the whole thing)
Answer: A usual method is to go down the edge of the wall with a surface bullnose, showing a "knife edge" at the corner.  This means the tile rolls down to meet the corner.
Another method, if your bullnose has finished bottoms, is to overlap the edge and go "around the corner", having the bullnose round into the outside wall (the wall that has the striped wallpaper) The bullnose will hide the tile edge, putting that grout line facing the side of the entrance.
This photo shows both methods:
the left with a flat-on-wall knife edge and
the right shows a wrap-around-the corner

You can use the edge profile pictured, in place of surface bullnose.  You would adjust your tile to end short of the corner (equal to the edge thickness of the profile.

In all cases, grout would be filled into the corner/wall junction.
Comment: I don't have a surface bullnose :( Not sure how I'd properly use edge profile like you described... Since the backerboard sits lower than drywall, I'll butt against an uneven cut of drywall, and will end up with a gap between this and drywall. Are you saying that grout should fill that gap? Are there any profiles like this: http://syrota.com/photos/diy/3.jpg (note height difference)?
Comment: Do I understand that the drywall/wallpaper is proud of the backerboard (by __ amount?) and that after tiling the tile will be proud of the wall by 1/8 (but contained by the drywall?)  It seems like you should just cutoff (neatly, with multiple new blades) the drywall flush with the wall prior to tiling and then butt the edge profile flush to the wallpaper wall.  Caulk or grout the small gap between the edge profile and the wall.
Comment: The profile in your drawing is know as a "carpet" tuck transition (where carpet and tile are on the same level, the carpet tucks under the raised section) .  I don't understand why you want it
Comment: The problem is that it's not a corner. It's a wall on the same plane like so: backerboard ▄▄▄▄▄█████ drywall. That's why this carpet tuck transition would've worked great. I'll try to see how caulking the gap would look, and may be this is the route I'll take. Thanks!
Comment: Ahhh. Glad I asked.  How close is the grout to the thinset?  Perhaps mix a small batch of grout to bed the profile against the wall (filling its side), while using thinset at the same time to set the bottom of the profile/tile
Comment: You then sponge off the edge of the profile, maybe use a thumb tack or 2 to hold the profile down af the top, until you get there with the tile.
Comment: I ended up going with your suggestion of just butting it to the drywall, and then caulking the gap.
Answer: For the sake of completeness, here's the alternative way of accomplishing this: Schluter RENO-TK

I'd prefer this one (exactly the offset I have), but unfortunately could not find it anywhere locally, and tile installation was already scheduled.


Linear dependence

Question: Let $X$ be a Hilbert space and let $f:X\to\mathbb{R}$.
Let $M=\{x\in X:f(x)=0\}$ be the nullspace of $f$.
Let $M^\perp=\{x\in X:(x,y)=0\text{ for all }y\in M\}$ be the orthogonal complement of $M$.
Show that $M^\perp$ is at most one-dimensional, i.e. that if $x$ and $y$ are members of $M^\perp$ then there exist scalars $a$ and $b$, not both zero, such that $ax+by=0$.
Comment: I take it $f$ is assumed linear.
Answer: (Assuming $f$ is linear, of course...)
If $M=X$, then $M^{\perp}=\{\mathbf{0}\}$ and we are done.
Assume then that $M\neq X$. Let $x,y\in M^{\perp}$. If $x=\mathbf{0}$, then $1x+0y = \mathbf{0}$ and we are done. If $y=\mathbf{0}$, then $0x+1y=\mathbf{0}$ and we are done. So we may assume that $x\neq\mathbf{0}$ and $y\neq\mathbf{0}$.
Then $x\notin M$, and $y\notin M$. Hence $f(x)=\alpha\in\mathbb{R}$, $f(y)=\beta\in\mathbb{R}$, and $\alpha\beta\neq 0$.
What can you say about $\beta x-\alpha y$?

Linear dependence


How do I manually beam an NdefMessage with Android?

Question: I have my NFC class which I assume people know what it looks like if you are looking at this question.  So OnCreate, I do:
mNfcAdapter.setNdefPushMessageCallback(this, this);

to be able to send messages and then I do:
public NdefMessage createNdefMessage(NfcEvent event){
    NdefMessage msg;

                msg = new NdefMessage(new NdefRecord[] {
                        createApplicationRecord(this.messageToSend.getBytes())
                });
                return msg;
}

to send my message.  and I have some code to receive a message as well but then I want to send another message back while the devices are still in "Connected" mode.  How can I manually give a "send message" command?
Answer: Your application gets a single chance to send out the message using setNdefPushMessageCallback. Once the message has been transfered you get the completionCallback (if you want to). Afterwards you can't interact with NFC anymore.
Sending a second message after you got your completion is unfortunately not possible with the API as it is right no.
Technically there is no reason for this by the way. Google could add the functionality with ease. It would only take them a day or two. In Android 2.3 there already was an undocumented way to access the LLCP (base protocol of Android Beam) protocol from applications. This is gone since Android 3.0
Comment: After a little more research, I can confirm that for now, this is the case


iOS and JSON nesting issue

Question: I am working on an app for a game server company and part of the app requires the user to see a list of his or her game servers and whether or not they are online, offline, how many players on them, the server name, etc. This data is all found in a JSON file hosted on the web updated from a MySQL database.
Using the code below, I can't seem to get it working. Now, please understand it is one of my first times working with JSON and have no choice as this is what the client requested. I talked to my partner but he couldn't seem to debug the issue himself.
The error I get is something like:
No visible @interface for 'NSArray' declares the selector 'objectForKey:'

I've tried several versions of the code, all with no success.
It would be much appreciated if you could please help me debug this code and get it working along with the commented out section near the bottom updating the TableViewCells with the server name, players online, and status (0=offline, 1=online, 2=busy, 3=suspended, -1=unable to start).
Please note that the format of the JSON file must remain as is and is only possible to make very minor changes.
Thank you,
Michael S.
My header file:

http://pastebin.com/EkuwVSmY

My main file:

http://pastebin.com/09Ju0uDu

My JSON file:
{
    "status": "OK",
    "error": "",
    "debug": "2 server(s)",
    "result": {
        "servers": [
            {
                "id": 1,
                "title": "Test",
                "players": 0,
                "slots": 10,
                "status": 3
            },
            {
                "id": 2,
                "title": "Creative Spawn",
                "players": 0,
                "slots": 5,
                "status": -1
            }
        ]
    }
}

The block that gives me the error is:
NSArray *serverResults = [[news objectForKey:@"result"] objectForKey:@"servers"];

if ([[[serverResults objectAtIndex:indexPath.row] objectForKey:@"status"] isEqual:@"1"]) {
    serverPlayers.text = @"10000000";
}

Comment: Go to json.org and study the syntax for the 5-10 minutes it takes to learn it.  Your problem is that you're trying to treat an array like an dictionary/"object" -- JSON is a mix of the two.  If you learn to read the syntax you can easily see which is which.
Comment: Could you post the block that gives you the error?
Comment: The code for assigning `serverResults` you posted is different than the whats on pastebin. Which one are you actually using?
Comment: Sorry for the confusion, @MikeD, I am using the one I posted here.
Comment: And don't do this: `if ([[[serverResults objectAtIndex:indexPath.row] objectForKey:@"status"] isEqual:@"1"])`. Instead, break it into an assignment to "serverItem" to receive the result of objectAtIndes, an assignment to "status" (starting with "serverItem" to receive the objectForKey result, and an then the if isEqual.  This makes things MUCH easier to understand and MUCH easier to debug.
Answer: The error you get points out that you are working on an NSArray instead of a NSDictionary. There is no method objectForKey defined on an NSArray. So what you could do is to debug your Webservice response and check the Data types.
Regarding at your output from JSON it should be like that:
NSDictionary (Keys: status, error, debug, result, servers)
servers is an NSArray which has NSDictionaries as elements.
To get the title of a server:
NSDictionary *resultDict = [news objectForKey:@"result"];
NSArray *servers = [resultDict objectForKey:@"servers"];
NSDictionary *firstServer = [servers objectAtIndex:0]; // I fetch here the first server
NSString *titleOfFirstServer = [firstServer objectForKey:@"title"];
NSNumber *statusOfFirstServer = [NSNumber numberWithInt:[[firstServer objectForKey:@"status"] intValue];

To iterate over all servers you should do it like that:
 NSDictionary *resultDict = [news objectForKey:@"result"];
 NSArray *servers = [resultDict objectForKey:@"servers"];

 for(NSDictionary *server in servers) {
   NSString *title = [server objectForKey:@"title"];
   NSNumber *status = [NSNumber numberWithInt:[[server objectForKey:@"status"] intValue];

   NSLog(@"%@%d", title, [status intValue]);
 }

Comment: thanks for the reply, except I am getting another error now, "use of undeclared identifier 'jsonResponse'"
Comment: Yeah, I didnt know what the variable name was. It should be "news" instead of jsonResponse.
Comment: Now I get this error after changing jsonResponse to news: No visible @interface for 'NSArray' declares the selector 'objectForKey:' Error's title is : ARC Semantic Issue.
Comment: What is news exactly? Can you please check with the debugger if its an NSArray or an NSDictionary.
Comment: 'news' is an NSArray as defined in the header file, I could change it if needed but this was the only way I found to get JSON file from the web, I used a tutorial.
Comment: Change it to NSDictionary. Then I guess it should work. If this JSON code is all you get from the server than its a NSDictionary instead of a NSArray :)
Comment: That fixed the error, but do I have to create variables like titleOfFirstServer and statusOfFirstServer for EACH server? I don't control how many the client has, he could have 1 or 100
Comment: No you don't. This was just an example to show you, how you can access the values of the Dictionaries. Please do not forget to mark this question as answered, if its solved now..
Comment: using that code I only get the first server, and it shows it 4 times (http://puu.sh/3QhOT.png) when in the JSON there are 2 completely seperate servers.

{"status":"OK","error":"","debug":"2 server(s)","result":{"servers":[{"id":1,"title":"Test","players":0,"slots":10,"status":3},{"id":2,"title":"Creative Spawn","players":0,"slots":5,"status":-1}]}}
Comment: Yeah this is because in the example code I'm only fetching the data from the first server  (objectAtIndex:0 with objectAtIndex:1 you could get the second one). But anyway if you want to get all servers you have to iterate over the NSArray.. But this is really basic stuff. You should first do some tutorials on Arrays, Dictionaries and so on..
Comment: Ok thanks for the reply, but is there any reason it shows it 4 times like in the provided screenshot? And changing it to this gives me a SIGABRT: http://pastebin.com/jTVAWCK0
Comment: I'll have to check into that, now to fix the SIGABRT! :/
Comment: Would it be possible for you to help me with setting up some sort of 'loop' or something to allow multiple servers to be listed and not just the 0th or 1st entry in JSON? ' NSDictionary *myServer = [servers objectAtIndex:indexPath.row]; ' seems to give me SIGABRT every time.

iOS and JSON nesting issue


How do I appropriately interact with PayPal IPN for the purpose of serial generation?

Question: Having a bit of trouble with the PayPal IPN.  I've seen variations on these questions listed about a million times, but not the answers I'm looking for.  As usual this may be a case of asking a question regarding the wrong thing, but I'm hoping there are some super geniuses out there that can assist.
Anyway, I basically need to set up the purchase of some readily available software, and, once the transaction is complete send them a serial.  One would think this is simple, but given the criteria it has proven anything but.  Basically the steps are as follows:
1) Custom form on website to enter personal (but not too =P) info
2) Forward them to PayPal site to make purchase
3) Upon successful transaction AUTOMATICALLY send them to a serial page and/or send an email with the registration.
I've looked at various IPN classes (using PHP btw) and nothing seems very straightforward, and it doesn't seem that PayPal likes any kind of custom variables being passed, nor does it seem to automatically send someone elsewhere after the purchase.  Also, which ID should I be looking at if/when the process finally does come back around?
Perhaps I need the "Pro" account?  In any event, please make no assumptions regarding my knowledge of PayPal IPN.  I'd simply like a class (hopefully one I haven't tried already) or at least some more straightforward (and concise) documentation on how to accomplish this.
Best.
EDIT #1:
Does anyone have any information on enabling the QUANTITY field during a purchase?  For some reason it's stuck at 1 and will not allow a change.  I'm not passing anything for quantity atm.
Answer: I suggest that you use ezimerchant -> www.ezimerchant.com
It's free to sign up. They have an excellent API that allows you to do all that, they hook straight in with paypal so you dont have to bother learning their complex api.
To get you started, you'd use the OrderCreate API, once you signed up you can access the documentation here:
https://api.ezimerchant.com/[merchantid]/orders/edit
Don't forget to add your merchant id.
then it's as simple as supplying a "RETURNURL" and a NOTIFYURL. The Notify url is a php script that accepts a ping back with the order details, you just check if payment was successful and email out the serial code.
I have some code in php for this stuff so let me know if you go down this path.
Answer: The Paypal API's are kinda known for not being super user-friendly. That said, I have successfully used the IPN API, however, I'm not sure this is what you want to use in your case. IPN (Instant Payment Notification) is 1.) For sending notifications to your server. 2.) It communicates with your server and therefore does not have any effect on a customer's transaction. 3.) Not instant all the time.
So, if you want to send a customer to a certain page, after purchase, you will not be able to accomplish this with IPN. You could start by looking at the Express Checkout API.
If you just need a simple redirect, Paypal provides a way to do this.
You could setup a PHP file on your server, which integrates with IPN. This file would work like this:

IPN sends a "transaction completed" notification to your PHP file.
Your PHP file reads the notification to ensure that it's a "completed transaction notification", as opposed to another notification (such as "transaction failed", etc).
If everything is ok, your PHP file can generate and email out whatever you need to the customer.
Comment: @humble_coder You can get pretty much any info related to the transaction when you receive an IPN. Go to https://www.x.com/docs/DOC-1084 and about 2/3 the way down the page - Note the `custom` field.
Comment: Thanks.  I actually have this much working.  What I need is the information from the initial "custom form" to be used on the back end.  I've been told to use a DB and simply generate what I need upon response, and I'm ok with that.  However, I cannot find any information upon which to base any decisions post-purchase.  For example I'd like to be able to pass a unique transaction variable so that I can then generate the appropriate serial once the payment is successful.  Getting the transaction ID after the fact does me no good if I don't know what custom data it matches.
Comment: Ezimerchant can do this. you pass it a variable in your API call, called CUSTOM and it will return you the same set of values that you post to it when you are creating the order.
Comment: Thanks Jason, but I've previewed the ezimerchant demo and it doesn't interest me.  As I've stated, I've got the PayPal portion working for "simple" IPN transactions.  I simply have a bit further to go to get the custom items working.


How can a connection started with NSURLConnection while in the foreground be continued in the background?

Question: I have been searching for literally weeks to try to find an answer, or an example of how to do this.
All the examples/tutorials for NSURLConnection show it starting in the foreground or starting in the background, ditto all the examples for beginBackgrounTaskWithExpirationHandler: show how to start a background task after entering the background.
As far as I can tell there is nothing out there on the internet or books that shows how to start a connection while in the foreground and then if its not finished continue it in the background.
The answer to this question does not actually answer the question:
How should beginbackgroundtaskwithexpirationhandler: be dealt with for an NSUrlConnection that is already in progress?
If you read the referened Beyond The Basics section it says: "While the app is in the foreground the background task won't have any effect". This means it is not possisble to start a background task using NSURLConnection while in the foreground if you want to download in the foreground.
Comment: The document says "While the app is in the foreground the background task won't have any effect; however, if the user moves the app to the background, the existence of the background task will automatically keep the app running so that it can complete the operation." This makes perfect sense to me as how the background task should work -- once you start the task(eg. download a file) in the foreground, if the app enters background, it will finish up the download task without cut down the network connection.
Answer: You simply call beginBackgroundTaskWithExpirationHandler: while your app is in the foreground right when you start the download process. Note that you have to store the return value in an ivar/property:
@property (nonatomic, assign) UIBackgroundTaskIdentifier backgroundTaskID;

@synthesize backgroundTaskID;

...

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
self.backgroundTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
    // Cancel the connection
    [connection cancel];
}];

This will allow your app to keep running if it gets sent to the background while the download is running. Then, in your delegate methods that signify the completion of the download, you have to place the matching endBackgroundTask::
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    // Handle the error
    ...

    [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskID];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // Save the downloaded data
    ...

    [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskID];
}

Comment: just tried this method, works like a charm with NSURLConnection! thanks!
Comment: Notice that nowadays this method just works for 3 minutes in background since iOS 7, is there any way to keep it alive?


jQuery set focus

Question: How do I set focus in jQuery?  I've been trying to keep focus on a control when inside a focusout event but it doesn't seem to be working.
$(function() {
    $("#Line1").focusout(function() {
        alert("YOU CAN'T LEAVE HERE");
        $("#Line1").focus();
    });
});

My particular application is attempting to validate a field when it leaves focus and if it fails the validation I want the focus to remain on that control.
Here is my code on jsfiddle.net
Answer: jsFiddle
In google chrome the above example is working
HTML

            Line 1:

                <input id="Line1" value="" maxlength="50" size="50" name="Line1"/>

                Error in Line 1

            Line 2:

                <input id="Line2" value="" maxlength="50" size="50" name="Line2"/>

JS
$(function() {
    $("#errorLine1").hide();
    $("#Line1").focusout(function() {
        $("#errorLine1").show().delay(3000).fadeOut();
        $("#Line1").focus();
    });
});

CSS
body {
    font-family:Tahoma;
    font-size : 8pt;
    padding-left:10px;
}
input[type="text"] {
    font-family:Tahoma;
    font-size : 8pt;
    width:150px;
}

Answer: You can't change the focus in the focusout event, as the browser is already in progress of changing the focus. You can use a timer to change the focus back once the browser is done changing the focus:
$(function() {
  $("#Line1").focusout(function() {
    alert("YOU CAN'T LEAVE HERE");
    window.setTimeout(function(){
      $("#Line1").focus();
    }, 0);
  });
});

Demo: http://jsfiddle.net/Guffa/12j5jraa/6/
Note: You should really consider if there is any other way of doing whatever it is you want to accomplish. The behaviour of not allowing the user to leave a field can be EXTREMELY ANNOYING!

jQuery set focus


How to use the gcloud storage.download attribute

Question: So basically im trying make a program in which i can upload and download files from my local to firebase storage. The following is the code used:
path_on_cloud = "test/test"
firebase = pyrebase.initialize_app(config)
storage = firebase.storage()
path_local = "code.py"
storage.child(path_on_cloud).put(path_local)

storage.child(path_on_cloud).download('python_file.py')

but im getting an error of
Traceback (most recent call last):
  File "C:/Parth/Python/udfirebase.py", line 26, in <module>
    storage.child(path_on_cloud).download('img.jpg')
TypeError: download() missing 1 required positional argument: 'filename'

Can someone tell what went wrong and how to solve this error
Comment: See: https://github.com/nhorvath/Pyrebase4/issues/7
Answer: Posting  as Community Wiki, as part of it is based in a comment.
The error you are seeing it's saying that you don't are still missing one argument in the method download() and this argument it's filename. Usually the download is made using a Blob - as the official documentation here indicates - ann in both situations, with Blob or not using Blob, which is your case, you will need to add the filename to the download method. You will always need to send the path and the name of the file you want to download.
Considering that, I would recommend you to give it a try changing the way you are calling your method and add the filename, while also including the path, as mentioned here, so you avoid the error.