gnokii-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SMS Send failed (error=19)


From: Pin~1.com.my
Subject: SMS Send failed (error=19)
Date: Thu, 18 Jul 2002 03:04:53 +0800

Dear All,

I send SMS in bulk using a queqe system (perl script). Sometimes, in the
middle of the process, the error 19 appeared, and once it happend, the less
of email in queqe will not be able to send anymore. The less attempt will
come out with error 23.

Do I have any way to prevent this?

Please advise, Thank you

Cheers,
Earnpin Lee


Queqe Script :
------------------------------
#!/usr/bin/perl -w

$gnokii   = "/usr/bin/gnokii";
@db_connect     = ("DBI:mysql:sms", "", "");
$delay_hour  = 5;

$sms_outbox   = 'sms_outbox';
$sms_state   = 'sms_state';
$sms_sent   = 'sms_sent';
$sms_retry   = 'sms_retry';
$sms_inbox    = 'sms_inbox';
$sms_fail     = 'sms_fail';

use lib "/usr/local/lib/perl5/site_perl/5.6.1/i686-linux";
use vars qw/%in $DBH/;
eval {
    require 5.002;
    require DBI;
};
$DBH = DBI->connect (@db_connect);

&sendsms;
&retry;
&receivesms;

END { $DBH and ($DBH->disconnect()); }
exit;


sub sendsms {
# ------------------------------------------
# send SMS from outbox

 my ($port_status, $outbox_status);
 my ($query, $sth, @rc, $rc, $reply);
 my ($id, $no, $msg, $userid, $goupid, $datetime, $error);
 my ($query1, $query2, $query3, $query4);


    $port_status = &check_port;
 $outbox_status = &check_outbox;

 if ($port_status eq "ok" and $outbox_status eq "ok") {
  `killall -9 gnokii`;
  &lock_port;

  $query = qq!
        SELECT * FROM $sms_outbox
        !;
  $sth = $DBH->prepare ($query);
  $sth->execute;

  while (@rc = $sth->fetchrow_array)

   $rc[2] =~ s/\'//g;
   $rc[2] =~ s/\s+/ /g;
   $id       = $DBH->quote($rc[0]);
   $no       = $DBH->quote($rc[1]);
   $msg      = $DBH->quote($rc[2]);
   $userid   = $DBH->quote($rc[3]);
   $groupid  = $DBH->quote($rc[4]);
   $datetime = $DBH->quote(&get_datetime);

   $reply = `echo $msg |$gnokii --sendsms $rc[1]`;
   sleep(14);
   chomp($reply);
   $error    = $DBH->quote($reply);

   if($reply eq "Send succeeded!") {
    $query1 = qq!
       DELETE FROM $sms_outbox
       WHERE (id = $id)
       !;
    $DBH->do($query1);

    $query2 = qq!
       INSERT INTO $sms_sent (no, msg, datetime, userid, groupid)
       VALUES ($no, $msg, $datetime, $userid, $groupid)
       !;
    $DBH->do($query2);
   }
   else {
    $query3 = qq!
       DELETE FROM $sms_outbox
       WHERE (id = $id)
       !;
    $DBH->do($query3);

    $query4 = qq!
       INSERT INTO $sms_retry (no, msg, datetime, userid, groupid, error,
state)
       VALUES ($no, $msg, $datetime, $userid, $groupid, $error, '1')
       !;
    $DBH->do($query4);
   }

   sleep(1);
   `killall -9 gnokii`;
  }
  $sth->finish;
  &free_port;
 }
}


sub retry {
# ------------------------------------------
# send SMS from retry

 my ($port_status, $retry_status);
 my ($query, $sth, @rc, $rc);
 my ($datetime_n, $date_n, $hour_n, $min_n, $sec_n, $time_n);
 my ($datetime_r, $date_r, $hour_r, $min_r, $sec_r, $time_r);
 my ($can_retry, $total_sec, $retry_time);
 my ($id, $no, $msg, $userid, $goupid, $datetime, $reply, $error);
 my ($query1, $query2, $query3, $query4);

 $port_status = &check_port;
 $retry_status = &check_retry;

 if ($port_status eq "ok" and $retry_status eq "ok") {
  `killall -9 gnokii`;
  &lock_port;

  $query = qq!
        SELECT * FROM $sms_retry
     WHERE state = 1
        !;
  $sth = $DBH->prepare ($query);
  $sth->execute;

  while (@rc = $sth->fetchrow_array) {

   $datetime_n = &get_datetime;
   chomp ($datetime_n);
   ($date_n, $hour_n, $min_n, $sec_n) = $datetime_n =~ /(.*)
(.*):(.*):(.*)/;
   $time_n = ($hour_n * 3600) + ($min_n * 60) + $sec_n;
   $date_n =~ s/-//g;

   $datetime_r = $rc[3];
   chown ($datetime_r);
   ($date_r, $hour_r, $min_r, $sec_r) = $datetime_r =~ /(.*)
(.*):(.*):(.*)/;
   $time_r = ($hour_r * 3600) + ($min_r * 60) + $sec_r;
   $date_r =~ s/-//g;

   $can_retry = 0;
   $total_sec = $delay_hour * 3600;

# If it is one day before, we check if retry_time >= $total_sec
   if ($date_n > $date_r) {
    if ($date_n - $date_r == 1) {
     $retry_time = (86400 - $time_r) + $time_n;
     if ($retry_time >= $total_sec) {
      $can_retry = 1;
     }
    }
    else {
     $can_retry =1;
    }
   }

# If is the same day, we can simply minus the time
   if ($date_n == $date_r) {
    $retry_time = $time_n - $time_r;
    if ($retry_time >= $total_sec) {
     $can_retry = 1;
    }
   }

   if ($can_retry == 1) {
    $id       = $DBH->quote($rc[0]);
    $no       = $DBH->quote($rc[1]);
    $msg      = $DBH->quote($rc[2]);
    $userid   = $DBH->quote($rc[4]);
    $groupid  = $DBH->quote($rc[5]);
    $datetime = $DBH->quote(&get_datetime);
    $reply = `echo $msg |$gnokii --sendsms $rc[1]`;
    sleep(1);
    chomp($reply);
    $error    = $DBH->quote($reply);

    if($reply eq "Send succeeded!") {
     $query1 = qq!
        UPDATE $sms_retry SET state = '0'
        WHERE (id = $id)
       !;
     $DBH->do($query1);

     $query2 = qq!
        INSERT INTO $sms_sent (no, msg, datetime, userid, groupid)
        VALUES ($no, $msg, $datetime, $userid, $groupid)
        !;
     $DBH->do($query2);
    }
    else {
     $query3 = qq!
        UPDATE $sms_retry SET state = '0'
        WHERE (id = $id)
        !;
     $DBH->do($query3);

     $query4 = qq!
        INSERT INTO $sms_fail (no, msg, datetime, userid, groupid, error)
        VALUES ($no, $msg, $datetime, $userid, $groupid, $error)
        !;
     $DBH->do($query4);
    }

    sleep(2);
    `killall -9 gnokii`;
   }
  }
  $sth->finish;
  &free_port;
 }
}


sub receivesms {
# ------------------------------------------
# retriving sms message

 my ($port_status);
 my ($reply, $place, $sender, $tmp, @tmp);
 my (@lines, $line, $msg, $datetime);

 $port_status = &check_port;

 if ($port_status eq "ok") {
  `killall -9 gnokii`;
  &lock_port;

  for ($place=1; $place<11; $place++) {
   $reply = `$gnokii --getsms ME $place`;
   `killall -9 gnokii`;
   chomp($reply);
   if ($reply ne "SMS location ME $place empty." && $reply ne "" && $reply
!~ /GetSMS ME $place failed!/) {
    @lines = split "\n",$reply;
    $sender="";
    $tmp="";
    foreach (@lines) {
     if (/Sender:/) {
      s/.*Sender:/Sender:/;
      ($tmp,$sender,@tmp) = split (" ",$_);
      chomp($sender);
      $sender =~ s/\+//g;
     }
     if (/^Date/) {
      ($datetime) = $_;
      chomp($datetime);
      $datetime =~ s/Date\/time: file://g;
      $datetime =~ /(.*)\/(.*)\/(.*) (.*):(.*):(.*) .*/;
      $datetime = "$3-$2-$1 $4:$5:$6";
     }
    }
    foreach $line (@lines) {
     next if ($line =~ /^Date/ || $line =~ /Sender:/);
     $msg .= $line . "\n";
    }
    $sender   = $DBH->quote($sender);
    $msg      = $DBH->quote($msg);
    $datetime = $DBH->quote($datetime);
    $query = qq!
       INSERT INTO $sms_inbox (no, msg, datetime, state)
       VALUES ($sender, $msg, $datetime, '1')
      !;
    $DBH->do($query);
    $msg = "";
    $reply = `$gnokii --deletesms ME $place $place`;
    `killall -9 gnokii`;
   }
   else {
    if ($place == 1) {
     $place = 11;
    }
   }
  }
  &free_port;
 }
}


sub lock_port {
# ------------------------------------------
# insert 1 to lock port

 my ($query);
 $query = qq!
       INSERT INTO $sms_state
    VALUES ('1')
       !;
 $DBH->do($query);
}


sub free_port {
# ------------------------------------------
# empty sms_state to free port

 my ($query);
 $query = qq!
       DELETE FROM $sms_state
       !;
 $DBH->do($query);
}


sub check_port {
# ------------------------------------------
# return ok if port is free

 my ($query, $sth, $rc, $status);
 $query = qq!
       SELECT * FROM $sms_state
       !;
 $sth = $DBH->prepare ($query);
 $sth->execute;
 $rc = $sth->rows();
 $status = "";
 if (!$rc) {
  $status = "ok";
 }
 $sth->finish;
 return ($status);
}


sub check_outbox {
# ------------------------------------------
# return ok if outbox is not empty

 my ($query, $sth, $rc, $status);
 $query = qq!
       SELECT * FROM $sms_outbox
       !;
 $sth = $DBH->prepare ($query);
 $sth->execute;
 $rc = $sth->rows();
 $status = "";
 if ($rc) {
  $status = "ok";
 }
 $sth->finish;
 return ($status);
}


sub check_retry {
# ------------------------------------------
# return ok if retry is not empty

 my ($query, $sth, $rc, $status);
 $query = qq!
       SELECT * FROM $sms_retry
    WHERE state = 1
       !;
 $sth = $DBH->prepare ($query);
 $sth->execute;
 $rc = $sth->rows();
 $status = "";
 if ($rc) {
  $status = "ok";
 }
 $sth->finish;
 return ($status);
}


sub get_datetime {
    my ($time);
    $_[0] ? ($time = $_[0]) : ($time = time());

    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
localtime ($time);
    $year = $year + 1900;
    ($mday < 10) and ($mday = "0$mday");

 if ($hour < 10) { $hour = "0" . $hour; }
    if ($min < 10) {  $min  = "0" . $min; }
    if ($sec < 10) {  $sec =  "0" . $sec; }

 my @months = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;
 $mon = $mon + 1;
 if ($mon < 10) {  $mon =  "0" . $mon; }
 if ($mday < 10) {  $mday =  "0" . $mday; }
    return "$year-$mon-$mday $hour:$min:$sec";

}








reply via email to

[Prev in Thread] Current Thread [Next in Thread]