NotionCommotion
Members-
Posts
2,446 -
Joined
-
Last visited
-
Days Won
10
Everything posted by NotionCommotion
-
Thanks requinix. I tried unplugging the Ethernet, and it did not trigger an error. Expected? As far as one or two arguments to the callback... https://github.com/reactphp/stream/blob/c3647ea3d338ebc7332b1a29959f305e62cf2136/src/Stream.php#L61 shows the following: $that = $this; $this->buffer->on('error', function ($error) use ($that) { $that->emit('error', array($error, $that)); $that->close(); }); And https://github.com/igorw/evenement/blob/master/src/Evenement/EventEmitterTrait.php#L62 shows the following: public function emit($event, array $arguments = []) { foreach ($this->listeners($event) as $listener) { call_user_func_array($listener, $arguments); } } So, is $foo in $stream->on('error', function($foo) {...}); an array with $foo[0] as the error and $foo[1] as the stream?
-
zend_mm_heap corrupted-Allowed memory size exhausted
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
How come yum list installed | grep "php" doesn't show php 5.3, and the following shows it as not being installed? [Michael@devserver datalogger]$ sudo yum remove php-5.3.3-48.el6_8.x86_64 [sudo] password for Michael: Loaded plugins: fastestmirror, priorities, security Setting up Remove Process No Match for argument: php-5.3.3-48.el6_8.x86_64 Loading mirror speeds from cached hostfile epel/metalink | 15 kB 00:00 * base: mirrors.cat.pdx.edu * epel: ftp.osuosl.org * extras: centos.chicago.waneq.com * ius: mirrors.kernel.org * updates: mirrors.kernel.org Webmin | 1.0 kB 00:00 base | 3.7 kB 00:00 epel | 4.3 kB 00:00 extras | 3.4 kB 00:00 ius | 2.3 kB 00:00 nodesource | 2.5 kB 00:00 updates | 3.4 kB 00:00 virtualbox | 951 B 00:00 192 packages excluded due to repository priority protections Package(s) php-5.3.3-48.el6_8.x86_64 available, but not installed. No Packages marked for removal [Michael@devserver datalogger]$ -
I have an error event which is triggered upon a communication error, and I "think" $error will be set to the error text. Am I correct? How can I test it? I wish to trigger a sockets/communication error somehow, and not just ideally wait for one to eventually happen. How can I trigger such an error? <?php $loop = Factory::create(); $socket = new React\Socket\Server($loop); $socket->on('connection', function (\React\Socket\ConnectionInterface $stream){ $stream->on('data', function($rsp) {/*bla bla bla*/}); $stream->on('close', function($conn) {/*bla bla bla*/}); $stream->on('error', function($error, $stream) { echo "Stream Error: $error"; $stream->close(); }); }); $socket->listen('0.0.0.0',1337); $loop->run();
-
zend_mm_heap corrupted-Allowed memory size exhausted
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Finally getting around to this. The PHP development package isn't compatible with my installed PHP. Also tried a pre-compiled version with no success. Any recommendations? Thanks EDIT. Never mind. sudo yum install php56u-devel [Michael@devserver www]$ yum list installed | grep "php" php-php-gettext.noarch 1.0.12-1.el6 @epel php-tcpdf.noarch 6.2.11-1.el6 @epel php-tcpdf-dejavu-sans-fonts.noarch 6.2.11-1.el6 @epel php56u.x86_64 5.6.29-1.ius.centos6 @ius php56u-bcmath.x86_64 5.6.29-1.ius.centos6 @ius php56u-cli.x86_64 5.6.29-1.ius.centos6 @ius php56u-common.x86_64 5.6.29-1.ius.centos6 @ius php56u-gd.x86_64 5.6.29-1.ius.centos6 @ius php56u-imap.x86_64 5.6.29-1.ius.centos6 @ius php56u-mbstring.x86_64 5.6.29-1.ius.centos6 @ius php56u-mcrypt.x86_64 5.6.29-1.ius.centos6 @ius php56u-mysqlnd.x86_64 5.6.29-1.ius.centos6 @ius php56u-opcache.x86_64 5.6.29-1.ius.centos6 @ius php56u-pdo.x86_64 5.6.29-1.ius.centos6 @ius php56u-pear.noarch 1:1.10.1-4.ius.centos6 @ius php56u-pecl-apcu.x86_64 4.0.11-2.ius.centos6 @ius php56u-pecl-igbinary.x86_64 2.0.1-1.ius.centos6 @ius php56u-pecl-jsonc.x86_64 1.3.10-2.ius.centos6 @ius php56u-pecl-redis.x86_64 2.2.8-2.ius.centos6 @ius php56u-pecl-redis-debuginfo.x86_64 2.2.8-2.ius.centos6 @ius php56u-process.x86_64 5.6.29-1.ius.centos6 @ius php56u-soap.x86_64 5.6.29-1.ius.centos6 @ius php56u-tidy.x86_64 5.6.29-1.ius.centos6 @ius php56u-xml.x86_64 5.6.29-1.ius.centos6 @ius phpMyAdmin.noarch 4.0.10.18-1.el6 @epel [Michael@devserver www]$ sudo yum install php-devel Loaded plugins: fastestmirror, priorities, security Setting up Install Process Loading mirror speeds from cached hostfile * base: mirror.pac-12.org * epel: ftp.osuosl.org * extras: centos.chicago.waneq.com * ius: mirrors.kernel.org * updates: mirrors.kernel.org 192 packages excluded due to repository priority protections Resolving Dependencies --> Running transaction check ---> Package php-devel.x86_64 0:5.3.3-48.el6_8 will be installed --> Processing Dependency: php(x86-64) = 5.3.3-48.el6_8 for package: php-devel-5.3.3-48.el6_8.x86_64 --> Running transaction check ---> Package php.x86_64 0:5.3.3-48.el6_8 will be installed --> Processing Dependency: php-common(x86-64) = 5.3.3-48.el6_8 for package: php-5.3.3-48.el6_8.x86_64 --> Processing Dependency: php-cli(x86-64) = 5.3.3-48.el6_8 for package: php-5.3.3-48.el6_8.x86_64 --> Running transaction check ---> Package php-cli.x86_64 0:5.3.3-48.el6_8 will be installed ---> Package php-common.x86_64 0:5.3.3-48.el6_8 will be installed --> Processing Conflict: php56u-5.6.29-1.ius.centos6.x86_64 conflicts php < 5.6 --> Processing Conflict: php56u-cli-5.6.29-1.ius.centos6.x86_64 conflicts php-cli < 5.6 --> Processing Conflict: php56u-common-5.6.29-1.ius.centos6.x86_64 conflicts php-common < 5.6 --> Finished Dependency Resolution Error: php56u conflicts with php-5.3.3-48.el6_8.x86_64 Error: php56u-cli conflicts with php-cli-5.3.3-48.el6_8.x86_64 Error: php56u-common conflicts with php-common-5.3.3-48.el6_8.x86_64 You could try using --skip-broken to work around the problem You could try running: rpm -Va --nofiles --nodigest [Michael@devserver www]$ sudo yum install php-pecl-xdebug.x86_64 Loaded plugins: fastestmirror, priorities, security Setting up Install Process Loading mirror speeds from cached hostfile * base: mirror.pac-12.org * epel: ftp.osuosl.org * extras: repos.forethought.net * ius: mirrors.kernel.org * updates: mirrors.kernel.org 192 packages excluded due to repository priority protections Resolving Dependencies --> Running transaction check ---> Package php-pecl-xdebug.x86_64 0:2.1.4-2.el6 will be installed --> Processing Dependency: php(zend-abi) = 20090626 for package: php-pecl-xdebug-2.1.4-2.el6.x86_64 --> Processing Dependency: php(api) = 20090626 for package: php-pecl-xdebug-2.1.4-2.el6.x86_64 --> Running transaction check ---> Package php-common.x86_64 0:5.3.3-48.el6_8 will be installed --> Processing Conflict: php56u-common-5.6.29-1.ius.centos6.x86_64 conflicts php-common < 5.6 --> Finished Dependency Resolution Error: php56u-common conflicts with php-common-5.3.3-48.el6_8.x86_64 You could try using --skip-broken to work around the problem You could try running: rpm -Va --nofiles --nodigest [Michael@devserver www]$ -
I think your list is pretty good. You will learn more as you go on. A couple items to add. Composer (look into right now) Template engines such as Twig (look into right after you have made a few simple pages) PHP configuration (when things don't work, you will need to know why) Apache, nginx, etc. Documentation (never too early) Unit testing (I still need to do) Security (look into before you go production, and even sooner so you form good habits) Command line uses Various 3rd party libraries (as needed)
-
Question about var_dump($object) output
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Thanks. Why doesn't SplObjectStorage use it to identify objects (I believe it uses a hash of the object)? Other than to PHP, is this number useful? I guess it can be used when debugging to confirm one is looking at the same object. Is there any a time one would use this number in a script? -
What does the #1 in object(stdClass)#1 (3) {} represent? <?php $a = (object) array('a'=>1, 'b'=>2, 'c'=>array("a", "b", "c")); var_dump($a); [Michael@devserver react]$ php var_dump.php object(stdClass)#1 (3) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" } } [Michael@devserver react]$
-
Quotes driving me nuts even after reading manual.
NotionCommotion replied to Jwest100's topic in PHP Coding Help
You want your single and double quotes to be nested like HTML tags. Don't put quotes next to your variables, but a period to append it to some text. Echo is to display on the page. You don't echo the variables when assigning to a string. .= is to append versus just = to replace the variable. -
Will do. I had put multiple create methods in a single class, but agree separate classes is much better. I take it I want to make the class as low level as possible (i.e. don't use MammalFactory and ReptileFactory) providing that the method name is generic (i.e. create(), and not like I am doing below). class Factory { public function createAnimal($id){ //do stuff... return $animal; } public function createPlant($id){ //do stuff... return $plant; } } Also, is a factory class a good application for a static class?
-
How to get content into a ReactPHP server?
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
I need to add use() to $socket->create and should probably remove some of the use() variables with $stream->on as shown below, right? function sendToClient($guid, $forwardData){ $success = false; $loop = new \React\EventLoop\StreamSelectLoop(); $socket = new \React\SocketClient\TimeoutConnector(new \React\SocketClient\TcpConnector($loop), 15, $loop); $socket->create($server, $port)->then(function($stream) use ($loop, $guid, $forwardData){ $client = new \Kicken\RLGL\JSONStream($stream); $stream->on('data', function($data) use (&$success, $loop, $stream){ if ($data['message'] == 'forward-result'){ $success = $data['success']; $stream->close(); $loop->stop(); } }); $client->send([ 'message' => 'forward' , 'guid' => $guid , 'data' => $forwardData ]); }); //Generic timeout so this function doesn't block forever. $loop->addTimer(30, function() use ($loop){ $loop->stop(); }); $loop->run(); return $success; } -
What makes you think it is abstract? Please know I only do so as I feel it makes it easier for others to provide answers, and definitely do so to make things easier for myself. I am using this pattern several places where the latest is a communication interface. One implementation is bi-directional using sockets and the other is a simple cron which utilizes curl. Good question which I should have better described. I am using a super table with sub-tables for the more specific instances, so ID is the primary key and unique across all animals. I could obviously query the database, but my dilemma is how to best create the objects which have both properties and methods. Yes, it is a primary key as I explained in response to requinix. My question is where to offload it: whether in a single factory classes which is only responsible to creates objects for all types, or in one of the parent classes. The problem with the parent class is it needs to extend and execute the animals class twice.
-
I have the following classes as shown below... <?php //All classes except maybe Animals are NOT abstract class Animals { public $fee, $fi, $fo, $fum; public function __construct($settings) { foreach($settings as $key=>$value) { $this->$key=$value; } } } class Mammals extends Animals { public $mammalSpecific; } class Reptiles extends Animals { public $reptileSpecific; } class Dogs extends Mammals {} class Cats extends Mammals {} class Snakes extends Reptiles {} class Lizards extends Reptiles {} $dog=new Dogs(['fee'=>'x', 'fi'=>'x', 'fo'=>'x', 'fum'=>'x', 'mammalSpecific'=>'x']); $snake=new Snakes(['fee'=>'x', 'fi'=>'x', 'fo'=>'x', 'fum'=>'x', 'reptileSpecific'=>'x']); All is good, but now I want to get an object based on its ID. One option is to add a new class as follows: <?php class Factories //extends Nothing { private $settings; public function __construct($settings) { $this->settings=$settings; } public function getMammalByID($id) { if($classnameAndSpecificAboutThisMammal=$this->getClassnameAndSpecificAboutThisMammalForGivenID($id)) { $class=$classnameAndSpecificAboutThisMammal['classname']; $obj=new $class(array_merge($settings,['mammalSpecific'=>$classnameAndSpecificAboutThisMammal['specificAboutThisMammal']])); } return empty($obj)?new invalidObject:$obj; } public function getReptileByID($id) { if($classnameAndSpecificAboutThisReptile=$this->getClassnameAndSpecificAboutThisReptileForGivenID($id)) { $class=$classnameAndSpecificAboutThisReptile['classname']; $obj=new $class(array_merge($settings,['reptileSpecific'=>$classnameAndSpecificAboutThisReptile['specificAboutThisReptile']])); } return empty($obj)?new invalidObject:$obj; } } $factory=new Factory(['fee'=>'x', 'fi'=>'x', 'fo'=>'x', 'fum'=>'x']); $mammal=$factory->getMammalByID(123); $reptile=$factory->getReptileByID(321); Or should I just put a getByID() method in both the Mammals and Reptiles classes instead of using a separate Factory class? But wouldn't this force the Animal constructor (which will likely do more than I have shown) to be executed twice? Thanks
-
zend_mm_heap corrupted-Allowed memory size exhausted
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Will do. Never used it before. Any ideas about the following error? Thanks [Michael@devserver db]$ sudo pecl install xdebug [sudo] password for Michael: downloading xdebug-2.5.0.tgz ... Starting to download xdebug-2.5.0.tgz (267,640 bytes) ........................................................done: 267,640 bytes 76 source files, building running: phpize Can't find PHP headers in /usr/include/php The php-devel package is required for use of this command. ERROR: `phpize' failed -
How to remove comma from last row in mysql query in php
NotionCommotion replied to Parvinder's topic in PHP Coding Help
Then, don't echo it. ],\r\n should be ]\r\n. Also, look into PDO. $stmt=$db->prepare("SELECT length, width FROM statxyx WHERE siteid=?"); $stmt->execute([$siteid]); while($row=$stmt->fetch(PDO::FETCH_ASSOC)) { echo "['7C6Buh',{$row['length']},{$row['width']}]\r\n"; } -
If I start this client when the server isn't started, I get the following error. If I get rid of the addPeriodicTimer, it doesn't appear to happen. What is going on? Thanks EDIT. A clue! The following makes the error go away, and exit('addPeriodicTimer'); is executed. Still would like to know what is going on. // $socket = new TimeoutConnector(new TcpConnector($this->loop), 10, $this->loop); $socket = new TcpConnector($this->loop); <?php namespace DataLogger; use React\EventLoop\Factory; use React\SocketClient\TcpConnector; use React\SocketClient\TimeoutConnector; class Client { private $loop,$connection; public function start() { $this->loop = Factory::create(); $this->connect(); $this->loop->addPeriodicTimer(5, function(){ exit('addPeriodicTimer'); }); $this->loop->run(); } private function connect(){ echo('.'); $socket = new TimeoutConnector(new TcpConnector($this->loop), 10, $this->loop); $socket->create('127.0.0.1', 1337)->then(function($stream){ echo('create connection'); })->otherwise(function($reason){ $this->connect(); }); } } require '../vendor/autoload.php'; require 'JSONStream.php'; $client=new Client(); $client->start(); [Michael@devserver kicken_original]$ php client2.php ...... etc, etc, etc, .............. Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 83 bytes) in /var/www/react/vendor/react/promise/src/Promise.php on line 102 zend_mm_heap corrupted [Michael@devserver kicken_original]$
-
How to get content into a ReactPHP server?
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Thanks guys -
How to get content into a ReactPHP server?
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Would server need a continuously loop which constantly checks if the queue has data in it? This seems to make sense, but to make sure I understand, I would like to explain it in my own words. Index.php creates a new client and connects to the server. It sends content with message of "forward", and waits for confirmation of message "forward-result". Upon confirmation or 30 seconds, it stops the loop. Will stopping the loop and/or the completion of the index.php script kill the connection to the server? Also, how would you recommend preventing one of the clients other than index.php which happens to know another's guid from forwarding something to that client? My thoughts are: Have index.php include a secret password which case 'forward': checks before forwarding. Have case 'forward': check that the client is on the same machine. I like option 2 more. Agree? Is it possible? Thanks -
A remote client has connected to the following server described at the bottom of this page, has told the server that its GUID is 123456789, and the server has created a map between this GUID and the specific client connection. Another script index.php is ran through Apache on the same machine as the sockets server. <?php $guid=123456789; $data='{"message":"hello"}'; someFunctionToSendDataToSocketsServer($guid,$data); Somehow, I would like to have index.php send $guid=123456789 and $data='{"message":"hello"}' to the sockets server, and then execute: $client=$this->findConnectionByGuid($guid); $client->write($data); someFunctionToSendDataToSocketsServer() should be a blocking function up to the sockets server writing the data (but not that the client received it and acknowledged it) and should return true/false (or a 200/400 header, or some other means to indicate status). At first, I was thinking of using a ReactPHP HTTP server operated on the same loop as the sockets server. index.php would then use curl to send it to the HTTP server, the HTTP server on request event would get the data, and if valid format send the data to the client and return a 200 header, and if not valid format, return a 400 header. ReactPHP, however, says that the HTTP server is not stable. Another option would be to add a sockets client to the server machine and have index.php use that client to send the data to the sockets server. I don’t think this make sense, however, as index.php is not communicating bi-directional with the sockets server. Also, it adds a little complication as the sockets server is currently designed to receive connections from remote clients which send the server their unique GUID. Lastly, from a security standpoint, I don’t want the remote clients to somehow gain privileges available to only index.php. Or maybe a redis queue where index.php places the content in the queue, and it pops out in the server script where it is sent. I am not sure how the true/false status would work. Or maybe something else altogether? Any advice? <?php namespace MyServer; use React\EventLoop\Factory; use React\Socket\Server as SocketServer; use SplObjectStorage; require 'JSONStream.php'; class Server { private $app, //The main application $url_sockets, //Host and port for the socket $clientList; //SplObjectStorage public function __construct($url_sockets,$app) { $this->app = $app; $this->url_sockets=$url_sockets; $this->clientList = new SplObjectStorage(); } public function start() { $loop = Factory::create(); $socket = new SocketServer($loop); $socket->on('connection', function (\React\Socket\ConnectionInterface $client){ $client = new \Kicken\RLGL\JSONStream($client); $this->clientList->attach($client, []); $client->on('data', function($data) use ($client){ // The server doesn't know the client's GUID until the client sends this data to the server if($guid=$this->getConnectionID($client)) { $this->app->process($data, $guid, $client); } elseif($guid=$this->app->getGUID($data)) { $this->addConnection($client, $guid); } }); $client->on('close', function($conn) use ($client) { if($this->socketClients->contains($client)) { $this->socketClients->detach($client); } }); $client->on('error', function($conn) use ($client) { $client->close(); }); echo "New connection accepted.\r\n"; }); $socket->listen($this->url_sockets['port'],$this->url_sockets['host']); $loop->run(); } private function addConnection($client, $guid) { $this->clientList[$client]['guid']=$guid; } private function getConnectionID($client) { return $this->clientList[$client]['guid']; } private function findConnectionByGuid($guid) { //There should be a better way to do this??? $this->socketClients->rewind(); while($this->socketClients->valid()) { if($guid == $this->socketClients->getInfo()) { return $this->socketClients->current(); } $this->socketClients->next(); } return false; } }
-
Of course. I wasn't thinking. There are not that many clients, however, I will probably go with the map approach just because...
-
The clients are not generating these keys, but rather have been manually assigned a key such as 3512e089-861e-427f-a4eb-c9187f03a7d4. Care will be taken never to duplicate a key. Because I don't know the key when I first receive the object. The object is created when a client makes a connection, and it is saved using $this->clientList->attach($client, []);. Immediately after the client connects to the server, the client will send the server its GUID. The reason I do so are twofold: For every subsequent data receipt, the client does not need to send its GUID again. More importantly, another script which only know's a given clients GUID needs to be able to send data to that client. I am struggling on determining the best way to do this, however, it is outside of the scope of this post. Does this make any sense? <?php namespace MyServer; use React\EventLoop\Factory; use React\Socket\Server as SocketServer; use SplObjectStorage; require 'JSONStream.php'; class Server { private $app, //The main application $url_sockets, //Host and port for the socket $clientList; //SplObjectStorage public function __construct($url_sockets,$app) { $this->app = $app; $this->url_sockets=$url_sockets; $this->clientList = new SplObjectStorage(); } public function start() { $loop = Factory::create(); $socket = new SocketServer($loop); $socket->on('connection', function (\React\Socket\ConnectionInterface $client){ $client = new \Kicken\RLGL\JSONStream($client); $this->clientList->attach($client, []); $client->on('data', function($data) use ($client){ // The server doesn't know the client's GUID until the client sends this data to the server if($guid=$this->getConnectionID($client)) { $this->app->process($data, $guid, $client); } elseif($guid=$this->app->getGUID($data)) { $this->addConnection($client, $guid); } }); $client->on('close', function($conn) use ($client) { if($this->socketClients->contains($client)) { $this->socketClients->detach($client); } }); $client->on('error', function($conn) use ($client) { $client->close(); }); echo "New connection accepted.\r\n"; }); $foo->on('connection', function ($guid, $data){ /* I haven't really figured out this part. Another file such as index.php will receive $guid and $data via POST or GET. It then somehow needs to get it to this service so it could be sent to the approriate client. Options are: 1) redis queue 2) HTTP server located here and index.php sends it via cURL 3) Use the existing Sockets server here and index.php needs to somehow need to send it 4) ??? */ $client=$this->findConnectionByGuid($guid); $client->write($data); }); $socket->listen($this->url_sockets['port'],$this->url_sockets['host']); $loop->addPeriodicTimer(60, function() { //Initiate communication to local SOAP servers and save data $this->app->comm->soapClient(); }); $loop->run(); } private function addConnection($client, $guid) { $this->clientList[$client]['guid']=$guid; } private function getConnectionID($client) { return $this->clientList[$client]['guid']; } private function findConnectionByGuid($guid) { //There should be a better way to do this??? $this->socketClients->rewind(); while($this->socketClients->valid()) { if($guid == $this->socketClients->getInfo()) { return $this->socketClients->current(); } $this->socketClients->next(); } return false; } }
-
Instead of having PHP do the trimming, have MySQL do it. See http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_left. Do, why won't DISTINCT or GROUP BY work?. Are there records like ABCfoo and ABCbar? Maybe the following? SELECT DISTINCT LEFT(order_discount_code,3) FROM orders ORDER BY order_discount_code ASC;
-
And then iterate over SplObjectStorage to find a specific object which has a unique property? What handshake? Wouldn't this require passing the key on every exchange?
-
Because I don't know the key when the objects are added to SplObjectStorage. They are clients which make a connection to a server, and later communicate to the server and give the server its unique key. Later, the server might be given a specific client's unique key and told to send something to that client. I could iterate over the list of objects every time (as I showed in my option 1), but as a mater of principle, think I shouldn't. Yes, they are like associate arrays, but my understanding is the key is not really the object but a hash of that object. Why shouldn't I just store the hash which identifies the object?