deprecated/buildtools/buildsystemtools/BuildClient.pm
author jascui
Wed, 17 Nov 2010 13:39:51 +0800
changeset 688 27f7c5c966fc
parent 655 3f65fd25dfd4
permissions -rw-r--r--
delete orig.txt
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     1
# Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     2
# All rights reserved.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     3
# This component and the accompanying materials are made available
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     4
# under the terms of "Eclipse Public License v1.0"
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     5
# which accompanies this distribution, and is available
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     6
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     7
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     8
# Initial Contributors:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     9
# Nokia Corporation - initial contribution.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    10
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    11
# Contributors:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    12
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    13
# Description:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    14
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    15
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    16
package BuildClient;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    17
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    18
use FindBin;		# for FindBin::Bin
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    19
use lib "$FindBin::Bin/lib/freezethaw"; # For FreezeThaw
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    20
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    21
use strict;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    22
use Carp;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    23
use Msg;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    24
use FreezeThaw qw(freeze thaw);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    25
use Cwd 'chdir';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    26
use Compress::Zlib;			# For compression library routines
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    27
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    28
# Global Varibales
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    29
my $gClientName;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    30
my ($gHiResTimer) = 0; #Flag - true (1) if HiRes Timer module available
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    31
my ($gDebug) = 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    32
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    33
# Check if HiRes Timer is available
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    34
if (eval "require Time::HiRes;") {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    35
  $gHiResTimer = 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    36
} else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    37
  print "Cannot load HiResTimer Module\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    38
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    39
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    40
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    41
# GetClientVersion
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    42
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    43
# Inputs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    44
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    45
# Outputs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    46
# Client Version Number
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    47
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    48
# Description
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    49
# This function returns the Client version number
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    50
sub GetClientVersion
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    51
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    52
  return "1.3";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    53
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    54
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    55
# rcvd_msg_from_server
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    56
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    57
# Inputs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    58
# $iConn (Instance of the Msg Module)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    59
# $msg (the recieved message from the server)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    60
# $err (any error message from the Msg Module)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    61
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    62
# Outputs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    63
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    64
# Description
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    65
# This function processes the incoming message from the Build Server and acts upon them
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    66
sub rcvd_msg_from_server {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    67
    my ($iConn, $msg, $err) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    68
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    69
    my ($iResults, $iChdir);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    70
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    71
    # if the message is empty or a "Bad file descriptor" error happens
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    72
    # This usually means the the Build Server has closed the socket connection.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    73
    # The client is returned to trying to connect to a build server
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    74
    if (($msg eq "") || ($err eq "Bad file descriptor"))
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    75
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    76
      print "Server Disconnected\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    77
      return 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    78
    } elsif ($err ne "") {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    79
      print "Error is communication occured:$err\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    80
      return 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    81
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    82
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    83
    # Thaw the message, this decodes the text string sent from the server back into perl variables
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    84
    my ($sub_name, $iID, $iStage, $iComp, $iCwd, $iCommandline) = thaw ($msg);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    85
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    86
    # The server has determined that this client is using a non-unique client name.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    87
    # The server has added a random number on to the client name to try and make it unique.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    88
    # The server send this new name back to the client, so the two are in sync.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    89
    if ($sub_name eq 'ChangeClientName')
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    90
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    91
      print "ClientName changed to: $iID by the server\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    92
      $BuildClient::gClientName = $iID;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    93
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    94
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    95
    # The server sent and exit message to this client, so exit.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    96
    if ($sub_name eq 'Exit')
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    97
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    98
      print "Server request the client to exit\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    99
      exit 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   100
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   101
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   102
    # If the command sent by the server is "SetEnv", call the SetEnv Function and respond to server when complete
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   103
    if ($sub_name eq 'SetEnv')
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   104
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   105
      &SetEnv($iID, $iStage);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   106
      # Prepare and send the "SetEnv Ready" message to the server with the client name
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   107
      my $serialized_msg = freeze ("SetEnv Ready", $BuildClient::gClientName);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   108
      $iConn->transmit_immediately($serialized_msg);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   109
    } elsif ($sub_name eq 'Execute') {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   110
      # Process the "Execute" command
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   111
      print "Executing ID ". ($iID+1) ." Stage $iStage\n"; 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   112
      # Add the client side per command start timestamp
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   113
      &TimeStampStart(\$iResults);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   114
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   115
      eval {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   116
          no strict 'refs';  # Because we call the subroutine using
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   117
                             # a symbolic reference
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   118
          # Change the working directory, first replacing the environment variables
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   119
          $iCwd =~ s/%(\w+)%/$ENV{$1}/g;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   120
          $iCommandline =~ s/%(\w+)%/$ENV{$1}/g;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   121
          # If the changing of the working directory fails it will remain in the current directory
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   122
          $iChdir = chdir "$iCwd";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   123
          # Don't execute the command if the changing of the working directory failed.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   124
          if ($iChdir)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   125
          {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   126
            # Log the directory change
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   127
            print "Chdir $iCwd\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   128
            $iResults .= "Chdir $iCwd\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   129
            # Execute the "Execute" function, passing it the commandline to execute and collect the results
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   130
            $iResults .= normalize_line_breaks(&{$sub_name} ($iCommandline));
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   131
          } else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   132
            $iResults .= "ERROR: Cannot change directory to $iCwd for $iComp\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   133
          }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   134
      # Add the client side per command end HiRes timestamp if available
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   135
      &TimeStampEnd(\$iResults);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   136
      };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   137
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   138
      # Send an appropriate message back to the server, depending on error situation
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   139
      if ($@ && $iChdir) {      # Directory changed OK, but an error occurred subsequently
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   140
          # Handle Generic errors
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   141
          $msg = bless \$@, "RPC::Error\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   142
          
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   143
          # Freeze the perl variables into a text string to send to the server
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   144
          $msg = freeze('Results', $BuildClient::gClientName, $iID, $iStage, $iComp, $iCwd, $iCommandline, Compress($msg));
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   145
      } else {                  # Directory change failed OR no error at all.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   146
          # $iResults will contain the error string if changing working directories failed
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   147
          #     otherwise it will contain the output of the execution of the commandline
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   148
          # Freeze the perl variables into a text string to send to the server
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   149
          $msg = freeze('Results', $BuildClient::gClientName, $iID, $iStage, $iComp, $iCwd, $iCommandline, Compress($iResults));
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   150
      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   151
      # Send the message back to the server
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   152
      $iConn->transmit_immediately($msg);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   153
      
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   154
    } 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   155
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   156
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   157
# normalize_line_breaks
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   158
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   159
# Inputs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   160
# $lines  Text string which may consist of many lines
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   161
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   162
# Outputs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   163
# $lines  Text string which may consist of many lines
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   164
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   165
# Description
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   166
# This subroutine converts any Unix, Macintosh or other line breaks into the DOS/Windows CRLF sequence
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   167
# Text in each line remains unchanged. Empty lines are discarded.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   168
sub normalize_line_breaks
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   169
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   170
    my $lines = '';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   171
    foreach my $line (split /\r|\n/, shift)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   172
        {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   173
        unless ($line) { next; }    # Discard empty line
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   174
        $lines .= "$line\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   175
        }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   176
    return $lines;    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   177
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   178
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   179
# Execute
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   180
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   181
# Inputs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   182
# @args
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   183
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   184
# Outputs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   185
# @results
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   186
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   187
# Description
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   188
# This Executes the command in the args, must return and array
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   189
# It combines STDERR into STDOUT
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   190
sub Execute
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   191
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   192
  my (@iCommandline) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   193
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   194
  print "Executing '@iCommandline'\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   195
  if (! defined($BuildClient::gDebug))
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   196
  {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   197
    return my $ireturn= `@iCommandline 2>&1`;   # $ireturn is not used but ensures that a scalar is returned.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   198
  } else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   199
    if ($BuildClient::gDebug ne "")
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   200
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   201
      # Open log file for append, if cannot revert to STDOUT
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   202
      open DEBUGLOG, ">>$BuildClient::gDebug" || $BuildClient::gDebug== "";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   203
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   204
    my $iResults;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   205
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   206
    print DEBUGLOG "Executing '@iCommandline'\n" if ($BuildClient::gDebug ne "");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   207
    open PIPE, "@iCommandline 2>&1 |";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   208
    while (<PIPE>)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   209
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   210
      if ($BuildClient::gDebug ne "")
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   211
      {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   212
        print DEBUGLOG $_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   213
      } else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   214
        print $_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   215
      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   216
      $iResults .= $_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   217
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   218
    close PIPE;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   219
    close DEBUGLOG if ($BuildClient::gDebug ne "");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   220
    return $iResults;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   221
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   222
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   223
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   224
# SetEnv
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   225
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   226
# Inputs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   227
# @args
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   228
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   229
# Outputs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   230
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   231
# Description
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   232
# This function sets the local Environment.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   233
sub SetEnv
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   234
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   235
  my ($iKey, $iValue) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   236
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   237
  # Replace an environment Variable referenced using %Variable% with the contents of the Environment Variable
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   238
  # This allows the use of one Environment Variable in another as long as it is already set
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   239
  $iValue =~ s/%(\w+)%/$ENV{$1}/g;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   240
  print "Setting Environment Variable $iKey to $iValue\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   241
  $ENV{$iKey} = $iValue;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   242
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   243
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   244
# Connect
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   245
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   246
# Inputs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   247
# $iDataSource - Reference to array of Hostname:Port of BuildServers to connect to)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   248
# $iConnectWait (How often it polls for a build server)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   249
# $iClientName (Client name used to help identify the machine, Must be unique)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   250
# $iDebug - Debug Option
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   251
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   252
# Outputs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   253
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   254
# Description
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   255
# This function connects to the BuildServer and reads commands to run
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   256
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   257
sub Connect
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   258
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   259
  my ($iDataSource, $iConnectWait, $iClientName, $iExitAfter, $iDebug) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   260
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   261
  my ($iSuccessConnect);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   262
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   263
  # Set the Client name
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   264
  $BuildClient::gClientName = $iClientName;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   265
  # Set Global Debug flag/filename
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   266
  $BuildClient::gDebug = $iDebug;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   267
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   268
  # In continual loop try and connect to the datasource
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   269
  while (($iExitAfter == -1) || ($iSuccessConnect < $iExitAfter))
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   270
  {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   271
    # Cycle through the datasource list
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   272
    my $iMachine = shift @$iDataSource;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   273
    push @$iDataSource, $iMachine;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   274
    print "Connecting to $iMachine\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   275
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   276
    # Process the datasource into hostname and port number
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   277
    my ($iHostname,$iPort) = $iMachine =~ /^(\S+):(\d+)/;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   278
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   279
    # Create an instance of the message Module to handle the TCP/IP connection
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   280
    my $iConn = Msg->associate($iPort, $iHostname, \&rcvd_msg_from_server);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   281
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   282
    # Check the status of the connection attempt
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   283
    if ($iConn)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   284
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   285
      # Connection was succesful
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   286
      print "Connection successful to $iMachine\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   287
      $iSuccessConnect++;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   288
      # Send a "Ready" command to the Server
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   289
      my $serialized_msg = freeze ("Ready", $BuildClient::gClientName, &GetClientVersion);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   290
      print "Sending Ready\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   291
      $iConn->transmit_immediately($serialized_msg);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   292
      # Start the message processing loop with inital timeout of 300 seconds
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   293
      Msg->result_iteration(300);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   294
      # Server disconnected, clean up by chdir to root
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   295
      chdir "\\";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   296
      # Set the client name back to the name specified on the commandline just in case it has had it's name changed.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   297
      $BuildClient::gClientName = $iClientName;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   298
    } else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   299
      # Connection Failed, wait specified time before continuing and trying another connection attempt
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   300
      print "Could not connect to $iHostname:$iPort\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   301
      print "Trying another connection attempt in $iConnectWait seconds\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   302
      sleep $iConnectWait;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   303
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   304
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   305
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   306
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   307
# TimeStampStart
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   308
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   309
# Inputs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   310
# $iData - Reference to variable to put the start time stamp
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   311
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   312
# Outputs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   313
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   314
# Description
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   315
# This places a timestamp in the logs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   316
sub TimeStampStart
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   317
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   318
  my $ref = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   319
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   320
  # Add the client side per command start timestamp
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   321
  $$ref = "++ Started at ".localtime()."\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   322
  # Add the client side per command start HiRes timestamp if available
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   323
  if ($gHiResTimer == 1)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   324
  {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   325
    $$ref .= "+++ HiRes Start ".Time::HiRes::time()."\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   326
  } else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   327
    # Add the HiRes timer unavailable statement
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   328
    $$ref .= "+++ HiRes Time Unavailable\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   329
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   330
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   331
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   332
# TimeStampEnd
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   333
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   334
# Inputs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   335
# $iData - Reference to variable to put the end time stamp
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   336
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   337
# Outputs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   338
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   339
# Description
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   340
# This places a timestamp in the logs
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   341
sub TimeStampEnd
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   342
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   343
  my $ref = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   344
 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   345
  # Add the client side per command end HiRes timestamp if available
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   346
  $$ref .= "+++ HiRes End ".Time::HiRes::time()."\n" if ($gHiResTimer == 1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   347
   # Add the client side per command end timestamp
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   348
  $$ref .= "++ Finished at ".localtime()."\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   349
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   350
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   351
# Subroutine for compressing data stream.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   352
# Input: message to be compressed.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   353
# Output: compressed message, ready for sending.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   354
sub Compress($)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   355
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   356
    my $msg = shift; # Get the message.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   357
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   358
    # Initialise deflation stream
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   359
    my $x;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   360
    eval {$x = deflateInit() or die "Error: Cannot create a deflation stream\n";};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   361
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   362
    if($@) # Deflation stream creationg has failed.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   363
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   364
	    return Compress("Error: creation of deflation stream failed: $@\n");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   365
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   366
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   367
    # Compress the message
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   368
    my ($output, $status);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   369
    my ($output2, $status2);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   370
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   371
    # First attempt to perform the deflation
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   372
    eval { ($output, $status) = $x -> deflate($msg); };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   373
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   374
    if($@) # Deflation has failed.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   375
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   376
	    $x = deflateInit();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   377
	    ($output, $status) = $x -> deflate("ERROR: Compression failed: $@\n");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   378
	    ($output2, $status2) = $x -> flush();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   379
	    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   380
	    return $output.$output2;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   381
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   382
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   383
    # Now attempt to complete the compression
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   384
    eval { ($output2, $status2) = $x -> flush(); };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   385
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   386
    if($@) # Deflation has failed.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   387
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   388
	    $x = deflateInit();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   389
	    ($output, $status) = $x -> deflate("ERROR: Compression failed: $@\n");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   390
	    ($output2, $status2) = $x -> flush();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   391
	    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   392
	    return $output.$output2;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   393
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   394
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   395
    if($status != Z_OK) # Deflation has failed.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   396
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   397
        $x = deflateInit();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   398
	    ($output, $status) = $x -> deflate("ERROR: Compression failed: $@\n");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   399
	    ($output2, $status2) = $x -> flush();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   400
	    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   401
	    return $output.$output2;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   402
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   403
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   404
    # Attempt to complete the compressions
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   405
    if($status2 != Z_OK)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   406
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   407
        $x = deflateInit();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   408
	    ($output, $status) = $x -> deflate("ERROR: Compression failed: $@\n");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   409
	    ($output2, $status2) = $x -> flush();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   410
	    return $output.$output2;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   411
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   412
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   413
    # Return the compressed output.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   414
    return $output . $output2;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   415
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   416
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   417
1;