Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- import std.socket;
- import std.conv;
- import std.process;
- enum MAX_ATTEMPTS = 5;
- enum TIMEOUT = 5;
- enum MAX_WAIT_REBOOT_TIME = 300; //5 минут
- void main(string[] argv) {
- if (argv.length < 2) { argv ~= "4455"; }
- ushort port = parse!ushort(argv[1]);
- auto addr = new InternetAddress(InternetAddress.ADDR_ANY, 4455);
- auto s = new UdpSocket();
- s.bind(addr);
- void[] buf = new void[1];
- writeln("Receiving datagrams on port ",port);
- s.setOption(SocketOptionLevel.SOCKET, SocketOption.RCVTIMEO, dur!"seconds"(TIMEOUT));
- auto attempts = MAX_ATTEMPTS;
- auto attemptsMultiplier = 1;
- while (true) {
- while (attempts > 0) {
- if (s.receiveFrom(buf) != Socket.ERROR) {
- write("\b",progressChar); stdout.flush;
- attempts = MAX_ATTEMPTS;
- attemptsMultiplier = 1;
- }
- else {
- attempts--;
- writeln(" [", currentDateTime, ": FAILED! ", to!string(attempts*TIMEOUT), "s. before eject CD]");
- }
- }
- writeln("[Ejecting CD-ROM]");
- system(`powershell $cds = (New-Object -ComObject "WMPlayer.OCX").cdromCollection; for($i=0;$i -lt $cds.Count;$i++) { $cds.Item($i).Eject() }`);
- attempts = attemptsMultiplier * (MAX_WAIT_REBOOT_TIME / TIMEOUT); //достаточное количество попыток для запуска компа
- attemptsMultiplier++;
- }
- }
- string currentDateTime() {
- import std.datetime;
- import std.algorithm;
- return findSplit(Clock.currTime().toString, ".")[0];
- }
- char progressChar() {
- static char[] chars = [ '-', '\\', '|', '/', ];
- static int index = 0;
- if (index == chars.length) index = 0;
- return chars[index++];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement