|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +/* |
| 5 | + * Copyright 2012 Facebook, Inc. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +$root = dirname(dirname(dirname(dirname(__FILE__)))); |
| 21 | +require_once $root.'/scripts/__init_script__.php'; |
| 22 | + |
| 23 | +$args = new PhutilArgumentParser($argv); |
| 24 | +$args->setTagline('test client for Aphlict server'); |
| 25 | +$args->setSynopsis(<<<EOHELP |
| 26 | +**aphlict_test_client.php** [__options__] |
| 27 | + Connect to the Aphlict server configured in the Phabricator config. |
| 28 | +
|
| 29 | +EOHELP |
| 30 | +); |
| 31 | +$args->parseStandardArguments(); |
| 32 | +$args->parse( |
| 33 | + array( |
| 34 | + array( |
| 35 | + 'name' => 'server', |
| 36 | + 'param' => 'uri', |
| 37 | + 'default' => PhabricatorEnv::getEnvConfig('notification.client-uri'), |
| 38 | + 'help' => 'Connect to __uri__ instead of the default server.', |
| 39 | + ), |
| 40 | + )); |
| 41 | +$console = PhutilConsole::getConsole(); |
| 42 | + |
| 43 | +$errno = null; |
| 44 | +$errstr = null; |
| 45 | + |
| 46 | +$uri = $args->getArg('server'); |
| 47 | +$uri = new PhutilURI($uri); |
| 48 | +$uri->setProtocol('tcp'); |
| 49 | + |
| 50 | +$console->writeErr("Connecting...\n"); |
| 51 | +$socket = stream_socket_client( |
| 52 | + $uri, |
| 53 | + $errno, |
| 54 | + $errstr); |
| 55 | + |
| 56 | +if (!$socket) { |
| 57 | + $console->writeErr( |
| 58 | + "Unable to connect to Aphlict (at '$uri'). Error #{$errno}: {$errstr}"); |
| 59 | + exit(1); |
| 60 | +} else { |
| 61 | + $console->writeErr("Connected.\n"); |
| 62 | +} |
| 63 | + |
| 64 | +$io_channel = new PhutilSocketChannel($socket); |
| 65 | +$proto_channel = new PhutilJSONProtocolChannel($io_channel); |
| 66 | + |
| 67 | +$json = new PhutilJSON(); |
| 68 | +while (true) { |
| 69 | + $message = $proto_channel->waitForMessage(); |
| 70 | + $console->writeOut($json->encodeFormatted($message)); |
| 71 | +} |
| 72 | + |
0 commit comments