HCE Project PHP language client API bindings OOP style  1.1.1
Hierarchical Cluster Engine PHP Client Interface API OOP style
 All Classes Namespaces Files Functions Variables Pages
IdGenerator.inc.php
Go to the documentation of this file.
1 <?php
2 
14 namespace HCE\transport {
15 
19  class IdGenerator {
23  public function __construct() {
24  }
25 
29  public function __destruct() {
30  }
31 
42  public static function getClientId($type = 1, $prefix = null) {
43  if ($prefix === null) {
44  $prefix = date ( 'Y-m-d H:i:s' ) . '-' . microtime ( true ) . '-';
45  }
46 
47  $ret = PROTOCOL_CLIENT_IDENTITY_PREFIX . $prefix;
48 
49  if ($type == 0) {
50  // Simple fast
51  $ret .= self::getUniqueId ( 1 );
52  } else {
53  // More accurate complexity
54  $ret .= self::getUniqueId ( 3 ) . '-' . self::getUniqueId ( 5 );
55  }
56 
57  return $ret;
58  }
59 
70  public static function getMessageId($type = 1, $prefix = '') {
71  $ret = PROTOCOL_MSG_ID_PREFIX . $prefix;
72 
73  if ($type == 0) {
74  // Simple fast
75  $ret .= self::getUniqueId ( 0 );
76  } else {
77  // More accurate complexity
78  $ret .= self::getUniqueId ( 4 );
79  }
80 
81  return $ret;
82  }
83 
92  public static function getUniqueId($type = 0) {
93  $ret = null;
94 
95  if ($type == 0) {
96  // Complex string
97  $ret = uniqid ( md5 ( rand () ), true );
98  } elseif ($type == 1) {
99  // Complex string hexadec
100  $ret = dechex ( time () ) . dechex ( mt_rand ( 1, 65535 ) );
101  } elseif ($type == 2) {
102  // Random for better crypto cypher hexadec
103  $r = unpack ( 'v*', fread ( fopen ( '/dev/random', 'r' ), 16 ) );
104  $ret = sprintf ( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', $r [1], $r [2], $r [3], $r [4] & 0x0fff | 0x4000, $r [5] & 0x3fff | 0x8000, $r [6], $r [7], $r [8] );
105  } elseif ($type == 3) {
106  // Host name and microseconds
107  $ret = uniqid ( php_uname ( 'n' ) . '-', true );
108  } elseif ($type == 4) {
109  // Another fast useful for file name
110  $ret = time () . substr ( md5 ( microtime ( true ) ), 0, rand ( 5, 12 ) );
111  } elseif ($type == 5) {
112  // The field names refer to RFC 4122 section 4.1.2
113  $ret = sprintf ( '%04x%04x-%04x-%03x4-%04x-%04x%04x%04x', mt_rand ( 0, 65535 ), mt_rand ( 0, 65535 ), // 32 bits for "time_low"
114  mt_rand ( 0, 65535 ), // 16 bits for "time_mid"
115  mt_rand ( 0, 4095 ), // 12 bits before the 0100 of (version) 4 for "time_hi_and_version"
116  bindec ( substr_replace ( sprintf ( '%016b', mt_rand ( 0, 65535 ) ), '01', 6, 2 ) ),
117  // 8 bits, the last two of which (positions 6 and 7) are 01, for "clk_seq_hi_res"
118  // (hence, the 2nd hex digit after the 3rd hyphen can only be 1, 5, 9 or d)
119  // 8 bits for "clk_seq_low"
120  mt_rand ( 0, 65535 ), mt_rand ( 0, 65535 ), mt_rand ( 0, 65535 ) ); // 48 bits for "node"
121  }
122 
123  return $ret;
124  }
125  }
126 }
127 
128 ?>