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>
13 #include <proto/alib.h>
15 #include <aros/debug.h>
17 const char version
[] = "$VER: WaitForPort 0.1 (26.12.2005)";
19 const char WaitForPort_ArgTemplate
[] = "P=PORT/A";
20 IPTR WaitForPort_Arguments
[2];
22 struct RDArgs
*WFP_rda
= NULL
;
24 struct Device
*TimerBase
= NULL
;
25 static struct MsgPort
*timerport
= NULL
; /* Timer message reply port */
26 static struct timerequest
*timerIORequest
= NULL
; /* template IORequest */
30 main(int argc
, char *argv
[])
32 struct MsgPort
*AROSTCP_Port
= NULL
;
36 if ((WFP_rda
= ReadArgs(WaitForPort_ArgTemplate
, WaitForPort_Arguments
, NULL
)))
39 if (WaitForPort_Arguments
[0])
41 D(bug("[WaitForPort] Waiting for '%s' port\n",WaitForPort_Arguments
[0]));
44 timerport
= CreateMsgPort();
45 if (timerport
!= NULL
)
47 /* allocate and initialize the template message structure */
48 timerIORequest
= (struct timerequest
*) CreateIORequest(timerport
, sizeof(struct timerequest
));
50 if (timerIORequest
!= NULL
)
52 if (!(OpenDevice(TIMERNAME
, UNIT_VBLANK
,
53 (struct IORequest
*)timerIORequest
, 0)))
55 /* Make sure that we got at least V36 timer, since we use some
56 * functions defined only in V36 and later. */
58 if ((timerIORequest
->tr_node
.io_Device
)->dd_Library
.lib_Version
>= 36)
60 /* initialize TimerBase from timerIORequest */
61 TimerBase
= timerIORequest
->tr_node
.io_Device
;
63 /* Initialize some fields of the IO request to common values */
64 timerIORequest
->tr_node
.io_Command
= TR_ADDREQUEST
;
66 /* NT_UNKNOWN means unused, too (see note on exec/nodes.h) */
67 timerIORequest
->tr_node
.io_Message
.mn_Node
.ln_Type
= NT_UNKNOWN
;
69 timerIORequest
->tr_time
.tv_micro
= 1000000;
70 wait_limit
= timerIORequest
->tr_time
.tv_micro
* 10; /* Default to a 10 second wait */
72 BeginIO((struct IORequest
*)timerIORequest
);
77 D(bug("[WaitForPort] In Wait Loop ..\n"));
78 ULONG mask
= SIGBREAKF_CTRL_C
| SIGBREAKF_CTRL_D
| (1 << timerport
->mp_SigBit
);
80 if (mask
& SIGBREAKF_CTRL_C
) break;
81 if (mask
& SIGBREAKF_CTRL_D
) break;
82 if (mask
& (1 << timerport
->mp_SigBit
))
84 D(bug("[WaitForPort] Received timer signal?...\n"));
85 timerIORequest
= (struct timerequest
*)GetMsg(timerport
);
88 AROSTCP_Port
= FindPort((char *)WaitForPort_Arguments
[0]);
92 if (wait_time
> wait_limit
)
94 D(bug("[WaitForPort] Timeout Reached\n"));
97 D(bug("[WaitForPort] Port not found .. secs=%d\n",wait_time
/1000000));
101 D(bug("[WaitForPort] Port found ... escaping from wait loop\n"));
104 timerIORequest
->tr_node
.io_Command
= TR_ADDREQUEST
;
105 timerIORequest
->tr_time
.tv_micro
= 1000000;
106 BeginIO((struct IORequest
*)timerIORequest
);
120 if (timerIORequest
->tr_node
.io_Device
!= NULL
) CloseDevice((struct IORequest
*)timerIORequest
);
122 DeleteIORequest((struct IORequest
*)timerIORequest
);
123 timerIORequest
= NULL
;
128 DeleteMsgPort(timerport
);
137 Printf("WaitForPort: Bad Arguments .. Use 'WaitForPort ?' for correct useage\n");
140 if (AROSTCP_Port
) return RETURN_OK
;