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 /******************************************************************************
26 Waits up to 10 seconds for a user specified port
31 PORT -- Name of the port to be waited.
47 ******************************************************************************/
49 #include <dos/rdargs.h>
50 #include <proto/exec.h>
51 #include <proto/dos.h>
52 #include <devices/timer.h>
53 #include <proto/alib.h>
55 #include <aros/debug.h>
57 const char version
[] = "$VER: WaitForPort 0.1 (26.12.2005)";
59 const char WaitForPort_ArgTemplate
[] = "P=PORT/A";
60 IPTR WaitForPort_Arguments
[2];
62 struct RDArgs
*WFP_rda
= NULL
;
64 struct Device
*TimerBase
= NULL
;
65 static struct MsgPort
*timerport
= NULL
; /* Timer message reply port */
66 static struct timerequest
*timerIORequest
= NULL
; /* template IORequest */
70 main(int argc
, char *argv
[])
72 struct MsgPort
*AROSTCP_Port
= NULL
;
76 if ((WFP_rda
= ReadArgs(WaitForPort_ArgTemplate
, WaitForPort_Arguments
, NULL
)))
79 if (WaitForPort_Arguments
[0])
81 D(bug("[WaitForPort] Waiting for '%s' port\n",WaitForPort_Arguments
[0]));
84 timerport
= CreateMsgPort();
85 if (timerport
!= NULL
)
87 /* allocate and initialize the template message structure */
88 timerIORequest
= (struct timerequest
*) CreateIORequest(timerport
, sizeof(struct timerequest
));
90 if (timerIORequest
!= NULL
)
92 if (!(OpenDevice(TIMERNAME
, UNIT_VBLANK
,
93 (struct IORequest
*)timerIORequest
, 0)))
95 /* Make sure that we got at least V36 timer, since we use some
96 * functions defined only in V36 and later. */
98 if ((timerIORequest
->tr_node
.io_Device
)->dd_Library
.lib_Version
>= 36)
100 /* initialize TimerBase from timerIORequest */
101 TimerBase
= timerIORequest
->tr_node
.io_Device
;
103 /* Initialize some fields of the IO request to common values */
104 timerIORequest
->tr_node
.io_Command
= TR_ADDREQUEST
;
106 /* NT_UNKNOWN means unused, too (see note on exec/nodes.h) */
107 timerIORequest
->tr_node
.io_Message
.mn_Node
.ln_Type
= NT_UNKNOWN
;
109 timerIORequest
->tr_time
.tv_micro
= 1000000;
110 wait_limit
= timerIORequest
->tr_time
.tv_micro
* 10; /* Default to a 10 second wait */
112 BeginIO((struct IORequest
*)timerIORequest
);
117 D(bug("[WaitForPort] In Wait Loop ..\n"));
118 ULONG mask
= SIGBREAKF_CTRL_C
| SIGBREAKF_CTRL_D
| (1 << timerport
->mp_SigBit
);
120 if (mask
& SIGBREAKF_CTRL_C
) break;
121 if (mask
& SIGBREAKF_CTRL_D
) break;
122 if (mask
& (1 << timerport
->mp_SigBit
))
124 D(bug("[WaitForPort] Received timer signal?...\n"));
125 timerIORequest
= (struct timerequest
*)GetMsg(timerport
);
128 AROSTCP_Port
= FindPort((char *)WaitForPort_Arguments
[0]);
129 wait_time
+= 1000000;
132 if (wait_time
> wait_limit
)
134 D(bug("[WaitForPort] Timeout Reached\n"));
137 D(bug("[WaitForPort] Port not found .. secs=%d\n",wait_time
/1000000));
141 D(bug("[WaitForPort] Port found ... escaping from wait loop\n"));
144 timerIORequest
->tr_node
.io_Command
= TR_ADDREQUEST
;
145 timerIORequest
->tr_time
.tv_micro
= 1000000;
146 BeginIO((struct IORequest
*)timerIORequest
);
160 if (timerIORequest
->tr_node
.io_Device
!= NULL
) CloseDevice((struct IORequest
*)timerIORequest
);
162 DeleteIORequest((struct IORequest
*)timerIORequest
);
163 timerIORequest
= NULL
;
168 DeleteMsgPort(timerport
);
177 Printf("WaitForPort: Bad Arguments .. Use 'WaitForPort ?' for correct useage\n");
180 if (AROSTCP_Port
) return RETURN_OK
;