Auto-translated
Let me know what you think, your opinion matters to me :)
New here? I'm Roha — want a quick tour?
Let me know what you think, your opinion matters to me :)
Someone, please rate it, because I don't know if it's bad or very bad)))
But I usually write in Delphi.
use cyrillic qw/upcase win2dos/;
use strict;
use Time::HiRes qw/gettimeofday/;
use Data::Dumper;
use Net::SMTP;
use Net::POP3;
use warnings;
use subs qw/load_config mail_auth send_mail read_file write_log write_noping/;
my $debug = 1;
my $config = load_config;
my $offline_text = read_file('offline.txt');
my $online_text = read_file('online.txt');
my $err = "Can't Ping Host";
my @hosts;
#foreach my $host_str (@{$config->{hosts}})
#{
# my $host = {};
# $host_str =~ /^\s*(.*?)\s*\=\s*(.*)\s*$/;
# $host->{descr} = $1;
# $host->{adres} = $2;
# $host->{state} = 'online';
# $host->{fromtime} = gettimeofday();
# $host->{oldstate} = 0;
# push @hosts, $host;
#}
foreach my $host_str (@{$config->{hosts}})
{
my $host = {};
$host_str =~ /^\s*(.*?)\s*\=\s*(.*)\s*$/;
$host->{descr} = $1;
$host->{adres} = $2;
my $result = "$err $host->{descr} ($host->{adres})";
my $tm = gettimeofday();
my $ping = qx/ping $host->{adres} -n 1/;
$tm = gettimeofday() - $tm;
foreach (split "\x0a", $ping)
{
if(/^.*?[\=\<\>]+.*?[\=\<\>]+(\d+)/)
{
$result = sprintf ("Ping to %s (%s) is %s", $host->{descr}, $host->{adres}, $1);
last;
}
}
my $state = $result =~ /$err/ ? 'offline' : 'online';
$host->{state} = $state;
$host->{fromtime} = gettimeofday();
$host->{oldstate} = 0;
push @hosts, $host;
}
my $host_descr = '';
my $host_adres = '';
my $namehost = '';
my $date = '';
my @ratio = (1900, 1, 0,0,0,0);
$date = sprintf("%04d-%02d-%02d %02d-%02d-%02d", map{$_+ shift(@ratio)}(localtime(time))[5,4,3,2,1,0]);
my $textlog = "Log file started ${date}\n";
my $namefile = $date;
open (LOGBIGIN, ">>$namefile") || die "Error opening $namefile: $!";
print LOGBIGIN $textlog;
close (LOGBIGIN);
my $sleep = 30; # In seconds
$date = '';
while(1)
{
foreach my $host (@hosts)
{
my $result = "$err $host->{descr} ($host->{adres})";
my $tm = gettimeofday();
my $ping = qx/ping $host->{adres} -n 1/;
$tm = gettimeofday() - $tm;
foreach (split "\x0a", $ping)
{
if(/^.*?[\=\<\>]+.*?[\=\<\>]+(\d+)/)
{
$result = sprintf ("Ping to %s (%s) is %s", $host->{descr}, $host->{adres}, $1);
last;
}
}
my $state = $result =~ /$err/ ? 'offline' : 'online';
if ($state ne $host->{state})
{
# state changed
$host->{oldstate} = gettimeofday()
if $host->{oldstate} eq 0;
my $idle = gettimeofday() - $host->{oldstate};
if ($idle > ($config->{timeout}*60))
{
# need to send emails
print win2dos("Status change to $state for $host->{descr} ($host->{adres})\n");
eval {mail_auth;};
print $@ if $@;
foreach my $rec (@{$config->{recipients}})
{
my $text;
if ($state eq 'online')
{
$text = $online_text;
}
else
{
$text = $offline_text;
}
$host_adres = $host->{adres};
$host_descr = $host->{descr};
my @ratio_time = (1900, 1, 0,0,0,0);
$date = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", map{$_+ shift(@ratio_time)}(localtime(time))[5,4,3,2,1,0]);
my $text2 = $text;
while($text =~ /\$([\w]+)/g)
{
my $var = $1;
my $val;
eval " \$val = \${$var};";
#print "$var = $val\n";
$text2 =~ s/\$$var/$val/ge;
}
eval { send_mail($rec, $text2);};
print $@ if $@;
write_log(sprintf("Change state of Host, last state: %3.2f %s time,", gettimeofday() - $host->{fromtime}, $host->{state})." $result (".sprintf("%0.5f sec", $tm).") $date\n", $namefile);
}
$host->{state} = $state;
$host->{fromtime} = gettimeofday();
$host->{oldstate} = 0;
}
}
else
{
$host->{oldstate} = 0;
}
# If ping fails
if ($state ne $host->{state})
{
if($state eq 'offline')
{
$namehost = $host->{adres};
my @ratio = (1900, 1, 0,0,0,0);
$date = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", map{$_+ shift(@ratio) }(localtime(time))[5,4,3,2,1,0]);
write_noping(sprintf("%3.2f %s time,", gettimeofday() - $host->{fromtime}, $host->{state})." $result (".sprintf("%0.5f sec", $tm).") $date\n", $namehost);
}
}
my @ratio = (1900, 1, 0,0,0,0);
$date = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", map{$_+ shift(@ratio) }(localtime(time))[5,4,3,2,1,0]);
write_log(sprintf("%3.2f %s time,", gettimeofday() - $host->{fromtime}, $host->{state})." $result (".sprintf("%0.5f sec", $tm).") $date\n", $namefile);
}
# write_log("Sleep for $sleep sec (Wait Ping Hosts)\n", $namefile);
print("Sleep for $sleep sec (Wait Ping Hosts)\n");
sleep($sleep);
}
sub store_arg
{
my $config = shift;
my $arg = shift;
if($arg =~ /^\s*(.*?)\s*\=\s*(.*)\s*$/)
{
$config->{$1} = $2;
}
else
{
$config->{$arg} = 1;
}
}
sub load_config
{
my $file_name = shift || 'pinger.config';
my $config = { };
my $cur_section;
open (FH, $file_name) || die "can't open config file";
while (my $line = readline FH)
{
next if $line =~ /^\s*\#|^$/;
$line =~ s/\#.*$//;#$line =~ s/\n//;
if ($line =~ /^\s*[(\S*)]\s*$/)
{
$cur_section = $1;
}
elsif ($line =~ /^\s*\$\s*$/)
{
undef $cur_section;
}
else
{
if (defined $cur_section)
{
push @{$config->{$cur_section}}, $line;
}
else
{
store_arg($config, $line);
}
}
}
close FH;
return $config;
}
sub mail_auth
{
# authorization
# print "$config->{pop3}\n$config->{user} : $config->{pass}\n";
my $pop = new Net::POP3($config->{pop3}, Timeout => 240, Debug => $debug) || die "$!";
my $msgs = $pop->login($config->{user}, $config->{pass});
die "Incorrect login or password for ".$config->{pop3} unless defined $msgs;
$pop->quit();
}
sub send_mail
{
my $recipient = shift;
my $text = shift;
my $smtp = Net::SMTP->new(Host => $config->{smtp},
Timeout => 30,
Debug => $debug, ); # Debug => 1 if you are interested in the communication protocol for debugging
my $dom = $smtp->domain;
$smtp->mail("$config->{user}\@$dom");
$smtp->to("$recipient");
$smtp->data();
$smtp->datasend("TO: $recipient\n");
foreach(split /\x0a/, $text)
{
$smtp->datasend("$_\n");
}
$smtp->dataend();
$smtp->quit;
}
sub read_file
{
my $fname = shift;
open FH, $fname;
my $text = '';
while (my $line = )
{
$text .= $line;
}
close FH;
return $text;
}
sub write_log
{
my $text = shift;
my $namefile = shift;
open (w_LOG, ">>$namefile") || die "Error opening $namefile: $!";
print w_LOG $text;
close (w_LOG);
print (win2dos($text));
}
sub write_noping
{
my $text = shift;
my $namehost = shift;
open (w_PING, ">>$namehost") || die "Error opening $namehost: $!";
print w_PING $text;
close (w_PING);
# print $text;
}
I plan to explore Assembler and Java as well.
#include
#include
#include
#include
#include
float f, k, d;
int page, t;
struct Y
{
private:
float zindex ( int a )
{
int i;
float r=0;
switch(a)
{
case 12: for(i=0;i<12;i++) r+=z[i]; r/=12; break;
case 13: for(i=0;i<12;i++) r+=z[i+12]; r/=12; break;
case 11: r=(z[11]+z[0]+z[23]+z[12])/4; break;
default: r=(z[a]+z[a+1]+z[a+12]+z[a+13])/4; break;
}
return r;
}
void DrawFlatness( int a )
{
int i, z[24];
switch(a)
{
case 12:
if((x[11]-x[0])*(y[1]-y[0])-(x[1]-x[0])*(y[11]-y[0])>=0)
{
for(i=0;i<12;i++) { z[2*i]=x[i]+320; z[2*i+1]=150-y[i]; }
fillpoly(12, z);
} break;
case 13:
if((x[12]-x[23])*(y[13]-y[12])-(x[13]-x[12])*(y[12]-y[23])>=0)
{
for(i=0;i<12;i++) { z[2*i]=x[i+12]+320; z[2*i+1]=150-y[i+12]; }
fillpoly(12, z);
} break;
case 11:
if((x[0]-x[11])*(y[12]-y[0])-(x[12]-x[0])*(y[0]-y[11])>=0)
{
z[0]=x[11]+320;
z[1]=150-y[11];
z[2]=x[0]+320;
z[3]=150-y[0];
z[4]=x[12]+320;
z[5]=150-y[12];
z[6]=x[23]+320;
z[7]=150-y[23];
fillpoly(4, z);
} break;
default:
if((x[a+1]-x[a])*(y[a+13]-y[a+1])-(x[a+13]-x[a+1])*(y[a+1]-y[a])>=0)
{
z[0]=x[a]+320;
z[1]=150-y[a];
z[2]=x[a+12]+320;
z[3]=150-y[a+12];
z[4]=x[a+13]+320;
z[5]=150-y[a+13];
z[6]=x[a+1]+320;
z[7]=150-y[a+1];
fillpoly(4, z);
}
}
}
public:
float x[24], y[24], z[24];
Y ( void )
{
int i;
x[0]=-50; y[0]=70;
x[1]=-30; y[1]=70;
x[2]=-1; y[2]=20;
x[3]=17; y[3]=70;
x[4]=37; y[4]=70;
x[5]=-10; y[5]=-50;
x[6]=-12; y[6]=-53;
x[7]=-18; y[7]=-54;
x[8]=-25; y[8]=-53;
x[9]=-28; y[9]=-52;
x[10]=-29; y[10]=-50;
x[11]=-10; y[11]=0;
for(i=0;i<12;i++) z[i]=10;
for(i=12;i<24;i++)
{
x[i]=x[i-12];
y[i]=y[i-12];
z[i]=-z[i-12];
}
}
void draw (int T)
{
int i, j, k, m[14];
float p;
setvisualpage(page);
delay(d);
page=(page==0)?1:0;
setactivepage(page);
cleardevice();
if(T==1)
{
for(i=0;i<11;i++)
line(x[i]+320, 150-y[i], x[i+1]+320, 150-y[i+1]);
line(x[0]+320, 150-y[0], x[11]+320, 150-y[11]);
for(i=12;i<23;i++)
line(x[i]+320, 150-y[i], x[i+1]+320, 150-y[i+1]);
line(x[12]+320, 150-y[12], x[23]+320, 150-y[23]);
for(i=0;i<12;i++)
line(x[i]+320, 150-y[i], x[i+12]+320, 150-y[i+12]);
}
else
{
if(zindex(0)<zindex(1)) { m[0]=0; m[1]=1; } else { m[0]=1; m[1]=0; } for(i=2;i<14;i++) { for(j=0;jzindex(i)&&i<12&&i!=4) { for(k=i; k>j; k--) m[k]=m[k-1]; break; }
m[j]=i;
}
for(i=0;i<14;i++)
DrawFlatness(m[i]);
}
}
void increase ( void )
{
int i;
for(i=0;i<24;i++) { x[i]*=k; y[i]*=k; z[i]*=k; }
}
void rotate_z( void )
{
int i;
float xn[24], yn[24];
for(i=0;i<24;i++)
{
xn[i]=x[i]*cos(f)-y[i]*sin(f);
yn[i]=x[i]*sin(f)+y[i]*cos(f);
}
for(i=0;i<24;i++) { x[i]=xn[i]; y[i]=yn[i]; }
}
void rotate_y( void )
{
int i;
float xn[24], zn[24];
for(i=0;i<24;i++)
{
xn[i]=x[i]*cos(f)-z[i]*sin(f);
zn[i]=x[i]*sin(f)+z[i]*cos(f);
}
for(i=0;i<24;i++) { x[i]=xn[i]; z[i]=zn[i]; }
}
void rotate_x( void )
{
int i;
float yn[24], zn[24];
for(i=0;i<24;i++)
{
yn[i]=y[i]*cos(f)-z[i]*sin(f);
zn[i]=y[i]*sin(f)+z[i]*cos(f);
}
for(i=0;i<24;i++) { y[i]=yn[i]; z[i]=zn[i]; }
}
void MoveAlongAxisX(void)
{
int i;
for(i=0;i<24;i++)
x[i]+=f;
}
void MoveAlongAxisY(void)
{
int i;
for(i=0;i<24;i++)
y[i]+=f;
}
void MoveAlongAxisZ(void)
{
int i;
for(i=0;i<24;i++)
z[i]+=f;
}
};
void main (void)
{
Y y;
int key, K, gd=VGA, gm=1;
page=0; d=10; t=1;
l:
closegraph;
printf("Just press F1 to show this help any time\n");
printf("Press F5 to see the framework\n");
printf("Press F6 for deleting invisible lines\n");
printf("Press F7 for color facets\n");
printf("Press w to rotate the letter around axis z clockwise\n");
printf("Press q to rotate the letter around axis z anticlockwise\n");
printf("Press s to rotate the letter around axis x clockwise\n");
printf("Press a to rotate the letter around axis x anticlockwise\n");
printf("Press x to rotate the letter around axis y clockwise\n");
printf("Press z to rotate the letter around axis y anticlockwise\n");
printf("Press 1 to increase the letter\n");
printf("Press 2 to decrease the letter\n");
printf("Press e or r to move the letter along axis x\n");
printf("Press d or f to move the letter along axis y\n");
printf("Press c or v to move the letter along axis z\n");
printf("Press up or down to accelerate or decelerate moving and rotating\n");
printf("Press Enter to stop any moving\n");
printf("Press any key to begin to work with the letter imidiately\n");
getch(); key=-1;
initgraph(&gd, &gm, "");
for(;;)
{
if(kbhit()!=0) key=getch();
switch(key)
{
case '1': k=1.001; K=key; break;
case '2': k=0.999; K=key; break;
case 'q': f=0.005; K=key; break;
case 'w': f=-0.005; K=key; break;
case 'a': f=0.005; K=key; break;
case 's': f=-0.005;K=key; break;
case 'z': f=0.005; K=key; break;
case 'x': f=-0.005; K=key; break;
case 'e': f=0.5; K=key; break;
case 'r': f=-0.5; K=key; break;
case 'd': f=0.5; K=key; break;
case 'f': f=-0.5; K=key; break;
case 'c': f=0.5; K=key; break;
case 'v': f=-0.5; K=key; break;
case 27: closegraph(); return;
case 72: f+=0.005; k+=0.001; d=d>5?d-0.01:d; key=0; break;
case 80: f-=0.005; k-=0.001; d=d<15?d+0.01:d; key=0; break;
case 13: K=key; break;
case 63: t=1; key=0; break;
case 64: t=2; setfillstyle(1, 0); key=0; break;
case 65: t=2; setfillstyle(1, 2); key=0; break;
case 59: goto l;
}
switch(K)
{
case '1': y.increase(); break;
case '2': y.increase(); break;
case 'q': y.rotate_z(); break;
case 'w': y.rotate_z(); break;
case 'a': y.rotate_x(); break;
case 's': y.rotate_x(); break;
case 'z': y.rotate_y(); break;
case 'x': y.rotate_y(); break;
case 'e': y.MoveAlongAxisX(); break;
case 'r': y.MoveAlongAxisX(); break;
case 'd': y.MoveAlongAxisY(); break;
case 'f': y.MoveAlongAxisY(); break;
case 'c': y.MoveAlongAxisZ(); break;
case 'v': y.MoveAlongAxisZ(); break;
case 27: closegraph(); return;
}
y.draw(t);
}
}
Today, I found a floppy disk with 5 programs that I wrote a very, very, very long time ago. Four of them can be run once just for fun. The two best ones, with textures and lighting, didn't run due to the video card, but I'm posting the other two. (I combined them into one.) I wrote them in C, but later I also rewrote them in Pascal. If you're going to watch them, be sure to press F7 during the process.
Where are you, programmers, when you are so needed?