Q:

How to use a class's method inside a function (PHP)?

I'm trying to use the new PHP mysqli extension. I've got a function (safe()) that recursively uses mysql_real_escape_string to make strings safe. How do I use my mysqli connection inside this function to call the mysqli::escape_string() function?
Example:
$db = new mysqli($host,$user,$password,$database_name);

function safe ($data) {
  if(!is_array($data)) {
     if(!get_magic_quotes_gpc()) {
       $data = **mysqli::escape_string($data)**
       return $data;
    }
  } else {
    return array_map('safe',$data);
  }
}

Where I have mysqli::escape_string() inside safe() how do I call that? Outside of a function it would be $db->escape_string() but I can't find a way to call it insde. I've tried passing $db to the function, making $db global etc. The alternative is to use the procedural mysqli_escape_string() but that requires the mysqli link resource to be explicitly passed to it, but I can't find a way to access that.

A:

pass your DB object in to the function.
 function safe ($data, $db) {
  if(!is_array($data)) {
     if(!get_magic_quotes_gpc()) {
       $data = $db->escape_string($data);
    }
  } else {
    return array_map('safe',$data);
  }
}


Q:

SQL search configuration

I am not very good at SQL and I have a simple zipcode finder that I created for my website.  The SQL is as follows:
$query = "select * from zipcodes2 where ZipCode like \"%$trimmed%\"

*\"%$trimmed%* of course is the actual zipcode the user enters in.  It works great but now I want to make it more advance by letting the user search for city or state and not just zipcode.  I already have the columns created and they are named "City" and "State".  What exactly should I put in the SQL line?  Will I have to change any other part of the code, specifically the \"%$trimmed%\ part?

A:

This is not a great approach but you can get close to what you want with this:
$query = "select * from zipcodes2 where ZipCode like '%$trimmed%' or (CONCAT(City, ',', State) = '$trimmed')"

But you must make sure the the user's response has no white space so it comes out as Chicago,IL and not anything like Chicago, IL or Chicago , IL.
You can do this by putting this before your query:
$trimmed = preg_replace( '/\s+/', ' ', $trimmed );


Q:

Iterate and join char arrays

I've got two char arrays, which i'm trying to join character by character. Here's an illustrated example:
both[10]
   |
   |______both[0]
            |_____one[0]
            |
          both[1]
            |_____two[0]
            |
          both[2]
            |_____one[1]
            |
         ..etc..

I tried using two loops, but that didn't work for the second array. It's probably something simple, although I've not found the solution.
char one = "ji-hs";
char two = "onti";
char both[10];

for (int i = 0; i < 10; ++i) {
    memcpy(&both[i], &one[i], 1);
}

for (int i = 0; i < 10; ++i) {
    memcpy(&both[i+1], &two[i], 1);
}

The two arrays joined together is supposed to read join-this

A:

If I understand you correctly:
char *one = "ji-hs";
char *two = "onti";
char both[11];

for (int i = 0; i < 5; i++) {
    both[i*2] = one[i];
    both[i*2+1] = two[i];
}
both[10] = 0;


Q:

How to vertically align first lines of text in table cell?

I have this situation where first table cell contains "name" and second cell some text. Text may have embedded images. When image appears on the first line it pushes down entire first line. It makes first text line to be no longer aligner with first cell text. I want to make first lines of both table cells aligned.

        one
        two  three three three three three three three three three three three three three three three three three three three

http://jsfiddle.net/gdx0qhcc/31/
In following fiddle goal is to align word "one" to word "two". I know i could vertical-align: top; on image, but it pulls text of second cell up and it looks bad therefore it is not an option. Any kind of solution would work really, be it css/javascript/webkit-only solution.
EDIT:
Note that height and width of image may vary. I also updated fiddle with image alignment being "middle". It does not change situation really.
EDIT2:
Final solution "hack" http://jsfiddle.net/Lqcx2vfg/
Thank you everyone

A:

HTML:

    one
    two  three three three three three three three three three three three three three three three three three three three

CSS:
*{
    margin: 0;
    padding: 0;
}
table, tr, td {
    border: solid 1px red;
    line-height: 16px;
}
.align {
     vertical-align: top;
}

JavaScript:

$(document).ready(function() {
$('.align').css('padding-top',(parseInt($('img').css('height'))-16)+"px");

//16 is the line-height });


Q:

Unable to delete the row, upon clicking the delete button in laravel/mysql

I have been working on an example with scratch given in:"https://laravel.com/docs/5.1/quickstart". I am unable to delete the row upon clicking the delete button!
Web/Route.php
Route::get('/', 'TaskDisplayController@taskdisplay');

Route::post('/taskdisplay', function () {
    return view('/taskdisplay');
});
Route::get('/taskdisplay', function () {
    return view('/taskdisplay');
});

Route::post('/tasklistdisplay', 'TaskDisplayController@tasklistdisplay');
Route::get('/tasklistdisplay', 'TaskDisplayController@tasklistdisplay');

Route::post('/TaskDisplay', 'TaskDisplayController@taskdisplay');
Route::get('/TaskDisplay', 'TaskDisplayController@taskdisplay');

Route::delete('/tasklistdisplay/{id}', function ($id) {
    DB::table('tasktable')->findOrFail($id)->delete();

    return redirect('/');
});

TaskDisplayController.php
class TaskDisplayController extends BaseController
{
    public function taskdisplay()
    {
        $task_ = DB::table('tasktable')->select('task','taskid','id')->get();
        return view('taskdisplay',['task'=> $task_]);
    }

    public function tasklistdisplay(Request $req)
    {
        $task_=$req->input('taskfield');
        if(DB::insert('insert into tasktable (id, task) values (1, ?)',[$task_]))
        {
            return redirect('/TaskDisplay');
        }
        else
        {
            echo "Login failed; Wrong data entered";
        }
    }
}

taskdisplay.blade.php

        Task ID
        User ID
        Task
        Options

        @foreach($task as  $tsk)
            {{$tsk->taskid }}
            {{$tsk->id }}
            {{$tsk->task }}

            <form action="/task/{{ $tsk->id }}" method="post" >
                {{ csrf_field() }}
                {{ method_field('DELETE') }}
                <button>Delete Task</button>
            </form>

            @endforeach
<input type="hidden" value={{csrf_token()}} name=_token>

Expect it to work like :https://laravel.com/docs/5.1/quickstart
But upon clicking the delete button 404 page not found is shown

A:

Adjust the action route as shown below:
action="/tasklistdisplay/{{ $tsk->id }}"

findOrFail is an eloquent function not Query Builder. So it should be:
DB::table('tasktable')->whereId($id)->delete();


Q:

Development Environment Setup Tips

Can anyone offer tips on how to setup the ideal windows development environment? Contemplating on moving to vmware or virtual box setup for development and keeping the development tools off of my main system but then think that would cause a headaches if I want to just try something really quick.
Just looking for suggestions from other developers on how they setup their development environment to achieve the maximum productivity.
Thanks in advance!

A:

I recommend VMWare or a similar Virtualization product. It saves so much time.
I setup a VMWare image with a clean Windows install all the development tools I needed, and I then made a copy of this. Now anytime I need a "clean" dev environment I can simply duplicate the VM Image and off I go!
It also means all my non-development files are seperate and are out of harms way.
And if I choose, I can take the VM file with me to another PC and have the same development environment.
Using a VM also lets you try stuff out without any harm. You can create a "snapshot" - run/perform some action, then easily revert back to the snapshot. Great for testing something over and over quite easily.


Q:

Question regarding removing source files after building

I've been building a lot of software from source as I've been wanting to learn it for quite a while now. The following happens:

Download source files (normally a tarball file)
Install dependencies (normally a bunch of libraries)
Configure, build and either install or just run

I am still confused as to what files relate to what etc. so my question might appear as stupid.
I have 128gb of ssd hard drive space. This is not a lot for me. I need every single bit!
When building from source it compiles the file into a so called dist folder which can then be run (or installed). So my question is:

As I assume the source files are not needed since I built the software, how do I remove these files safely?

A:

In most cases (source files are not consistent), you can remove the source files, tarball etc if you want. This however may make it difficult to remove the software depending on how you installed it, and the software itself.
If you used checkinstall, or don't think you want to remove the packages later, you can delete the source tarball(s) and free up space easily. If not, see below.
Tarball installers generally work by building anything they need to build, and then copying/moving the files to where they need to be. Some also are helpful by deleting temporary build files after compiling, and/or make a list of what was installed (which often is needed to uninstall, and is why you might not want to delete those files). Anyway some installers may have a uninstall option - e.g. to reverse sudo make install:
sudo make uninstall

This varies by installers, you can see if it supports by reading the documentation of the software (this should be included with the package), or by examining the uninstall file. So if you need to use the uninstall option, you may want to keep the source files. They should not be necessary to run most programs normally.
N.B. You probably can also remove some of the dependencies (the devel packages etc) as those may have only been needed for compiling the software, you don't need them to run it. To free up space generally you can also use the autoremove option with the apt-get command to remove unneeded dependcies.


Q:

How to get form data from POST request in Java

How to I extract form data from a POST request eg. $ curl -X POST -d asdf=blah http://localhost:8000/proxy/http://httpbin.org/post - and I need to extract asdf=blah.
The way I am currently doing it relies heavily on the data I have read being in a certain format (I am assuming the form data is always on the last line). Is there a better (and/or simpler)  way to get the data which does not depend on the format of the data being read ?
(Note: the proxy deals with both GET and POST requests)
Here is code I have written :
public class ProxyThread3 extends Thread {
private Socket clientSocket = null;

private OutputStream clientOutputStream;

private static final int BUFFER_SIZE = 32768;

public ProxyThread3(Socket socket) {
    super("ProxyThread");
    this.clientSocket = socket;

}

public void run() {
    try {

        clientOutputStream = clientSocket.getOutputStream();

        // Read request
        InputStream clientInputStream = clientSocket.getInputStream();

        byte[] b = new byte[8196];
        int len = clientInputStream.read(b);

        String urlToCall = "";

        if (len > 0) {

            String userData = new String(b, 0, len);

            String[] userDataArray = userData.split("\n");

            //Parse first line to get URL
            String firstLine = userDataArray[0];
            for (int i = 0; i < firstLine.length(); i++) {

                if (firstLine.substring(i).startsWith("http://")) {

                    urlToCall = firstLine.substring(i).split(" ")[0];
                    break;
                }

            }

            //get request type
            String requestType = firstLine.split(" ")[0];

            String userAgentHeader = "";

            //get USER-AGENT Header and Accept Header
            for (String data : userDataArray) {

                if (data.startsWith("User-Agent")) {

                    userAgentHeader = data.split(":")[1].trim();
                    break;

                }

            }

            switch (requestType) {

                case "GET": {

                    sendGetRequest(urlToCall, userAgentHeader);
                    break;
                }

                case "POST": {

                    String postParams = null;

                    //Get Form Data
                    if (!userDataArray[userDataArray.length - 1].isEmpty()) {

                        postParams = userDataArray[userDataArray.length - 1];

                    }

                    sendPostRequest(urlToCall, userAgentHeader, postParams);
                    break;
                }

            }

        } else {
            clientInputStream.close();
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            clientSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

private void sendPostRequest(String urlToCall, String userAgentHeader,  String postParams) throws IOException {

    URL urlToWriteAndReadFrom = new URL(urlToCall);

    HttpURLConnection httpURLConnection = (HttpURLConnection) urlToWriteAndReadFrom.openConnection();

    httpURLConnection.setRequestMethod("POST");

    // set User-Agent header
    httpURLConnection.setRequestProperty("User-Agent", userAgentHeader);

    httpURLConnection.setDoOutput(true);

    OutputStream urlOutputStream = httpURLConnection.getOutputStream();

    if (postParams != null) {
        urlOutputStream.write(postParams.getBytes());
        urlOutputStream.flush();

    }

    urlOutputStream.close();

    int responseCode = httpURLConnection.getResponseCode();

    if (responseCode == HttpURLConnection.HTTP_OK) { // success

        InputStream dataReader = httpURLConnection.getInputStream();

        //begin send response to client
        byte inputInBytes[] = new byte[BUFFER_SIZE];

        assert dataReader != null;

        int index = dataReader.read(inputInBytes, 0, BUFFER_SIZE);

        while (index != -1) {
            clientOutputStream.write(inputInBytes, 0, index);
            index = dataReader.read(inputInBytes, 0, BUFFER_SIZE);
        }
        clientOutputStream.flush();

    }

}

private void sendGetRequest(String urlToCall, String userAgentHeader) throws IOException {

    URL urlToReadFrom = new URL(urlToCall);
    HttpURLConnection httpURLConnection = (HttpURLConnection) urlToReadFrom.openConnection();

    // set True since reading and getting input
    httpURLConnection.setDoInput(true);
    httpURLConnection.setRequestMethod("GET");

    // set User-Agent header
    httpURLConnection.setRequestProperty("User-Agent", userAgentHeader);

    int responseCode = httpURLConnection.getResponseCode();

    if (responseCode == HttpURLConnection.HTTP_OK) { // success

        InputStream dataReader = httpURLConnection.getInputStream();

        //begin send response to client
        byte inputInBytes[] = new byte[BUFFER_SIZE];

        assert dataReader != null;

        int index = dataReader.read(inputInBytes, 0, BUFFER_SIZE);

        while (index != -1) {
            clientOutputStream.write(inputInBytes, 0, index);
            index = dataReader.read(inputInBytes, 0, BUFFER_SIZE);
        }
        clientOutputStream.flush();

    }

}

}
PS. I am new to all of this so if there are any errors in my code, please do point them out

A:

Here is a full example on how to create a Java HTTP server that handles POST and GET requests.

https://www.codeproject.com/Tips/1040097/Create-a-Simple-Web-Server-in-Java-HTTP-Server

That being shared, this is quite primitive, I'd recommend using any third party library or light-weight Java server like Grizzly or Jetty if not a server like Apache Tomcat utilizing J2EE Servlets which are made for this purpose.


Q:

Does the Sun move?

I'm curious; does the Sun actually move? The concept behind the question is that if you wear a shirt and you move, does the shirt actually move? The Solar System orbits around the Milky Way, but does the Sun move?

A:

The Sun orbits in the Galactic potential. The motion is quite complex; it takes about 230 million years to make a circuit (meaning an orbital speed of around 220 km/s), but at the same time it oscillates up and down with respect to the Galactic plane every $\sim 70$ million years and also wobbles in and out every $\sim 150$ million years (this called epicyclic motion).
One can also measure how the Sun moves with respect to the average motion of the stars in the solar vicinity - the so-called local standard of rest. Estimates of the Sun's motion with respect to the LSR vary a little. According to Dehnen (Walter!) & Binney 1998 the Sun moves at 10 km/s inwards, at about 5 km/s faster than the average star tangentially to the Galactic centre and at about 7 km/s upwards out of the Galactic plane. Though as I mentioned previously, these motions are of an oscillatory nature around the mean value.
We could cast our net wider and ask how fast Sun moves with respect to the standard of rest defined by the cosmic microwave background - the local cosmological rest frame. The Sun moves at about 370 km/s relative to the CMB in the direction of the constellation Leo (as revealed by the dipole-like anisotropy in the CMB temperatures).


Q:

Form field without entity property in Symfony2

I have a form type which has a field that is not in the entity as a property, but the entity has a getter and a setter with the same name as form field, explaining:
Form type:
$builder->add('theField', 'entity', array(
    'label' => 'The field',
    'class' => 'MyAppBundle:AnEntity',
    'empty_value' => '',
));

Entity:
class User
{
    //There is NOT a property called "theField"

    public function setTheField($value)
    {
        ...
    }

    public function getTheField()
    {
        ...
    }
}

So, I'm expected that Symfony2 call the getter and setter to bind and show the form field, but I get this error:
Property theField does not exists in class My\AppBundle\Entity\User
Is there a way to create this form field without having the property declared in entity?
EDIT
Is strange, but when I declare a private property theField, it's works (BTW, that is not what I looking for).

A:

Did you try:
$builder->add('theField', 'entity', array(
    'label' => 'The field',
    'class' => 'MyAppBundle:AnEntity',
    'empty_value' => '',
    'property_path' => false,
));

Update
Change your field name to the same as attribute in the entity or change 'property_path' to the attribute name.
$builder->add('theField', 'entity', array(
    'label' => 'The field',
    'class' => 'MyAppBundle:AnEntity',
    'empty_value' => '',
    'property_path' => 'theField',
));

And in your entity add:
private $theField = null;


Q:

FtpWebRequest 30 minute time out

My code is experiencing a time out exception after exactly 30 minutes when downloading a large file over FTP. The server is FileZilla running on Windows. We have an SSL certificate configured with options Enable FTP over SSL/TLS support (FTPS) and Allow explicit FTP over TLS enabled. I have access to the server and the FileZilla configuration but can't see anything that might cause this behavior. Below is the source code which is running on .NET 4.6.2 on a Windows 2012 Server Machine. It can download files from the FTP server but will time out with the exception (listed below) after exactly 30 minutes if the file takes longer than 30 minutes to download.
As a test I have used FileZilla Client, running from the same client PC, to download multiple large files from the same server end point simultaneously so that the download for each file took over 30 minutes to complete. No errors occurred in this scenario.
I have searched on StackOverflow as well as Google but nothing promising turned up. If anyone has some tips on where to look (server side or client side) I would be most appreciative.

Application code
public class FtpFileDownloader
{
    // log4net
    private static readonly ILog Logger = LogManager.GetLogger(typeof(FtpFileDownloader));

    public void DownloadFile()
    {
        // setting the SecurityProtocol did not change the outcome, both were tried. Originally it was not set at all.
        // ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

        ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;

        const int timeout = 7200000;
        const string file = "some-existing-file";
        try
        {
            var request = (FtpWebRequest) WebRequest.Create("uri-path-to-file");
            request.KeepAlive = false;
            request.Timeout = -1;
            request.ReadWriteTimeout = timeout;

            request.Credentials = new NetworkCredential("userName", "password");
            request.UsePassive = true;
            request.EnableSsl = true;
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            Logger.Debug($"Downloading '{file}'");
            using (var response = (FtpWebResponse) request.GetResponse())
            using (var sourceStream = response.GetResponseStream())
            using (var targetStream = new FileStream("some-target-on-disk", FileMode.Create, FileAccess.Write))
            {
                try
                {
                    sourceStream.CopyTo(targetStream);
                    targetStream.Flush();
                    Logger.Debug($"Finished download '{file}'");
                }
                catch (Exception exInner)
                {
                    Logger.Error($"Error occurred trying to download file '{file}'.", exInner);
                }
            }
        }
        catch (Exception ex)
        {
            Logger.Error($"Error occurred trying to dispose streams when downloading file '{file}'.", ex);
        }
    }
}

Application Log
ERROR FtpFileDownloader - Error occurred trying to download file 'some-existing-file'.
System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream.
   at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.Security._SslStream.StartFrameBody(Int32 readBytes, Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security._SslStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security._SslStream.StartReading(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security._SslStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.TlsStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.FtpDataStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.IO.Stream.InternalCopyTo(Stream destination, Int32 bufferSize)
   at System.IO.Stream.CopyTo(Stream destination)
   at FtpFileDownloader.DownloadFile

ERROR FtpFileDownloader - Error occurred trying to dispose streams when downloading file 'some-existing-file'.
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
   at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   at System.Net.FtpWebRequest.RequestCallback(Object obj)
   at System.Net.CommandStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at System.IO.Stream.Dispose()
   at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
   at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse)
   at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
   at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   at System.Net.FtpWebRequest.RequestCallback(Object obj)
   at System.Net.CommandStream.Abort(Exception e)
   at System.Net.CommandStream.CheckContinuePipeline()
   at System.Net.FtpWebRequest.DataStreamClosed(CloseExState closeState)
   at System.Net.FtpDataStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
   at System.Net.FtpDataStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at System.IO.Stream.Dispose()
   at FtpFileDownloader.DownloadFile

FileZilla - General Settings
 Listen on these ports: 21
  Max. number of users: 0 (infinite)
     Number of threads: 2
    Connection timeout: 120 (seconds)
   No Transfer timeout: 9000 (seconds)
           Log timeout: 60 (seconds)

FileZilla Server Log
23-2-2018 11:40:40 - (not logged in) (194.123.75.2)> Connected on port 21, sending welcome message...
23-2-2018 11:40:40 - (not logged in) (194.123.75.2)> 220 Welcome
23-2-2018 11:40:40 - (not logged in) (194.123.75.2)> AUTH TLS
23-2-2018 11:40:40 - (not logged in) (194.123.75.2)> 234 Using authentication type TLS
23-2-2018 11:40:40 - (not logged in) (194.123.75.2)> TLS connection established
23-2-2018 11:40:40 - (not logged in) (194.123.75.2)> USER  my-user-account
23-2-2018 11:40:40 - (not logged in) (194.123.75.2)> 331 Password required for my-user-account
23-2-2018 11:40:40 - (not logged in) (194.123.75.2)> PASS **************
23-2-2018 11:40:40 - my-user-account (194.123.75.2)> 230 Logged on
23-2-2018 11:40:40 - my-user-account (194.123.75.2)> PBSZ 0
23-2-2018 11:40:40 - my-user-account (194.123.75.2)> 200 PBSZ=0
23-2-2018 11:40:40 - my-user-account (194.123.75.2)> PROT P
23-2-2018 11:40:40 - my-user-account (194.123.75.2)> 200 Protection level set to P
23-2-2018 11:40:40 - my-user-account (194.123.75.2)> OPTS utf8 on
23-2-2018 11:40:40 - my-user-account (194.123.75.2)> 202 UTF8 mode is always enabled. No need to send this command.
23-2-2018 11:40:40 - my-user-account (194.123.75.2)> PWD
23-2-2018 11:40:40 - my-user-account (194.123.75.2)> 257 "/" is current directory.
23-2-2018 11:40:40 - my-user-account (194.123.75.2)> TYPE I
23-2-2018 11:40:40 - my-user-account (194.123.75.2)> 200 Type set to I
23-2-2018 11:40:40 - my-user-account (194.123.75.2)> PASV
23-2-2018 11:40:40 - my-user-account (194.123.75.2)> 227 Entering Passive Mode (IP-ADDRESS,245,222)
23-2-2018 11:40:40 - my-user-account (194.123.75.2)> RETR path-to-file
23-2-2018 11:40:40 - my-user-account (194.123.75.2)> 150 Opening data channel for file download from server of "/path-to-file"
23-2-2018 11:40:40 - my-user-account (194.123.75.2)> TLS connection for data connection established
23-2-2018 12:10:41 - my-user-account (194.123.75.2)> disconnected.

Notice that there is 30 minutes between the disconnect (last line) and the line before it. If it had completed the transfer with success there would also be a line that read 226 Successfully transferred "/path-to-file" before the disconnected line

Progress update:

(2018.02.20) I asked our network team to check the fire wall rules but they could not find anything of interest.
(2018.02.22) I found that the version of FileZilla Server being used is 0.9.43 beta (release date 2014-01-02 according to the change log). Although I did not find anything in the change log that suggests this behavior was ever a fixed bug I am going to upgrade to the latest version 0.9.60.2 (release date 2017-02-08) and execute the test again. Will report back within 24 hours.
(2018.02.23) FileZilla has been updated to the latest version. This did not fix the problem, I updated the server log but it looks almost identical to the previous log with the exception being that this last transfer took place over TLS instead of SSL.
(2018.02.23) I found the following link Timeouts on large files on the FileZilla support page. I am going to push it back to the network staff of our hosting provider to take another look.
(2018.02.27) It turned out that the company firewall (network where the client executes) and not the hosting firewall (network where FileZilla is hosted) was the culprit. It was configured to drop idle connections after 1800 seconds. A rule has been added to override this between these 2 end points.

Culprit / Answer
It turned out that the company firewall (network where the client executes) and not the hosting firewall (network where FileZilla is hosted) was the culprit. It was configured to drop idle connections after 1800 seconds. A rule has been added to override this between these 2 end points.

A:

Probably you should try another implementation of FTP protocol client that isn't built on top of the FtpWebRequest.
Related issues exist for a long time, they don't have a clear solution or answer. So I would try something like FluentFTP, it uses Winsock API directly. XML Documentation Comment states that DownloadFile() should handle large file downloads well:
/// <summary>
/// Downloads the specified file onto the local file system.
/// High-level API that takes care of various edge cases internally.
/// Supports very large files since it downloads data in chunks.
/// </summary>

For more information check:

Downloading large files(~150MB) from FTP server hangs discussion on StackOverflow
FtpWebRequest is Broken article by Matt Mitchell
Winsock API page on Microsoft TechNet
Source Code section in Partial FTP Downloader article by Elmue


Q:

C99 Enum - Need Clarification

I have reviewed this but the accepted answer doesn't make sense to me.  I should be able to define an enum in C99 as
enum WeekDays
{
    MON, TUES, WED, THURS, FRI, SAT, SUN
}days;

and utilize the enum as follows in main as
days = FRI;
if (days == FRI)
{
    printf("Thank God it's Friday!");
}

Why the additional work in the accepted answer to utilize the enum?

A:

Your code should work.  In general though the accepted answer you point to is better programming practice.  It's desirable to separate the declaration of new types from the use of those types.  For example if you wrote a library for day manipulation, you might include the enum weekdays in that library.  But that would be a bad place to define a variable for your program to use.
Over time programmers have found this sort of separation valuable.  It generally helps code be more readable


Q:

the spectrum of matrix with positive entries

It is  well known that  a matrix which  all entries are positive real numbers, has  a positive eigenvalue.(see algebraic topology, by Allen Hatcher). Now is the following generalization, true?
Let A be  a  C*  algebra and X is a matrix which entries  are positive elements of  A.
Does sp(X) has nonempty intersection with positive real line?

A:

The answer is no. For example, the 4-by-4 matrix
$\begin{pmatrix} p & e \\ f & q \end{pmatrix}$,
with $p=\begin{pmatrix} 1 & 0\\ 0 & 0\end{pmatrix}$, $q=1-p$, $e=\begin{pmatrix} 1/2 & 1/2\\ 1/2 & 1/2\end{pmatrix}$, and $f=1-e$ has the characteristic polynomial $x^4-2x^3+x^2+1/4=(x^2-x)^2+1/4\geq1/4$ and has no real eigenvalues.


Q:

Proxy request on firebase

I have created a project using Ionic and deployed it as a PWA to firebase. I have got around CORS utilising a proxy to call google maps api sevices. This works locally however once deployed this is no longer the case.
The response I am getting on the server is:
SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at XMLHttpRequest.l (https://atomic-affinity-127705.firebaseapp.com/build/vendor.js:1:312114)
at t.invokeTask (https://atomic-affinity-127705.firebaseapp.com/build/polyfills.js:3:15660)
at Object.onInvokeTask (https://atomic-affinity-127705.firebaseapp.com/build/vendor.js:1:26996)
at t.invokeTask (https://atomic-affinity-127705.firebaseapp.com/build/polyfills.js:3:15581)
at r.runTask (https://atomic-affinity-127705.firebaseapp.com/build/polyfills.js:3:10834)
at e.invokeTask [as invoke] (https://atomic-affinity-127705.firebaseapp.com/build/polyfills.js:3:16794)
at p (https://atomic-affinity-127705.firebaseapp.com/build/polyfills.js:2:27648)
at XMLHttpRequest.v (https://atomic-affinity-127705.firebaseapp.com/build/polyfills.js:2:27893)

"Http failure during parsing"

When looking at the text field I get the contents of my index.html page being parsed. Starting with the Doctype explaining why the error fails on <.
Ionic.config.json has the following:
  "proxies": [{
    "path": "/api",
    "proxyUrl": "https://maps.googleapis.com/"
  }]

and is called as such:
/api/maps/api/place/nearbysearch/ ....etc
Any help would be greatly appreciated.

A:

For those that are interested in this solution, I ended up utilising the js libraries provided by Google. The alternative to get around this is create a functions set on firebase and run express to do the calls for with the appropriate CORS headers however I didn't want to add extra calls in.


Q:

Monit server went down

One of our developers needs some sort of notification in order to know whether the application is running smoothly or not.And We already decided that we will implement monit in order to get this things done.
Unfortunately,here comes another challenge. What happens if the monit itself went down?Surely they will not know whether their application is running or not?
Did anyone have any idea to make sure that this things can be notified (whether the server is going up or down)?
Thanks.

A:

Monit typically does not die on its own. It's pretty stable/mature from that perspective.
If you use M/Monit, the relatively inexpensive central Monit console, you'll be able to poll the health of your individual Monit instances.
There is also the approach of running Monit from inittab/getty, respawning it automatically, but it's not necessary in my experience.
Edit:
Default Monit service messages:
Monit instance changed Service localhost

    Date:        Thu, 11 Oct 2012 05:17:13
    Action:      start
    Host:        OGC
    Description: Monit started

Your faithful employee,
Monit

And:
Monit instance changed Service localhost

    Date:        Thu, 11 Oct 2012 05:17:09
    Action:      stop
    Host:        OGC
    Description: Monit stopped

Your faithful employee,
Monit


Q:

How do I add an Action to a Project Node when writing a Netbeans plugin module

I want to write a Netbeans plugin to allow the running of gulp.js tasks from a context menu on the project node.
How do I add extra items (Actions) to a Project node? I can't find anything in the docs.

A:

Register your action as shown in http://wiki.netbeans.org/DevFaqActionAddProjectPopUp ? IIRC there is also a way to add the action to a specific project type.


Q:

I need a word that means "easily understood or relatable to the common man"

I can't think of a word that would summarize this phrase into one coherent adjective. It's for a thesis I'm writing about a musical composer.

A:

accessible - easily understood or appreciated.

A:

Well, there's perspicuous:

Clearly expressed or presented; easy to understand

You'd definitely get points for using a difficult word to mean easily understood!  I'd recommend:

clear
clear-cut
comprehensible
explicit
intelligible
straightforward

Of which I personally prefer straightforward.


Q:

BackReferences, R1, R2 in URL Redirect rule questions

I tried to look for more info on back reference. But the ones i found aren't very clear. So I am really confused. I have this in a web.config
  <system.webServer>
<rewrite>
  <rules>
    <clear />

    <rule name="CTVNews Articles" stopProcessing="true">
      <match url="(.*?)CTVNews(/?)(.*)" />
      <action type="Redirect" url="http://www.ctvnews.ca{R:2}{R:3}" appendQueryString="true" redirectType="Permanent" />
    </rule>

so, I know  that is the rule with regular expression.That is easy to understand, but what about url="http://www.ctvnews.ca/generic{R:2}{R:3}. Can someone please kindly explain what is R2 and R3? and why we have R2, R3, instead of R1 and R2?
All help is greatly appreciate it.

A:

In the XML snippet you provided, the regular expression has three "capturing groups" - that is, use of parentheses ("(" and ")"). These "capture" whatever is inside them, and can then be referred to by number. There are numbered left-to-right, starting from 1.
Then in the <action> tag above, the {R:2} parts are referring to what the groups in the regular expression captured. There are three such groups, but the <action> is only using the second and third. The first one is being captured but is not being used.


Q:

LinearLayout even width distribution, but no wider than x

I have LinearLayout(horizontal) and I would like its children to have equal width, but, no more than some value.
The code below does not work.
<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:maxWidth="20dp"
            android:text="Button 1" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:maxWidth="20dp"
            android:text="Button 2" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:maxWidth="20dp"
            android:text="Button 2" />
    </LinearLayout>

Do you have any idea how to solve this?
Thanks.

A:

Change the width of each fragment programatically. You will have to have to width dimension of your windows and divide it by the number of your fragment. If it's > 30 width = 30.
int width = sizescreen/nbframent;
if (width > 30)
   width = 30;
frag.setLayoutParams(new LayoutParams(width, LayoutParams.WRAP_CONTENT));//do that for each of your child

Hope it helps.


Q:

React native's react-navigation is deleting modules

I am currently working with React Native and I want to use its navigation component. The documentation (https://facebook.github.io/react-native/docs/navigation.html) says I should install react-navigation module for using it, but when I install it
npm install react-navigation

the application stops working the moment it finishes installing. It seems it removes some modules; I get the following output:
added 16 packages, removed 1003 packages and updated 18 packages

Any idea why this is happening? How can this be fixed?

A:

What version of npm are you using? You may be experiencing this bug: https://github.com/npm/npm/issues/17379


Q:

Configure sails.js not to cache the response

I have an application on node.js using sails.js as a framework. For displaying the informations to the user I have implemented some .ejs files.
When navigating using links within the app menu, I receive a "304 - Nod Modified" response. Also, within headers in the response I see Etag, If-None-Match or Expires.
After reading some posts, I have added the "app.disable('etag')" statement within /config/express.js file in the customMiddleware function and etag and if-none-match are not sent anymore.
Anyway, now I do not see any request made to the server (just the first one is made) when accessing different pages (not even 304 response).
How can I tell sails.js to stop caching my pages?

A:

You can add this to a policy instead and then just apply it to the specific pages/requests you want - if you do not want to set this for the entire application.
/api/policies/nocache.js:
/**
 * Sets no-cache header in response.
 */
module.exports = function (req, res, next) {
    sails.log.info("Applying disable cache policy");
    res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
    res.header('Expires', '-1');
    res.header('Pragma', 'no-cache');
    next();
};


Q:

How to show only date part dynamically in gridview while using auto generated coloumn true

I want to show only the data part in grid view. I'm using auto generated column is true. So I have to write code in rowdatabound. While writing code its giving error saying;

The best overloaded method match for
  'string.ToString(System.IFormatProvider)' has some invalid arguments

if (e.Row.RowType == DataControlRowType.DataRow)
{
    e.Row.Cells[3].Text = e.Row.Cells[3].Text.ToString("MM/dd/yyyy);
}

A:

Since Text property returns string, not a DateTime, you need to parse it to DateTime first then you can get it's string representation as MM/dd/yyyy format.
I assume your e.Row.Cells[3].Text value is a standard date and time format of your CurrentCulture, you can use it as;
e.Row.Cells[3].Text = DateTime.Parse(e.Row.Cells[3].Text)
                              .ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);


Q:

Pickle: Reading a dictionary, EOFError

I recently found out about pickle, which is amazing. But it errors on me when used for my actual script, testing it with a one item dictionary it worked fine. My real script is thousands of lines of code storing various objects within maya into it. I do not know if it has anything to do with the size, I have read around a lot of threads here but none are specific to my error.
I have tried writing with all priorities. No luck.
This is my output code:
    output = open('locatorsDump.pkl', 'wb')
    pickle.dump(l.locators, output, -1)
    output.close()

This is my read code:
    jntdump = open('locatorsDump.pkl', 'rb')
    test = pickle.load(jntdump)
    jntdump.close()

This is the error:
    # Error: Error in  maya.utils._guiExceptHook:
    #   File "C:\Program Files\Autodesk\Maya2011\Python\lib\site-packages\pymel-1.0.0-py2.6.egg\maya\utils.py", line 277, in formatGuiException
    #     exceptionMsg = excLines[-1].split(':',1)[1].strip()
    # IndexError: list index out of range
    #
    # Original exception was:
    # Traceback (most recent call last):
    #   File "<maya console>", line 3, in <module>
    #   File "C:\Program Files\Autodesk\Maya2011\bin\python26.zip\pickle.py", line 1370, in load
    #     return Unpickler(file).load()
    #   File "C:\Program Files\Autodesk\Maya2011\bin\python26.zip\pickle.py", line 858, in load
    #     dispatch[key](self)
    #   File "C:\Program Files\Autodesk\Maya2011\bin\python26.zip\pickle.py", line 880, in load_eof
    #     raise EOFError
    # EOFError #

A:

Try using pickle.dumps() and pickle.loads() as a test.
If you don't recieve the same error, you know it is related to the file write.


Q:

Adding to a double-nested array in MongoDB

I have a double nested array in my MongoDB schema and I'm trying to add an entirely new array element to a second-level nested array using $push. I'm getting the error cannot use the part (...) to traverse the element
A documents have the following structure
{
    "_id" : ObjectId("5d8e37eb46c064790a28a467"),
    "org-name" : "Manchester University NHS Foundation Trust",
    "domain" : "mft.nhs.uk",
    "subdomains" : [ {
            "name" : "careers.mft.nhs.uk",
            "firstSeen" : "2017-10-06 11:32:00",
            "history" : [
                {
                    "a_rr" : "80.244.185.184",
                    "timestamp" : ISODate("2019-09-27T17:24:57.148Z"),
                    "asn" : 61323,
                    "asn_org" : "Ukfast.net Limited",
                    "city" : null,
                    "country" : "United Kingdom",
                    "shodan" : {
                        "ports" : [
                            {
                                "port" : 443,
                                "versions" : [
                                    "TLSv1",
                                    "-SSLv2",
                                    "-SSLv3",
                                    "TLSv1.1",
                                    "TLSv1.2",
                                    "-TLSv1.3"
                                ],
                                "cpe" : "cpe:/a:apache:http_server:2.4.18",
                                "product" : "Apache httpd"

                                    }
                                  ],
                                    "timestamp" : ISODate("2019-09-27T17:24:58.538Z")
                                }
                            }
                        ]

                    }
                ]
}

What I'm attempting to do is refresh the details held in the history array and add another entire array entry to represent the most recently collected data for the subdomain.name
The net result is that I will have multiple entries in the history array, each one timestamped the the date that the data was refreshed. That way I have a historical record of changes to any of the data held.
I've read that I can't use $push on a double-nested array but the other advice about using arrayfilters all appear to be related to updating an entry in an array rather than simply appending an entirely new document - unless I'm missing something!
I'm using PyMongo and would simply like to build a new dictionary containing all of the data elements and simply append it to the history.
Thanks!

A:

Straightforward in pymongo:
record = db.mycollection.find_one()
record['subdomains'][0]['history'].append({'another': 'record'})
db.mycollection.replace_one({'_id': record['_id']}, record)


Q:

Function that returns $0$ for all negative values

I need a function that returns $0$ if the given number is negative, and otherwise doesn't change the number.
Example:
$$y(-5)=0,\ y(-2)=0,\ y(0)=0,\ y(3)=3,\ y(2566)=2566.$$
Does such a function exist?

A:

You can take for example
$$\frac{|x|+x}{2}.$$
You can construct similar examples.


Q:

get subset of table based on unique column values

H- I am looking for a bash/awk/sed solution to get subsets of a table based on unique column values. For example if I have:
chrom1 333
chrom1 343
chrom2 380
chrom2 501
chrom1 342
chrom3 102

I want to be able to split this table into 3:
chrom1 333
chrom1 343
chrom1 342

chrom2 380
chrom2 501

chrom3 102

I know how to do this in R using the split command, but I am specifically looking for a bash/awk/sed solution.
Thanks

A:

alternative awk which keeps the original order of records within each block
$ awk '{a[$1]=a[$1]?a[$1] ORS $0:$0}
    END{for(k in a) print a[k] ORS ORS}' file

generates
chrom1 333
chrom1 343
chrom1 342

chrom2 380
chrom2 501

chrom3 102

there are 2 trailing empty lines at the end but not displayed in the formatted output.


Q:

Proper Unit Testing in void method in java without mocking

Is there a way to test a method that doesn't return any value without the use of Mockito?
This is the sample method. I'm new in Unit Testing.
public void addMantisData(ArrayList<Mantis> list) {

    try {
        connection = dataSource.getConnection();

        PreparedStatement preparedStatement = connection
                .prepareStatement("INSERT INTO mantises (ticketId, startDate, endDate, hours, minutes, " +
                        "employeeId, timeIn, timeOut, dateSubmitted, category, status, timestamp) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())");

        for (Mantis mantis : list) {
            preparedStatement.setInt(1, mantis.getTicketId());
            preparedStatement.setString(2, mantis.getStartDate());
            preparedStatement.setString(3, mantis.getEndDate());
            preparedStatement.setInt(4, mantis.getHours());
            preparedStatement.setInt(5, mantis.getMinutes());
            preparedStatement.setInt(6, mantis.getEmployeeId());
            preparedStatement.setString(7, mantis.getStartTime());
            preparedStatement.setString(8, mantis.getEndTime());
            preparedStatement.setString(9, mantis.getDateSubmitted());
            preparedStatement.setString(10, mantis.getCategory());
            preparedStatement.setString(11, mantis.getStatus());
            preparedStatement.addBatch();
        }

        preparedStatement.executeBatch();
        preparedStatement.close();

    } catch (SQLException e) {

    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {

            }
        }
    }
}

I apprieciate the help :)

A:

This seems more like it would be an integration test with the DB then a pure unit test. Also im not sure what it is that you want to test, if it is the query you will need to perform the insert and then try to get expected values from the DB and assert with expected result. Make sure that you use your own instance of the DB and also rollback after the test. DBUnit will make this happen for you, check it out


Q:

normal distribution in R

I want to use rnorm(n, mean, sd).  I can understand that the n is the number of observations with mean and sd. But please tell me in simple words what the results after running rnorm(n,mean,sd) are!
Thanks

A:

Let's say you run the following:

rnorm(5, 10, 2)

What you get is the value of five points randomly drawn from a normal distribution that has a mean of 10 and a standard deviation of 2.
It's a random draw, so each time you rerun this line you will get a different set of numbers. They all belong to the same normal distribution, though.
(If you want to obtain the same set of values every time you run the command you need to use set.seed() first)


Q:

Getting "Index was out of range" exception in DataGridView when clicking header

I am using a DataGridView to display my data from a SQLite database. One column is a directory to open pdfs assigned to the row. The code works but, every time I click on the column title, it gives me the error:

Index was out of range. Must be non-negative and less than the size of the collection.

Actually, any time I click the column text (just "PDF", or any other column's text) it throws that error. But when I click outside the text (anywhere in the ordering box), it reorders my columns, which is ok. Any ideas?
The code works, opens up the PDF, but I don't want the user accidentally clicking the title text and the program crash. Here is the code for the datagridview to open the pdf.
  private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
    {
        string filename = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
        if (e.ColumnIndex == 3 && File.Exists(filename))
        {
            Process.Start(filename);
        }
   }

A:

You're getting the exception when you click the header because the RowIndex is -1. You don't want anything to happen when they click the header anyway, so you can check for that value and ignore it.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex == -1 || e.ColumnIndex != 3)  // ignore header row and any column
        return;                                  //  that doesn't have a file name

    var filename = dataGridView1.CurrentCell.Value.ToString();

    if (File.Exists(filename))
        Process.Start(filename);
}

Also, FWIW, you're only getting the exception when you click the text in the header because you subscribed to CellContentClick (only fires when you click the content of the cell, such as the text). I'd suggest using the CellClick event (fires when any part of the cell is clicked).


Q:

What factors cause data not to go through in sockets in python (or python/node.js)?

(Suggestions for rephrasing questions?)
I'm sending data over a socket with client/server pattern. When
I run python (in pycharms) the output on the receiving end doesn't get data.
However, when I use the re-rerun icon  (in pycharms) the data goes through.
I'm confused to be honest by this behavior and not sure what to ask besides telling you what I observe.
Here is the client code. It's talking to server setup with net (node.js)
client.py
import socket   // python version 2.7.*

if __name__ == "__main__":
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect(('127.0.0.1', 7000))
    client.sendall("test data to transmit")

    data = client.recv(50)
    client.close()

    print 'Received', repr(data)

server.js
var net = require('net');  // node v0.10.21 (latest)
var PYTHON = {HOST :'127.0.0.1', PORT :7000};

net.createServer(function(socket) {
    console.log('CONNECTION for Python: ' + socket.remoteAddress +':'+ socket.remotePort);

    var body = '';
    socket.on('data', function(data) {
        console.log('DATA ' + socket.remoteAddress );
        body += data;
    });

    socket.on('close', function(err) {
        console.log('finish transmitting data... ');
        console.log(body);
    });
}).listen(PYTHON.PORT, PYTHON.HOST, function() {
    console.log('---->socket to talk to python '
                    + PYTHON.HOST + ':' + PYTHON.PORT);
});

update: added server.js code

A:

I have a guess at the problem.
The client calls sendall to send some data, then calls recv to get a response.
But the server doesn't have any code anywhere to send a response. So the client will wait forever. That means it will never close the socket. Which means the socket.on('close') callback in your server will never get called. Since that's where you're printing the body, nothing will get printed out.
When you hit the "re-run" icon, presumably it kills the previous instance of your client before launching a new one. Killing the client will cause the socket to close, so the server will finally get to the console.log(body) call.
And it's definitely possible (but certainly not guaranteed) for the new client to start up and connect before the server can finish processing the old client's close, so these could show up in either order.

So, how can you fix this?
Well, I'm not sure whether the problem is that the client is trying to receive data when it shouldn't be expecting any, or that the server isn't sending data when it should be.
If it's the former, just remove the data = client.recv(50) line (and the print at the end) from the client.
If it's the latter, add a line somewhere in the server that sends something back to the client. You most likely want it in the on('data') callback, but that's not the only place it could conceivably make sense (for example, you could do it right at the top of the socket connection callback). And you might as well add a callback so you can see it succeeding. So:
socket.on('data', function(data) {
    // your existing code here
    socket.write("Here's a response", function() {
        console.log('finish writing...');
    });
});


Q:

TypeError: 'str' object cannot be interpreted as an integer (Python)

I have a problem in the following code what happens is that insert, in an array a string and an integer, at the time of starting the code I get the following error.
TypeError: 'str' object cannot be interpreted as an integer

This error comes from the line where the IF and insert the two variables in the array noRepetidos, how can I fix this? Thank you very much to everyone who can help me.
for i in range (len(carta3)):
 for j in range (len(carta3[i])):
  counted = Counter([item for sublist in carta3 for item in sublist])
  a = counted.get(carta3[i][j])
   if(carta[i][j] in noRepetidos) == False:
    repetidos.insert(carta[i][j], int(a))

A:

Ola
It looks like you want to show the duplicate in a nested list? Try this:
counter = Counter([
    item for sublist in carta3 for item in sublist
])
repetidos = [
    (key, count) for key, count in counter.most_common()
    if count > 1
]

Chio


Q:

Error launching activity from fragment android

I have MainActivity with 3 fragments in it, and I want to switch to Activity2 from one of the fragments in MainActivity but my attempts always fail. My app crashes when I press the button ok in the third fragment to connect me to Activity2. I am working on a code I found in one tutorial. Thank you in advance!
public class ProfileFragment extends Fragment implements View.OnClickListener {

    private TextView tv_name,tv_email,tv_message;
    private SharedPreferences pref;
    private AppCompatButton btn_change_password,btn_logout, btn_ok;
    private EditText et_old_password,et_new_password;
    private AlertDialog dialog;
    private ProgressBar progress;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_profile,container,false);
        initViews(view);
        return view;

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

        pref = getActivity().getPreferences(0);
        tv_name.setText("Здравей, "+pref.getString(Constants.NAME,"")+"!");
        tv_email.setText(pref.getString(Constants.EMAIL,""));
        btn_ok=(AppCompatButton)view.findViewById(R.id.btn_ok);
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(),activity2.class);
                startActivity(intent);
            }
        });

    }

    private void initViews(View view){

        tv_name = (TextView)view.findViewById(R.id.tv_name);
        tv_email = (TextView)view.findViewById(R.id.tv_email);
        btn_change_password = (AppCompatButton)view.findViewById(R.id.btn_chg_password);
        btn_logout = (AppCompatButton)view.findViewById(R.id.btn_logout);
        btn_ok=(AppCompatButton)view.findViewById(R.id.btn_ok);
        btn_change_password.setOnClickListener(this);
        btn_logout.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.btn_chg_password:
                showDialog(); //I deleted this method from the code, it doesnt have a lot in common with my question
                break;
            case R.id.btn_logout:
                logout();
                break;
}

    private void logout() {
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean(Constants.IS_LOGGED_IN,false);
        editor.putString(Constants.EMAIL,"");
        editor.putString(Constants.NAME,"");
        editor.putString(Constants.UNIQUE_ID,"");
        editor.apply();
        goToLogin();
    }

    private void goToLogin(){

        Fragment login = new LoginFragment();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.fragment_frame,login);
        ft.commit();
    }

}

A:

add getActivity as context before startActivity()
also add NewTask Flag.
Intent intent = new Intent(getActivity(), activity2.class);
intent.addflags(intent.flag_activity_new_task);
getActivity().startActivity(intent);


Q:

Geometric Transformations on the CPU vs GPU

I've noticed that many 3d programs normally do vector/matrix calculations as well as geometric transformations on the CPU. Has anyone found an advantage in moving these calculations into vertex shaders on the GPU?

A:

Generally speaking:
Mesh transformations are done on the GPU. You send in the transformation matrix to the GPU and the shader applies it to all verticies of the mesh.
Using the GPU to calculate the Matrix itself is a different matter & is actually slower on the GPU because there are so many stored values that change from frame to frame that are necessary to help determine the final transformation matrix. Sending this data to & from CPU - GPU is slow. Also, on the CPU, the calculations are done once, whereas on the GPU, they would be done for each vertex.

A:

Many geometric transformations can be done on non-GPU processors, however one must consider the  target platform.  Your mileage will vary based on what platform you're targeting, and the bottlenecks of that platform.
One consideration is bus bandwidth between the device that is generating the geometry, and the device that is rendering the geometry.
In a typical modern PC system, the CPU is on one side of the PCIe bus (http://en.wikipedia.org/wiki/PCI_Express), and the GPU is on the other. The only way you can transfer per-frame generated data from CPU to GPU (and vice-versa) is via this bus. This means, you can be limited by the transfer speed of this bus. If your target platform has PCIe 2.x with 16 lanes, you have 8GB/s bandwidth. In practice, transfers across the PCIe are not 100% efficient, as some of the bandwidth is consumed for the protocol during your transfers. Depending on the size of your transfers, you could lose 5-10% of your bandwidth just on the per-packet overhead.
eg. Given a PC platform that is running PCIe 2.x with 16 lanes, how much data can you generate per frame for feeding to the GPU? Assuming you want the run at 60fps, this translates to 8GB/60 = 136MB per frame for PCIe 2.x. Multiplying by some (guestimated) 90% factor to account for driver communication overhead and PCIe transfer protocol overhead, you can generate around 120Mb data per frame without being limited by PCIe 2.x bandwidth.
Another question you have to answer: will the generation of this 120Mb of data be easily achievable in 1/60th of a second on your target CPU? Remembering that you have to perform a number of other game tasks on your CPU, you can run into a lack of time to generate the transformed data. In terms of just pure ALU throughput, this can limit you on CPU. In terms of CPU to sysmem buses, you can also be limited by the bandwidth (which varies, but is around ~8.5GB/s on recent CPUs).
Alright, so what factors makes it more viable to do on a GPU then? One factor is GPU memory bandwidth, which is the bandwidth between the GPU and it's local video memory. On contemporary mid-range GPUs this video memory bandwidth can be as high as 200GB/s (yes, that's 25x the PCIe 2.x bandwidth). Another factor is that the GPU is massively parallel, has hundreds of ALUs and is able to hide memory access latency by running thousands of threads at a time.
All of these factors can contribute to the obvious win of pushing more work onto the GPU, but again YMMV depending on your target platform.


Q:

Increasing speed of crop, mask, & extract raster by many polygons in R?

I'm extracting the area and percent cover of different land use types from a raster  based on several thousand polygon boundaries. I've found that the extract function works much faster if I iterate through each individual polygon and crop then mask the raster down to the size of the particular polygon. Nonetheless, it's pretty slow, and I'm wondering if anyone has any suggestions for improving the efficiency and speed of my code.
The only thing I've found related to this is this response by Roger Bivand who suggested using GDAL.open() and GDAL.close() as well as getRasterTable() and getRasterData(). I looked into those, but have had trouble with gdal in the past and don't know it well enough to know how to implement it.
Reproducible Example:
library(maptools)  ## For wrld_simpl
library(raster)

## Example SpatialPolygonsDataFrame
data(wrld_simpl) #polygon of world countries
bound <- wrld_simpl[1:25,] #name it this to subset to 25 countries and because my loop is set up with that variable

## Example RasterLayer
c <- raster(nrow=2e3, ncol=2e3, crs=proj4string(wrld_simpl), xmn=-180, xmx=180, ymn=-90, ymx=90)
c[] <- 1:length(c)

#plot, so you can see it
plot(c)
plot(bound, add=TRUE)

Fastest Method so far
result <- data.frame() #empty result dataframe

system.time(
     for (i in 1:nrow(bound)) { #this is the number of polygons to iterate through
      single <- bound[i,] #selects a single polygon
      clip1 <- crop(c, extent(single)) #crops the raster to the extent of the polygon, I do this first because it speeds the mask up
      clip2 <- mask(clip1,single) #crops the raster to the polygon boundary

      ext<-extract(clip2,single) #extracts data from the raster based on the polygon bound
      tab<-lapply(ext,table) #makes a table of the extract output
      s<-sum(tab[[1]])  #sums the table for percentage calculation
      mat<- as.data.frame(tab)
      mat2<- as.data.frame(tab[[1]]/s) #calculates percent
      final<-cbind(single@data$NAME,mat,mat2$Freq) #combines into single dataframe
      result<-rbind(final,result)
      })

   user  system elapsed
  39.39    0.11   39.52

Parallel Processing
Parallel processing cut the user time by half, but negated the benefit by doubling the system time.  Raster uses this for the extract function, but unfortunately not for the crop or mask function.  Unfortunately, this leaves a slighly larger amount of total elapsed time due to "waiting around" by the "IO."
beginCluster( detectCores() -1) #use all but one core

run code on multiple cores:
  user  system elapsed
  23.31    0.68   42.01

then end the cluster
endCluster()

Slow Method:
The alternative method of doing an extract directly from the raster function takes a lot lot longer, and I'm not sure about the data management to get it into the form I want:
system.time(ext<-extract(c,bound))
   user  system elapsed
1170.64   14.41 1186.14

A:

I have finally gotten around to improving this function. I found that for my purposes, it was fastest to rasterize() the polygon first and use getValues() instead of extract(). The rasterizing isn't much faster than the original code for tabulating raster values in small polygons, but it shines when it came to large polygon areas that had large rasters to be cropped and the values extracted.  I also found getValues() was much faster than the extract() function.
I also figured out the multi-core processing using foreach().
I hope this is useful for other people who want an R solution for extracting raster values from a polygon overlay. This is similar to the "Tabulate Intersection" of ArcGIS, which did not work well for me, returning empty outputs after hours of processing, like this user.
#initiate multicore cluster and load packages
library(foreach)
library(doParallel)
library(tcltk)
library(sp)
library(raster)

cores<- 7
cl <- makeCluster(cores, output="") #output should make it spit errors
registerDoParallel(cl)

Here's the function:
multicore.tabulate.intersect<- function(cores, polygonlist, rasterlayer){
  foreach(i=1:cores, .packages= c("raster","tcltk","foreach"), .combine = rbind) %dopar% {

    mypb <- tkProgressBar(title = "R progress bar", label = "", min = 0, max = length(polygonlist[[i]]), initial = 0, width = 300)

    foreach(j = 1:length(polygonlist[[i]]), .combine = rbind) %do% {
      final<-data.frame()
      tryCatch({ #not sure if this is necessary now that I'm using foreach, but it is useful for loops.

        single <- polygonlist[[i]][j,] #pull out individual polygon to be tabulated

        dir.create (file.path("c:/rtemp",i,j,single@data$OWNER), showWarnings = FALSE) #creates unique filepath for temp directory
        rasterOptions(tmpdir=file.path("c:/rtemp",i,j, single@data$OWNER))  #sets temp directory - this is important b/c it can fill up a hard drive if you're doing a lot of polygons

        clip1 <- crop(rasterlayer, extent(single)) #crop to extent of polygon
        clip2 <- rasterize(single, clip1, mask=TRUE) #crops to polygon edge & converts to raster
        ext <- getValues(clip2) #much faster than extract
        tab<-table(ext) #tabulates the values of the raster in the polygon

        mat<- as.data.frame(tab)
        final<-cbind(single@data$OWNER,mat) #combines it with the name of the polygon
        unlink(file.path("c:/rtemp",i,j,single@data$OWNER), recursive = TRUE,force = TRUE) #delete temporary files
        setTkProgressBar(mypb, j, title = "number complete", label = j)

      }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")}) #trycatch error so it doesn't kill the loop

      return(final)
    }
    #close(mypb) #not sure why but closing the pb while operating causes it to return an empty final dataset... dunno why.
  }
}

So to use it, adjust the single@data$OWNER to fit with the column name of your identifying polygon (guess that could have been built into the function...) and put in:
myoutput <- multicore.tabulate.intersect(cores, polygonlist, rasterlayer)

A:

Speed up extracting raster (raster stack) from point, XY or Polygon
Great answer Luke. You must be a R wizard! Here is a very minor tweak to simplify your code (may improve performance slightly in some cases). You can avoid some operations by using cellFromPolygon (or cellFromXY for points) and then clip and getValues.
Extract polygon or points data from raster stacks ------------------------
 library(raster)
 library(sp)

  # create polygon for extraction
  xys= c(76.27797,28.39791,
        76.30543,28.39761,
        76.30548,28.40236,
        76.27668,28.40489)
  pt <- matrix(xys, ncol=2, byrow=TRUE)
  pt <- SpatialPolygons(list(Polygons(list(Polygon(pt)), ID="a")));
  proj4string(pt) <-"+proj=longlat +datum=WGS84 +ellps=WGS84"
  pt <- spTransform(pt, CRS("+proj=sinu +a=6371007.181 +b=6371007.181 +units=m"))
  ## Create a matrix with random data & use image()
  xy <- matrix(rnorm(4448*4448),4448,4448)
  plot(xy)

  # Turn the matrix into a raster
  NDVI_stack_h24v06 <- raster(xy)
  # Give it lat/lon coords for 36-37°E, 3-2°S
  extent(NDVI_stack_h24v06) <- c(6671703,7783703,2223852,3335852)
  # ... and assign a projection
  projection(NDVI_stack_h24v06) <- CRS("+proj=sinu +a=6371007.181 +b=6371007.181 +units=m")
  plot(NDVI_stack_h24v06)
  # create a stack of the same raster
  NDVI_stack_h24v06 = stack( mget( rep( "NDVI_stack_h24v06" , 500 ) ) )

  # Run functions on list of points
  registerDoParallel(16)
  ptm <- proc.time()
  # grab cell number
  cell = cellFromPolygon(NDVI_stack_h24v06, pt, weights=FALSE)
  # create a raster with only those cells
  r = rasterFromCells(NDVI_stack_h24v06, cell[[1]],values=F)
  result = foreach(i = 1:dim(NDVI_stack_h24v06)[3],.packages='raster',.combine=rbind,.inorder=T) %dopar% {
     #get value and store
     getValues(crop(NDVI_stack_h24v06[[i]],r))
  }
  proc.time() - ptm
  endCluster()

user  system elapsed
   16.682   2.610   2.530

  registerDoParallel(16)
  ptm <- proc.time()
  result = foreach(i = 1:dim(NDVI_stack_h24v06)[3],.packages='raster',.inorder=T,.combine=rbind) %dopar% {
        clip1 <- crop(NDVI_stack_h24v06[[i]], extent(pt)) #crop to extent of polygon
        clip2 <- rasterize(pt, clip1, mask=TRUE) #crops to polygon edge & converts to raster
         getValues(clip2) #much faster than extract
  }
  proc.time() - ptm
  endCluster()

user  system elapsed
   33.038   3.511   3.288


Q:

How do I sanitize form input in Silverstripe CMS

Can someone please direct me to an example of how to sanitise a field from a dataObject BEFORE it gets dumped in the DB?
I don't know where to look.
I want to clean some user input before it is saved to the SiteConfig.

A:

You can customize saving-behaviour of a Page, DataObject or SiteConfig using the onBeforeWrite function.
The function is triggered when calling write() to save the object to the database. This includes saving a page in the CMS or altering a ModelAdmin record.
Here is an example for Silverstripe 3.1 of using onBeforeWrite on a SiteConfig Extension to strip characters and convert characters to lowercase on a text field:
SiteConfig onBeforeWrite example
class CustomSiteConfig extends DataExtension {

    private static $db = array(
        'TwitterUsername' => 'Text'
    );

    public function updateCMSFields(FieldList $fields) {
        $fields->addFieldToTab('Root.Main', new TextField('TwitterUsername', 'Twitter Username'));
    }

    public function onBeforeWrite() {
        $this->owner->TwitterUsername = strtolower($this->owner->TwitterUsername);
        $this->owner->TwitterUsername = preg_replace("/[^a-z0-9]/", '', $this->owner->TwitterUsername);

        parent::onBeforeWrite();
    }
}

Note: For the above example to work the CustomSiteConfig extensions needs to be applied to the SiteConfig. This can either be done through a yml config file or through a _config.php file.
mysite/_config/config.yml
---
Name: site
After: 'framework/*','cms/*'
---

# ...

# SiteConfig Extension

SiteConfig:
  extensions:
    - CustomSiteConfig

# ...

Or
mysite/_config.php
global $project;
$project = 'mysite';

// ...

SiteConfig::add_extension('CustomSiteConfig');

// ...


Q:

How would I serialize XML in a JSON string?

How would I serialize XML into a valid value in a JSON string?
Let's say I have this XML:
<root>
   <item label="hello's there" />
</root>

And I want to pass it with a JSON object / string like so:
var myJSON = "{'name':'dork','value':" + xml + "'}";

A:

Instead of using a string create an object and pass it to the JSON.stringify method like so:
var object = {};
object.name = "dork";
object.xml = myXML;
var result = JSON.stringify(object);


Q:

Plugin to manually retry a parametrized build

I have the following scenario:

A parameterized Jenkins project that is triggered by another project.
The build fails because of an external resources such as disk space.

What I want is this, if the build fails, then I can fix the external problem, and simply click a button to repeat the build again with the same parameters.
Does this plugin exists? I haven't found one yet.

A:

The Rebuild plugin is what you want


Q:

Joomla 2.5 component update

I have built a custom component in joomla 2.5 and want to know how to issue updates either in the form on an upload package installed via the joomla 2.5 installer or a trigger in main component upload package if already installed to upgrade to the new version im uploading. I assume i will also need to create version numbers in my component. Obviously if upgrading i only want the files i want to change replacing.

A:

You have 2 choices. You can either use the Joomla Update Manager method, or use Akeeba Release System. Personally, I would use the Akeeba extension as it's much more flexible and contain many more features. For new versions, you will need to zip up the full extension, not just the files you have edited.


Q:

When to use enumerateObjectsUsingBlock vs. for

Besides the obvious differences:

Use enumerateObjectsUsingBlock when you need both the index and the object
Don't use enumerateObjectsUsingBlock when you need to modify local variables (I was wrong about this, see bbum's answer)

Is enumerateObjectsUsingBlock generally considered better or worse when for (id obj in myArray) would also work? What are the advantages/disadvantages (for example is it more or less performant)?

A:

Ultimately, use whichever pattern you want to use and comes more naturally in the context.
While for(... in ...) is quite convenient and syntactically brief, enumerateObjectsUsingBlock: has a number of features that may or may not prove interesting:

enumerateObjectsUsingBlock: will be as fast or faster than fast enumeration (for(... in ...) uses the NSFastEnumeration support to implement enumeration).  Fast enumeration requires translation from an internal representation to the representation for fast enumeration.  There is overhead therein.  Block-based enumeration allows the collection class to enumerate contents as quickly as the fastest traversal of the native storage format.  Likely irrelevant for arrays, but it can be a huge difference for dictionaries.
"Don't use enumerateObjectsUsingBlock when you need to modify local variables" - not true; you can declare your locals as __block and they'll be writable in the block.
enumerateObjectsWithOptions:usingBlock: supports either concurrent or reverse enumeration.
with dictionaries, block based enumeration is the only way to retrieve the key and value simultaneously.

Personally, I use enumerateObjectsUsingBlock: more often than for (... in ...), but - again - personal choice.

A:

For simple enumeration, simply using fast enumeration (i.e. a for…in… loop) is the more idiomatic option. The block method might be marginally faster, but that doesn't matter much in most cases — few programs are CPU-bound, and even then it's rare that the loop itself rather than the computation inside will be a bottleneck.
A simple loop also reads more clearly. Here's the boilerplate of the two versions:
for (id x in y){
}

[y enumerateObjectsUsingBlock:^(id x, NSUInteger index, BOOL *stop){
}];

Even if you add a variable to track the index, the simple loop is easier to read.
So when you should use enumerateObjectsUsingBlock:? When you're storing a block to execute later or in multiple places. It's good for when you're actually using a block as a first-class function rather than an overwrought replacement for a loop body.

A:

Although this question is old, things have not changed, the accepted answer is incorrect.
The enumerateObjectsUsingBlock API was not meant to supersede for-in, but for a totally different use case:

It allows the application of arbitrary, non-local logic. i.e. you don’t need to know what the block does to use it on an array.
Concurrent enumeration for large collections or heavy computation (using the withOptions: parameter)

Fast Enumeration with for-in is still the idiomatic method of enumerating a collection.
Fast Enumeration benefits from brevity of code, readability and additional optimizations which make it unnaturally fast. Faster than a old C for-loop!
A quick test concludes that in the year 2014 on iOS 7, enumerateObjectsUsingBlock is consistently 700% slower than for-in (based on 1mm iterations of a 100 item array).
Is performance a real practical concern here?
Definitely not, with rare exception.
The point is to demonstrate that there is little benefit to using enumerateObjectsUsingBlock: over for-in without a really good reason. It doesn't make the code more readable... or faster... or thread-safe. (another common misconception).
The choice comes down to personal preference. For me, the idiomatic and readable option wins. In this case, that is Fast Enumeration using for-in.
Benchmark:
NSMutableArray *arr = [NSMutableArray array];
for (int i = 0; i < 100; i++) {
    arr[i] = [NSString stringWithFormat:@"%d", i];
}
int i;
__block NSUInteger length;

i = 1000 * 1000;
uint64_t a1 = mach_absolute_time();
while (--i > 0) {
    for (NSString *s in arr) {
        length = s.length;
    }
}
NSLog(@"For-in %llu", mach_absolute_time()-a1);

i = 1000 * 1000;
uint64_t b1 = mach_absolute_time();
while (--i > 0) {
    [arr enumerateObjectsUsingBlock:^(NSString *s, NSUInteger idx, BOOL *stop) {
        length = s.length;
    }];
}
NSLog(@"Enum %llu", mach_absolute_time()-b1);

Results:
2014-06-11 14:37:47.717 Test[57483:60b] For-in 1087754062
2014-06-11 14:37:55.492 Test[57483:60b] Enum   7775447746


Q:

How to show the continuity of the following operator?

Let $T:\mathcal{l}^2\to\mathcal{l}^2$ be an operator with
$$(Tu)_n:= \frac{1}{2}u_n + \frac{1}{n^2}u_n.$$
I've found that the operator is not compact, since
$$(Tu)_n = \left(\frac{1}{2}+\frac{1}{n^2}\right)u_n$$
consists a sequence which converges not to zero. But how can I show whether the operator is continuous?

A:

Suppose that $u^{(k)} \to u$ and $Tu^{(k)} \to v$ in $\ell^2$. Then, for all $n\geq 1$,
\begin{align}
|(Tu)_n-v_n|&\leq |(Tu)_n - (Tu^{(k)})_n|+|(Tu^{(k)})_n-v_n|\\
&= \left(\frac{1}{2}+\frac{1}{n^2}\right)|u_n-u^{(k)}_n|+|(Tu^{(k)})_n-v_n|\\
&\leq \frac{3}{2}\Vert u-u^{(k)} \Vert_2 + \Vert Tu^{(k)}-v \Vert_2 \to 0,
\end{align}
as $k\to\infty$,
so $(Tu)_n = v_n$ for each $n\geq 1$, and thus $Tu=v$.
The Closed graph theorem does the rest.
Alternatively, observe that
$$ \Vert Tu \Vert_2^2 = \sum_{n=1}^\infty \left(\frac{1}{2}+\frac{1}{n^2}\right)^2 |u_n|^2 \leq \frac{9}{4} \sum_{n=1}^\infty |u_n|^2 = \frac{9}{4} \Vert u\Vert_2^2. $$


Q:

How to write 'x is not converging to 0'?

To write 'x converges to 0' I can use x\to 0 that produces $x\to 0$. But how do I write "x does not converge to 0'? I need to strike out $\to$. Well cancel package will be ok.

A:

The cancel package provides some ways to strike through. See here for more, but basically \require{cancel} \cancel \to produces: $\require{cancel} \cancel \to$.
However \not \rightarrow produces something smaller, like: $\not \to$.


Q:

Why would IPv6 be necessary for the IoT?

I recently ran across this quote from Security Intelligence about the Internet of things and IPv6:

Analysts predict that there will be 30 billion connected “things” by 2020, yet the IPv4 address space only accommodates 4 billion and change. Even with network address translation (NAT) and private address space, the IoT’s appetite for addresses will overcome IPv4’s ability to sate it.
Enter IPv6, which expands the address space to 340 undecillion, or 3.4×1038. Well, it’s technically a bit less than that, since some combinations are reserved; nonetheless, that’s still enough usable addresses to allocate about 4,000 to every person on the planet.

What puzzles me is why the Internet of Things would make any difference to the need to switch to IPv6.  It seems to me that the vast majority of Things are connected to a router, hence a need only for one world-wide IP.
For instance, your smart oven's (or whatever) IP is 192.168.0.52, that doesn't prevent your neighbour's Echo from having the same IP, because in order to access that IP from outside your home, you have to go through your home's IP address, ex: 148.238.24.9.
Why would the advent of IoT necessitate the switch to IPv6?

A:

There are two reasons.
(1) First is simpler, end-to-end connectivity. If both source and destination have public IPv4 (or IPv6, of course) address, they can connect to each other in any direction anytime.
Your IoT with private IP 192.168.0.52 however can use NAT ONLY to connect to any public IP on the Internet whenever it wants, but the rest of the Internet cannot connect to it. There were kludges like DNAT and uPNP that used to allow you to specify that some incoming connections are enabled, but they are breaking more and more nowadays due to implementation of CGNAT because of IPv4 shortages.
A common (so-called) "solution" to this problem is that all your (NATed) devices connect to some central location with public IP (usually hosted by the manufacturer of device). This makes it work technically, but involves a privacy issue (you're giving all the data from your IoTs), security issue (as you're wide open to them, breach or disgruntled employee can do anything your IoT device can do and access), and reliability issue (when the manufacturer goes out of business or decides to stop supporting old devices or is suffering outages) all your (and everybody elses) perfectly functional devices will stop working.
(2) second problem is that it will stop working anyway (even for outgoing connections) some time in the future (not in a year or two, but still. The more IoT and services catch on, the sooner it will start breaking).
That is because NAT allows private addresses like 192.168.0.52 to reach the Internet at large. It does that by changing source address 192.168.0.52 to public IP of your router, but replaces source port with free one from the pool.
For example, your first connection might be 192.168.0.52:1000 might be (CG)-NATed to (public IP) 198.51.100.1:1000, and your neighbour 192.168.0.77:1000 might get NATed to 198.51.100.1:1001. Your second connection from 192.168.0.52:1001 would then be NATed to 198.51.100.1:1002 etc.
Problem is, even simple stuff like opening a web page will likely open dozens of connections and use a dozen of ports (for DNS queries, HTTP(S) connection for different elements, JS analytics on different sites etc).
More expensive programs, like torrent clients, will easily use up a thousands of ports. And there is only 65535 ports available for any IP.
Which means several of your neighbours sharing the same CGNAT IP use a bigger share of connections (and more IoTs will mean more connections), and suddenly all of 65535 ports on that public IP 198.51.100.1 are used. Which means no new connections can be established for you and your neighbours. Which on bigger scale means lots people are cut from their IoTs, and civilisation as we know it collapses :-)
Since we would like to delay this civilisation collapse as long as possible, we're transitioning to IPv6 instead. Please support continued existence of this civilisation by using IPv6 if possible. Thanks!

A:

IPv6 is a necessity now; we're nearly out of IPv4 addresses already. As more and more people come online, we're starting to reach the point where IPs have to be shared across multiple people, not just one household (carrier-grade NAT), which is unacceptable, and not just a problem for IoT.
IPv6 allows us to move to a more semantic representation where one IP = one device, which has several advantages. If you're directly able to address your smart device (be it a toaster, oven, light bulb, TV, or something else), you can just send your commands directly to the device, rather than needing to go through a hub. At the moment, NAT makes this difficult to set up, because it requires specifically port forwarding your IoT devices (and this may not work at all for carrier-grade NAT).
It might be worth reading 'Switching to IPv6 implies dropping NAT. Is that a good thing?' from Server Fault if you're worried about the security implication; having all your IoT devices given a public IPv6 address is not really a big security flaw; it's something that would still cause a problem on a NAT-enabled network.
This IEEE article has some good points:

The next logical step from networks of mobile devices to networks of communicating "Things" is IoT. That next step will mirror the sequence of events experienced by mobile networks. Proprietary protocols came first, because an individual company’s profits often come before consideration of the common good. But the use of IP and transparency (i.e., open source protocols) is fundamental to IoT development, just as the ease of use and the invisibility of the technology is important to end users. Our view, based on our research, is that the value of transparency and ease-of-use, and even more importantly the need for interoperability, will favor IPv6 adoption by the IoT market.

So, in short:

At the moment, 1 IPv4 address represents... who knows? Sometimes a device, sometimes a router, sometimes a whole network of different customers.
Using an IPv6 lets you give each IoT device a 'name' on the Internet.
Being able to address your devices lets you control them, and simplifies setup and management.


Q:

ExtJS 4: Ext.grid.Panel Ext.PagingToolbar buttons (first, previous, next, last) not passing start and limit parameters

I'm having difficulty getting the paging toolbar to work.  The initial load passes the "start" and "limit" parameter to the server proxy and the data loads fine in the grid.  However, when clicking the "Next" button in the paging toolbar, the "start" and "limit" parameters don't get passed properly and the web method bombs out because it expects those parameters.  If you can help me fix the root problem, that would be awesome.  But if not, and you're able to help me override the buttons, that's also fine, so I can put in a temporary fix until I figure out what's wrong.
Exception:
POST http://{domain}/Controls/ObjectList/ObjectListService.asmx/GetObjectList?_dc=1348591244365 500 (Internal Server Error) ext-all-debug.js:36837
Ext.define.request                  ext-all-debug.js:36837
Ext.define.doRequest                ext-all-debug.js:49508
Ext.define.read                     ext-all-debug.js:39082
Ext.define.load                     ext-all-debug.js:47668
Base.implement.callParent           ext-all-debug.js:3728
Ext.define.load                     ext-all-debug.js:50160
Ext.define.read                     ext-all-debug.js:47399
Ext.define.loadPage                 ext-all-debug.js:50404
Ext.define.nextPage                 ext-all-debug.js:50409
Ext.define.moveNext                 ext-all-debug.js:102105
Ext.define.fireHandler              ext-all-debug.js:79530
Ext.define.onClick                  ext-all-debug.js:79520
(anonymous function)
Ext.apply.createListenerWrap.wrap   ext-all-debug.js:9171

{"Message":"Invalid web service call, missing value for parameter: \u0027query\u0027.","StackTrace":"   at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n   at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

===========================
Source Code:
    var itemsPerPage = 30;

    var store = new Ext.data.Store({
        proxy: new Ext.ux.AspWebAjaxProxy({
            url: '/Controls/ObjectList/ObjectListService.asmx/GetObjectList',
            actionMethods: {
                create: 'POST',
                destroy: 'DELETE',
                read: 'POST',
                update: 'POST'
            },
            reader: {
                type: 'json',
                model: 'Object',
                totalProperty: 'd.recordCount',
                //idProperty: 'object_id',
                root: 'd.resultSet'
            },
            headers: {
                'Content-Type': 'application/json; charset=utf-8'
            }
        }),
        pageSize: itemsPerPage,
        noCache: false,
        //autoLoad: true
        autoLoad: {
            params: {
                //query: '',
                start: 0,
                limit: itemsPerPage
            }
        }
    });

...

    Ext.define('ObjectGrid', {
        extend: 'Ext.grid.Panel',

        initComponent: function () {
            var me = this;

            Ext.applyIf(me, {
                store: store,
                forceFit: true,
                autoScroll: true,
                height: 750,
                loadMask: true,

                columns: [
                ],

...

                bbar: Ext.create('Ext.PagingToolbar', {
                    store: store,
                    displayInfo: true,
                    pageSize: 30,
                    displayMsg: 'Displaying records {0} - {1} of {2}',
                    emptyMsg: 'No records to display'

//                    type: 'pagingmemory',

//                    listeners: {
//                        afterrender: {
//                            single: true,
//                            fn: function (thispaging) {
//                                thispaging.first.on('click', function () {
//                                    Ext.Msg.alert('first', 'first');
//                                });
//                                thispaging.prev.on('click', function () {
//                                    Ext.Msg.alert('prev', 'prev');
//                                });
//                                thispaging.next.on('click', function () {
//                                    Ext.Msg.alert('next', 'next');
//                                });
//                                thispaging.last.on('click', function () {
//                                    Ext.Msg.alert('last', 'last');
//                                });
//                            }
//                        }
//                    }

//                    listeners: {
//                        'click' : function(which) {
//                            alert('you have clicked');
//                        }
//                    }

                })  // bbar

            });

            me.callParent(arguments);
        }
    });

update #1:
The parameters are being lost between these two calls in the stack trace mentioned above.

line 39082:
this.requests.5.options.params = "{}"
(5) being the current object

line 47668:
operation.start = 2
operation.limit = 30

update #2:
Ext.define('Ext.ux.AspWebAjaxProxy', {
    extend: 'Ext.data.proxy.Ajax',
    require: 'Ext.data',
    //processData: false,

    buildRequest: function (operation) {
        var params = Ext.applyIf(operation.params || {}, this.extraParams || {}),
                                request;
        params = Ext.applyIf(params, this.getParams(params, operation));
        if (operation.id && !params.id) {
            params.id = operation.id;
        }

        params = Ext.JSON.encode(params);

        request = Ext.create('Ext.data.Request', {
            params: params,
            action: operation.action,
            records: operation.records,
            operation: operation,
            url: operation.url
        });
        request.url = this.buildUrl(request);
        operation.request = request;
        return request;
    }
});

A:

Well I guess the trick lies within
// Clone params right now so that they can be mutated at any point further down the call stack
var me = this,
    params = operation.params = Ext.apply({}, operation.params, me.extraParams),
    request;

I think you can use this to replace your first lines, I guess it should work then.
Please look also at
// Set up the entity id parameter according to the configured name.
// This defaults to "id". But TreeStore has a "nodeParam" configuration which
// specifies the id parameter name of the node being loaded.
if (operation.id !== undefined && params[me.idParam] === undefined) {
    params[me.idParam] = operation.id;
}

dunno if you need to update this too.


Q:

neo4j cypher shell - memory limit exception

I am a newbie to neo4j. I have done some research before posting this question here, but couldn't find a solution that works for me. I have a ubuntu VM with 32GB RAM. I am currently using neo4j-community edition 4.0.2 and got below configurations in my neo4j.conf file.
dbms.memory.heap.initial_size=5120m
dbms.memory.heap.max_size=5120m
dbms.memory.pagecache.size=10g

I am trying to import CSV files into the default neo4j database using the cypher-shell. It works well for small files, but I have a problem with csv files of size > 1GB. It fails with error below
Can't allocate 524288 bytes due to exceeding memory limit; used=2147266560, max=2147483648
I tried setting the JAVA_OPTS=-Xmx4G to increase the java heap space but it still fails with the same error. Can someone please assist? Is the cypher-shell utility looking for more heap space or is it the neo4j database looking for additional heap space? If cypher-shell cannot be used to import huge files what are other options for me to import huge files?
edit: I am using a periodic commit for every 200 rows and I have the below cypher script in a .cypher file and using the cypher-shell to run the .cypher file
CREATE INDEX ON:Review (nr);

USING PERIODIC COMMIT 200 LOAD CSV WITH HEADERS FROM "file:///12Review.csv" AS row
MERGE (R:Review {nr: toInteger(row.nr)})
WITH row, R
FOREACH(ignoreMe in CASE WHEN row.reviewDate IS NULL THEN [] ELSE [1] END| SET R.reviewDate =row.reviewDate)
FOREACH(ignoreMe in CASE WHEN row.title IS NULL THEN [] ELSE [1] END| SET R.title = row.title)
FOREACH(ignoreMe in CASE WHEN row.rating1 IS NULL THEN [] ELSE [1] END| SET R.rating1 =row.rating1)
FOREACH(ignoreMe in CASE WHEN row.rating2 IS NULL THEN [] ELSE [1] END| SET R.rating2 =row.rating2)
FOREACH(ignoreMe in CASE WHEN row.rating3 IS NULL THEN [] ELSE [1] END| SET R.rating3 =row.rating3)
FOREACH(ignoreMe in CASE WHEN row.rating4 IS NULL THEN [] ELSE [1] END| SET R.rating4 =row.rating4)

A:

I'm running an EXPLAIN on this and seeing an Eager operator in the query plan, that basically cancels out the periodic commit and causes all results to manifest in memory, that's leading to the out of heap operation. The FOREACHes here are causing this, you won't be able to use this approach when using periodic commit.
Instead try using coalesce() to use the first non-null value, first attempting to get the row value, then use the node value:
USING PERIODIC COMMIT 10000 LOAD CSV WITH HEADERS FROM "file:///12Review.csv" AS row
MERGE (R:Review {nr: toInteger(row.nr)})
SET R.reviewDate = coalesce(row.reviewDate, R.reviewDate),
R.reviewDate = coalesce(row.title, R.title),
R.reviewDate = coalesce(row.rating1, R.rating1),
R.reviewDate = coalesce(row.rating2, R.rating2),
R.reviewDate = coalesce(row.rating3, R.rating3),
R.reviewDate = coalesce(row.rating4, R.rating4)