From cd2b562f00e37a3aa2d7b21eaad8472feaedf4ae Mon Sep 17 00:00:00 2001 From: rettenbe Date: Fri, 11 Apr 2008 08:12:30 +0000 Subject: [PATCH] first code snippets of messaging handling git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@10346 594d385d-05f5-0310-b6e9-bd551577e9d8 --- gosa-si/client/events/gosaTriggered.pm | 2 +- gosa-si/gosa-si-server | 89 +++++++++++++++++++++++++- gosa-si/server/events/gosaTriggered.pm | 57 +++++++++++++++-- gosa-si/tests/client.php | 10 +-- 4 files changed, 148 insertions(+), 10 deletions(-) diff --git a/gosa-si/client/events/gosaTriggered.pm b/gosa-si/client/events/gosaTriggered.pm index 9e57a5460..38c969b4a 100644 --- a/gosa-si/client/events/gosaTriggered.pm +++ b/gosa-si/client/events/gosaTriggered.pm @@ -96,7 +96,7 @@ sub trigger_action_reboot { close(FILE); } else { - system( "/sbin/shutdown -r +$timeout &" ); + #system( "/sbin/shutdown -r +$timeout &" ); } return; diff --git a/gosa-si/gosa-si-server b/gosa-si/gosa-si-server index b93f07cc7..5ee945afe 100755 --- a/gosa-si/gosa-si-server +++ b/gosa-si/gosa-si-server @@ -64,6 +64,7 @@ my (%cfg_defaults, $foreground, $verbose, $ping_timeout); my ($bus_activ, $bus, $msg_to_bus, $bus_cipher); my ($server); my ($gosa_server, $job_queue_timeout, $job_queue_loop_delay); +my ($messaging_db_loop_delay); my ($known_modules); my ($pid_file, $procid, $pid, $log_file); my ($arp_activ, $arp_fifo); @@ -162,7 +163,7 @@ my $arch = "i386"; # holds all messages which should be delivered to a user our $messaging_db; our $messaging_tn = "messaging"; -our @messaging_col_names = ('subject', 'from', 'to', 'flag', 'direction', 'delivery_time', 'message', 'timestamp', 'id INTEGER', ); +our @messaging_col_names = ('subject', 'message_from', 'message_to', 'flag', 'direction', 'delivery_time', 'message', 'timestamp', 'id INTEGER' ); my $messaging_file_name; # path to directory to store client install log files @@ -203,6 +204,7 @@ my $max_children = 2; "port" => [\$gosa_port, "20082"], "job-queue" => [\$job_queue_file_name, '/var/lib/gosa-si/jobs.db'], "job-queue-loop-delay" => [\$job_queue_loop_delay, 3], + "messaging-db-loop-delay" => [\$messaging_db_loop_delay, 3], "key" => [\$GosaPackages_key, "none"], }, "SIPackages" => { @@ -1235,6 +1237,8 @@ sub trigger_db_loop { my ($kernel) = @_ ; $kernel->delay_set('watch_for_new_jobs', $job_queue_loop_delay); $kernel->delay_set('watch_for_done_jobs', $job_queue_loop_delay); + $kernel->delay_set('watch_for_new_messages', $messaging_db_loop_delay); + $kernel->delay_set('watch_for_done_messages', $messaging_db_loop_delay); } sub watch_for_done_jobs { @@ -1334,6 +1338,87 @@ sub watch_for_new_jobs { } +sub watch_for_new_messages { + my ($kernel,$heap) = @_[KERNEL, HEAP]; + my @coll_user_msg; # collection list of outgoing messages + + # check messaging_db for new incoming messages with executable timestamp + my $timestamp = &get_time(); + #my $sql_statement = "SELECT * FROM $messaging_tn WHERE (CAST (delivery_time AS INTEGER) + 120) < $timestamp"; + my $sql_statement = "SELECT * FROM $messaging_tn WHERE ( (CAST(timestamp AS INTEGER))<$timestamp AND flag='n' AND direction='in' )"; + my $res = $messaging_db->exec_statement( $sql_statement ); + + foreach my $hit (@{$res}) { + + # create outgoing messages + my $message_to = @{$hit}[2]; + + # translate message_to to plain login name + my @reciever_l = ($message_to); + my $message_id = @{$hit}[8]; + + #add each outgoing msg to messaging_db + my $reciever; + foreach $reciever (@reciever_l) { + my $sql_statement = "INSERT INTO $messaging_tn (subject, message_from, message_to, flag, direction, delivery_time, message, timestamp, id) ". + "VALUES ('". + @{$hit}[0]."', '". # subject + @{$hit}[1]."', '". # message_from + $reciever."', '". # message_to + "none"."', '". # flag + "out"."', '". # direction + @{$hit}[5]."', '". # delivery_time + @{$hit}[6]."', '". # message + $timestamp."', '". # timestamp + @{$hit}[8]. # id + "')"; + &daemon_log("M DEBUG: $sql_statement", 1); + my $res = $messaging_db->exec_statement($sql_statement); + &daemon_log("M INFO: message '".@{$hit}[8]."' is prepared for delivery to reciever '$reciever'", 5); + } + + # send outgoing messages + my $sql_statement = "SELECT * FROM $messaging_tn WHERE ( flag='p' AND direction='out' )"; + my $res = $messaging_db->exec_statement( $sql_statement ); + foreach my $hit (@{$res}) { + # add subject, from, to and message to list coll_user_msg + my @user_msg = [@{$hit}[0], @{$hit}[1], $reciever, @{$hit}[6]]; + push( @coll_user_msg, \@user_msg); + } + + # send outgoing list to myself (gosa-si-server) to deliver each message to user + # reason for this workaround: if to much messages have to be delivered, it can come to + # denial of service problems of the server. so, the incoming message list can be processed + # by a forked child and gosa-si-server is always ready to work. + my $collection_out_msg = &create_xml_hash("collection_user_messages", $server_address, $server_address); + # add to hash 'msg1' => [subject, from, to, message] + # hash to string + # send msg to myself +# TODO + + # set incoming message to flag d=deliverd + $sql_statement = "UPDATE $messaging_tn SET flag='p' WHERE id='$message_id'"; + &daemon_log("M DEBUG: $sql_statement", 7); + $res = $messaging_db->update_dbentry($sql_statement); + &daemon_log("M INFO: message '".@{$hit}[8]."' is set to flag 'p' (processed)", 5); + + } + + $kernel->delay_set('watch_for_new_messages', $messaging_db_loop_delay); + + + return; +} + + +sub watch_for_done_messages { + my ($kernel,$heap) = @_[KERNEL, HEAP]; + + $kernel->delay_set('watch_for_done_messages', $messaging_db_loop_delay); + return; +} + + sub get_ldap_handle { my ($session_id) = @_; my $heap; @@ -2234,6 +2319,8 @@ POE::Session->create( inline_states => { _start => \&_start, sig_handler => \&sig_handler, + watch_for_new_messages => \&watch_for_new_messages, + watch_for_done_messages => \&watch_for_done_messages, watch_for_new_jobs => \&watch_for_new_jobs, watch_for_done_jobs => \&watch_for_done_jobs, create_packages_list_db => \&run_create_packages_list_db, diff --git a/gosa-si/server/events/gosaTriggered.pm b/gosa-si/server/events/gosaTriggered.pm index 30f946424..3f651b8be 100644 --- a/gosa-si/server/events/gosaTriggered.pm +++ b/gosa-si/server/events/gosaTriggered.pm @@ -54,6 +54,57 @@ sub get_events { } sub send_user_msg { + my ($msg, $msg_hash, $session_id) = @_ ; + my $header = @{$msg_hash->{'header'}}[0]; + my $source = @{$msg_hash->{'source'}}[0]; + my $target = @{$msg_hash->{'target'}}[0]; + + #my $subject = &decode_base64(@{$msg_hash->{'subject'}}[0]); + my $subject = @{$msg_hash->{'subject'}}[0]; + my $from = @{$msg_hash->{'from'}}[0]; + my $to = @{$msg_hash->{'to'}}[0]; + my $delivery_time = @{$msg_hash->{'delivery_time'}}[0]; + #my $message = &decode_base64(@{$msg_hash->{'message'}}[0]); + my $message = @{$msg_hash->{'message'}}[0]; + + # keep job queue uptodate if necessary + my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0]; + if( defined $jobdb_id) { + my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'"; + &main::daemon_log("$session_id DEBUG: $sql_statement", 7); + my $res = $main::job_db->exec_statement($sql_statement); + } + + # error handling + if (not $delivery_time =~ /^\d{14}$/) { + my $error_string = "delivery_time '$delivery_time' is not a valid timestamp, please use format 'yyyymmddhhmmss'"; + &main::daemon_log("$session_id ERROR: $error_string", 1); + return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string)); + } + + # add incoming message to messaging_db + my $func_dic = {table=>$main::messaging_tn, + primkey=>['id'], + subject=>$subject, + message_from=>$from, + message_to=>$to, + flag=>"n", + direction=>"in", + delivery_time=>$delivery_time, + message=>$message, + timestamp=>&get_time(), + }; + my $res = $main::messaging_db->add_dbentry($func_dic); + if (not $res == 0) { + &main::daemon_log("$session_id ERROR: gosaTriggered.pm: cannot add message to message_db: $res", 1); + } else { + &main::daemon_log("$session_id INFO: gosaTriggered.pm: message with subject '$subject' successfully added to message_db", 5); + } + + return; +} + +sub send_user_msg_OLD { my ($msg, $msg_hash, $session_id) = @_ ; my @out_msg_l; my @user_list; @@ -86,16 +137,14 @@ sub send_user_msg { } - my $ldap_handle = &main::get_ldap_handle($session_id); # resolve groups to users + my $ldap_handle = &main::get_ldap_handle($session_id); if( @group_list ) { - # build ldap connection if( not defined $ldap_handle ) { &main::daemon_log("$session_id ERROR: cannot connect to ldap", 1); return (); } - foreach my $group (@group_list) { - # Perform search + foreach my $group (@group_list) { # Perform search my $mesg = $ldap_handle->search( base => $main::ldap_base, scope => 'sub', diff --git a/gosa-si/tests/client.php b/gosa-si/tests/client.php index 330121249..b3e63b2ac 100755 --- a/gosa-si/tests/client.php +++ b/gosa-si/tests/client.php @@ -59,6 +59,7 @@ if($sock->connected()){ #$data = "
gosa_new_key_for_client
00:01:6c:9d:b9:fa 10.89.1.31:20081
"; #$data = "
job_trigger_action_wake
00:01:6c:9d:b9:fa GOSA 19700101000000
"; #$data = "
gosa_trigger_action_faireboot
00:01:6c:9d:b9:fa GOSA
"; +#$data = "
gosa_trigger_action_reboot
00:01:6c:9d:b9:fa GOSA
"; #$data = "
job_trigger_action_reinstall
GOSA 00:01:6c:9d:b9:fa 00:01:6c:9d:b9:fa 19700101000000
"; #$data = "
job_trigger_action_instant_update
00:01:6c:9d:b9:fa GOSA 19700101000000
"; #$data = "
gosa_ping
00:01:6c:9d:b9:fa GOSA
"; @@ -73,10 +74,12 @@ if($sock->connected()){ # recreate_fai_server_db #$data = "
gosa_recreate_fai_server_db
GOSA GOSA
"; -# testing +########### +# messaging #$data = "
gosa_send_user_msg
GOSA GOSA susi harald susi gosa-admins-all
"; #$data = "
gosa_send_user_msg
GOSA GOSA gosa-admins-all kaffeepause
"; -#$data = "
gosa_send_user_msg
GOSA GOSA cajus.pollmeier kaffeepause
"; +#$data = "
gosa_send_user_msg
GOSA GOSA rettenbe kaffeepause
"; +$data = "
gosa_send_user_msg
GOSA GOSA eine wichtige nachricht me you 20130101235959 kaffeepause
"; ################ @@ -84,12 +87,11 @@ if($sock->connected()){ # all date and mac parameter accept regular expression as input unless other instructions are given # show_log_by_mac, show_log_by_date, show_log_by_date_and_mac, show_log_files_by_date_and_mac, # get_log_file_by_date_and_mac, delete_log_by_date_and_mac, get_recent_log_by_mac -$data = "
gosa_show_log_by_mac
GOSA GOSA 00:01:6C:9D:B9:FA 00:01:6c:9d:b9:fb
"; +#$data = "
gosa_show_log_by_mac
GOSA GOSA 00:01:6C:9D:B9:FA 00:01:6c:9d:b9:fb
"; #$data = "
gosa_show_log_by_date
GOSA GOSA 20080313 20080323
"; #$data = "
gosa_show_log_by_date_and_mac
GOSA GOSA 200803 00:01:6c:9d:b9:FA
"; #$data = "
gosa_delete_log_by_date_and_mac
GOSA GOSA 00:01:6c:9d:b9:fa
"; #$data = "
gosa_get_recent_log_by_mac
GOSA GOSA 00:01:6c:9d:b9:fa
"; - # exact date and mac are required as input #$data = "
gosa_show_log_files_by_date_and_mac
GOSA GOSA install_20080311_090900 00:01:6c:9d:b9:fa
"; #$data = "
gosa_get_log_file_by_date_and_mac
GOSA GOSA install_20080311_090900 00:01:6c:9d:b9:fa boot.log
"; -- 2.30.2