2 Copyright (C) 2004, The AROS Development Team. All rights reserved.
5 Desc: Waits up to 10 seconds for a user specified Port to become available
9 #include <dos/rdargs.h>
10 #include <proto/exec.h>
11 #include <proto/dos.h>
12 #include <devices/timer.h>
14 #include <proto/alib.h>
16 #include <aros/debug.h>
18 const char version
[] = "$VER: WaitForPort 0.1 (26.12.2005)";
20 const char WaitForPort_ArgTemplate
[] = "P=PORT/A";
21 IPTR WaitForPort_Arguments
[2];
23 struct RDArgs
*WFP_rda
= NULL
;
25 struct Device
*TimerBase
= NULL
;
26 static struct MsgPort
*timerport
= NULL
; /* Timer message reply port */
27 static struct timerequest
*timerIORequest
= NULL
; /* template IORequest */
31 main(int argc
, char *argv
[])
33 struct MsgPort
*AROSTCP_Port
= NULL
;
37 if ((WFP_rda
= ReadArgs(WaitForPort_ArgTemplate
, WaitForPort_Arguments
, NULL
)))
40 if (WaitForPort_Arguments
[0])
42 D(bug("[WaitForPort] Waiting for '%s' port\n",WaitForPort_Arguments
[0]));
45 timerport
= CreateMsgPort();
46 if (timerport
!= NULL
)
48 /* allocate and initialize the template message structure */
49 timerIORequest
= (struct timerequest
*) CreateIORequest(timerport
, sizeof(struct timerequest
));
51 if (timerIORequest
!= NULL
)
53 if (!(OpenDevice(TIMERNAME
, UNIT_VBLANK
,
54 (struct IORequest
*)timerIORequest
, 0)))
56 /* Make sure that we got at least V36 timer, since we use some
57 * functions defined only in V36 and later. */
59 if ((timerIORequest
->tr_node
.io_Device
)->dd_Library
.lib_Version
>= 36)
61 /* initialize TimerBase from timerIORequest */
62 TimerBase
= timerIORequest
->tr_node
.io_Device
;
64 /* Initialize some fields of the IO request to common values */
65 timerIORequest
->tr_node
.io_Command
= TR_ADDREQUEST
;
67 /* NT_UNKNOWN means unused, too (see note on exec/nodes.h) */
68 timerIORequest
->tr_node
.io_Message
.mn_Node
.ln_Type
= NT_UNKNOWN
;
70 timerIORequest
->tr_time
.tv_micro
= 1000000;
71 wait_limit
= timerIORequest
->tr_time
.tv_micro
* 10; /* Default to a 10 second wait */
73 BeginIO((struct IORequest
*)timerIORequest
);
78 D(bug("[WaitForPort] In Wait Loop ..\n"));
79 ULONG mask
= SIGBREAKF_CTRL_C
| SIGBREAKF_CTRL_D
| (1 << timerport
->mp_SigBit
);
81 if (mask
& SIGBREAKF_CTRL_C
) break;
82 if (mask
& SIGBREAKF_CTRL_D
) break;
83 if (mask
& (1 << timerport
->mp_SigBit
))
85 D(bug("[WaitForPort] Recieved timer signal? ..\n"));
86 timerIORequest
= (struct timerequest
*)GetMsg(timerport
);
89 AROSTCP_Port
= FindPort((char *)WaitForPort_Arguments
[0]);
93 if (wait_time
> wait_limit
)
95 D(bug("[WaitForPort] Timeout Reached\n"));
98 D(bug("[WaitForPort] Port not found .. secs=%d\n",wait_time
/1000000));
102 D(bug("[WaitForPort] Port found ... escaping from wait loop\n"));
105 timerIORequest
->tr_node
.io_Command
= TR_ADDREQUEST
;
106 timerIORequest
->tr_time
.tv_micro
= 1000000;
107 BeginIO((struct IORequest
*)timerIORequest
);
121 if (timerIORequest
->tr_node
.io_Device
!= NULL
) CloseDevice((struct IORequest
*)timerIORequest
);
123 DeleteIORequest((struct IORequest
*)timerIORequest
);
124 timerIORequest
= NULL
;
129 DeleteMsgPort(timerport
);
138 printf("WaitForPort: Bad Arguments .. Use 'WaitForPort ?' for correct useage\n");
141 if (AROSTCP_Port
) return RETURN_OK
;