Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #20855 > unrolled thread

single instance

Started byRoedy Green <see_website@mindprod.com.invalid>
First post2013-01-01 12:23 -0800
Last post2013-01-16 15:09 -0800
Articles 20 on this page of 100 — 15 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  single instance Roedy Green <see_website@mindprod.com.invalid> - 2013-01-01 12:23 -0800
    Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-01 16:40 -0500
      Re: single instance Robert Tomsick <robert+usenet@tomsick.net> - 2013-01-03 01:20 -0500
    Re: single instance Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2013-01-03 00:55 -0800
      Re: single instance Knute Johnson <nospam@knutejohnson.com> - 2013-01-03 19:31 -0800
        Re: single instance Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2013-01-03 19:49 -0800
        Re: single instance Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2013-01-03 19:56 -0800
          Re: single instance Twirlip of the Mists <twirlip@killfile.me.now.invalid> - 2013-01-04 12:18 -0500
            Re: single instance Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2013-01-04 10:22 -0800
              Re: single instance Twirlip of the Mists <twirlip@killfile.me.now.invalid> - 2013-01-04 13:44 -0500
                Re: single instance Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2013-01-04 11:03 -0800
                  Re: single instance Twirlip of the Mists <twirlip@killfile.me.now.invalid> - 2013-01-04 14:12 -0500
                Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-05 21:56 -0500
                  Re: single instance Twirlip of the Mists <twirlip@killfile.me.now.invalid> - 2013-01-06 19:22 -0500
                    Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-06 20:23 -0500
                      Re: single instance Twirlip of the Mists <twirlip@killfile.me.now.invalid> - 2013-01-06 20:43 -0500
                        Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-06 20:47 -0500
                          Re: single instance Twirlip of the Mists <twirlip@killfile.me.now.invalid> - 2013-01-06 20:51 -0500
                    Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-06 20:24 -0500
                      Re: single instance Twirlip of the Mists <twirlip@killfile.me.now.invalid> - 2013-01-06 20:46 -0500
                        Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-06 20:58 -0500
                          Re: single instance Twirlip of the Mists <twirlip@killfile.me.now.invalid> - 2013-01-06 21:08 -0500
                            Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-06 21:19 -0500
                              Re: single instance Twirlip of the Mists <twirlip@killfile.me.now.invalid> - 2013-01-06 21:31 -0500
                                Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-06 21:41 -0500
                                  Re: single instance Twirlip of the Mists <twirlip@killfile.me.now.invalid> - 2013-01-06 22:00 -0500
                                    Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-06 22:11 -0500
                                      Re: single instance Twirlip of the Mists <twirlip@killfile.me.now.invalid> - 2013-01-07 00:23 -0500
                                        Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-02-24 18:20 -0500
                                Re: single instance Joshua Cranmer <Pidgeot18@verizon.invalid> - 2013-01-06 21:39 -0600
                                  Re: single instance Twirlip of the Mists <twirlip@killfile.me.now.invalid> - 2013-01-07 00:30 -0500
                        Re: single instance lipska the kat <lipskathekat@yahoo.co.uk> - 2013-01-07 08:53 +0000
                          Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-02-24 18:18 -0500
                            Re: single instance lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-02-25 08:31 +0000
                        Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-02-24 18:17 -0500
                    Re: single instance Lew <lewbloch@gmail.com> - 2013-01-06 17:32 -0800
                      Re: single instance Twirlip of the Mists <twirlip@killfile.me.now.invalid> - 2013-01-06 20:47 -0500
                        Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-06 20:53 -0500
                          Re: single instance Twirlip of the Mists <twirlip@killfile.me.now.invalid> - 2013-01-06 21:01 -0500
            Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-05 21:59 -0500
              Re: single instance Twirlip of the Mists <twirlip@killfile.me.now.invalid> - 2013-01-06 19:34 -0500
                Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-06 20:00 -0500
    Re: single instance Roedy Green <see_website@mindprod.com.invalid> - 2013-01-03 07:12 -0800
      Re: single instance Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2013-01-03 09:56 -0800
        Re: single instance Martin Gregorie <martin@address-in-sig.invalid> - 2013-01-03 21:05 +0000
          Re: single instance Martin Gregorie <martin@address-in-sig.invalid> - 2013-01-03 22:08 +0000
          Re: single instance "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2013-01-05 12:48 +0000
            Re: single instance Martin Gregorie <martin@address-in-sig.invalid> - 2013-01-05 17:43 +0000
              Re: single instance Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2013-01-05 09:49 -0800
              Re: single instance Twirlip of the Mists <twirlip@killfile.me.now.invalid> - 2013-01-05 13:02 -0500
                Re: single instance Martin Gregorie <martin@address-in-sig.invalid> - 2013-01-05 20:29 +0000
                  Re: single instance Roedy Green <see_website@mindprod.com.invalid> - 2013-01-05 19:07 -0800
                  Re: single instance Twirlip of the Mists <twirlip@killfile.me.now.invalid> - 2013-01-06 20:04 -0500
              Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-05 21:40 -0500
      Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-05 22:10 -0500
        Re: single instance Knute Johnson <nospam@knutejohnson.com> - 2013-01-05 19:49 -0800
          Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-05 23:09 -0500
            Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-06 11:00 -0500
              Re: single instance Lew <lewbloch@gmail.com> - 2013-01-06 09:41 -0800
                Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-06 20:41 -0500
          Re: single instance Roedy Green <see_website@mindprod.com.invalid> - 2013-01-15 22:51 -0800
            Re: single instance Roedy Green <see_website@mindprod.com.invalid> - 2013-01-15 23:12 -0800
              Re: single instance Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2013-01-15 23:49 -0800
            Re: single instance Roedy Green <see_website@mindprod.com.invalid> - 2013-01-15 23:16 -0800
              Re: single instance Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2013-01-15 23:52 -0800
              Re: single instance Knute Johnson <nospam@knutejohnson.com> - 2013-01-16 08:46 -0800
                Re: single instance Roedy Green <see_website@mindprod.com.invalid> - 2013-01-16 10:46 -0800
                  Re: single instance markspace <markspace@nospam.nospam> - 2013-01-16 13:01 -0800
                  Re: single instance Knute Johnson <nospam@knutejohnson.com> - 2013-01-16 17:10 -0800
            Re: single instance Roedy Green <see_website@mindprod.com.invalid> - 2013-01-15 23:50 -0800
              Re: single instance Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2013-01-16 00:13 -0800
                Re: single instance Roedy Green <see_website@mindprod.com.invalid> - 2013-01-16 02:48 -0800
                  Re: single instance Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2013-01-16 07:28 -0800
                    Re: single instance Roedy Green <see_website@mindprod.com.invalid> - 2013-01-16 10:46 -0800
                      Re: single instance Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2013-01-16 16:53 -0800
                        Re: single instance Roedy Green <see_website@mindprod.com.invalid> - 2013-01-16 23:44 -0800
                          Re: single instance Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2013-01-17 07:03 -0800
                            Re: single instance Roedy Green <see_website@mindprod.com.invalid> - 2013-01-17 14:25 -0800
                              Re: single instance Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2013-01-17 16:31 -0800
                                Re: single instance Roedy Green <see_website@mindprod.com.invalid> - 2013-01-17 22:11 -0800
                                Re: single instance Roedy Green <see_website@mindprod.com.invalid> - 2013-01-17 22:36 -0800
                Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-16 13:34 -0500
            Re: single instance Knute Johnson <nospam@knutejohnson.com> - 2013-01-16 08:45 -0800
            Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-16 13:29 -0500
              Re: single instance Knute Johnson <nospam@knutejohnson.com> - 2013-01-16 17:14 -0800
                Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-16 20:20 -0500
                Re: single instance Roedy Green <see_website@mindprod.com.invalid> - 2013-01-16 23:52 -0800
                Re: single instance Roedy Green <see_website@mindprod.com.invalid> - 2013-01-17 01:44 -0800
          Re: single instance Roedy Green <see_website@mindprod.com.invalid> - 2013-01-18 01:47 -0800
            Re: single instance Knute Johnson <nospam@knutejohnson.com> - 2013-01-18 20:50 -0800
              Re: single instance Roedy Green <see_website@mindprod.com.invalid> - 2013-01-20 00:53 -0800
                Re: single instance Lew <lewbloch@gmail.com> - 2013-01-20 12:00 -0800
                  Re: single instance Knute Johnson <nospam@knutejohnson.com> - 2013-01-20 13:33 -0800
                    Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-02-24 18:12 -0500
                Re: single instance Arne Vajhøj <arne@vajhoej.dk> - 2013-01-20 21:33 -0500
        Re: single instance "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2013-01-06 13:34 +0000
    Re: single instance Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-01-04 10:26 -0800
      Re: single instance Twirlip of the Mists <twirlip@killfile.me.now.invalid> - 2013-01-04 14:04 -0500
    Re: single instance stledger@lanl.gov - 2013-01-16 14:51 -0800
      Re: single instance stledger@lanl.gov - 2013-01-16 15:09 -0800

Page 5 of 5 — ← Prev page 1 2 3 4 [5]


#21511

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-01-17 22:36 -0800
Message-ID<3hqhf89c01asa7l606pfgb6g9jh4kj845j@4ax.com>
In reply to#21503
On Thu, 17 Jan 2013 16:31:14 -0800, Peter Duniho
<NpOeStPeAdM@NnOwSlPiAnMk.com> wrote, quoted or indirectly quoted
someone who said :

>It would not be possible in the way you seem to envision it.

What we have now works because you need only one socket number per
incoming service.  All callers share the same port. So we can have 64K
incoming services, not enough for all possible services but enough for
the number currently active.

We need one socket number per outgoing connection. They can overlap
incoming service numbers.  So we can have 64K outgoing connections,
plenty, for now.

Lets say I set up a port assigning service. When you a need to assign
a port for an incoming service that does not have a fixed assignment,
you send me, Roedy, an email, along with a UUID for the service.  I
have a list of the ports you have already assigned.  I assign a free
one, and tell you to use it for your service.  I make a record of
that.  When someone wants to connect to you service, then send me an
email with the UUID and the name of the machine they want to connect
to.  I look the port number up in my database and tell him which port
to use.

Let's say the service provider shuts the service down and restarts it.
All works just as if would now with a well-known port.

Let's say the service provider wants never again to provide the
service. They could send me a email tell my that service UUID X is
freeing up its socket for reuse.  I will do that, but only after I
have used all the other free sockets.

We need some sort of system to ensure the callers recheck the socket
with me to check every once in a while if the service  has been
deleted/moved (a rare instance).  If they don't notice a deletion, no
harm done.  If they don't notice a move, serious harm is done.

My imaginary email service would be a demon running on each server
handing just dynamic socket assignments for that server.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
The first 90% of the code accounts for the first 90% of the development time.
The remaining 10% of the code accounts for the other 90% of the development 
time. 
~ Tom Cargill  Ninety-ninety Law 

[toc] | [prev] | [next] | [standalone]


#21441

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-01-16 13:34 -0500
Message-ID<50f6f2c8$0$285$14726298@news.sunsite.dk>
In reply to#21429
On 1/16/2013 3:13 AM, Peter Duniho wrote:
> On Tue, 15 Jan 2013 23:50:05 -0800, Roedy Green wrote:
>> You ask the OS , please assign me a free port. Here is a UUID. If
>> anyone asks for a port presenting the same UUID, please give him the
>> same one, no matter if he is calling from within the machine or
>> without.  With such a scheme, you would not need reserved ports
>> (except for the port assigning port), and you could not have
>> collisions.  Internally arrays of sockets could be dense.  You can
>> then hard code in UUIDs into apps. Users don't need to worry about
>> collisions. Perhaps in IPV8.
>
> Since the problem is solveable using the existing mechanisms, I don't see a
> compelling reason the above feature should be included in future versions
> of IP.
>
> Also, I'm no expert, but I'm not sure that any of the IP specifications
> concern themselves with ports at all.  That might be at the TCP and UDP
> protocol levels, independent of the IP stuff.

Port numbers are indeed in the TCP and UDP header not in the IP header.

Arne

[toc] | [prev] | [next] | [standalone]


#21438

FromKnute Johnson <nospam@knutejohnson.com>
Date2013-01-16 08:45 -0800
Message-ID<kd6lg1$go$1@dont-email.me>
In reply to#21423
On 1/15/2013 10:51 PM, Roedy Green wrote:
> On Sat, 05 Jan 2013 19:49:08 -0800, Knute Johnson
> <nospam@knutejohnson.com> wrote, quoted or indirectly quoted someone
> who said :
>
>>
>> The risk is, that in Windows anyway, where the system clock granularity
>> is 17ms it is possible to start two copies of a program with a batch
>
> I have been studying Knute's code and Peter's commentary hoping to
> come up with a canned package for ensuring single Instance.
>
> Using system.nanotime aught to help avoid granularity trouble.

The problem with nanotime is that it is not guaranteed to use the same 
starting point on different invocations of the VM.  The granularity of 
currentTimeMillis in Windows is large but not on Linux.

> I am puzzled about   "227.228.229.230" the IP used. Where did that
> value come from?  Can this be made to work with IPV6?

Arbitrary choice by me of a valid Multicast IP.  I don't know any reason 
why it would not work with IPV6 but I've never seen any discussion of it.


-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#21440

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-01-16 13:29 -0500
Message-ID<50f6f198$0$285$14726298@news.sunsite.dk>
In reply to#21423
On 1/16/2013 1:51 AM, Roedy Green wrote:
> On Sat, 05 Jan 2013 19:49:08 -0800, Knute Johnson
> <nospam@knutejohnson.com> wrote, quoted or indirectly quoted someone
> who said :
>> The risk is, that in Windows anyway, where the system clock granularity
>> is 17ms it is possible to start two copies of a program with a batch
>
> I have been studying Knute's code and Peter's commentary hoping to
> come up with a canned package for ensuring single Instance.
>
> Using system.nanotime aught to help avoid granularity trouble.

1) It uses a smaller unit, but does not guarantee a smaller
    granularity.

2) It is only guaranteed to work within the same program as
    the base is not fixed.

3) No matter how small a granularity, then it still just reduces
    the risk of concurrency problems but does not eliminate the risk.

Arne



[toc] | [prev] | [next] | [standalone]


#21459

FromKnute Johnson <nospam@knutejohnson.com>
Date2013-01-16 17:14 -0800
Message-ID<kd7j97$3i7$1@dont-email.me>
In reply to#21440
On 1/16/2013 10:29 AM, Arne Vajhøj wrote:
> On 1/16/2013 1:51 AM, Roedy Green wrote:
>> On Sat, 05 Jan 2013 19:49:08 -0800, Knute Johnson
>> <nospam@knutejohnson.com> wrote, quoted or indirectly quoted someone
>> who said :
>>> The risk is, that in Windows anyway, where the system clock granularity
>>> is 17ms it is possible to start two copies of a program with a batch
>>
>> I have been studying Knute's code and Peter's commentary hoping to
>> come up with a canned package for ensuring single Instance.
>>
>> Using system.nanotime aught to help avoid granularity trouble.
>
> 1) It uses a smaller unit, but does not guarantee a smaller
>     granularity.

At least on my version of Windows, it was much finer.

> 2) It is only guaranteed to work within the same program as
>     the base is not fixed.
>
> 3) No matter how small a granularity, then it still just reduces
>     the risk of concurrency problems but does not eliminate the risk.
>
> Arne

It doesn't have to be perfect just good enough to stop what you want to 
stop.  For the application I have, I'm not worried about somebody 
starting two copies with a batch file only by clicking on an icon or 
typing into a command line.  For that it will be more than adequate.

-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#21461

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-01-16 20:20 -0500
Message-ID<50f751de$0$294$14726298@news.sunsite.dk>
In reply to#21459
On 1/16/2013 8:14 PM, Knute Johnson wrote:
> On 1/16/2013 10:29 AM, Arne Vajhøj wrote:
>> On 1/16/2013 1:51 AM, Roedy Green wrote:
>>> On Sat, 05 Jan 2013 19:49:08 -0800, Knute Johnson
>>> <nospam@knutejohnson.com> wrote, quoted or indirectly quoted someone
>>> who said :
>>>> The risk is, that in Windows anyway, where the system clock granularity
>>>> is 17ms it is possible to start two copies of a program with a batch
>>>
>>> I have been studying Knute's code and Peter's commentary hoping to
>>> come up with a canned package for ensuring single Instance.
>>>
>>> Using system.nanotime aught to help avoid granularity trouble.
>>
>> 1) It uses a smaller unit, but does not guarantee a smaller
>>     granularity.
>
> At least on my version of Windows, it was much finer.
>
>> 2) It is only guaranteed to work within the same program as
>>     the base is not fixed.
>>
>> 3) No matter how small a granularity, then it still just reduces
>>     the risk of concurrency problems but does not eliminate the risk.
>
> It doesn't have to be perfect just good enough to stop what you want to
> stop.  For the application I have, I'm not worried about somebody
> starting two copies with a batch file only by clicking on an icon or
> typing into a command line.  For that it will be more than adequate.

Sure.

But it is not a "canned package for ensuring single instance".

Arne


[toc] | [prev] | [next] | [standalone]


#21470

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-01-16 23:52 -0800
Message-ID<0bbff81phl4vtc8qqd7lenqtrebr78kvpm@4ax.com>
In reply to#21459
On Wed, 16 Jan 2013 17:14:14 -0800, Knute Johnson
<nospam@knutejohnson.com> wrote, quoted or indirectly quoted someone
who said :

>At least on my version of Windows, it was much finer.

Timer Resolution
Resolution	Platform
55 ms	              Windows 95/98
10 ms	              Windows NT, Windows 2000,
                             Windows XP single processor
15.625 ms	Windows NT, Windows 2000, Windows XP dual processor
1 or 15.625 ms	Vista. 1 only if you sleep between calls to
                             currentTimeMillis. I kid you not.
?	               Windows 7.
1 ms	               Mac OS X
-- 
Roedy Green Canadian Mind Products http://mindprod.com
The first 90% of the code accounts for the first 90% of the development time.
The remaining 10% of the code accounts for the other 90% of the development 
time. 
~ Tom Cargill  Ninety-ninety Law 

[toc] | [prev] | [next] | [standalone]


#21472

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-01-17 01:44 -0800
Message-ID<5uhff89tr28it11vbtpoitsn6lkjk0cbtc@4ax.com>
In reply to#21459
On Wed, 16 Jan 2013 17:14:14 -0800, Knute Johnson
<nospam@knutejohnson.com> wrote, quoted or indirectly quoted someone
who said :

>
>At least on my version of Windows, it was much finer.

I was able to determine it was 300 ns or less, but a ns is a pretty
short time.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
The first 90% of the code accounts for the first 90% of the development time.
The remaining 10% of the code accounts for the other 90% of the development 
time. 
~ Tom Cargill  Ninety-ninety Law 

[toc] | [prev] | [next] | [standalone]


#21514

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-01-18 01:47 -0800
Message-ID<v96if8h6c8e39hr4vodej7kp27kqnbbdgu@4ax.com>
In reply to#21010
On Sat, 05 Jan 2013 19:49:08 -0800, Knute Johnson
<nospam@knutejohnson.com> wrote, quoted or indirectly quoted someone
who said :

>> // if we are seeing our own packet, do nothing
>>                  if ( theirTime == myTime )
>>                      {
>> </quote>
>>
>> Who can see a concurrency problem in that logic?

I have been working on polishing Knute's code. Mostly I have been
adding informal comments and renaming to help myself understand how it
works.

I have not run it yet, but it is getting close to a test.

see
https://wush.net/svn/mindprod/com/mindprod/singleinstance/SingleInstance.java

I have added UUIDs to break the tie for equal start times.
I have added app ids so different apps can share the same port.

I have added the ability avoid several different apps.
I have added the ability to permit two apps to run, so long as they
run on different files.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
The first 90% of the code accounts for the first 90% of the development time.
The remaining 10% of the code accounts for the other 90% of the development 
time. 
~ Tom Cargill  Ninety-ninety Law 

[toc] | [prev] | [next] | [standalone]


#21567

FromKnute Johnson <nospam@knutejohnson.com>
Date2013-01-18 20:50 -0800
Message-ID<kdd8mt$pjs$1@dont-email.me>
In reply to#21514
On 1/18/2013 1:47 AM, Roedy Green wrote:
> I have been working on polishing Knute's code. Mostly I have been
> adding informal comments and renaming to help myself understand how it
> works.
>
> I have not run it yet, but it is getting close to a test.
>
> see
> https://wush.net/svn/mindprod/com/mindprod/singleinstance/SingleInstance.java
>
> I have added UUIDs to break the tie for equal start times.
> I have added app ids so different apps can share the same port.
>
> I have added the ability avoid several different apps.
> I have added the ability to permit two apps to run, so long as they
> run on different files.
>

Below is what I ended up with.  I'm curious about the UUID.  How do you 
create a time based UUID?  And does it have less granularity that 
currentTimeMillis?

package com.knutejohnson.classes;

import java.awt.*;
import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.nio.charset.*;
import java.util.*;
import javax.swing.*;

public class Exclusive implements Runnable {
     private static final int port = 35798;
     private static final String addr = "228.237.246.255";

     private final long myTime;
     private final String name;
     private final InetAddress group;
     private final MulticastSocket socket;
     private final String token;

     private volatile boolean runFlag;
     private volatile Thread thread;

     public Exclusive(String name) throws IOException {
         myTime = System.currentTimeMillis();

         this.name = name;
         if (name.indexOf(",") >= 0)
             throw new IllegalArgumentException(
              "Comma character not allowed in name");

         group = InetAddress.getByName(addr);
         socket = new MulticastSocket(port);
         socket.joinGroup(group);

         token = String.format("%s,%d",name,myTime);
     }

     public void start() throws IOException {
         if (thread == null || !thread.isAlive()) {
             runFlag = true;
             thread = new Thread(this);
             thread.start();

             sendToken();
         }
     }

     public void sendToken() throws IOException {
         byte[] buf = token.getBytes(StandardCharsets.US_ASCII);
         DatagramPacket dp = new DatagramPacket(buf,buf.length,group,port);
         socket.send(dp);
     }

     @Override public void run() {
         while (runFlag) {
             try {
                 // receive their time
                 byte[] buf = new byte[64];
                 DatagramPacket dp = new DatagramPacket(buf,buf.length);
                 socket.receive(dp);
                 String recStr = new String(dp.getData(),dp.getOffset(),
                  dp.getLength(),StandardCharsets.US_ASCII);
                 String[] arr = recStr.split(",");
                 // if names don't match there is nothing to do
                 if (!name.equals(arr[0]))
                     continue;
                 long theirTime = Long.parseLong(arr[1]);
                 // if we are seeing our own packet, do nothing
                 if (theirTime == myTime) {
                 // if their time is before my time, we need to shut down
                 } else if (theirTime < myTime) {
                     stop();
                     // can't use invokeLater()
                     try {
                         EventQueue.invokeAndWait(new Runnable() {
                             public void run() {
                                 JOptionPane.showMessageDialog(null,
                               "Another Copy of this Program is Already 
Running",
                                  "Start 
Inhibited",JOptionPane.ERROR_MESSAGE);
                             }
                         });
                     } catch (InterruptedException|
                      InvocationTargetException ex) {
                         ex.printStackTrace();
                     }
                     shutdown();
                 // if their time is after my time, send out my time
                 } else if (theirTime > myTime) {
                     sendToken();
                 }
             } catch (IOException|NumberFormatException ex) {
                 ex.printStackTrace();
                 stop();
             }
         }
     }

     private void stop() {
         if (thread != null && thread.isAlive()) {
             runFlag = false;
             if (socket != null)
                 socket.close();
         }

         // signal the waitFor() method to stop waiting
         synchronized (this) {
             notify();
         }
     }

     // wait for up to two seconds to see if any other copy responds
     //  returns true if no other copy is running.
     public synchronized boolean waitFor() throws InterruptedException {
         wait(2000);

         return runFlag;
     }

     public void shutdown() {
         System.exit(0);
     }

     public static void main(String[] args) {
         try {
             Exclusive e = new Exclusive("Test");
             e.start();
             if (e.waitFor())
                 System.out.println("no other copy running!");
             else
                 System.out.println("another copy is running");
         } catch (IOException|InterruptedException ex) {
             // probably don't want to start if you get an exception either
             ex.printStackTrace();
         }
     }
}



-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#21581

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-01-20 00:53 -0800
Message-ID<2sbnf8lu7vidolv11m33tcsmuh4olkt21h@4ax.com>
In reply to#21567
On Fri, 18 Jan 2013 20:50:37 -0800, Knute Johnson
<nospam@knutejohnson.com> wrote, quoted or indirectly quoted someone
who said :

>
>Below is what I ended up with.  I'm curious about the UUID.  How do you 
>create a time based UUID?  And does it have less granularity that 
>currentTimeMillis?

there are specified formulas for creating UUIDs. I think one way of
rapidly generating them would be to use System.currentTimeMillis for
the high 64 bits and System.nanotime for the lower 64 bits, but I
suspect that would not be considered kosher.

Another would be to use SecureRandom to generate 16 bytes, though that
probably is not as unique as you might hope.


a UUID is not just 128 bits. It has embedded fields that tell you how
it was generated.


-- 
Roedy Green Canadian Mind Products http://mindprod.com
The first 90% of the code accounts for the first 90% of the development time.
The remaining 10% of the code accounts for the other 90% of the development 
time. 
~ Tom Cargill  Ninety-ninety Law 

[toc] | [prev] | [next] | [standalone]


#21590

FromLew <lewbloch@gmail.com>
Date2013-01-20 12:00 -0800
Message-ID<4735bb8e-f2d8-4de9-9d5f-cd62d4c77dff@googlegroups.com>
In reply to#21581
Roedy Green wrote:
> Knute Johnson wrote, quoted or indirectly quoted someone who said :
>> Below is what I ended up with.  I'm curious about the UUID.  How do you 
>> create a time based UUID?  And does it have less granularity that 
>> currentTimeMillis?
> 
> there are specified formulas for creating UUIDs. I think one way of
> rapidly generating them would be to use System.currentTimeMillis for
> the high 64 bits and System.nanotime for the lower 64 bits, but I
> suspect that would not be considered kosher.
> 
> Another would be to use SecureRandom to generate 16 bytes, though that
> probably is not as unique as you might hope.
> 
> a UUID is not just 128 bits. It has embedded fields that tell you how
> it was generated.

http://docs.oracle.com/javase/7/docs/api/java/util/UUID.html
«The layout of a variant 2 (Leach-Salz) UUID is as follows: The most significant long consists of the following unsigned fields:

 0xFFFFFFFF00000000 time_low
 0x00000000FFFF0000 time_mid
 0x000000000000F000 version
 0x0000000000000FFF time_hi
...

«For more information including algorithms used to create UUIDs, see RFC 4122: A Universally Unique IDentifier (UUID) URN Namespace, 
http://www.ietf.org/rfc/rfc4122.txt
section 4.2 "Algorithms for Creating a Time-Based UUID".»

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#21591

FromKnute Johnson <nospam@knutejohnson.com>
Date2013-01-20 13:33 -0800
Message-ID<kdhnrj$mu8$1@dont-email.me>
In reply to#21590
On 1/20/2013 12:00 PM, Lew wrote:
> Roedy Green wrote:
>> Knute Johnson wrote, quoted or indirectly quoted someone who said :
>>> Below is what I ended up with.  I'm curious about the UUID.  How do you
>>> create a time based UUID?  And does it have less granularity that
>>> currentTimeMillis?
>>
>> there are specified formulas for creating UUIDs. I think one way of
>> rapidly generating them would be to use System.currentTimeMillis for
>> the high 64 bits and System.nanotime for the lower 64 bits, but I
>> suspect that would not be considered kosher.
>>
>> Another would be to use SecureRandom to generate 16 bytes, though that
>> probably is not as unique as you might hope.
>>
>> a UUID is not just 128 bits. It has embedded fields that tell you how
>> it was generated.
>
> http://docs.oracle.com/javase/7/docs/api/java/util/UUID.html
> «The layout of a variant 2 (Leach-Salz) UUID is as follows: The most significant long consists of the following unsigned fields:
>
>   0xFFFFFFFF00000000 time_low
>   0x00000000FFFF0000 time_mid
>   0x000000000000F000 version
>   0x0000000000000FFF time_hi
> ...
>
> «For more information including algorithms used to create UUIDs, see RFC 4122: A Universally Unique IDentifier (UUID) URN Namespace,
> http://www.ietf.org/rfc/rfc4122.txt
> section 4.2 "Algorithms for Creating a Time-Based UUID".»
>

After reading the RFC, using an UUID doesn't sound too practical for the 
purposes we have been discussing.  It also seems clear that with the 
slow clock on a Windows system that one would need a UUID generator that 
could be accessed by all interested parties.  If that generator were 
give two requests in short order, it would have to hold up issuing a new 
UUID until the clock had sequenced to the next time.

-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#22493

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-02-24 18:12 -0500
Message-ID<512a9e63$0$287$14726298@news.sunsite.dk>
In reply to#21591
On 1/20/2013 4:33 PM, Knute Johnson wrote:
> On 1/20/2013 12:00 PM, Lew wrote:
>> Roedy Green wrote:
>>> Knute Johnson wrote, quoted or indirectly quoted someone who said :
>>>> Below is what I ended up with.  I'm curious about the UUID.  How do you
>>>> create a time based UUID?  And does it have less granularity that
>>>> currentTimeMillis?
>>>
>>> there are specified formulas for creating UUIDs. I think one way of
>>> rapidly generating them would be to use System.currentTimeMillis for
>>> the high 64 bits and System.nanotime for the lower 64 bits, but I
>>> suspect that would not be considered kosher.
>>>
>>> Another would be to use SecureRandom to generate 16 bytes, though that
>>> probably is not as unique as you might hope.
>>>
>>> a UUID is not just 128 bits. It has embedded fields that tell you how
>>> it was generated.
>>
>> http://docs.oracle.com/javase/7/docs/api/java/util/UUID.html
>> «The layout of a variant 2 (Leach-Salz) UUID is as follows: The most
>> significant long consists of the following unsigned fields:
>>
>>   0xFFFFFFFF00000000 time_low
>>   0x00000000FFFF0000 time_mid
>>   0x000000000000F000 version
>>   0x0000000000000FFF time_hi
>> ...
>>
>> «For more information including algorithms used to create UUIDs, see
>> RFC 4122: A Universally Unique IDentifier (UUID) URN Namespace,
>> http://www.ietf.org/rfc/rfc4122.txt
>> section 4.2 "Algorithms for Creating a Time-Based UUID".»
>>
>
> After reading the RFC, using an UUID doesn't sound too practical for the
> purposes we have been discussing.  It also seems clear that with the
> slow clock on a Windows system that one would need a UUID generator that
> could be accessed by all interested parties.  If that generator were
> give two requests in short order, it would have to hold up issuing a new
> UUID until the clock had sequenced to the next time.


Note that randomUUID() does not return type 2 UUID.

Arne

[toc] | [prev] | [next] | [standalone]


#21592

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-01-20 21:33 -0500
Message-ID<50fca90f$0$287$14726298@news.sunsite.dk>
In reply to#21581
On 1/20/2013 3:53 AM, Roedy Green wrote:
> On Fri, 18 Jan 2013 20:50:37 -0800, Knute Johnson
> <nospam@knutejohnson.com> wrote, quoted or indirectly quoted someone
> who said :
>> Below is what I ended up with.  I'm curious about the UUID.  How do you
>> create a time based UUID?  And does it have less granularity that
>> currentTimeMillis?
>
> there are specified formulas for creating UUIDs. I think one way of
> rapidly generating them would be to use System.currentTimeMillis for
> the high 64 bits and System.nanotime for the lower 64 bits, but I
> suspect that would not be considered kosher.

No.

The two parts are related.

And it does not do much to protect against two doing it at the
same time.

> Another would be to use SecureRandom to generate 16 bytes, though that
> probably is not as unique as you might hope.

SecureRandom is probably a lot better.

To be secure the chance of getting duplicate values
if two runs it at the same time should be purely random.

But it can still not guarantee uniqueness.

Arne

[toc] | [prev] | [next] | [standalone]


#21023

From"Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org>
Date2013-01-06 13:34 +0000
Message-ID<Yr-dnTSQ2blZ4HTNnZ2dnUVZ7oWdnZ2d@bt.com>
In reply to#21009
Arne Vajhøj wrote:

> Who can see a concurrency problem in that logic?

There is clearly no way to avoid concurrency problems without either the 
cooperation of the OS, or the cooperation of some always-running service. 
Since Java has no way to create always-running services (i.e. any such solution 
is necessarily OS-dependent), it seems better to let the OS handle it for you. 
The only OS services (that I know of) that can be used for this, and which are 
also available despite Java's least-common-denominator philosophy, are network 
related.

To me the approach of opening a server socket at a fixed (but configurable) 
port number seems by far the simplest approach.  Also things built over that, 
such as setting up a small RMI server, might make sense (e.g. to simplify the 
logic of passing over the invocation arguments to the previously running 
process).

If you can live with (partial) OS-dependence then you might also try using 
java.nio.channels.FileChannel.lock() or tryLock().

    -- chris 

[toc] | [prev] | [next] | [standalone]


#20946

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2013-01-04 10:26 -0800
Message-ID<_8FFs.54988$LS5.54894@newsfe10.iad>
In reply to#20855
On 1/1/13 12:23 PM, Roedy Green wrote:
> What is the best way to ensure only a single instance of a Java
> program is running.
>
> I have used indicator files, but they can get screwed up if the user
> kills the program without going through the standard shutdown.
>
> Ideally, net new instance would just join the one already running.
> This is pure GUI, so I am not worried about adding new command line
> parms.
>
A common approach I've seen is to use an indicator file, which contains 
the PID of the main process.  That way you can check if that PID is 
still active.  This of course isn't platform independent.  You could 
combine the idea with others in this thread though.  Check the file, 
which contains a TCP port number, connect to that port number to check 
if the process is still alive.

Just a thought.

I might also have my program "watch" that file, and if it disappears or 
becomes modified, then shut down because I know the user is trying 
something they shouldn't.

[toc] | [prev] | [next] | [standalone]


#20950

FromTwirlip of the Mists <twirlip@killfile.me.now.invalid>
Date2013-01-04 14:04 -0500
Message-ID<kc793t$ivk$1@news.mixmin.net>
In reply to#20946
On Fri, 04 Jan 2013 10:26:01 -0800, Daniel Pitts wrote:

> I might also have my program "watch" that file, and if it disappears or 
> becomes modified, then shut down because I know the user is trying 
> something they shouldn't.

I'm HIGHLY dubious of nannying your users to that extent. Unless it's very
dangerous, in some sense, to allow multi-instances (data-destroying race
conditions in important files in the install directory?), if the user takes
specific and knowledgeable action to circumvent the default behavior of
only having a single instance, the user should probably be deferred to and
the perceived problem solved in other ways (e.g. using a lightweight but
properly ACID database instead of just a file, or locking the file and
requiring separate concurrent instances use separate files or even distinct
installs altogether).

I know that if I deliberately circumvent something like that, I do so at my
own risk and buggy, even data-destroying behavior is theoretically possible
among the consequences (so I should either not do so or back up the
program's data files first).

I also know that there have been many occasions when I've been frustrated
by a program "straight-jacketing" me into being unable to do something the
programmer thought unwise or even just unnecessary, but where the
programmer clearly hadn't anticipated all the uses to which the program
might be put, especially by someone that's a hacker and not just a garden
variety end-user.

Programs are the user's tools and, as such, should empower, not limit, the
user. Whereas it can make plenty of sense to include safety features
designed to make it difficult for the user to accidentally shoot himself in
the foot, cut off his finger in the saw, or shoot his co-worker from across
the room with the nailgun, it is rarely if ever worthwhile to additionally
try to design in a "cop-in-the-box" intended to catch and "punish"
deliberate attempts to circumvent such features. If I purposely circumvent
the safety on a nailgun, I do so at my own risk and probably because a
burglar broke into the shop and I need a makeshift weapon for self-defense
in a hurry, rather than because I'm an irresponsible idiot, and if I *am*
an irresponsible idiot, I'll probably find some other way to shoot myself
or my co-worker no matter how idiot-proof you try to make the nailgun.

Disclaiming all warranty and liability in the event of such tampering is
the furthest it's generally reasonable to take matters -- and, of course,
software makers are already in the habit of blanket disclaiming ALL
liability, even for actual defects in workmanship!

So I'd suggest that instead of monitoring the file for deletion or
tampering you just name it something cutesy like "DELETING ME VOIDS
WARRANTY AND MAY CAUSE MALFUNCTION.txt". :)

Nah, probably not even that, since if the program hangs and has to be
force-quit, then won't restart properly because of a stale lockfile, your
support forums will end up chock full of long threads going

Q: It hung, I end-tasked it, and it wont restart!
A: Delete the lockfile and try restarting it again.
Q: What's the "lockfile"?
A: It's the file called "DELETING ME VOIDS WARRANTY..."
Q: But I don't want to void my warranty and maybe cause even more bugs!
A: The name's a joke. There's no warranty anyway and deleting it won't
   cause a malfunction if the program isn't currently running.
Q: Are you sure?
A: Yes.
Q: Are you really, REALLY sure?
A: Yes!
Q: It won't void the warranty on my *computer itself* will it?
A: It didn't come with the computer, so, no, it can't.
Q: Hmm. It still doesn't seem like a good idea. I think I'll get a second
   opinion. <starts new thread on the same topic>
A: <bangs head against wall>

-- 
Hexapodia is the key insight.

[toc] | [prev] | [next] | [standalone]


#21451

Fromstledger@lanl.gov
Date2013-01-16 14:51 -0800
Message-ID<a3f89eb0-abfb-4256-a81e-6d48cbca3830@googlegroups.com>
In reply to#20855
On Tuesday, January 1, 2013 1:23:45 PM UTC-7, Roedy Green wrote:
> What is the best way to ensure only a single instance of a Java program is running. I have used indicator files, but they can get screwed up if the user kills the program without going through the standard shutdown. Ideally, net new instance would just join the one already running. This is pure GUI, so I am not worried about adding new command line parms. -- Roedy Green Canadian Mind Products http://mindprod.com Students who hire or con others to do their homework are as foolish as couch potatoes who hire others to go to the gym for them.

I worked on a project with a requirement to run only one instance of our software at a time. We had a configuration file that had initialization values for the software. If the file was missing, then the code had default values. The code was running on one computer. Our code worked like this:

try{
Socket sock = new Socket();
sock.bind(new InetSocketAddress(int value read from configuration file or default value));
} catch (java.net.BindException)
{ tell user they are trying to start more than one instance and shutdown this copy after user acknowledges }

It's simple and seemed to always work. We only had to worry about a user double clicking the application. We did not worry about a batch file trying to start more than one instance.  Obviously, you could start one instance, delete the configuration file, then start the second instance.  We judged this to be a very unlikely possibility given our users, so we didn't worry about it. The software backed up its data on shutdown, and at regular intervals, and kept several backup files going back over the last week, so it was possible to restore the data to a given time over the past week of operation. YMMV.

John

[toc] | [prev] | [next] | [standalone]


#21452

Fromstledger@lanl.gov
Date2013-01-16 15:09 -0800
Message-ID<f361b210-2be4-4bac-a32b-3fddb2563ff5@googlegroups.com>
In reply to#21451
On Wednesday, January 16, 2013 3:51:57 PM UTC-7, stle...@lanl.gov wrote:
> On Tuesday, January 1, 2013 1:23:45 PM UTC-7, Roedy Green wrote: > What is the best way to ensure only a single instance of a Java program is running. I have used indicator files, but they can get screwed up if the user kills the program without going through the standard shutdown. Ideally, net new instance would just join the one already running. This is pure GUI, so I am not worried about adding new command line parms. -- Roedy Green Canadian Mind Products http://mindprod.com Students who hire or con others to do their homework are as foolish as couch potatoes who hire others to go to the gym for them. I worked on a project with a requirement to run only one instance of our software at a time. We had a configuration file that had initialization values for the software. If the file was missing, then the code had default values. The code was running on one computer. Our code worked like this: try{ Socket sock = new Socket(); sock.bind(new InetSocketAddress(int value read from configuration file or default value)); } catch (java.net.BindException) { tell user they are trying to start more than one instance and shutdown this copy after user acknowledges } It's simple and seemed to always work. We only had to worry about a user double clicking the application. We did not worry about a batch file trying to start more than one instance. Obviously, you could start one instance, delete the configuration file, then start the second instance. We judged this to be a very unlikely possibility given our users, so we didn't worry about it. The software backed up its data on shutdown, and at regular intervals, and kept several backup files going back over the last week, so it was possible to restore the data to a given time over the past week of operation. YMMV. John

Oops! Need to catch an IOException, not a BindException!!

[toc] | [prev] | [standalone]


Page 5 of 5 — ← Prev page 1 2 3 4 [5]

Back to top | Article view | comp.lang.java.programmer


csiph-web