Monitor UDP broadcasts from 4D Server

Keisuke Miyako (5/30/14 3:08AM)
Keisuke Miyako (5/30/14 3:48AM)


Keisuke Miyako (5/30/14 3:08 AM)

as explained in the 4D Internet Commands documentation,
it is possible to listen to 4D Server publishing its name over the
network.

http://doc.4d.com/4D-Internet-Commands-12.1/IC-UDP/UDP-Commands-Overview.3
00-505949.en.html

which is fine if you want 4D to watch if a 4D Server is running,
but you might argue that actually it makes more sense that an
application /other than/ 4D should do that job.

so here I posted a small command-line program that does the same thing.

https://github.com/miyako/4d-utility-ls4d

it comes with an AppleScript example.
if you prefer to write a shell script then by all means go ahead.

some context:

a built 4D Remote can connect to its server based on its internal
4dlink file.
normally you build the client without a 4dlink file,
run it for the first time on the deployment network,
and select the server.
the IP address will be recorded in the 4dlink file,
so from the next time,
it will automatically connect to the server,
as long as the location doesn?=A2?=82=E2Ñt change.

now, suppose you have a mirror server on the same network.
obviously its IP address is different from that of the main server.

if the main server becomes unavailable,
then the next time a client is launched it will attempt to connect to
the dead server for about a minute,
then eventually display a "runtime error" dialog that the "process has
been aborted".

runtime errors shown to the end user is never a good thing.
first, the phrase ?=A2?=82=C5ìerror?=A2?=82=C2ù is
patronizing,
and the error message almost always impenetrable (?=A2?=82=C5ìpro
cess
aborted?=A2?=82=C2ù what does that even mean?)

so I am working with a customer,
to setup a mirror system where the IP address of the mirror and main
automatically switches if the main goes down.
the end user will launch the client first thing in the morning and not
even know that it has connected to a mirror.

the mirror will send a notification to the developer if it accepts a
client connection,
but at least it will buy some time to restore the main by say, lunch
break.

its is still just a proof of concept for this particular customer,
but the command like tool is generic so I?=A2?=82=E2Ñve
released it on GitHub.

the code is almost generic C but requires some modification to compile
on Windows.
(especially the part the converts the UDP packet to Unicode)

https://github.com/miyako/4d-utility-ls4d/blob/master/ls4d/main.cpp

miyako

Keisuke Miyako (5/30/14 3:48 AM)

slightly off topic but on the subject of ?=A2?=82=C5ìsingle
threaded?=A2?=82=C2ù

I've been forced to learn that AppleScript is single threaded,
and that an application written in AS should be coded accordingly.
It is also well known that JavaScript is also single threaded. (with
some
exceptions)

So what does it mean to code ?=A2?=82=C5ìsingle threaded?=A2?
Ç=C2ù ?

My very short answer would be, ?=A2?=82=C5ìdon?=A2?=82=E2Ñ
t run in a large infinite
loop?=A2?=82=C2ù.

bad example in AppleScript is a single "Repeat~End repeat?=A2?=82=C2
ù that goes
on
forever.
bad example in 4D is a Repeat~MODIFY SELECTION~Until(OK=0) written in
On
Startup,
or a long loop inside an object method.

maybe programmers coming from a C background where the main() is the
application entry point,
tends to code their application like a single infinite loop,
but that style is not really suited for a single threaded
language/runtime.

instead, in AppleScript,
the correct way to code an application is to not run any code in the
outmost scope,
but rather,
define event handlers,
like On run, On Idle, On Exit.

sounds familiar?

4D is also event driven,
so it is better to passively process events dispatched by the runtime,
rather than to code as if you are writing the main application
yourself.

It is therefore very important to be familiar with the full range of
events,
and not abuse them.

As Josh mentioned in another thread,
if an event is called ?=A2?=82=C5ìOn Web Authentication?=A2?=82
=C2ù,
then it should only do what is it supposed to do during that phase,
and not go off in a tangent and do all sorts of other things.

form events can be quite tolerant,
but database events, such as
On Startup, On Exit, COMPILER_WEB, SOAP/WEB/REST/SQL connections and
triggers
should aim to run code that is simple and/or quick,
in order not to block the main application.

miyako

Reply to this message

Summary created 5/30/14 at 9:17AM by Intellex Corporation

Comments welcome at: feedback@intellexcorp.com