]> err.no Git - sope/blob - sope-core/EOControl/EOGlobalID.m
Drop apache 1 build-dependency
[sope] / sope-core / EOControl / EOGlobalID.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "EOGlobalID.h"
23 #include "common.h"
24 #include <time.h>
25 #include <unistd.h>
26 #if !defined(__MINGW32__)
27 #  include <netdb.h>
28 #endif
29
30 @implementation EOGlobalID
31
32 - (BOOL)isTemporary {
33   return NO;
34 }
35
36 - (id)copyWithZone:(NSZone *)_zone {
37   [self doesNotRecognizeSelector:_cmd];
38   return nil;
39 }
40
41 @end /* EOGlobalID */
42
43 @implementation EOTemporaryGlobalID
44
45 static unsigned short sequence = 0;
46 static unsigned int   ip;
47
48 + (void)initialize {
49   static BOOL isInitialized = NO;
50   if (!isInitialized) {
51     char buf[1024];
52     struct hostent *hostEntry;
53     
54     isInitialized = YES;
55     gethostname(buf, 1024);
56     // THREADING
57     if ((hostEntry = gethostbyname(buf))) {
58       char **ptr;
59
60       ptr = hostEntry->h_addr_list;
61       if (*ptr) {
62         NSAssert((unsigned)hostEntry->h_length >= sizeof(ip),
63                  @"invalid host address !");
64         memcpy(&ip, *ptr, sizeof(ip));
65       }
66       else {
67         NSLog(@"WARNING: set IP address for EO key generation to 0.0.0.0 !");
68         ip = 0;
69       }
70     }
71     else {
72       NSLog(@"WARNING: set IP address for EO key generation to 0.0.0.0 !");
73       ip = 0;
74     }
75   }
76 }
77
78 + (void)assignGloballyUniqueBytes:(unsigned char *)_buffer {
79   struct {
80     unsigned short sequence;
81     unsigned short pid;
82     unsigned int   time;
83     unsigned int   ip;
84   } *bufPtr;
85
86   bufPtr = (void *)_buffer;
87   bufPtr->sequence = sequence++;
88 #if defined(__WIN32__)
89   bufPtr->pid      = (unsigned short)GetCurrentProcessId();
90 #else
91   bufPtr->pid      = getpid();
92 #endif
93   bufPtr->time     = time(NULL);
94   bufPtr->ip       = ip;
95 }
96
97 - (id)init {
98   [self->isa assignGloballyUniqueBytes:&(self->idbuffer[0])];
99   return self;
100 }
101
102 - (BOOL)isTemporary {
103   return YES;
104 }
105
106 - (BOOL)isEqual:(id)_other {
107   return _other == self ? YES : NO;
108 #if 0
109   EOTemporaryGlobalID *otherKey;
110   
111   if (_other == nil)  return NO;
112   if (_other == self) return YES;
113   otherKey = _other;
114   if (otherKey->isa != self->isa) return NO;
115   // compare bytes
116   return NO;
117 #endif
118 }
119
120 @end /* EOTemporaryGlobalID */