How to avoid blocking EDT with JPA lazy loading in Swing desktop apps

Question: I'm struggling with real-world use of JPA (Hibernate, EclipseLink, etc) in a Swing desktop application.
JPA seems like a great idea, but relies on lazy loading for efficiency. Lazy loading requires the entity manager exist for the lifetime of the entity beans, and offers no control over what thread is used for loading or any way to do the loading in the background while the EDT gets on with other things. Accessing a property that happens to be lazily loaded on the EDT will block your app's UI on database access, without even the opportunity to set a busy cursor. If the app is running on wifi/3G or slow Internet that can make it look like it has crashed.
To avoid lazy loading stalling the EDT I have to work with detached entities. Then, if I actually need the value of a lazy property all my components (even those that should supposedly be able to be unaware of the database) have to be prepared to handle lazy loading exceptions or use PersistenceUtil to test for property state. They have to dispatch entities back to the database worker thread to be merged and have properties loaded before being detached and returned again.
To make that efficient, my components need to know in advance what properties of a bean will be required.
So, you'll see all these shiny tutorials demonstrating how to whip up a simple CRUD app on the NetBeans Platform, Eclipse RCP, Swing App Framework, etc using JPA, but in reality the approaches demonstrated violate basic Swing practices (don't block the EDT) and are completely non-viable in the real world.
( More detail in write-up here: http://soapyfrogs.blogspot.com/2010/07/jpa-and-hibernateeclipselinkopenjpaetc.html )
There are some related questions with somewhat helpful responses, but none of them really cover the edt blocking / lazy loading / entity manager lifetime management issues together.
Lazy/Eager loading strategies in remoting cases (JPA)
How are others solving this? Am I barking the wrong tree by trying to use JPA in a desktop app? Or are there obvious solutions I'm missing? How are you avoiding blocking the EDT and keeping your app responsive while using JPA for transparent database access?
Answer: I've only used JPA with an embedded database, where latency on the EDT wasn't a problem. In a JDBC context, I've used SwingWorker to handle background processing with GUI notification. I haven't tried it with JPA, but here's a trivial JDBC example.
Addendum: Thanks to @Ash for mentioning this SwingWorkerbug. A workaround is to build from source has been submitted.
Comment: For reference, the version of SwingWorker in the Sun/Oracle JVMs from JDK6u17 on apparently has a pretty major bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6880336. There are some workarounds at the end of the comments.
Comment: Re the SwingWorker bug: I typically use the Executor system directly anyway, with a background database worker processing runnables with on-completion callbacks. But thanks for the tip.
Comment: Actually doing the background processing is fuss free. The trouble arises, with JPA, because you don't know *when* that background processing is required, as it's all "transparent" with lazy-loading via cglib proxies.

When you're working with database-aware code and explicit background loading it's easy to know when to call into your background database worker with a callback Runnable, use SwingWorker, or whatever.

With JPA, though, you don't know you need to load something until the UI is already blocked waiting for it to load.
Comment: @trashgod Thanks for the example man... I'm implementing a design using Spring Data for persistence and the controller architecture is a bit challenging with SwingWorkers...
Answer: I have encountered the same problem. My solution was to disable lazy loading and ensure that all entities are fully initialised before they are returned from the database layer. The implications of this is that you need to carefully design your entities so that they can be loaded in chunks. You have to limit the number of x-to-many associations, otherwise you end up retrieving half the database on every fetch.
I do not know if this is the best solution but it does work. JPA has been designed primarily for a request-response stateless app. It is still very useful in a stateful Swing app - it makes your program portable to multiple databases and saves a lot of boilerplate code. However, you have to be much more careful using it in that environment.
Comment: In that case the only thing I can suggest is that you build a layer on top of your entities that uses an asynchronous mechanism to gain access to lazy loaded properties. So you would have something like a get method but you would pass an interface to it that would get notified of the response when it is ready. If you only used this top layer from your GUI it would prevent EDT blocking and avoid your GUI from having to deal with the messy property state checking and merging process. However, it would probably require a significant rewrite of the GUI.
Comment: Unfortunately, not using some form of lazy loading isn't viable in this environment. I have 10,000 customers with hundreds of thousands of related entities in the database. Some entities necessarily have large or complex data associated with them that's required by some parts of the app, but not most.

This article covers some of the design issues with using JPA / Hibernate / etc in a 2-tier desktop app: http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/

... and concludes that, really, there just aren't any good solutions. I'm inclined to agree.
Answer: We wrap every significant operation into SwingWorkers which may trigger lazy-loading of single-objects or collections. This is annoying, but cannot be helped.
Answer: Sorry being late!
As any other swing developer, i guess we all came to this kind of problem when JPA is incorporated hoping to deal with all persistence aspects, by encapsulating all of that logic in single isolated tier, also promoting a more clean separation of concerns, believing that it is totally free...but the truth is that it's definitely not.
As you stated before there are a problem with detached entities that makes us create workarounds to solve this problem. The problem is not only working with lazy collections, there is a problem working with the entity itself, first at all, any changes that we do to our entity must be reflected to repository (and with a detached this is not going to happen). I am not an expert on this.. but i will try to highlight my thoughts on this and expose several solutions (many of them had been previously announced by other folks).
From the presentation tier (that is, the code where resides all the user interface and interactions , this includes the controllers) we access the repository tier to do simple CRUD operations, despite the particular repository and the particular presentation, i think this is a standard fact accepted by the community. [I guess this a notion written down very well by Robert Martin in one of DDD books]
So, basically one can wander "if my entity is detached, why I not leave it attached" doing so, it will stay synchronized with my repository an all changes done to the entity will be reflected "immediately" to my repository. And yes.... that is where a first answer appears to this problem..
1) Use a single entity manager object and keep it open from the start of the app to the end.

At a glance it seems very simple (and it is, just open an EntityManager and store its reference globally and access the same instance everywhere in the application)
Not recommended by the community as it not safe to keep an entity manager open for too long. The repository connection (hence session/entityManager) may drop due to various reasons.

So despise it's simple, it's not the best options.... so let's move to another solution provided by the JPA API.
2) Use eager loading of fields, so there is no need to be attached to the repository.

This works well, but if you want to add or remove to a collection of the entity, or modify some field value directly, this will not be reflected in the repository.. you will have to manually merge or update the entity by using some method. Therebefore, if you are working with multi tier app where from the presentation tier you must include an extra call to repository tier you are contaminating the code of the presentation tier to be attach to a concrete repository that works with JPA (what happens is the repository is just a collection of entities in memory? ... does a memory repository need an extra call to "update" a collection of an object... the answer is no, so this is good practice but it is done for the sake of make thing "finally" works)
Also you have to consider to what happens is the object graph retrieved is too big to be stored at the same time in memory, so it would probably fail. (Exactly as Craig commented)

Again.. this not resolve the problem.
3) Using the proxy design pattern, you could extract the Interface of the Entity (let's call it EntityInterface) and work in your presentation layer with those interfaces (supposing that you actually can force the client of your code to this). You can be cool and use dynamic proxy or static ones (really don't care) to create a ProxyEntity in the repository tier to return object that implement that interface. This object that return actually belongs to a class whose instance method are exactly the same (delegating the calls to the proxied object) except for those that works with collections that need to be "attached" to the repostory. That proxyEntity contains a reference to the proxied object (the entity itself) necessary to the CRUD operations on the repository.

This resolves the problem at the cost of forcing use Interfaces instead of plain domain classes. Not a bad think actually... but also i guess is neither and standard. I think we all want to use the domain classes. Also for every domain object we have to write an interface... what happens if the object came in .JAR... aha! touche! We cannon't extract an interface an runtime :S, and therebefore we cannot create proxys.

For the purposes of explain this better i write down an example of doing this...
On the domain tier (where the core business class resides)
@Entity
public class Bill implements Serializable, BillInterface
{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToMany(fetch=FetchType.LAZY, cascade = {CascadeType.ALL}, mappedBy="bill")
    private Collection<Item> items = new HashSet<Item> ();

    @Temporal(javax.persistence.TemporalType.DATE)
    private Date date;

    private String descrip;

    @Override
    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public void addItem (Item item)
    {
        item.setBill(this);
        this.items.add(item);
    }

    public Collection<Item> getItems()
    {
        return items;
    }

    public void setItems(Collection<Item> items)
    {
        this.items = items;
    }

    public String getDescrip()
    {
        return descrip;
    }

    public void setDescrip(String descrip)
    {
        this.descrip = descrip;
    }

    public Date getDate()
    {
        return date;
    }

    public void setDate(Date date)
    {
        this.date = date;
    }

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Bill))
        {
            return false;
        }
        Bill other = (Bill) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)))
        {
            return false;
        }
        return true;
    }

    @Override
    public String toString()
    {
        return "domain.model.Bill[ id=" + id + " ]";
    }

    public BigDecimal getTotalAmount () {
        BigDecimal total = new BigDecimal(0);
        for (Item item : items)
        {
            total = total.add(item.getAmount());
        }
        return total;
    }
}

Item is another entity object modelling an item of a Bill (a Bill can contains many Items, an Item belongs only to one and only one Bill).
The BillInterface is simply an interface declaring all Bill Methods.
On the persistence tier i place the BillProxy...
The BillProxy has this look :
class BillProxy implements BillInterface
{
    Bill bill; // protected so it can be used inside the BillRepository (take a look at the next class)

    public BillProxy(Bill bill)
    {
        this.bill = bill;
        this.setId(bill.getId());
        this.setDate(bill.getDate());
        this.setDescrip(bill.getDescrip());
        this.setItems(bill.getItems());
    }

    @Override
    public void addItem(Item item)
    {
        EntityManager em = null;
        try
        {
            em = PersistenceUtil.createEntityManager();
            this.bill = em.merge(this.bill); // attach the object
            this.bill.addItem(item);
        }
        finally
        {
            if (em != null)
            {
                em.close();
            }
        }
    }

    @Override
    public Collection<Item> getItems()
    {
        EntityManager em = null;
        try
        {
            em = PersistenceUtil.createEntityManager();
            this.bill = em.merge(this.bill); // attach the object
            return this.bill.getItems();
        }
        finally
        {
            if (em != null)
            {
                em.close();
            }
        }
    }

    public Long getId()
    {
        return bill.getId(); // delegated
    }

    // More setters and getters are just delegated.
}

Now let's take a look to the BillRepository (loosely based on a template given by NetBeans IDE)
public class DBBillRepository implements BillRepository
    {
        private EntityManagerFactory emf = null;
    public DBBillRepository(EntityManagerFactory emf)
    {
        this.emf = emf;
    }

    private EntityManager createEntityManager()
    {
        return emf.createEntityManager();
    }

    @Override
    public void create(BillInterface bill)
    {
        EntityManager em = null;
        try
        {
            em = createEntityManager();
            em.getTransaction().begin();
            bill = ensureReference (bill);
            em.persist(bill);
            em.getTransaction().commit();
        }
        finally
        {
            if (em != null)
            {
                em.close();
            }
        }
    }

    @Override
    public void update(BillInterface bill) throws NonexistentEntityException, Exception
    {
        EntityManager em = null;
        try
        {
            em = createEntityManager();
            em.getTransaction().begin();
            bill = ensureReference (bill);
            bill = em.merge(bill);
            em.getTransaction().commit();
        }
        catch (Exception ex)
        {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0)
            {
                Long id = bill.getId();
                if (find(id) == null)
                {
                    throw new NonexistentEntityException("The bill with id " + id + " no longer exists.");
                }
            }
            throw ex;
        }
        finally
        {
            if (em != null)
            {
                em.close();
            }
        }
    }

    @Override
    public void destroy(Long id) throws NonexistentEntityException
    {
        EntityManager em = null;
        try
        {
            em = createEntityManager();
            em.getTransaction().begin();
            Bill bill;
            try
            {
                bill = em.getReference(Bill.class, id);
                bill.getId();
            }
            catch (EntityNotFoundException enfe)
            {
                throw new NonexistentEntityException("The bill with id " + id + " no longer exists.", enfe);
            }
            em.remove(bill);
            em.getTransaction().commit();
        }
        finally
        {
            if (em != null)
            {
                em.close();
            }
        }
    }

    @Override
    public boolean createOrUpdate (BillInterface bill)
    {
        if (bill.getId() == null)
        {
            create(bill);
            return true;
        }
        else
        {
            try
            {
                update(bill);
                return false;
            }
            catch (Exception e)
            {
                throw new IllegalStateException(e.getMessage(), e);
            }
        }
    }

    @Override
    public List<BillInterface> findEntities()
    {
        return findBillEntities(true, -1, -1);
    }

    @Override
    public List<BillInterface> findEntities(int maxResults, int firstResult)
    {
        return findBillEntities(false, maxResults, firstResult);
    }

    private List<BillInterface> findBillEntities(boolean all, int maxResults, int firstResult)
    {
        EntityManager em = createEntityManager();
        try
        {
            Query q = em.createQuery("select object(o) from Bill as o");
            if (!all)
            {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            List<Bill> bills = q.getResultList();
            List<BillInterface> res = new ArrayList<BillInterface> (bills.size());
            for (Bill bill : bills)
            {
                res.add(new BillProxy(bill));
            }
            return res;
        }
        finally
        {
            em.close();
        }
    }

    @Override
    public BillInterface find(Long id)
    {
        EntityManager em = createEntityManager();
        try
        {
            return new BillProxy(em.find(Bill.class, id));
        }
        finally
        {
            em.close();
        }
    }

    @Override
    public int getCount()
    {
        EntityManager em = createEntityManager();
        try
        {
            Query q = em.createQuery("select count(o) from Bill as o");
            return ((Long) q.getSingleResult()).intValue();
        }
        finally
        {
            em.close();
        }
    }

    private Bill ensureReference (BillInterface bill) {
        if (bill instanceof BillProxy) {
            return ((BillProxy)bill).bill;
        }
        else
            return (Bill) bill;
    }

}

as you noticed, the class is actually called DBBillRepository... that is because there can be several repositories (memory, file, net, ??) types an from others tiers there is no need to know from what kind of repository i am working.
There is also a ensureReference internal method used by to get the real bill object, just for the case we pass a proxy object from the presentation layer. And talking about presentation layer we just use BillInterfaces instead of Bill an all will work well.
In some controller class (or a callback method, in case of a SWING app), we can work the following way...
BillInterface bill = RepositoryFactory.getBillRepository().find(1L);
bill.addItem(new Item(...)); // this will call the method of the proxy
Date date = bill.getDate(); // this will deleagte the call to the proxied object "hidden' behind the proxy.
bill.setDate(new Date()); // idem before
RepositoryFactory.getBillRepository().update(bill);

This is one more approach, at the cost of forcing using interfaces.
4) Well there is actually one more thing that we can do to avoid working with interfaces... using somekind of degenerated proxy object...
We could write a BillProxy this way :
class BillProxy extends Bill
{
    Bill bill;

    public BillProxy (Bill bill)
    {
        this.bill = bill;
        this.setId(bill.getId());
        this.setDate(bill.getDate());
        this.setDescrip(bill.getDescrip());
        this.setItems(bill.getItems());
    }

    @Override
    public void addItem(Item item)
    {
        EntityManager em = null;
        try
        {
            em = PersistenceUtil.createEntityManager();
            this.bill = em.merge(this.bill);
            this.bill.addItem(item);
        }
        finally
        {
            if (em != null)
            {
                em.close();
            }
        }
    }

    @Override
    public Collection<Item> getItems()
    {
        EntityManager em = null;
        try
        {
            em = PersistenceUtil.createEntityManager();
            this.bill = em.merge(this.bill);
            return this.bill.getItems();
        }
        finally
        {
            if (em != null)
            {
                em.close();
            }
        }
    }

}

So in the presentation tier we could use the Bill class, also in the DBBillRepository without using the interface, so we get one constraint less :). I am not sure if this is good... but it works, and also maintains the code not polluted by adding additional calling to a specific repository type.
If you want i can send you my entire app and you can see for yourself.
Also, there are several post explaining the same thing, that are very interesting to read.

Swing and lazy loading components
jpa on a Desktop SWING Application
Should JPA Entity Manager be closed?
Equals and hashcode in conjunction with collections

Also i will appoint this references that i still don't read completely, but looks promising.
http://javanotepad.blogspot.com/2007/08/managing-jpa-entitymanager-lifecycle.html
http://docs.jboss.org/hibernate/orm/4.0/hem/en-US/html/transactions.html
Well we reach the end of the answer here... i know that it is so long and probably somekind of pain to read all of this :D (made more complicated by my grammatical errors jeje) but anyway hope it helps **us to find a more stable solution to a problem that we just cannot erase jeje.
Greetings.
Victor!!!


MySite profile category names

Question: For SharePoint 2010, is it possible to edit/change MySite profile category names.  As an example, I would like to change "Ask Me About:" to "Skills & Talents:".
Answer: Go to "Manage User Properties" option under your the User profile application's setting page in Central Administration. Locate the "Ask Me About" and select "edit". You can change the display name in the edit page.

MySite profile category names


Experimental evidence of two neutrinos in muon decay?

Question: We know that the pion decays into a lepton/neutrino pair while the muon decays into a muon neutrino, electron and electron neutrino. How do we distinguish them experimentally? Would the muon decay have two 'missing widths' compared to the pion decay?
Comment: What do you want to distinguish here ? Decaying muon from decaying pions ?
Answer: It is the classic two body versus three body decay.
For a two body decay the energy in the center of mass of the two bodies is fixed for the individual decay products, a delta function for the muon, in the above example, within uncertainties.
For a three body decay there is a variable spectrum for the energy of the electron.  (Analogous in kinematics to the beta decay of a neutron) . Thus the missing energy has to be carried by something, in this case the electron neutrino was postulated and contributed to the lepton number conservation rule.
The distinction comes from measuring  the momenta and fitting the whole interaction to the hypothesis of the outgoing particles, for three bodies a 1 constraint fit, although it is obvious in the picture below:

The green is the pion loosing energy with ionization and it stops and then decays to a muon, a tiny line because the differences in the masses of pion and muon are only about 30 meV and there is a three body decay at the end. These momenta can be measured  and can be fitted with a one constraint fit , in principle.
Comment: This and the fact that we observe both flavor components in neutrino beams.


Communicating Drupal module settings module to module every page load for use in drupalSettings in Drupal 8

Question: I have a code base I am working in that has plenty of custom modules. It also has an analytics scheme already in place that have been trying to make work with certain customer requirements.
One of these is to report a 'page_type' value, that if set on certain landing pages is reported correctly in another module called my_analytics. The landing pages are custom module also, named my_landing_page.
I added the page_type property successfully to the module and have developed code to add analytics information from other modules. The set up I have works- actually, but it only works on the first page load, due to caching. I know why I am having the issue but am looking for solutions to get around it. I am an experienced programmer, but not an experienced Drupal-person.
Here is how I had it set up. The only place I could find to grab the field info from the my_landing_page module is in the preprocess hook, as follows:
function my_landing_page_preprocess(&$variables) {
  if (isset($variables['entity'])) {
    $node = $variables['entity'];

      my_analytics_get_landing_page_analytics($node);
  }
}

This calls a series of functions in the other module my_analytics which eventually merges these global additions into an array that becomes drupalSettings. There some analytics populated in my_analytics_page_bottom as well. This allows the javascript to use these values to send the analytics call with the info on the client side. The use of globals is unfortunate, but do you have a better idea?
function my_analytics_get_landing_page_analytics($node) {
  if ($node instanceof Node && $node->getEntityTypeId() == 'node' && $node->getType() == 'landing_page') {

    if ($node->hasField('field_page_type') && count($node->get('field_page_type')->getValue()) > 0) {
      my_analytics_add_tags_global([
        'TagID_channel' => $node->get('field_page_type')->getValue()[0]['value'],
      ]);
    }

  }
}

function my_analytics_add_tags_global(array $tags) {
  if (isset($GLOBALS['my_dtm_global_tags']) && is_array($GLOBALS['my_dtm_global_tags'])) {
    $GLOBALS['my_dtm_global_tags'] = array_merge($GLOBALS['my_dtm_global_tags'], $tags);
  }
  else {
    $GLOBALS['my_dtm_global_tags'] = $tags;
  }
}

function my_analytics_add_set_tags(array &$build, array $tags) {
  if (!empty($build['#attached']['drupalSettings']['my_analytics']['setTags'])) {
    $build['#attached']['drupalSettings']['my_analytics'['setTags'] = [];
  }

  // Search for global tags added from other modules.
  if (isset($GLOBALS['my_dtm_global_tags']) && is_array($GLOBALS['my_dtm_global_tags'])) {
    $tags = array_merge($tags, $GLOBALS['my_dtm_global_tags']);
  }

  foreach ($tags as $tag_id => $tag_value) {
    $build['#attached']['drupalSettings']['my_analytics']['setTags'][] = [
      'id' => $tag_id,
      'value' => $tag_value,
    ];
  }
}

So as I mentioned, this works on the first page load. I have done enough of my own investigating to know this is because the preprocess hook in my_landing_page does not fire after the page is cached and that this is intended behavior.
Keep in mind I do not have complete control over how the rest of the set up was written. I have inherited it.
So here are the options I have considered, but in most cases don't know how to accomplish.
1) Find something better than preprocess, a way to get the info (page_type) from my_landing_page each page load so that it populates my_analytics properly each time the page loads.
2) Somehow cache the drupalSettings with the page (or something similar) be it by using the Cache API or addCacheableDependency or something I am unaware of so that the original cached version is used for subsequent page loads.
3) ???
Does anyone know how to accomplish what I am trying here? Basically, grabbing info from one module and sharing it with another in a way that happens every page load? I want to get the page_type value from my_landing_page and into the drupalSettings object we populate in my_analytics and that to happen every visit to the page.
Thanks.
Answer: In general when you add attachments to a build array, then add the cache metadata in the same place:
$build = [];

// attach libraries and their drupal settings
$build['#attached'] = [...];

// add the cache dependencies for this data
$build['#cache'] = [
  'contexts' => ['url'],
  'tags' => ['dtm_global'],
  'max-age' => 60,
];

If you want to add cacheable dependencies of objects to the build array, you can use addCacheableDependency(), see https://www.drupal.org/docs/8/api/render-api/cacheability-of-render-arrays
Now sharing this with other modules is no problem anymore, Drupal builds a page with hundreds of render arrays like this, all from different modules.


Can I Use "You?"

Question: Is it usual to refer to the reader in a novel? The point of view is first person, and she refers to the reader at times. Can I do this?
Comment: Freedom of speech means you can write just about anything you like, and no one can tell you different. Now, whether you can pull it off.... well, that's up to you. For more information, I suggest you ask questions like this over at our sister stack, [Writers.Se] (we can't answer questions like this on EL&U, unfortunately).
Comment: For an example of this you could see "Earthly Powers" (Anthony Burgess). He starts from the first paragraph and uses it continually throughout the book. The technique is used from time to time on narrative film (fourth wall); on stage, it dates from at least Ancient Greece.
Answer: Directly addressing the reader(s) -- both in singular and plural -- is a device that was very common in seventeenth to nineteenth century literature. It has fallen a bit out of use today, but is by no means uncommon.
Usually, if a narrator (who is not the author) addresses the reader, there is some kind of framing narrative that explains who the narrator is -- e.g. a person looking back on his life or a chance witness to the narrated events -- and who the reader is to him  -- e.g. his son or a chance companion in a bar. A recent example is Patrick Rothfuss' Kingkiller Chronicles, where the first chapters relate in third person how the narrator-protagonist is visited in his exile and then tells his life to his visitor in first person -- occcasionally addressing his listener ("you").
In young adult fiction there are many examples where the first person narrator-protagonist addresses the readers without such a framing narrative, thus drawing the reader into her confidence and into the narrative.
If addressing the reader feels right to you, don't worry about it and just do it. It pushes the events clearly into the past (because if you can talk to the reader, who wasn't there, then obviously you are "now" elsewhere, too), creating a more reflective narrative style. But if that style fits you, it can work very well.
Comment: Makes me think of "A Tell-Tale Heart" by Edgar Allen Poe.
Answer: Charles Stross' Rule 34 does this, but the book is fully 2nd person. That means that you are the characters.

Some say the Internet is for porn but you know that in truth the
  Internet is for spam.

Rule 34 by Charles Stross

As @DanBron Said, it can be done, but it's a huge challenge to pull off.
One thing that's worth noting though is that there is a different between a 1st person that breaks the 4th Wall (addressing the audience) and a true 2nd person. If you only address the reader through the medium of the character addressing the reader, and you don't do it often, it will break the flow of the story simply because it is unexpected. To avoid this:

use the device often,
make it clear what you are doing,
have a very good reason for doing it
and make that reason clear to the reader.
Answer: The rule of writing states "Don't break the fourth wall".
That means don't remind the reader they are the reader, they read a book, the characters are in a book. Immersion is a volatile thing and worth fighting for - if the reader forgets the world around them and the book they hold in hand, but just lives and feels together with the protagonist, in the world of your story, you're succeeding - it's what puts mediocre stories apart from really good ones.
But as every rule, this one can be broken too. Break it if you want to achieve certain effects. Be it the narrator playing coy with the reader, be it humor, be it sixth sense of a character detecting the reader observing them, be it merging the worlds, the protagonists achieving a new level of consciousness and reaching across dimensions out to the reader, requesting their personal aid.

Let me quote a fragment from my most recent story, to give an example.
There's this horse - a sapient mare - right here with the protagonist (1st person narrator), inside his apartment. A bunch of thugs try to storm the apartment but circumstances (including the landlord with an Airsoft gun on a balcony above) really play against them. And including the mare standing with her back turned right towards the doorway. Two of them met her hind hooves already. Since the protagonist is in the room, without any clear view outside, most of the thug actions are reported through sounds.

Landlord's voice. "I'll keep shooting until you leave. I've got three hundred BBs in here, and another five thousand in the room."
Another series of cracks, and several thug voices cursing in sequence. They ran around the corner of the building. [...]
Through the closed drapes I saw movement outside the window.
"Look, that's a window to his apartment."
"Slab, did you take that molotov?"
"On a skirmish? Are you stupid? Someone hits me and I'm all soaked in gasoline."
"Frank, go fetch the angle grinder. We'll cut the bars on the window."
"You go yourself."
"The hell?"
"You've got hair. You got any clue how much these things hurt on a bald head?"
"Fine... pussy."
Steps running. Another burst from the AUG from above.
A moment of silence, sound of a lighter. Window opening. Cracks of BBs being launched at some six hundred FPS right onto heads of the thugs. Curses, steps running back around the corner. "Not inside!"
For a second Celestia stood only on her front hooves, as her hind legs swung in a graceful arc. A dull thud. Have you ever seen a smug horse? Imagine a smug horse. And now imagine it being twice as smug. That was Celestia at the moment.

This is the narrator being coy with the reader. It's a humorous scene where the serious enemy force met a very ragtag but surprisingly efficient resistance. The narrator is amused. The reader is amused. The mare is very amused. We can afford a small wink towards the reader, and break the fourth wall for a second, instructing them how to imagine the scene just right - just for added comical effect.
Answer: Yes, but relatively rare.
See second person point of view:

Definition: In second person point of view, the narrator tells the story to another character using "you"; the story is being told through the addressee's point of view. Second person is the least commonly used POV in fiction, though there are a few examples. Tom Robbins's Half Asleep in Frog Pajamas is one example of a novel told in second person. Many of the stories in Lorrie Moore's book Self-Help are also written in the second person.
Answer: Of course you can do it. In the humorous pseudo-autobiography The Life and Opinions of Tristram Shandy, Gentleman, Lawrence Sterne even went as far as inviting certain of his readers to skip the remainder of the chapter, and then welcoming them back at the beginning of the next one. Just make sure, like he did, that it's clear who you are adressing. (He often used the reader and he for that, though occasionally he also used you or thou.)
Why would anyone go out of their way to be unoriginal in original writing? Are you sure you are writing a novel, not a school assignment?
Here is a famous example of addressing the reader (from Tristram Shandy):

I define a nose as follows—intreating only beforehand, and beseeching my readers, both male and female, of what age, complexion, and condition soever, for the love of God and their own souls, to guard against the temptations and suggestions of the devil, and suffer him by no art or wile to put any other ideas into their minds, than what I put into my definition—For by the word Nose, throughout all this long chapter of noses, and in every other part of my work, where the word Nose occurs—I declare, by that word I mean a nose, and nothing more, or less.

(The protagonist lost part of his nose in an accident that occurred while he was peeing out of the window. In the 'chapter of noses', women show particular interest in men's nose sizes.) And another one:

I am this month one whole year older than I was this time twelve-month; and having got, as you perceive, almost into the middle of my third volume (According to the preceding Editions.)—and no farther than to my first day's life—'tis demonstrative that I have three hundred and sixty-four days more life to write just now, than when I first set out; so that instead of advancing, as a common writer, in my work with what I have been doing at it—on the contrary, I am just thrown so many volumes back—was every day of my life to be as busy a day as this—And why not?—and the transactions and opinions of it to take up as much description—And for what reason should they be cut short? as at this rate I should just live 364 times faster than I should write—It must follow, an' please your worships, that the more I write, the more I shall have to write—and consequently, the more your worships read, the more your worships will have to read.

And another:

I told the Christian reader—I say Christian—hoping he is one—and if he is not, I am sorry for it—and only beg he will consider the matter with himself, and not lay the blame entirely upon this book—

Sterne also keeps making promises to readers, and occasionally even keeping them after great delay.
Obviously it's a bad idea to be as eccentric as this unless you are really good at it and want to make the eccentricity the focus of your work. But a bit of breaking the fourth wall here and there can often be beneficial in 'normal' writing.
Answer: Is it usual? No. Can you do this? Of course.
The question is not, Is there some rule against it in the Laws of Writing that All Authors Must Obey Under Pain of Death? The question is, Is it effective?
Any time you do something unusual, there is the danger that it will come across as a gimmick. If you do something unusual just to do something unusual, odds are it will sound lame. Sure, you could write a story where every sentence begins with the letter "w", and it would no doubt be challenging to write a coherent story that way. But would anyone care? Or would your efforts to make the story fit this arbitrary constraint just end up making a weaker story? But if you have a good reason for doing it, and it works in your story, than by all means do it.
Answer: Like all the other answers state, it's not a common thing to do, but there is nothing to prevent you from doing otherwise. A great example that I'd suggest you read is Italo Calvino's If on a winter's night a traveler. It's exactly what you are looking for. An entire novel whose protagonist is you.
It is not only good received by the critics and readers, but it also considered one of Calvino's masterpieces.
Another notable example is Anthony Burgess' Clockwork Orange. The narrator of the book often refers to the reader (usually when in a tough position) with phrases like 'Oh, my dear reader' and 'You are the only friend left' etc.

Can I Use "You?"


If Magnus Carlsen had the Bishop Pair would he be able to beat Stockfish?

Question: If Magnus Carlsen had neither knight at the start of the game and stockfish had neither bishop would Magnus Carlsen be able to beat Stockfish?
Comment: stockfish too strong
Answer: Possible but unlikely
Many people consider knights and bishops to be equal. The rest would agree that they are very close. This means that stockfish isn't very handicapped. Magnus's rating (any mode) ranges from 2800-2900. I have found Stockfish ratings from 3400-4000. This means the rating difference is somewhere from 500-1200. While that is a large range, it doesn't matter where the true value is. With a minor/nonexistent handicap, it rare for a player to beat another player that is 500-1200 ELO higher than them. Assuming that knight and bishop are equal, the probability of Magnus winning is around 1/9 to 1/729 (the big range is from expontetial extrapolation of the ELO difference). Even if you say bishop is better, there will be little difference. So, yes it is possible. Anything is possible. But is very unlikely.
Comment: https://wismuth.com/elo/calculator.html#name1=Carlsen%2C+Magnus&rating2=3620&best_of=100&first_move=player1 Material odds -1.21, so it seems to me if the position is +1 to start with, 1/9 is a reasonable bet. That said it totally depends what depth you allow Stockfish to run to, and on the time control. And of course on the air quality ;-)
Comment: Agreed that 1/9 seemed more reasonable, but my reasoning doesnt appear to have any MGFs (Major Gaping Flaws) so I still included it @anonymous2
Answer: No
We can give this position to Lichess (with White to move, because it's the bigger advantage), and note that the evaluation is about +1. This evaluation is not large enough for a human player to win.
See also this question about exactly how much advantage a human needs to be able to beat a computer. A handicap of ~two pawns (opening eval -3) yields a roughly equal game. A handicap of a piece (opening eval -5) gives the human an advantage. At opening eval +1, Carlsen is unlikely to win, and in fact he is likely to lose, as Nakamura did against Komodo with a +2 opening eval (see game 4, and note that Komodo is weaker than Stockfish + that game was also several years ago, with engines constantly improving).
Comment: @Starshipisgoforlaunch if you think you can beat full-power Stockfish with no handicap, prove it and post the game (also post the Stockfish version, nps and time controls you used).
Comment: You propose that is it is impossible? That is wrong, theoritically I could beat stockfish with no handicap. I could also be struck by lightning. It is possible, but unlikely. An evaluation of +1 makes it unlikely, not impossible @Allure
Comment: I dont. I think that if I played a google (thats a number) games against it I would win 1. Meaning it is possible @Allure
Comment: @Starshipisgoforlaunch Stockfish is virtually unbeatable from the starting position (which is why so many correspondence games are drawn), so you'd be playing for a loooong time. But I suppose it's possible an asteroid strikes the computer and it fails to play a move, something like that /shrug.
Comment: @Starshipisgoforlaunch not trying to be rude but it is more probable to get hit by lightning than beating stockfish :P
Comment: You're not wrong. However, people do still get hit by lightning, albeit very rarely. Being hit by lightning is not impossible @cmgchess


Sack team_id leaking in source code

Question: I found slack team_id in vdp source code, is it a sensitive token if yes how can I use it for a PoC?
Answer: Team_id is not a sensitive token in slack, nor can it be used in a Proof-of-Concept. The team_id variable is just used to indicate the ID of the team's slack channel in a programmatic way. If you're looking for bugs in slack, imo I think their websockets implementation may be worth a look if you're looking for potential targets.

Sack team_id leaking in source code


Discrete time Solow model golden rule saving rate

Question: I have a discrete time Solow model and need to derive the golden rule for the economy by setting maximization problem. How can I do that, if I can't take derivative?
Note: required investment can't be approximated.
Answer: What is discrete is the time dimension, not the range of capital, the values that it can take. Also, at the steady-state we have $k_{t+1}=k_t = k^*(s)$. So proceed as in the continuous-time case.
Comment: @Ksenia But you can take derivatives. The other answer posted gives a detailed explanation.
Comment: I need to find golden rule saving rate, i.e. maximize consumption. How can I do that without derivative?
Answer: As Alec Papadopulous said, Solow model in discrete time means that time cannot take all the values on the positive real line, but some discrete values only, so that the variables, thought of as function of time, take only values in correspondence of those discrete values of time. Continuous time model means that time varies continuously, that is it takes all the value of an interval (say the positive values) of the real line.
But that's all. The discrete  values concern  only time. Only time is discrete.
On the contrary, the other variables and functions are 'normal' variables and functions, defined on intervals of $\mathbb{R}$.
So, functions as the intensive production function $f(k)$, the saving function $sf(k)$ and so on, are not functions defined on discrete sets, and they are usually assumed to be differentiable, considered as function of $k$ or other variables of the model.
When deriving the Golden Rule, you look for the value of $s$ that maximizes, in steady state, the per capita consumption. This is a hypothetical, conceptual  experiment, time is not involved: you are comparing different steady states with different values of $s$.
Therefore, you mustn't differentiate anything with respect to time. The variable considered, with respect to which you are maximizing consumption, is the saving rate $s$.  You must take the derivative with respect to the saving rate $s$, and no discrete variable is involved.
So, there is no difference between discrete and continuous time approaches to find the Golden Rule rate of saving, you can use the derivative in the same way in both cases.
For an explanation of how to derive the Golden Rule level of saving, see the answer of Alecos Papadopoulos in
Deriving the golden-rule savings rate in a Solow Model
Comment: In the task it was emphasized that I should derive the golden rule saving rate for discrete time, else "no points will be given". Moreover, I don't have any production function (the only function I have is the one in the picture).
Comment: You should refer to the version of Solow model you studied. The formula in your question is the difference equation that rules the system, I suppose, so you have to find the steady state $k^*$ setting $k_{t+1}=k_t$, then to find the steady state consumption, then maximizing it with respect to $s$. By the way, you have a production function, is $f(k)$ in your formula. I think you should review  the Solow model. Without remembering the overall model  it is difficult to solve your problem. It is not a matter of discrete or continuous time, but of remembering the overall functioning of the model.


Software to help port VxWorks application to Windows

Question: My company uses a product called VxWorks and I am looking for a software to help port the VxWorks application into Windows. Note that the motherboard was changed. The OS would be changed to Windows-7 embedded too. Now, the question is how to port the original VxWorks' application to Windows platform. I wonder if there are any tools to help to do such a job.
Comment: Is your application compiled with Diab Compiler, GNU, Intel C++ Compiler? Are you OK with emulation or do you want native code?
Comment: How big is the application, and what do your forsee as major problems during the migration if you do it by hand?
Comment: 1. In original project, the codes were compiled by Tornado (fm WindRiver).
2. There are two platforms: one has about 110K lines and the other has about 45K lines.
Answer: I came back to share my experience with it during the recent months. I found the GUI programs calling two libraries: Zinc & WindML. The former is higher level calling the latter. I got a trial version of OS Changer from MapuSoft and found that they don't support porting of Zinc/VxWorks to Zinc/Windows. Instead, I found there is another version of Zinc, Zinc for Windows. Since that, I can deal the portion of Zinc-based codes by just recompiling it using Zinc for Windows. What OS Changer can help is just WindML-based codes, which are just small portion since most codes calling Zinc library.
In the other hand, the reason why I purchase tools like Zinc for Windows or OS Changer is just for saving time. The quota for OS Changer is more than year income of an average Taiwanese SW engineer, but it can save only about 1 month engineering time, at most. So, what sense does it make to purchase it for a small project?
Instead, the price of Zinc for Windows is less than half month salary of Taiwanese SW engineer, but it can save several months engineering time and give better code quality.
So, I decided to purchase Zinc for Windows and port WindML-based codes by hand.
Comment: So how much time do you expect to use converting the application going down this path?
Comment: Two to three months.
Answer: I just found a software from MapuSoft: http://www.mapusoft.com/
Their products are mainly for helping porting applications among different platforms to secure your investment on SW development. I even had a meeting with its FAE and saw his simple presentation & demo. I got trial version. It's very likely this could be what I want. Using this tool, I don't need to port all codes line by line.
There are more benefit in using their tools compared to porting manually.
I'll come back here to confirm if it works well after I verify it. It'll be weeks later.
Comment: I didn't accept this solution from Mapusoft. It's too expensive. In Taiwan, porting a project like what I said may cost about less than US$40K, but it cost almost 1.5 of it and I still need to spend much time to it. It's not very high percentage to do the porting automatically.
Comment: I look forward to your post. If it works, don't forget to accept your own answer, as that will help otehrs who read this question in future.
Answer: After some trial, I don't appreciate that tool. It's not so fancy as they said. My solution is still to write the program in hand.


Having Your Own Copyrighted App Automatically After Publishing

Question: I have a question regarding for the game app and in business. You see, I decided to publish an app to Google Play. When I published it, does this app automatically declared that this app is copyrighted or copyright protected by the publisher and creator like me itself or not? If not, does it have to seek something or required to register this app for copyright authentication via online or at office or not?
Comment: Yes, at least in the United States. If you google "should I register for copyright?" you will get better and more authoritative answers than in here. You should really try google first, it will save you a lot of time.
Comment: That depends on where you live, but most likely you don't have to register anything. Copyright is automatically assigned on creation. As always, consult a specialized lawyer for advice.
Comment: Is it automatically acknowledged/recognized by users even if he/she didn't see the copyright logo/icon, this app is still copyrighted?
Comment: The fact that it exists means someone created it, that creator has certain legal rights over their own creation simply because they were the one who created it, i.e. copyright.
Answer: The moment you create a creative work, you automatically have an exclusive copyright on it. This is guaranteed by various international copyright treates (like the Berne Convention) which are recognized by almost all countries worldwide.
In some jurisdictions you can register a copyright for a work (in the USA, you can do so at the United States Copyright Office), but this is optional and not necessarily required to enforce your rights in court when you have other prove that you created the work.
An explicit copyright notice ("Copyright ©2013 John Doe, all rights reserved") is not required either. It is just used as a friendly reminder to people that you are the copyright owner and that you care about your copyright. But such a notice is not required to enforce it, should someone violate it.
When you publish something on google play, you give google a non-exclusive license to distribute your software (otherwise they wouldn't be allowed to publish it). Non-exclusive means that you retain the copyright. You can still publish it yourself, license it to other entities and sue entities which distribute it without your permission.
Comment: So that means no worries. There are some process to take in order to publish apps at Google Play and this app is automatically copyright protected without further notice?
Comment: I saw something from your link you've gave me an it said that under Article 3(4), " `simultaneously" is defined as "within 30 days` ". Is there a limit?
Comment: I would recommend you to read the [original text](http://www.wipo.int/treaties/en/ip/berne/trtdocs_wo001.html#P98_14701) of the convention. This is only relevant for publishing in countries which are not part of the Berne convention - and those are very few. But keep in mind that the Berne convention itself is not a law. The convention is implemented in each participating country with local laws which interprete the convention differently. Check your local copyright laws and those of the United States (because they are the laws which apply to Google).


Link2sd pkg error

Question: I have problem with Link2sd app. When i'm trying to move any app to sd card following error appears. Any ideas how to fix it?
Samsung galaxy J3, 5.1.1 Android
Answer: fixed, solution for the future;
Just open Link2Sd and convert the system app to user app by clicking on the app, then click on the three dots on the top-right corner, then select Converto to user app. After that you have to restart and then you can move to sd, or link if you want to.

Link2sd pkg error


easelJS - Resize an image with Javascript then use it with SpriteSheet

Question: In my code, I have an image stored into a variable directly load from Javascript (I didn't create any HTML tag link to this image)and this image is in High Definition.
Depending on the screen size of the user, I would like to resize the image with specific values (width and height) and I already calculated these values.
I modified the size of the image using Javascript
imgHD.width = modifiedWidth;
imgHD.height= modifiedHeight;

This is working nice.
I checked it using
alert('newWidth: ' + imgHD.width + ' / newHeight: ' + imgHD.height);

and the values are corrects.
I create the SpriteSheet and easelJS used the image in HD without the width and height modifications.
var spriteSheet = new createjs.SpriteSheet({
    // image to use
    images: [imgHD],
    // width, height & registration point of each sprite
    frames: { width: imgHD.width / imgSpriteNbr, height: imgHD.height / imgSpriteNbr, regX: imgHD.width / imgSpriteNbr / 2, regY: imgHD.height / imgSpriteNbr / 2},
    animations: {
        walk: [0, imgSpriteNbr - 1, "walk"]
    }
});

Could I resize my image directly from easelJS, could I resize the image from Javascript in another way which could be interpreted correctly by easelJS after ?
I'm a beginner with easelJS,
Thanks
Answer: To create the SpriteSheet at a different size, you would have to use an intermediary canvas, and draw your image at a different size first, and then use it with a SpriteSheet.
An easier way would be to just scale the sprites created from the SpriteSheet.


In what contexts can I say "in the school"?

Question: In what cases it is correct to say "in the school"? Are there any situations, in which that combination of words placed in the end of a sentence would be correct?
Comment: It might be better if you came up with the cases you think it might be OK to use it in and asked us to let you know if they're correct.  Otherwise, you're asking us to make a list and lists are very difficult to make complete.
Comment: The phrase "in the school" can mean "inside the school [buildiing]." _Last week there was a flood. There was a lot of damage in the fire station, but not much damage at all in the school._
Comment: @J.R. - Thanks for your example. Can you, please, provide some explanation for it that you think is right. Is it like when I talk about some "human-driven" activity (party, contest, competition, lesson, meeting, etc.) than I should say "at the school", but if it's some weather phenomenon than it should be "in the school"?
Answer: You can say

...in the school

when talking about something that happened, happens, is happening, will happen, or could happen, etc. inside the school.
There are 44 students and 6 teachers in the school.
I left my book in the school.
Tonight's dance will be in the school.
No strangers have been spotted in the school.
We don't like the food at the cafeteria in the school.
The flood damaged a lot of walls in the school.
An ant fell in love with another ant and had 6000 baby ants, and they now all live in the school.
Scientists think traces of radiation from the damage last year to the nearby nuclear plant can be found in the school.
Comment: Also, there is a difference between *at school* and *at the school*. The former means you are there for its intended function: for teachers to educate students; the latter means you are there for some other purpose, such as to clean the carpets or something. A student or teacher would say they are *at school*. A carpet cleaner would say he is *at the school*, because he is not there as a student or teacher. A parent picking up a student would probably say they are *at [student's] school.*
Comment: Wait a moment, but would "at the school" not be okay in your examples also meaning "inside the school"?
Comment: No, because the **conception** that the speaker has of the location is different. *At* refers to a place as a point. *In* refers to a place as something that *contains* things, or that one can be *inside*. See [my answer](http://ell.stackexchange.com/questions/40439/should-i-say-she-is-in-the-park-or-she-is-at-the-park/40488#40488) about *in*, *at*, and *on*. *At* does not tell us that someone is *inside* the school, only that they are at the location of the school; they could be inside or outside (in the parking lot for example).
Comment: At is more useful when you are talking about the location of the school itself, or giving directions. "I'll go get your backpack for you, where did you leave it?" "Thanks, I left it at the school, in room 103." Or a similar sentence, "where is the dance tonight?" "it's at the school."
Comment: Also, "in school" is idiomatic. "Stay in school kids" doesn't mean go to school and stay there forever. It means keep going to school, keep learning, don't break the rules. Or even more generally, it means behave.
Answer: In school vs in the school.
When you talk about activities other than school actvities, you use the phrase "in the school".  Otherwise, you use "in school" about school/educational activities.  look at the following sentences to distinguish between these phrases:
My kids are still in/at school.
Some visitors are in the school.
There is a canteen in the school.
The carpenter is repairing chairs in the school.
Answer: Not really, 'in the school' is perhaps more common American English while 'at school' is more British but both are equally 'correct'. Similarly an American would probably say 'in college' while a Brit would say 'at university'.
In tends to be used for institutions, so your are 'in hospital' or rather than 'at hospital' but 'at home' not 'in home' - although you might be put 'in a home'
It's just one of those things!
there is perhaps a slight subtle difference that 'in school' means they attend school - as opposed to having finished school, while 'at school' means they are there now.
So "are your children in school" = are they under 16 or 18 ? But "are your children at school" = are they at school today or are they at home.
(but that's from a BE perspective)


svnserve vs xinetd + svnserve

Question: What would be the best solution considering, scalability/load and security, svnserve + xinetd or standalone svnserver? Thank you!
Comment: How many concurrent uses do you have to serve ?
Comment: It would be aprox. up to 100 concurrent uses.
Answer: Don't forget there is always svn over ssh, and mod_dav_svn, both widely deployed, and highly reliable.
Comment: It is on the internal network, so I don't think that ssh is needed. mod_dav_Svn is already in use. What I'm curios of if there are some advantages (beside the ones I can think of) of running svnserve with xinetd instead of running it standalone. Basically I'm making a small cleanup and I want to get rid of the xinetd daemon if there is no need for it.
Comment: I second the desire to drop `xinetd` if you don't need it. I've had no problems running `svnserve` over internal and external networks.
Answer: One thing that I can think of as a benefit of doing it with xinetd is with regards to multiple repositories.
If you are looking to have one svnserve serving up multiple repositories, you'd need to use the -r switch, and load up svnserve.
However adding a new repository tree would require a restart of the entire svnserve process, possibly interrupting current users.
If you use xinetd it will automatically be picked up the next time the system is quite (and you don't have to wait around for it to happen).
At the moment that's the only advantage I can see to using xinetd.

svnserve vs xinetd + svnserve


Sending large data over sockets in java

Question: I am working on client server architecture and i am just beginner in this thing. Here my server is of C and client is of Java and i want to send a binary/database (.db)/image file of size around 10 - 20 MB from C server to the Java client. But data is lost while implementing the following code:
Server side C code is:
int sockfd, newsockfd, portno, clilen;

struct sockaddr_in serv_addr, client_addr;

sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
    strcpy(message,"Error opening socket");
    eFlag = 1;
    send_message(message);
}

bzero((char *)&serv_addr, sizeof(serv_addr));
portno = 6789;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);

if(bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
    strcpy(message,"Error on binding");
    eFlag = 1;
    send_message(message);
}

listen(sockfd, 5);
clilen = sizeof(client_addr);
newsockfd = accept(sockfd, (struct sockaddr *)&client_addr, &clilen);

if(newsockfd < 0)
{
    strcpy(message,"Error on accept");
    eFlag = 1;
    send_message();
    exit(4);
}

void send_file()
{
    buffer = (char *)malloc(sizeof(char)*lSize);

    result = fread(buffer, sizeof(char), lSize, fp);
    printf("\n\n########## data read into buffer successfully #######");

    if(newsockfd)
    {
        n = send(newsockfd, buffer, lSize);
        printf("\n\n$$$$$$$$ Data sent successfully $$$$$");
        strcpy(message,"Data sent successfully");
        send_message(message);
    }

    else
    {
        strcpy(message, "Error writing on socket");
        send_message(message);
    }

    sleep(sleepTime);
    sleepTime = (int) (sleepTime*1.02);

    free(buffer);
}

And, my client side Java code is:
                    final int PORT_NO = 6789;
        final String Server_name = "220.127.116.11";
        Socket m_socket = null;
        String str;
        int ch;

    try {
        if(m_socket == null)
            m_socket = new Socket(Server_name, PORT_NO);
        if(m_socket == null)
            System.out.println("Unable to open the socket");

    DataInputStream dis = new DataInputStream(m_socket.getInputStream());
    PrintStream ps = new PrintStream(m_socket.getOutputStream());

    DataInputStream ds = new DataInputStream(System.in);

    while(true)
    {
        System.out.println("1. Synchronize");
        System.out.println("2. Exit");
        System.out.println("Enter your choice...");
        str = ds.readLine();
        ch = Integer.parseInt(str);

        switch(ch)
        {
        case 1:
     ps.println("<message action='buttonpress' value='synchronize' />");
     System.out.println("avilable data to read is:"+dis.available());
     FileOutputStream fos = new FileOutputStream("mukul.db");

            byte data[] = new byte[102400]; //100 Kb byte array

                            dis.read(data);

            String str_data = new String(data);
            str_data = str_data.trim();
            data = str_data.getBytes();

            fos.write(data);
            fos.close();

            break;

here only part of data is read i.e around 10 kb or less.
I am just a beginner to such posts and my code may be cumbersome, so please ignore all the mistakes in posting.
So please kindly tell me how can i receive 1 MB/10 MB of data in this client-server architecture without the loss of data.
What if i use "sendfile(out_fp, in_fp, pos, len)" method in C code instead of "send()". This method sends file handle. So what will be the corresponding function in Java to capture file handle.
Thank you in advance.
Answer: You're misusing the send()/recv() functions. send() and recv() are not required to send as much data as you request, due to limits that may be present in the kernel. You have to call send() over and over until all data has been pushed through.
e.g.:
int sent = 0;
int rc;
while ( sent < should_send )
{
   rc = send(sock, buffer + sent, should_send - sent, 0);
   if ( rc <= 0 ) // Error or hangup
      // do some error handling;

   sent += rc;
}

Comment: Thank you. it helped in receiving data successfully on client.
Answer: Java side,
int lent2 = 0;
int LengthToReceive = 102400;
char[] chTemp = new char[LengthToReceive];

while (true) {

    int readlength = bufferedreader.read(chTemp, lent2,LengthToReceive - lent2);
    if(readlength==-1){
        break;
    }

    lent2 += readlength;
    if (lent2 >= LengthToReceive) {
        flag = false;
        break;
    }
}

Answer:
m_socket can't possibly be null the line after you call m_socket = new Socket(...). It will either throw an exception or assign a Socket to m_socket, never null. So that test is pointless.
After you call readLine() you must check for a null return value, which means EOS, which means the other end has closed the connection, which means you must exit the reading loop and close the socket.
As it says in the Javadoc, InputStream.available() shouldn't be used for a test for EOS. Its contract is to return the number of bytes that can be read without blocking. That's rarely the same as the length of an incoming file via a socket. You must keep reading the socket until EOS:
int count;
byte[] buffer = new byte[8192];
while ((count = in.read(buffer)) > 0)
    out.write(buffer, 0, count);

If your sending end doesn't close the socket when it finishes sending the file you will have to have it send the file length ahead of the file, and modify the loop above to read exactly that many bytes.
Comment: Thank you, it helped me in sending the data from server successfully.

Sending large data over sockets in java


How to check if a file is already open by another process in C?

Question: I see that standard C has no way of telling if a file is already opened in another process. So the answer should contain several examples for each platform. I need that check for Visual C++ / Windows though.
Comment: Given your answer, you have a file locking problem and every platform has well-defined ways to do that.

As far as being uninterested in re-thinking the problem, I'll merely note that changing it from "who has the file open" to "how do I lock the file" is re-thinking the problem, and changes it from a very difficult one into a much simpler one.
Comment: I am not interested in "which" process is accessing information.
This question is here because no one else seems to answer this question on other platforms but it might be necessary to know that. So please keep it short, and don't tell to "re-think" about the problem. Think about explorer, it is as if before removing the file it checks to see if any other process is accessing it.
Comment: "each platform"?!?!? Do you know how many of those there are?
Comment: Why do you want to find out if a file is open by another process?  Also, do you want to find out if the file is open by a specific (ie, known) process, or just open by any other process?
Comment: Just if another process already accesses at the same time. If the file is in use, I might damage somethings when proceeding.
Comment: Not the same application. I am modifying another process's file.
Comment: [Find out which process is locking a file or folder in Windows](https://superuser.com/q/117902/173513) on SuperUser.
Answer: Windows: Try to open the file in exclusive mode. If it works, no one else has opened the file and will not be able to open the file
HANDLE fh;
fh = CreateFile(filename, GENERIC_READ, 0 /* no sharing! exclusive */, NULL, OPEN_EXISTING, 0, NULL);
if ((fh != NULL) && (fh != INVALID_HANDLE_VALUE))
{
   // the only open file to filename should be fh.
   // do something
   CloseHandle(fh);
}

MS says: dwShareMode

The sharing mode of an object, which can be read, write, both, delete, all of these, or none (refer to the following table).
If this parameter is zero and CreateFile succeeds, the object cannot be shared and cannot be opened again until the handle is closed.
You cannot request a sharing mode that conflicts with the access mode that is specified in an open request that has an open handle, because that would result in the following sharing violation: ERROR_SHARING_VIOLATION.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx
extension:
how to delete a (not readonly) file filesystem which no one has open for read/write?
access right FILE_READ_ATTRIBUTES, not DELETE. DELETE could cause problems on smb share (to MS Windows Servers) - CreateFile will leave with a still open FileHandle /Device/Mup:xxx filename - why ever and whatever this Mup is. Will not happen with access right  FILE_READ_ATTRIBUTES
use FILE_FLAG_OPEN_REPARSE_POINT to delete filename. Else you will delete the target of a symbolic link - which is usually not what you want
HANDLE fh;
fh = CreateFile(filename,  FILE_READ_ATTRIBUTES, FILE_SHARE_DELETE /* no RW sharing! */, NULL, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_DELETE_ON_CLOSE, NULL);
if ((fh != NULL) && (fh != INVALID_HANDLE_VALUE))
{
    DeleteFile(filename); /* looks stupid?
                          * but FILE_FLAG_DELETE_ON_CLOSE will not work on some smb shares (e.g. samba)!
                          * FILE_SHARE_DELETE should allow this DeleteFile() and so the problem could be solved by additional DeleteFile()
                          */
    CloseHandle(fh); /* a file, which no one has currently opened for RW is delete NOW */

}

what to do with an open file? If the file is open and you are allowed to do an unlink, you will be left a file where subsequent opens will lead to ACCESS_DENIED.
If you have a temporary folder, then it could be a good idea to rename(filename, tempdir/filename.delete) and delete tempdir/filename.delete.
Answer: There's no way tell, unless the other process explicitly forbids access to the file.  In MSVC, you'd do so with _fsopen(), specifying _SH_DENYRD for the shflag argument.  The notion of being interested whether a file is opened that isn't otherwise locked is deeply flawed on a multitasking operating system.  It might be opened a microsecond after you'd have found it wasn't.  That's also the reason that Windows doesn't have a IsFileLocked() function.
If you need synchronized access to files, you'll need to add this with a named mutex, use CreateMutex().
Comment: Thanks. For Windows this works and really helped my problem. The rest is for other people seeking an answer for other platforms may be.
Answer: Getting the open_files information is DIFFICULT, it's like pulling teeth, and if you don't have an immediate need for it you shouldn't be asking for "several examples for each platform" just for the hell of it. Just my opinion, of course.
Linux and many Unix systems have a system utility called lsof which finds open file handles and stuff. The way it does so is by accessing /dev/kmem, which is a pseudo-file containing a copy of "live" kernel memory, i.e. the working storage of the operating system kernel. There are tables of open files in there, naturally, and the memory structure is open-source and documented, so it's just a matter of a lot of busywork for lsof to go in there, find the information and format it for the user.
Documentation for the deep innards of Windows, on the other hand, is practically nonexistent, and I'm not aware that the data structures are somehow exposed to the outside. I'm no Windows expert, but unless the Windows API explicitly offers this kind of information it may simply not be available.
Whatever is available is probably being used by Mark Russinovich's SysInternals utilities; the first one that comes to mind is FileMon. Looking at those may give you some clues. Update: I've just been informed that SysInternals Handles.exe is even closer to what you want.
If you manage to figure that out, good; otherwise you may be interested in catching file open/close operations as they happen: The Windows API offers a generous handful of so-called Hooks: http://msdn.microsoft.com/en-us/library/ms997537.aspx. Hooks allow you to request notification when certain things happen in the system. I believe there's one that will tell you when a program –systemwide– opens a file. So you can make your own list of files opened for the duration you're listening to your hooks. I don't know for sure but I suspect this may be what FileMon does.
The Windows API, including the hook functions, can be accessed from C. Systemwide hooks will require you to create a DLL to be loaded alongside your program.
Hope these hints help you get started.
Comment: It is possible to do this in Windows. The Sysinternals handle.exe tool does it, for example. http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx
Answer: For Windows, this code works also:
boolean isClosed(File f) { return f.renameTo(f); }

An opened file can not be renamed, and a rename to same name does not cause another error. So if the rename succeeds, not having really done something, you know the file is not open.
Comment: Which library are you using to the rename action?, because if I try to rename the file with `std::rename`, Windows allows to rename the file although it is opened.
Comment: This works for me, and is very simple to implement! It is in Java rather than C, which is perhaps why you have been downvoted. I found this answer extremely helpful, thanks.
Answer: Any such check would be inherently racy.  Another process could always open the file between the point where you did the check and the point where you accessed the file.
Answer: The answers so far should tell you that finding out the information you've asked for is tricky, non-portable, and often inherently unreliable.  So, from my perspective, the real answer is don't do that.  Try to find a way to think about your real problem so that this question doesn't arise.
Answer: this can't be that hard guys.
do this:
try{
File fileout = new File(path + ".xls");
FileOutPutStream out = new FileOutPutStream(fileout);
}
catch(FileNotFoundException e1){

 // if a MS Windows process is already using the file, this exception will be thrown
}
catch(Exception e){

}

Comment: True, LOL whoops sorry
Comment: Which "version" of C++ are you using?
Answer: You can use something like this. It is not a proper solution. But it works,
 bool IsFileDownloadComplete(const std::wstring& dir, const std::wstring& fileName)
    {
        std::wstring originalFileName = dir + fileName;
        std::wstring tempFileName = dir + L"temp";

        while(true)
        {
            int ret = rename(convertWstringToString(originalFileName).c_str(), convertWstringToString(tempFileName).c_str());
            if(ret == 0)
                break;

            Sleep(10);
        }

        /** File is not open. Rename to original. */
        int ret = rename(convertWstringToString(tempFileName).c_str(), convertWstringToString(originalFileName).c_str());
        if(ret != 0)
            throw std::exception("File rename failed");

        return true;
    }


Red color in the nozzles of Soyuz-2 rocket engines RD-107 and RD-108, is it only a decoration?

Question: Many pictures of the Soyuz-2 show the inside of the nozzles painted with a brilliant red. Is there any function of the paint during ignition and operation of the rocket engines, or is it just a traditional decoration? The paint will burn away very quickly.
Answer: They aren't painted red. If you zoom in on the picture, you'll see that the red parts are actually protective inserts clipped onto the nozzles to keep debris out of them during transport.

It's a widespread aerospace-industry convention to make such "remove before flight" items bright red so it's obvious if you've left one on.
Apparently the covers stay in place until very late in the launch process. In a description of the lead-up to a Soyuz launch, "Soyuz: A Universal Spacecraft" says:

The crew start the onboard tape recorder at T-15 min[utes], and ensure that their safety harnesses are tightened as the automated flight sequencer takes over. Combustion chamber covers on the first and second stages are blown off with nitrogen, while topping off of the liquid oxygen tanks is completed...

Here they're referring to the boosters and core as first and second stages.
Comment: Very good answer, thank you! Your zoom of the picture show the "remove before flight" items very precise. So there is a function of the bright red.
Comment: I cannot find the exact moment when they are removed, that should be some time before refuelling I guess. No pictures or videos of this event for what you know?
Comment: The protective covers should be mounted in the assembley building and during horizontal transport to the launch pad. Also when the rocket is turned from horizontal to vertical at the launch pad. If the launch pad is evacuated just before fuelling, the covers should be removed before. If launchpad security regulations allow human access to the nozzles of a tanked rocket, they may be removed short before launch.
Comment: @VeronicaRemondini I ran across a line in "Soyuz: A Universal Spacecraft" that suggests they're removed only 15 minutes before launch, but it's not 100% clear.


My son won't stop tickling my feet

Question: I feel like I can't be barefoot around him because every time I'm in the same room as him without my shoes on, he'll find some excuse to sit next to me and he'll start to tickle my feet. He isn't disobedient, so I can tell him to stop and he stops but the very next day he tries to tickle me again. Just last summer we were at the pool and I noticed he sat next to me while I was dangling my bare feet in the water and he began to tickle my feet while I was talking with my friend! Also, every time i sit in the recliner with my feet up, he will come and grab my ankle and tickle my foot for a long time until I can get it loose. I'm very ticklish, and this is inappropriate. How can I tell my son not to tickle my feet without upsetting or otherwise embarrassing him?
Comment: You posted a very similar question about personal dislikes and boundaries just a few days ago; the answers are going to be very similar as well.
Comment: I would appreciate any help I can get, my children are very similar.
Answer: Articulate and enforce personal boundaries.  Make it clear to your son that tickling your feet is not something that you are okay with, ever.  When he does it, instead of telling him to stop, remind him it simply isn't okay, and impose consequences if necessary, as this kind of boundary violation will be much more significant later on in life.  This approach would probably also work on your daughter's poking of your belly button.

My son won't stop tickling my feet


Can a relative not based in the U.K. sponsor me for U.K. visitor visa?

Question: I intend to visit the U.k. for tourism this summer. Is it possible that my brother (who is not based in the U.K. nor does he have indefinite leave to stay or British citizenship nor does he live in the U.K. ) sponsors the expenses of my trip? If so, would an affidavit from him, his bank statement and proof of familial ties be sufficient evidence?
Answer: Yes. There is no rule that the sponsor as to be a UK resident, citizen, etc. The sponsor has to show that they have sufficient funds to support your trip and that they are willing to do so.
Just think about foreign students in the UK. Many are sponsored by their parents, a large majority of which are not UK residents, citizens - some may even have never visited the UK before!
So there is absolutely nothing wrong with your brother sponsoring your trip.
Evidence would be his financial statements, where he is working (a salary certificate), how much and what is the paying for, and of course his relationship to you.
Comment: The two important things are that your brother earned this money legally and that he can afford to give it to you with no expectation of repayment. How much money one would credibly give as a gift depends on the relationship between donor and recipient, so it matters how he is related to you.


Was it ever stated that monks can't show their psychic powers to lay people?

Question: I heard it was one of the monk rules. What sutta says monks can't show their powers to lay people? The Buddha showed his psychic powers to many lay people so why can't monks?
Answer: Perhaps you have confused with this rule from the Bhikkhu Patimokkha:

Should any bhikkhu report (his own) superior human state to an
unordained person, when it is factual, it is to be confessed. -
Pācittiya 8

This rule is more about attainments (e.g. jhana, stream entry etc.) rather than about psychic powers.
Comment: A related rule at Cv.V.8.2 states that to display psychic powers to lay people is a dukkaṭa. In the origin story leading up to that rule, the Buddha levels strong criticism at such an act: "Just as a woman might expose her vagina for a miserable wooden māsaka coin, so too have you displayed a superior human state, a wonder of psychic power, to lay people for the sake of a miserable wooden bowl." https://www.dhammatalks.org/vinaya/bmc/Section0016.html
Comment: @Dhammadhatu Then this is the best answer to the question that "A related rule at Cv.V.8.2 states that to display psychic powers to lay people is a dukkaṭa".
Comment: Cv.V.8.2 is not sutta. Regards
Answer: DN 11 describes the drawbacks of the miracle of psychic power.
SN 41.4 is related to this topic.
Also, are we certain the Buddha showed his psychic powers to many lay people? Which suttas describe this?
Answer: It's right, good householder, that a Bhikkhu would do something like this - in showing his powers ('let them/him see my power') - isn't given by the Buddha to do so, and is breaking a rule with it. (mentioned as confused here already, but right: Pācittiya 8, as the 'smallest' transgression)
If mention over-worldly powers, yet not having such, it can even touch a crown rule, downfall rule, meaning no more Bhikkhu (yet not if simply over-estimating ones gains). If having, telling, showing, it's a break of the main rules Bhikkus use to cite all 14/15 days.
Although there are many instances found in the Suttas where Bhikkhus deliberately, even on request by lay-people, showed miracles and higher powers (thinking on Ven. Moggallāna, the foremost skilled in it, and praised for it by the Buddha), it seems that the Buddha later formulated this rule, some say maybe correct "he didn't trust his monks in this regard, and sure a corrupt instance (where such was made for gain or favor) gave reason to take care here.
This being one reason: using it not to inspire Faith and cut down pride (like for example the Uruvela-miracles, as maybe the most excessive sample, the Buddha made use of such)
Another, very important: if a monk does not really know the right time for a good use, even if well meant, most people are strong attached to such, would not be much attracted by the Dhamma and seek for such powers, or people able to do, as their refuge, cutting them off from Dhamma.
For the actor of such, there is huge danger to get attached to such skill, likewise mistakenly taking it as refuge.
Such is actually very common, such bad ways, even corrupt and as lies, if thinking on those providing spells, water cleaning, chanting for expelling, ... and goes up till higher powers, aside of what's called 'animal trade'.
It's, how ever, not to blame if Monks use there skills to turn others toward the Dhamma, path, yet not 'showing', letting know... how ever, sensitive (starting usually with Stream-enter) people might get aware.
Since people of this days would not even see miracles as such, would at large doubt all even if confronted, there is also less use of it in a 'god-less' society, under materialists and marxists. So one might need to go for a land of miracles to get such things known, loses doubts in regard of such.
(and as someone here as usual is up speading doubt with strawmans, although knowing it clear by himself, in saying 'we aren't sure that he did, which sutta says...': many, many Suttas, not to speak about Vinaya, commentaries...,  provide that the Sublime Buddha used them often, showed them at proper occasions)
[Note that this 'miracle' isn't given for stacks, exchange, other world-binding trades, but for increase of Saddha and toward vimutti from this wheel]

Was it ever stated that monks can’t show their psychic powers to lay people?


cloud based synchronization architecture

Question: I am thinking of building a cloud base synch solution (something like Dropbox):

What would a robust architecture look like?
What technologies would you need to support different platforms
like windows, mac, linux, and mobile devices?
What efficient synchronization algorithms would you use?

I know a naive architecture/solution would be:

Make a network call to your cloud storage and get the sync folder
tree structure (just metadata info).
Have a filesystem monitor on the client to construct the local sync
folder tree structure (I guess you would use something like lsyncd for
the filesystem monitor?)
Retrieve the sync folder structure from the previous sync. Now you
have 3 folder tree structures on the client. Using these 3 trees can
determine what needs to be done on the local folder and what needs to
be done on the remote folder on the server. For instance, add, delete,
edit, conflict resolution, etc using some kind of pre-determined rules
that is application specific.

This architecture might be sufficient but the devil is in the details. What if the sync folder tree is very large (that is very broad and very deep). Clearly an efficient algorithm to determine diffs would be needed. What if network connection dropped and you didn't get or send the entire tree properly? Also sending only file diffs to reduce network payload, etc..
I am aware these are things I have design for but my question is if this architecture is sufficient and if I should spend my time in the details? How is dropbox designed and what technologies and algorithms do they use to make synching of large folder structures and data size so efficient? Are there any resources/books I can consult on designing something like this?
Thanks in advance.
Answer: A good place to start would be to evaluate the Dropbox API to get ideas about how they designed the Dropbox API. This will likely provide you with ideas on how the techniques could be adapted to your solution. As with any sync solution, the devil is in the details.
-bill


Is wpa_supplicant for android device specific or android version specific?

Question: I recently purchased sony xperia P, but it was not able to detect ad-hoc networks.
After some googling I came to know that android does not support ad-hoc network and I haven't modified the /system/bin/wpa_supplicant file so that it can also show ad-hoc networks. I found a lot of modified files on github but I dont know which one should I use. Is the file wpa_supplicant device specific or it is android version specific?
My android version is 4.0.4 (Sony xperia P), can anyone give me the exact file I can use with my phone.
Answer: Because the wpa_supplicant interact with the network card driver, and the driver is associated with the network card chips. And different network card support different range of of IEEE 802.11 (includes optional and mandatory standards  ) standard, so I guess the wpa_supplicant is specific to the network card chips.While it's possible that a version of wpa_supplicant supports several network driver and network card.
Answer: Modifying wpa_supplicant alone won't be enough to get ad-hoc working in Android (unless you plan to use it via command line and not Settings app). The Settings app and WLAN APIs filter out ad-hoc networks.
Also wpa_supplicant is not tied to particular hardware, but some changes that Android makes to wpa_supplicant are Android version dependant, based on what new feature gets added in new Android version.


Why Will 2012 Mac Pro will work with 2 graphics cards, but not one?

Question: I have a 2012 Mac Pro which must be updated to a Metal compatible card. When I swap the cards, the screen shows nothing and I do not believe it boots. When I put in the original card and the new card however, it works fine and video works from both cards. The problem is, the Mac will not allow a Mojave update with both cards in, and when I remove the original card, I am back to the same issue regardless of which x16 slot I use. What could be the cause of this?
Comment: Which cards do you have and what version of macOS do you currently have installed?
Answer: It appears the problem was that FileVault was turned on. Even without Mojave, some graphics cards without special firmware (although approved by Apple), are unable to render when in FileVault. This is why Apple says to turn it off before moving to Mojave, but they do not mention that it will not work with their new graphics card recommendation. Therefore the order is very important

Turn off FileVault
Install new card
Update to Mojave
Answer: Sounds like the new graphics card does not have a Mac video firmware. That is essential in order for the graphics card to be supported during boot.
After boot, ordinary drivers for the graphics can be loaded and the card starts working letting you use both the new card and the old card.
You must replace the firmware or entire card with one that is supported during boot on a Mac. Whether you can update the firmware depends on the type of card.
Comment: If you don't have a Mac-flashed Metal-capable card you must disable FileVault & any pre-boot requirement such as a firmware password.


How to rotate images saved in Google Drive?

Question: I often upload the images in Gdrive taken from my camera, some images are in different orientation. While viewing an image in drive, how to rotate it, as there is no option provided by Google to rotate the image.
Comment: duplicate: https://webapps.stackexchange.com/questions/52073/how-to-rotate-images-saved-in-google-drive
Answer: Based from this forum, you can't rotate in Google Drive.
However, there is another workaround on how to do this in a native way.
See How to rotate images saved in Google Drive

There is a native way: access Google Photos.
First you need to enable google drive pictures as shown here.
  Please once in that page, read below "View & edit photos stored in
  Google Drive using Google Photos"
Then select the image, and either press shift+r, or in the top right
  corner choose "more options"> Rotate
Edit: Please note, according to this official help page:

If you edit photos in Google Photos, those changes will not show in
    Google Drive.

That means you have to manually move the edited photos in google
  photos to the desired goal location, such as google drive.
Answer: If the uploaded orientation of the photo is wrong due to oversight while uploading, I have found it much easier to orient the photos right in the original and upload again as version two.  It takes much less time than editing individually in a online photo editor


Canon Pixma MP 160 doesn't recognize ink cartridges

Question: My Canon Pixma MP 160 printer/scanner/copier is not recognizing my printer cartridges. The alarm light as well as the black and color light all come on and it's like it's locked up.
Answer: I've had it happen with another pixma where I refill ink. If you use the newer cartridges, with the chips, try cleaning the contacts on the cartridge with alcohol wipes - this happens with dirty contacts


What's wrong with saying "he has his mind in the right mindset?"

Question: My friend says its gramatically incorrect, what do you guys think?
Comment: to have a mind and you have a mindset but having your mind in a mindset is redundancy.
Comment: Hello, Bryan. There are plenty of Google hits for "in the right mindset", but I can't find any where the PP is used to describe '[his] mind'. It's just not used that way. It (and the related ... _into_ ...) is usually used to refer directly to a person (He's not in the right mindset / She got into the right mindset ...). It's more meaningful to say that 'it's unidiomatic' rather than 'it's ungrammatical'. Probably equally serious errors.
Comment: It's *grammatically* correct, but it's not something native speakers would say. Perhaps it's a "malapropism" for *He has his **heart in the right place** / His heart's in the right place*.
Comment: I see,  I'm getting mixed responses from different sources. Thank you guys for clarifying
Comment: It's really simple, the word "mindset" already indicates you're talking about the mind. There's no need to further emphasize this because it will sound weird despite being grammatically correct. I think it could even be considered a pleonasm (a phrase like "cash money", "true fact" or "LAN network")
Comment: @FumbleFingers - I don't see what his heart has to do with it.
Answer: For informal speech, this phrase is okay.  For formal speech, or for something written, such as a persuasive letter or a term paper, it would be better to avoid combining "mind" and "mindset" in close proximity.  The awkwardness would be kind of like writing, "Next, set the new combination on the combination lock."
Oxford's definition of mindset:

The established set of attitudes held by someone.

To be in the right mindset means to have a helpful mental attitude.  For example: if you  go to college thinking it's good enough to show up to class and spend an hour a day on homework, you're not in the right mindset.  Meaning, you aren't looking at the college experience the right way -- you're not thinking about it right.
The simple correction to "he has his mind in the right mindset" would be "he has the right mindset."
Comment: @JasonBassford - Oh jeesh, I guess I wasn't in the right mindset.  Thanks.
Answer: "He has his mind in the right mindset" is not grammatically wrong but it is an awkward sentence.
Either "He is in his right mind" or "He has the right mindset" would work. Putting the two together creates an awkward sentence.
in (one's) right mind

Thinking soundly; rational. Usually used to set up a context in which any sane person would or could do, say, or think a certain thing.
Free Dictionary

mindset

noun
  [usually in singular]
  The established set of attitudes held by someone.
  'the region seems stuck in a medieval mindset'
Lexico
Comment: *"He is in his right mind"* generally means *"he hasn't gone insane,"* not that he is in the right mindset to do some specific task. What the Free Dictionary means is that it's generally used in phrases like *"anybody in their right mind,"* which means *"anybody who isn't batsh!t crazy."*
Comment: @DavidD - We may have to agree to disagree.  I think that the phrase "in one's right mind" has as much relevance to this question as *male pattern baldness* or *mindfulness training* would.  (I.e., none.)  But I do agree with what you said about *mindset*.
Comment: I agree with what you said about mindset, but "in one's right mind" has nothing to do with the question.
Comment: @aparente001 I agree, the question had nothing to do with  "in one's right mind". The question had nothing to do with any definitions at all. It was a grammar question and I offered an answer - the grammar is fine but awkward.

What’s wrong with saying “he has his mind in the right mindset?”


One-sided hypothesis test (error rates)

Question: Can someone please help me understand how the expressions after the equals (marked in red) are arrived at? I don't quite understand the very last one where the c.d.f. $\phi$ comes into the picture. My guess for the first expression is that the term added to $c$ is always going to be negative and hence its added in order to get to the next expression involving a c.d.f.
Answer: They are calculating the power function $\beta(\theta)$ of the test.
Note that $\beta(\theta)$, a function of $\theta$, is the probability of rejecting the null hypothesis $H_0$ under $\theta$.
Formally, for testing $H_0:\theta\in\Theta_0$ vs $H_1:\theta\in\Theta_1$, where $\Theta_0\subset\Theta$ and $\Theta_1\subset \Theta-\Theta_0$, we define the power function of the test as
$$\beta(\theta)=P_{\theta}(\text{reject }H_0)\quad\forall\,\theta\in\Theta$$
Distribution of the sample mean $\overline X_n$ is given by
$$\overline X_n\sim N(\theta,\sigma^2/n)$$
So they standardize $\overline X_n$ after the second equal sign:
\begin{align}
\beta(\theta)&=P_{\theta}\left(\overline X_n>c\sigma/\sqrt{n}+\theta_0\right)
\\&=P_{\theta}\left(\frac{\overline X_n-\theta}{\sigma/\sqrt{n}}>c+\frac{\theta_0-\theta}{\sigma/\sqrt{n}}\right)
\end{align}
Hence the cdf $\Phi$ comes into play after a missing fourth equal sign:
$$\beta(\theta)=1-\Phi\left(c+\frac{\theta_0-\theta}{\sigma/\sqrt{n}}\right)$$
Comment: Thanks. Makes sense. The missing fourth equal sign did put me off.
Answer: For the first $=$, in the numerator of the term on the LHS of the equality, add and subtract $\theta$, rearrange terms to get $\bar{X}_n-\theta-(\theta_0-\theta)$ on the numerator and then move the second part to the RHS. For the second $=$, the LHS of the inequality inside the $P()$ is just a standardized $\bar{X}_n$, so the probability it exceeds the RHS is the probability a standard normal ($Z$) exceeds the RHS. Note that there should be another $=$ sign just before the $1-\Phi()$ term at the end.

One-sided hypothesis test (error rates)