From: Roland Häder Date: Sun, 2 Feb 2014 02:34:27 +0000 (+0000) Subject: moved for svn-git transission X-Git-Url: https://git.mxchange.org/?p=hub.git;a=commitdiff_plain;h=b8a4e85ccecf32f130dd38fd3ce13eddef155f23 moved for svn-git transission --- b8a4e85ccecf32f130dd38fd3ce13eddef155f23 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..90c3f55dd --- /dev/null +++ b/.gitattributes @@ -0,0 +1,9 @@ +* text=auto !eol +/LICENSE -text +/chat-server.php -text svneol=unset#text/plain +codeswarm-config/hub.config -text +/mhash-benchmark.php svneol=native#text/plain +/patch_core.sh -text +/udp-client.php -text svneol=unset#text/plain +/udp-inc.php -text svneol=unset#text/plain +/udp-server.php -text svneol=unset#text/plain diff --git a/LICENSE b/LICENSE new file mode 100644 index 000000000..79b8ae4a4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,4 @@ +These contributed codes, mostly by myself, are copied from online documentation +pages of www.php.net with some modifications and are licensed under the same +license as on the mentioned website. + diff --git a/chat-server.php b/chat-server.php new file mode 100644 index 000000000..4544db4b2 --- /dev/null +++ b/chat-server.php @@ -0,0 +1,167 @@ + 0) { + // create a copy, so $clients doesn't get modified by socket_select() + $read = $clients; + + // get a list of all the clients that have data to be read from + // if there are no clients with data, go to next iteration + $left = @socket_select($read, $write = null, $except = null, 0, 150); + if ($left < 1) { + continue; + } + + // check if there is a client trying to connect + if (in_array($main_sock, $read)) { + // accept the client, and add him to the $clients array + $new_sock = socket_accept($main_sock); + $clients[] = $new_sock; + + // send the client a welcome message + socket_write($new_sock, "No noobs, but I'll make an exception. :)\n". + "There are ".(count($clients) - 1)." client(s) connected to the server.\n"); + + socket_getpeername($new_sock, $ip); + out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:New client connected: {$ip}"); + + // Notify all chatter + if (count($clients) > 2) { + foreach ($clients as $send_sock) { + if ($send_sock != $main_sock && $send_sock != $new_sock) { + socket_write($send_sock, "Server: Chatter has joined from {$ip}. There are now ".(count($clients) - 1)." clients.\n"); + } + } + } + + // remove the listening socket from the clients-with-data array + $key = array_search($main_sock, $read); + unset($read[$key]); + } + + // loop through all the clients that have data to read from + foreach ($read as $read_sock) { + // Get client data + socket_getpeername($read_sock, $ip); + + // read until newline or 1024 bytes + // socket_read while show errors when the client is disconnected, so silence the error messages + $data = @socket_read($read_sock, 1024, PHP_NORMAL_READ); + + // check if the client is disconnected + if (($data === FALSE) || (in_array(strtolower(trim($data)), $leaving))) { + + // remove client for $clients array + $key = array_search($read_sock, $clients); + unset($clients[$key]); + out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client from {$ip} disconnected. Left: ".(count($clients) - 1).""); + + // Notify all chatter + if (count($clients) > 1) { + foreach ($clients as $send_sock) { + if ($send_sock != $main_sock) { + socket_write($send_sock, "Server: Chatter from {$ip} has logged out. ".(count($clients) - 1)." client(s) left.\n"); + } + } + } + + // continue to the next client to read from, if any + socket_write($read_sock, "Server: Good bye.\n"); + socket_shutdown($read_sock, 2); + socket_close($read_sock); + continue; + } elseif (in_array(trim($data), $shutdown)) { + // Is he allowed to shutdown? + if (!in_array($ip, $masters)) { + out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} has tried to shutdown the server!"); + socket_write($read_sock, "Server: You are not allowed to shutdown the server!\n"); + $data = ""; + continue; + } + + // Close all connections a leave here + foreach ($clients as $client) { + // Send message to client + if ($client !== $main_sock && $client != $read_sock) { + socket_write($client, "Server: Shutting down! Thank you for joining us.\n"); + } + + // Quit him + socket_shutdown($client, 2); + socket_close($client); + } // end foreach + + // Leave the loop + $data = ""; + $clients = array(); + continue; + } + + // trim off the trailing/beginning white spaces + $data = trim($data); + + // Test for HTML codes + $tags = strip_tags($data); + + // check if there is any data after trimming off the spaces + if (!empty($data) && $tags == $data && count($clients) > 2) { + // Send confirmation to "chatter" + socket_write($read_sock, "\nServer: Message accepted.\n"); + + // send this to all the clients in the $clients array (except the first one, which is a listening socket) + foreach ($clients as $send_sock) { + + // if its the listening sock or the client that we got the message from, go to the next one in the list + if ($send_sock == $main_sock || $send_sock == $read_sock) + continue; + + // write the message to the client -- add a newline character to the end of the message + socket_write($send_sock, "{$ip}:{$data}\n"); + + } // end of broadcast foreach + } elseif ($tags != $data) { + // HTML codes are not allowed + out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} has entered HTML code!"); + socket_write($read_sock, "Server: HTML is forbidden!\n"); + } elseif ((count($clients) == 2) && ($read_sock != $main_sock)) { + // No one else will hear the "chatter" + out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} speaks with himself."); + socket_write($read_sock, "Server: No one will hear you!\n"); + } + } // end of reading foreach +} + +// close the listening socket +socket_close($main_sock); + +?> diff --git a/codeswarm-config/hub.config b/codeswarm-config/hub.config new file mode 100644 index 000000000..3570d0923 --- /dev/null +++ b/codeswarm-config/hub.config @@ -0,0 +1,147 @@ +# This is a configuration for generating a "code-swarm" video from the hub +# project. If you like to create such video, please use the following commands +# on Linux-based systems (or similars): +# +# # Change to 'trunk' and get a logfile +# cd ../trunk/ +# svn log -v > ~/SVN/codeswarm/hub.log +# # Copy this hub.config over there +# cp ../contrib/hub.config ~/SVN/codeswarm/ +# # Change there and convert the logfile +# cd ~/SVN/codewarm/ +# python ./convert_logs/convert_logs.py -s hub.log -o hub.xml +# # Run codewarm (you may have to alter the Xmx value) +# ./run.sh hub.config +# # Now the frames are called hub-swarm-XXXXXX.png, e.g. use mencoder: +# mencoder "mf://hub-swarm*.png" -mf fps=25 -o hub-codeswarm.avi -ovc lavc -lavcopts vcodec=mpeg4 +# # This will producea similar movie to mine + +# Frame width +Width=800 + +# Frame height +Height=600 + +# Input file +InputFile=hub.xml + +# Particle sprite file +ParticleSpriteFile=src/particle.png + +#Font Settings +Font=SansSerif +BoldFont=SansSerif +InfoFont=SansSerif +FontSize=10 +BoldFontSize=14 +InfoFontSize=20 + +# Project time per frame +MillisecondsPerFrame=21600000 + +# Maximum number of Background processes +MaxThreads=4 + +# Optional Method instead of MillisecondsPerFrame +FramesPerDay=10 + +# Background in R,G,B +Background=0,0,0 + +# Color assignment rules +# Keep in order, do not skip numbers. Numbers start +# at 1. +# +# Pattern: "Label", "regex", R,G,B, R,G,B +# Label is optional. If it is omitted, the regex +# will be used. +# +ColorAssign1="Docs",".*txt", 0,0,255, 0,0,255 +ColorAssign2="PHP",".*php", 0,255,255, 0,255,255 +ColorAssign3="Template",".*tpl", 102,0,255, 102,0,255 +ColorAssign4="CSS",".*css", 255,0,0, 255,0,0 +ColorAssign5="JavaScript",".*js", 255,255,0, 255,255,0 +ColorAssign6="Shell",".*sh", 119,68,119, 119,68,119 +ColorAssign7="XML",".*xml", 136,51,17, 136,51,17 +ColorAssign8="Code",".*ctp", 250,110,110, 250,110,130 +#ColorAssign9="Code8",".*src8.*", 238,102,68, 238,102,68 +#ColorAssign10=".*src9.*", 238,68,119, 238,68,119 + +# Save each frame to an image? +TakeSnapshots=true + +# Where to save each frame +SnapshotLocation=hub-swarm-######.png + +# Draw names (combinatory) : +# Draw sharp names? +DrawNamesSharp=true +# And draw a glow around names? (Runs slower) +DrawNamesHalos=true + +# Draw files (combinatory) : +# Draw sharp files +DrawFilesSharp=false +# Draw fuzzy files +DrawFilesFuzzy=true +# Draw jelly files +DrawFilesJelly=true + +# Show the Legend at start +ShowLegend=true + +# Show the History at start +ShowHistory=true + +# Show the Activity histogram at bottom +ShowActivity=true + +# Show the Date at start +ShowDate=true + +# Show edges between authors and files, mostly for debug purpose +ShowEdges=false + +# Turn on Debug counts. +ShowDebug=false + +# Natural distance of files to people +EdgeLength=25 + +# Amount of life to decrement +EdgeDecrement=-2 +FileDecrement=-2 +PersonDecrement=-1 + +#Speeds. +#Optional: NodeSpeed=7.0, If used, FileSpeed and PersonSpeed need not be set. +# +FileSpeed=7.0 +PersonSpeed=2.0 + +#Masses +FileMass=1.0 +PersonMass=10.0 + +# Life of an Edge +EdgeLife=250 + +# Life of a File +FileLife=200 + +# Life of a Person +PersonLife=255 + +# Highlight percent. +# This is the amount of time that the person or +# file will be highlighted. +HighlightPct=5 + +## Physics engine selection and configuration +# Directory physics engine config files reside in. +PhysicsEngineConfigDir=physics_engine +# Force calculation algorithms ("PhysicsEngineLegacy", "PhysicsEngineSimple"...) : +PhysicsEngineSelection=PhysicsEngineLegacy + +# OpenGL is experimental. Use at your own risk. +UseOpenGL=false diff --git a/mhash-benchmark.php b/mhash-benchmark.php new file mode 100644 index 000000000..41061d39e --- /dev/null +++ b/mhash-benchmark.php @@ -0,0 +1,67 @@ + diff --git a/patch_core.sh b/patch_core.sh new file mode 100755 index 000000000..1285ae8f0 --- /dev/null +++ b/patch_core.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +echo "Please use core/contrib/patch_core.sh instead!" diff --git a/remove-deprecated.sh b/remove-deprecated.sh new file mode 100755 index 000000000..ed3424727 --- /dev/null +++ b/remove-deprecated.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +if ! test -e "index.php"; then + echo "$0: Please execute this script from root directory." + exit 1 +fi + +echo "$0: Searching for deprecated PHP scripts ..." +LIST1=`find -type f -name "*.php" -size 24c -exec grep -H "@DEPRECATED" {} \;` + +LIST="${LIST1} ${LIST2}" + +if test "${LIST}" != " "; then + echo "${LIST}" | cut -d ":" -f 1 | xargs svn --force del + sh ./todo-builder.sh +fi diff --git a/udp-client.php b/udp-client.php new file mode 100644 index 000000000..d5dd3610e --- /dev/null +++ b/udp-client.php @@ -0,0 +1,175 @@ + 0)) { + out(__FILE__, __LINE__, "ERROR: $errno - $errstr"); + exit; +} + +if (!stream_set_blocking($socket, FALSE)) { + out(__FILE__, __LINE__, "ERROR: Cannot set non-blocking mode!"); + exit; +} + +out(__FILE__, __LINE__, "Negotiating with host ${ip} ..."); +while (!feof($socket)) { + //*DEBUG: */ out(__FILE__, __LINE__, "Sending ping..."); + stream_socket_sendto($socket, 'PING'); + + //*DEBUG: */ out(__FILE__, __LINE__, "Reading reply..."); + $read = trim(stream_socket_recvfrom($socket, 1500, 0, $peer)); + + if ($failed == constant('MAX_FAILURES')) { + //*DEBUG: */ out(__FILE__, __LINE__, "Too many failures! (failed={$failed})"); + break; + } // END - if + + // Is the peer the same? + if (empty($peer)) { + out(__FILE__, __LINE__, "Connection lost? read={$read} (".strlen($read).")"); + $failed++; + continue; + } elseif ($peer != "$ip:9060") { + out(__FILE__, __LINE__, "Peer mismatch: {$ip}!={$peer}"); + $failed++; + continue; + } + + if (empty($read)) { + $failed++; + //*DEBUG: */ out(__FILE__, __LINE__, "Empty line received. Is the server there?"); + continue; + } elseif ($read == 'INVALID') { + $failed++; + out(__FILE__, __LINE__, "Server has not accepted our message."); + continue; + } else { + $failed = 0; + //*DEBUG: */ out(__FILE__, __LINE__, "Response {$read} received."); + } + + $rec = explode(':', $read); + + if (count($rec) < 2) { + out(__FILE__, __LINE__, "Invalid packet {$read} received. count=".count($rec)); + $invalid++; + continue; + } // END - if + + $time = trim($rec[0]); + $right = explode('=', trim($rec[1])); + $hash = trim($right[1]); + $hasher = trim($right[0]); + + if (validate($time) != $hash) { + out(__FILE__, __LINE__, "Invalid: ({$time}/{$hash}/{$hasher})"); + $invalid++; + continue; + } // END - if + + if (!isset($data[$rec[0]])) { + if (count($data) > 0) { + print $data[$rec[0]-1]."\n"; + $cnt++; + if ($cnt > constant('ROUNDS')) break; + } + $data[$rec[0]] = 0; + } + $data[$rec[0]]++; + + // Sleep a little + if (function_exists('time_nanosleep')) time_nanosleep(0, 500000); +} // END - while + +array_shift($data); + +// Send BYE +sendBye($socket); + +stream_socket_shutdown($socket, STREAM_SHUT_RDWR); + +$avg = 0; +$min = 0; +$max = 0; + +foreach ($data as $cnt) { + if ($cnt > $max) { + $max = $cnt; + } + if (($cnt < $min) || ($min == 0)) { + $min = $cnt; + } + $avg += $cnt; +} // END - foreach + +if (count($data) > 0) { + $avg = round($avg / count($data)); +} // END - if + +out(__FILE__, __LINE__, 'MIN/AVG/MAX=' . $min . '/' . $avg . '/' . $max . ''); +out(__FILE__, __LINE__, 'INVALID=' . $invalid . ''); +out(__FILE__, __LINE__, 'FAILED=' . $failed . ''); + +?> diff --git a/udp-inc.php b/udp-inc.php new file mode 100644 index 000000000..47f2607bf --- /dev/null +++ b/udp-inc.php @@ -0,0 +1,11 @@ + diff --git a/udp-server.php b/udp-server.php new file mode 100644 index 000000000..55d58efa1 --- /dev/null +++ b/udp-server.php @@ -0,0 +1,69 @@ + 0)) { + out(__FILE__, __LINE__, $errstr . ' (' . $errno . ')'); + exit; +} + +if (!stream_set_blocking($socket, FALSE)) { + out(__FILE__, __LINE__, 'ERROR: Cannot set non-blocking mode!'); + exit; +} + +$pkt = ''; + +out(__FILE__, __LINE__, 'Waiting for connections...'); + +do { + $pkt = trim(stream_socket_recvfrom($socket, 1500, 0, $peer)); + + if ($pkt == 'PING') { + // Peer in list? + if (!isset($connections[$peer])) { + out(__FILE__, __LINE__, 'New peer ' . $peer . ' detected.'); + $connections[$peer] = 1; + } elseif ($connections[$peer] == 0) { + out(__FILE__, __LINE__, 'Peer ' . $peer . ' has returned.'); + $connections[$peer] = 1; + } + + //out(__FILE__, __LINE__, 'Sending data to peer ' . $peer . '.'); + stream_socket_sendto($socket, (time() . ':md5=' . md5(time())), 0, $peer); + } elseif ($pkt == 'BYE') { + // Peer in list? + if (!isset($connections[$peer])) { + out(__FILE__, __LINE__, 'Peer ' . $peer . ' is NOT in peer list.'); + } else { + out(__FILE__, __LINE__, 'Peer ' . $peer . ' is leaving us.'); + stream_socket_sendto($socket, 'BYE', 0, $peer); + $connections[$peer] = 0; + } + } elseif (!empty($peer)) { + out(__FILE__, __LINE__, 'Invalid packet ' . $pkt . ' from peer ' . $peer . '.'); + stream_socket_sendto($socket, 'INVALID', 0, $peer); + } else { + // Waiting for packages + $pkt = ''; + } + + $test = (round(date('s', time()) / 10)); + if ($first != $test) { + out(__FILE__, __LINE__, count($connections) . ' total connections so far.', true); + $first = $test; + } + + // Sleep a little + if (function_exists('time_nanosleep')) time_nanosleep(0, 500000); +} while ($pkt !== FALSE); + +?>