1
0
mirror of https://frontier.innolan.net/rainlance/amiga-ntimed.git synced 2025-11-21 01:13:45 +00:00

Cleanup and reimplemented SYNC function

This commit is contained in:
llsth
2015-03-17 10:03:48 +01:00
parent d361fc7ec7
commit 18381bc7c1
2 changed files with 160 additions and 153 deletions

View File

@ -68,10 +68,10 @@
#endif #endif
#include "tz.h" #include "tz.h"
#include "atimed.h"
#include "ntimed.h" #include "ntimed.h"
#include "ntp.h" #include "ntp.h"
#include "udp.h" #include "udp.h"
#include "ntimed_platform.h"
#define PARAM_CLIENT PARAM_INSTANCE #define PARAM_CLIENT PARAM_INSTANCE
#define PARAM_TABLE_NAME client_param_table #define PARAM_TABLE_NAME client_param_table
@ -81,108 +81,135 @@
const char *vers = "\0$VER: ntimed 1.0a"; const char *vers = "\0$VER: ntimed 1.0a";
#define ARG_TEMPLATE "P=PARAM/K,T=TRACEFILE/K,Z=TIMEZONE/K,SERVERS/M,SYNC/S,SAVE/S,QUIET/S" BOOL tracefile;
enum
{
ARG_PARAM,
ARG_TRACE,
ARG_ZONE,
ARG_SERVERS,
ARG_SYNC,
ARG_SAVE,
ARG_QUIET,
ARG_LAST
};
extern int errno;
BOOL started_from_wb; BOOL started_from_wb;
struct Library *BattClockBase; struct Library *BattClockBase;
struct Library *UtilityBase = NULL; struct Library *UtilityBase = NULL;
struct Library *SocketBase = NULL; struct Library *SocketBase = NULL;
struct RDArgs *rda = NULL; struct RDArgs *rdargs = NULL;
struct ntp_peerset *nps = NULL; struct ntp_peerset *nps = NULL;
struct todolist *tdl = NULL; struct todolist *tdl = NULL;
struct combine_delta *cd = NULL;
struct udp_socket *usc = NULL; struct udp_socket *usc = NULL;
int saveclock = 0; struct ntimedargs {
int tracefile = 0; char *params;
char *tracefile;
char *timezone;
char **servers;
long synchronize;
long save;
long quiet;
};
static void set_params(const char *params); static void init_libs(struct ntimedargs *args);
static void init_logic(struct ntimedargs *args);
static void clean_exit(); static void clean_exit();
static enum todo_e __match_proto__(todo_f)
sync_end(struct ocx *ocx, struct todolist *tdl, void *priv)
{
(void)tdl;
(void)priv;
Put(ocx, OCX_TRACE, "# Sync done\n");
return(TODO_FAIL);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
IPTR args[ARG_LAST]; struct ntimedargs args = {
const char **servers; NULL, NULL, NULL, NULL,
struct ntp_peer *np; FALSE, FALSE, FALSE
};
int i; started_from_wb = (BOOL)argc;
int sync = 0;
int npeer = 0;
atexit(clean_exit); atexit(clean_exit);
started_from_wb = (BOOL)argc; rdargs = ReadArgs(ARGS_FORMAT, (APTR)&args, NULL);
args[ARG_PARAM] = (IPTR)NULL; if (!rdargs)
args[ARG_TRACE] = (IPTR)NULL;
args[ARG_ZONE] = (IPTR)NULL;
args[ARG_SERVERS] = (IPTR)NULL;
args[ARG_SYNC] = (IPTR)NULL;
args[ARG_SAVE] = (IPTR)NULL;
args[ARG_QUIET] = (IPTR)NULL;
rda = (struct RDArgs*)ReadArgs((CONST_STRPTR)ARG_TEMPLATE, (ARGSPTR)args, NULL);
if (!rda)
{ {
PrintFault(IoErr(), (CONST_STRPTR)argv[0]); PrintFault(IoErr(), (ARGPTR)argv[0]);
exit(1); exit(2);
} }
if(!(UtilityBase = (struct Library *)OpenLibrary((CONST_STRPTR)"utility.library", 33))) { if (!args.quiet) {
PrintFault(IoErr(), (CONST_STRPTR)argv[0]);
exit(1);
}
if (args[ARG_SYNC]) {
sync = 1;
}
if (!args[ARG_QUIET]) {
EnableDebug(); EnableDebug();
} }
if (args[ARG_SAVE]) { init_libs(&args);
saveclock = 1;
BattClockBase = OpenResource((CONST_STRPTR)BATTCLOCKNAME); if (args.timezone) {
ArgTimezone(args.timezone);
}
if (args.tracefile) {
ArgTracefile(args.tracefile);
tracefile = 1;
}
init_logic(&args);
if (args.synchronize) {
CreateWaitTimer();
}
NTP_PeerSet_Poll(NULL, nps, usc, tdl);
(void)TODO_Run(NULL, tdl);
if (args.save) {
Time_Amiga_Save();
Put(NULL, OCX_DEBUG, "Saved time to hardware clock\n");
}
return 0;
}
static void init_libs(struct ntimedargs *args)
{
if(!(UtilityBase = (struct Library *)OpenLibrary(UTILLIB_NAME, UTILLIB_REV))) {
Put(NULL, OCX_DIAG, "Cannot open utility library.\n");
exit(2);
}
if (args->save) {
BattClockBase = OpenResource(BATTCLOCK_NAME);
}
if(!(SocketBase = OpenLibrary(BSDLIB_NAME, BSDLIB_REV))) {
Put(NULL, OCX_DIAG, "No TCP/IP Stack running.\n");
exit(2);
} }
#ifdef AROS #ifdef AROS
if(!(SocketBase = OpenLibrary((CONST_STRPTR)"bsdsocket.library", 4))) {
PrintFault(IoErr(), (CONST_STRPTR)argv[0]);
exit(1);
}
if(SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), (IPTR)&errno, if(SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), (IPTR)&errno,
SBTM_SETVAL(SBTC_HERRNOLONGPTR), (IPTR)&h_errno, TAG_DONE)) { SBTM_SETVAL(SBTC_HERRNOLONGPTR), (IPTR)&h_errno, TAG_DONE)) {
PrintFault(IoErr(), (CONST_STRPTR)argv[0]); Put(NULL, OCX_DIAG, "Error initializing bsdsocket.\n");
exit(1); exit(2);
} }
#endif #endif
}
// *** INITIALIZE LOGIC *** static void set_params(char *params)
{
char *token;
const char d[2] = ",";
if (params == NULL)
return;
token = strtok(params, d);
while (token != NULL)
{
Param_Tweak(NULL, token);
token = strtok(NULL, d);
}
}
static void init_logic(struct ntimedargs *args)
{
struct ntp_peer *np;
struct combine_delta *cd;
int i;
int npeer = 0;
Put(NULL, OCX_DEBUG, "Timezone set to %s (UTC%s%.2f) %s\n",
tza[tzid].abbr,
tza[tzid].offset < 0.0 ? "-" : "+",
fabs(tza[tzid].offset),
tza[tzid].name);
tdl = TODO_NewList(); tdl = TODO_NewList();
Time_Amiga(tdl); Time_Amiga(tdl);
@ -194,26 +221,14 @@ int main(int argc, char **argv)
Param_Register(client_param_table); Param_Register(client_param_table);
NF_Init(); NF_Init();
if (args[ARG_ZONE]) { set_params(args->params);
ArgTimezone((const char*)args[ARG_ZONE]);
}
if (args[ARG_PARAM]) { for (i = 0; args->servers[i]; i++)
set_params((const char*)args[ARG_PARAM]); npeer += NTP_PeerSet_Add(NULL, nps, args->servers[i]);
}
if (args[ARG_TRACE]) {
ArgTracefile((const char*)args[ARG_TRACE]);
tracefile = 1;
}
servers = (const char**)args[ARG_SERVERS];
for (i = 0; servers[i]; i++)
npeer += NTP_PeerSet_Add(NULL, nps, servers[i]);
if (npeer == 0) { if (npeer == 0) {
Fail(NULL, 0, "No NTP peers found"); Put(NULL, OCX_DEBUG, "No NTP peers found\n");
exit(1);
} }
Put(NULL, OCX_TRACE, "# NTIMED Format client 1.0\n"); Put(NULL, OCX_TRACE, "# NTIMED Format client 1.0\n");
@ -222,7 +237,8 @@ int main(int argc, char **argv)
usc = UdpTimedSocket(NULL); usc = UdpTimedSocket(NULL);
if (usc == NULL) { if (usc == NULL) {
Fail(NULL, errno, "Could not open UDP socket"); Put(NULL, OCX_DEBUG, "Could not open UDP socket\n");
exit(1);
} }
cd = CD_New(); cd = CD_New();
@ -232,83 +248,37 @@ int main(int argc, char **argv)
np->combiner = CD_AddSource(cd, np->hostname, np->ip); np->combiner = CD_AddSource(cd, np->hostname, np->ip);
} }
Put(NULL, OCX_DEBUG, "Timezone set to %s (UTC%s%.2f) %s\n", if (args->synchronize) {
tza[tzid].abbr, Put(NULL, OCX_DEBUG, "Synchronizing local clock to server(s)\n");
tza[tzid].offset < 0.0 ? "-" : "+",
fabs(tza[tzid].offset),
tza[tzid].name);
if (sync) {
TODO_ScheduleRel(tdl, sync_end, NULL, 15.0, 0, "End sync task");
Put(NULL, OCX_DEBUG, "Synchronizing local time to server(s)\n");
} else { } else {
Put(NULL, OCX_DEBUG, "Synchronizing ...\n"); Put(NULL, OCX_DEBUG, "Synchronizing ...\n");
} }
NTP_PeerSet_Poll(NULL, nps, usc, tdl);
(void)TODO_Run(NULL, tdl);
return 0;
}
static void set_params(const char *params)
{
char *token;
const char d[2] = ",";
token = strtok((char*)params, d);
while (token != NULL)
{
Param_Tweak(NULL, token);
token = strtok(NULL, d);
}
} }
static void clean_exit() static void clean_exit()
{ {
if (saveclock) { DeleteWaitTimer();
Time_Amiga_Save(); ArgTracefile(NULL);
Put(NULL, OCX_DEBUG, "Saved time to hardware clock\n");
}
// Free peer set && peers && NTP filters
if(nps) {
}
// Free combine delta
if(cd) {
}
// Free todo list
if(tdl) {
}
// Free UDP socket
if(usc) { if(usc) {
UDP_Socket_Destroy(usc); UDP_Socket_Destroy(usc);
usc = NULL; usc = NULL;
} }
if(tracefile) {
ArgTracefile(NULL);
}
if (rda) {
FreeArgs(rda);
rda = NULL;
}
if (UtilityBase != NULL) { if (UtilityBase != NULL) {
CloseLibrary(UtilityBase); CloseLibrary(UtilityBase);
UtilityBase = NULL;
} }
#ifdef AROS
if (SocketBase != NULL) { if (SocketBase != NULL) {
CloseLibrary(SocketBase); CloseLibrary(SocketBase);
SocketBase = NULL;
}
if (rdargs) {
FreeArgs(rdargs);
rdargs = NULL;
} }
#endif
} }

View File

@ -44,14 +44,17 @@
#undef Debug #undef Debug
#endif #endif
#include "atimed.h"
#include "ntimed.h" #include "ntimed.h"
#include "ntimed_platform.h"
int amiga_get_time(struct timeval *tv); int amiga_get_time(struct timeval *tv);
int amiga_set_time(struct timeval *tv); int amiga_set_time(struct timeval *tv);
struct timerequest* create_timer(); struct timerequest* create_timer();
void delete_timer(struct timerequest*); void delete_timer(struct timerequest*);
void adjust_timeval(struct timeval *tv, double offset); static void adjust_timeval(struct timeval *tv, double offset);
ULONG stopsig = 0;
struct timerequest *stoprequest = NULL;
// Ntimed internals // Ntimed internals
static double adj_offset = 0; static double adj_offset = 0;
@ -166,9 +169,9 @@ amiga_sleep(double dur)
timersig = (1L << request->tr_node.io_Message.mn_ReplyPort->mp_SigBit); timersig = (1L << request->tr_node.io_Message.mn_ReplyPort->mp_SigBit);
SendIO((struct IORequest*)request); SendIO((struct IORequest*)request);
sigs = Wait(SIGBREAKF_CTRL_C | timersig); sigs = Wait(SIGBREAKF_CTRL_C | timersig | stopsig);
if (sigs & SIGBREAKF_CTRL_C) { if (sigs & SIGBREAKF_CTRL_C || sigs & stopsig) {
AbortIO((struct IORequest*)request); AbortIO((struct IORequest*)request);
WaitIO((struct IORequest*)request); WaitIO((struct IORequest*)request);
delete_timer(request); delete_timer(request);
@ -214,7 +217,7 @@ struct timerequest* create_timer()
return NULL; return NULL;
} }
error = OpenDevice(ATIMER, UNIT_MICROHZ, (struct IORequest*)request, 0L); error = OpenDevice(TIMER_NAME, UNIT_MICROHZ, (struct IORequest*)request, 0L);
if (error != 0) if (error != 0)
{ {
delete_timer(request); delete_timer(request);
@ -279,6 +282,7 @@ int amiga_set_time(struct timeval *tv)
return 0; return 0;
} }
/**********************************************************************/
void amiga_save_time(void) void amiga_save_time(void)
{ {
@ -291,7 +295,40 @@ void amiga_save_time(void)
/**********************************************************************/ /**********************************************************************/
void adjust_timeval(struct timeval *tv, double offset) void create_wait_timer()
{
struct timeval tv;
tv.tv_secs = 15;
tv.tv_micro = 0;
stoprequest = create_timer();
if (stoprequest == NULL) {
exit(2);
}
stoprequest->tr_node.io_Command = TR_ADDREQUEST;
stoprequest->tr_time = tv;
stopsig = (1L << stoprequest->tr_node.io_Message.mn_ReplyPort->mp_SigBit);
SendIO((struct IORequest*)stoprequest);
}
void delete_wait_timer()
{
if (stoprequest == NULL)
return;
if (CheckIO((struct IORequest*)stoprequest)) {
AbortIO((struct IORequest*)stoprequest);
}
WaitIO((struct IORequest*)stoprequest);
delete_timer(stoprequest);
}
/**********************************************************************/
static void adjust_timeval(struct timeval *tv, double offset)
{ {
double d1, d2; double d1, d2;
long sec, micro; long sec, micro;