| 1 | #!/usr/bin/perl |
| 2 | ################################################################################ |
| 3 | # |
| 4 | # File: cqc.pm,v |
| 5 | # Revision: 1.1.1.1 |
| 6 | # Description: Perl Module interface to cqd (ClearQuest Daemon). This is used |
| 7 | # by cqc and cgi script to talk to cqd. |
| 8 | # Author: Andrew@DeFaria.com |
| 9 | # Created: Fri May 31 15:34:50 2002 |
| 10 | # Modified: 2007/05/17 07:45:48 |
| 11 | # Language: Perl |
| 12 | # |
| 13 | # (c) Copyright 2007, ClearSCM, Inc., all rights reserved. |
| 14 | # |
| 15 | ################################################################################ |
| 16 | use IO::Socket; |
| 17 | |
| 18 | package cqc; |
| 19 | require (Exporter); |
| 20 | @ISA = qw (Exporter); |
| 21 | |
| 22 | @EXPORT = qw (Connect, GetBugRecord, Disconnect %fields $command $verbose); |
| 23 | |
| 24 | my $host; |
| 25 | my $port = 1500; |
| 26 | my $command; |
| 27 | my $default_server = "sons-clearcase"; |
| 28 | my $verbose; |
| 29 | |
| 30 | BEGIN { |
| 31 | my $cqcversion = "1.0"; |
| 32 | |
| 33 | # Reopen STDOUT. This is because cqperl screws around with STDOUT in some |
| 34 | # weird fashion |
| 35 | open STDOUT, ">-" or die "Unable to reopen STDOUT\n"; |
| 36 | # Set unbuffered output for the same reason (cqperl) |
| 37 | $| = 1; |
| 38 | } # BEGIN |
| 39 | |
| 40 | sub verbose { |
| 41 | print "@_\n" if defined ($verbose); |
| 42 | } # verbose |
| 43 | |
| 44 | sub Connect { |
| 45 | my $host = shift; |
| 46 | |
| 47 | my $result; |
| 48 | |
| 49 | if (!defined ($host)) { |
| 50 | $host = "localhost"; |
| 51 | } # if |
| 52 | |
| 53 | $cqserver = ConnectToServer ($host); |
| 54 | |
| 55 | if ($cqserver) { |
| 56 | verbose "Connected to $host"; |
| 57 | SendServerAck ($cqserver); |
| 58 | } # if |
| 59 | |
| 60 | return $cqserver; |
| 61 | } # Connect |
| 62 | |
| 63 | sub Disconnect { |
| 64 | my $msg; |
| 65 | if ($cqserver) { |
| 66 | if ($cqc::command eq "shutdown") { |
| 67 | $msg = "Disconnected from server - shutdown server"; |
| 68 | } else { |
| 69 | $cqc::command = "quit"; |
| 70 | $msg = "Disconnected from server"; |
| 71 | } # if |
| 72 | SendServerCmd ($cqserver, $cqc::command); |
| 73 | GetServerAck ($cqserver); |
| 74 | verbose "$msg"; |
| 75 | close ($cqserver); |
| 76 | undef $cqserver; |
| 77 | } # if |
| 78 | } # Disconnect |
| 79 | |
| 80 | sub GetBugRecord { |
| 81 | my $bugid = shift; |
| 82 | %fields = @_; |
| 83 | |
| 84 | my $result; |
| 85 | |
| 86 | if (!$cqserver) { |
| 87 | verbose "Not connected to server yet!\n"; |
| 88 | verbose "Attempting connection to $default_server...\n"; |
| 89 | $result = Connect ($default_server); |
| 90 | return -1 if !defined ($result); |
| 91 | } # if |
| 92 | |
| 93 | SendServerCmd ($cqserver, $bugid); |
| 94 | GetServerAck ($cqserver); |
| 95 | $result = GetServerResponse ($cqserver, %fields); |
| 96 | SendServerAck ($cqserver); |
| 97 | |
| 98 | return $result; |
| 99 | } # GetBugRecord |
| 100 | |
| 101 | END { |
| 102 | Disconnect; |
| 103 | } # END |
| 104 | |
| 105 | sub ConnectToServer { |
| 106 | my $host = shift; |
| 107 | |
| 108 | # create a tcp connection to the specified host and port |
| 109 | return IO::Socket::INET->new(Proto => "tcp", |
| 110 | PeerAddr => $host, |
| 111 | PeerPort => $port); |
| 112 | } # ConnectToServer |
| 113 | |
| 114 | sub SendServerAck { |
| 115 | my $server = shift; |
| 116 | |
| 117 | print $server "ACK\n"; |
| 118 | } # SendServerAck |
| 119 | |
| 120 | sub GetServerAck { |
| 121 | my $server = shift; |
| 122 | my $srvresp; |
| 123 | |
| 124 | while (defined ($srvresp = <$server>)) { |
| 125 | chomp $srvresp; |
| 126 | if ($srvresp eq "ACK") { |
| 127 | return; |
| 128 | } # if |
| 129 | print "Received $srvresp from server - expected ACK\n"; |
| 130 | } # while |
| 131 | } # GetServerAck |
| 132 | |
| 133 | sub GetServerResponse { |
| 134 | my $server = shift; |
| 135 | %fields = @_; |
| 136 | |
| 137 | %fields = (); |
| 138 | my $srvresp; |
| 139 | my $result = 0; |
| 140 | |
| 141 | while (defined ($srvresp = <$server>)) { |
| 142 | chomp $srvresp; |
| 143 | last if $srvresp eq "ACK"; |
| 144 | if ($srvresp =~ m/Bug ID.*was not found/) { |
| 145 | $result = 1; |
| 146 | } else { |
| 147 | $srvresp =~ /(^\w+):\s+(.*)/s; |
| 148 | $fields {$1} = $2; |
| 149 | } # if |
| 150 | } # while |
| 151 | |
| 152 | return $result; |
| 153 | } # GetServerResponse |
| 154 | |
| 155 | sub SendServerCmd { |
| 156 | my $server = shift; |
| 157 | my $command = shift; |
| 158 | |
| 159 | print $server "$command\n"; |
| 160 | } # SendServerCmd |
| 161 | |
| 162 | 1; |