#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "uuidgen.h"
struct shmuuid {
unsigned char shm_mac[6];
struct timeval timestamp;
unsigned short clock_seq;
};
static struct sembuf _lockop = { 0, -1, 0};
static struct sembuf _unlockop = { 0, 1, 0};
/* compares two timestamps */
static int tscomp(const struct timeval* lhs, const struct timeval* rhs);
/* retreives the semaphore */
static int getsem(key_t key);
/* releases the semaphore */
static void releasesem(int sem);
/* retreives the shared memory */
static struct shmuuid* getsharedmem(key_t shmkey);
/* releases the shared memory */
static void releaseshm(struct shmuuid* shared);
static void getmac(unsigned char mac[6]);
static key_t getshmkey();
void uuidgen(struct uuid* puuid)
{
key_t shmkey;
int shmseg;
int sem;
shmkey = getshmkey();
sem = getsem(shmkey);
if (sem != -1) {
struct shmuuid* shared = getsharedmem(shmkey);
if (shared != (struct shmuuid*)-1) {
struct timeval now;
int cmp;
gettimeofday(&now, NULL);
if (tscomp(&(shared->timestamp),&now)) {
//srand(shared->timestamp.tv_usec);
srand(shared->clock_seq);
shared->clock_seq = (unsigned short)rand();
memcpy(&shared->timestamp,&now,sizeof(now));
} else
shared->clock_seq++;
// takes bottom 12 bits of tv_sec
puuid->guid.d1 = ((shared->timestamp.tv_sec<<28)&0xF0000000)|((shared->timestamp.tv_usec<<8)&0x0FFFFFFF);
// takes bottom 16 of top 20
puuid->guid.d2 = (unsigned short)(((shared->timestamp.tv_sec>>4) & 0x00FFFF));
// takes top 4 of top 16
puuid->guid.d3 = (unsigned short)(((shared->timestamp.tv_sec>>16) & 0x00FF00) | 0x01);
memcpy(&(puuid->guid.d4), &(shared->clock_seq), 2);
puuid->guid.d4[0] = (puuid->guid.d4[0] | 0x80) & 0x9F;
memcpy((puuid->guid.d4)+2, shared->shm_mac, 6);
releaseshm(shared);
} else
perror("getting shared memory");
releasesem(sem);
} else
perror("getting semaphore");
}
static key_t getshmkey()
{
static const char path[] = "/tmp/uuid";
int fd = open(path, O_CREAT, 0666);
if (fd > 0) {
close(fd);
return ftok(path, 0);
}
return ftok("/", 999);
}
/*
Gets and locks the semaphore
*/
static int getsem(key_t key)
{
int sem = semget(key, 1, IPC_CREAT | IPC_EXCL);
if (sem == -1 && errno == EEXIST) {
sem = semget(key, 1, 0);
if (sem == -1)
return -1;
semop(sem, &_lockop, 1);
return sem;
} else if (sem == -1) {
return -1;
}
semctl(sem, 0, SETVAL, (int)0);
return sem;
}
static void releasesem(int sem)
{
semop(sem, &_unlockop, 1);
}
static struct shmuuid* getsharedmem(key_t shmkey)
{
int seg;
struct shmuuid* pmem;
seg = shmget(shmkey, sizeof(struct shmuuid), IPC_CREAT|IPC_EXCL|0666);
if (seg == -1 && errno == EEXIST) {
seg = shmget(shmkey, sizeof(struct shmuuid), 0666);
if (seg == -1)
return (struct shmuuid*)-1;
return (struct shmuuid*)shmat(seg, NULL, 0);
}
pmem = (struct shmuuid*)shmat(seg, NULL, 0);
if (pmem != (struct shmuuid*)-1) {
memset(pmem,0,sizeof(struct shmuuid));
getmac(pmem->shm_mac);
}
return pmem;
}
static void releaseshm(struct shmuuid* shared)
{
shmdt(shared);
}
#ifdef linux
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <linux/if.h>
#endif
#ifdef HPUX
#include <netio.h>
#endif
#ifdef AIX
#include <sys/ndd_var.h>
#include <sys/kinfo.h>
#endif
static long mac_addr_sys ( u_char *addr)
{
/* implementation for Linux */
#ifdef linux
struct ifreq ifr;
struct ifreq *IFR;
struct ifconf ifc;
char buf[1024];
int s, i;
int ok = 0;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s==-1) {
return -1;
}
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
ioctl(s, SIOCGIFCONF, &ifc);
IFR = ifc.ifc_req;
for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; IFR++) {
strcpy(ifr.ifr_name, IFR->ifr_name);
if (ioctl(s, SIOCGIFFLAGS, &ifr) == 0) {
if (! (ifr.ifr_flags & IFF_LOOPBACK)) {
if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0) {
ok = 1;
break;
}
}
}
}
close(s);
if (ok) {
bcopy( ifr.ifr_hwaddr.sa_data, addr, 6);
}
else {
return -1;
}
return 0;
#endif
/* implementation for HP-UX */
#ifdef HPUX
#define LAN_DEV0 "/dev/lan0"
int fd;
struct fis iocnt_block;
int i;
char net_buf[sizeof(LAN_DEV0)+1];
char *p;
(void)sprintf(net_buf, "%s", LAN_DEV0);
p = net_buf + strlen(net_buf) - 1;
/*
* Get 802.3 address from card by opening the driver and interrogating it.
*/
for (i = 0; i < 10; i++, (*p)++) {
if ((fd = open (net_buf, O_RDONLY)) != -1) {
iocnt_block.reqtype = LOCAL_ADDRESS;
ioctl (fd, NETSTAT, &iocnt_block);
close (fd);
if (iocnt_block.vtype == 6)
break;
}
}
if (fd == -1 || iocnt_block.vtype != 6) {
return -1;
}
bcopy( &iocnt_block.value.s[0], addr, 6);
return 0;
#endif /* HPUX */
/* implementation for AIX */
#ifdef AIX
int size;
struct kinfo_ndd *nddp;
size = getkerninfo(KINFO_NDD, 0, 0, 0);
if (size <= 0) {
return -1;
}
nddp = (struct kinfo_ndd *)malloc(size);
if (!nddp) {
return -1;
}
if (getkerninfo(KINFO_NDD, nddp, &size, 0) < 0) {
free(nddp);
return -1;
}
bcopy(nddp->ndd_addr, addr, 6);
free(nddp);
return 0;
#endif
/* Not implemented platforms */
return -1;
}
static void getmac(unsigned char mac[6])
{
// TODO: Get the mac address
int iret = mac_addr_sys(mac);
if (iret)
memset(mac,1,sizeof(mac));
}
static int tscomp(const struct timeval* lhs, const struct timeval* rhs)
{
int ret;
ret = lhs->tv_sec - rhs->tv_sec;
if (!ret)
ret = lhs->tv_usec - rhs->tv_usec;
return ret;
}
static void rmshm(int shmid)
{
printf("removeing shared memory for shmid:%ld\n", shmid);
if (shmid != -1) {
if (shmctl(shmid, IPC_RMID, 0) == -1)
perror("removing shared memory");
}
}
#if 0
int main(int argc, char* argv[])
{
struct uuid Uuid = {0};
int x;
if (argc > 1 && strcmp(argv[1], "clear")==0)
rmshm(shmget(getshmkey(), sizeof(struct shmuuid), 0));
while (98751) {
uuidgen(&Uuid);
printf("%08lx-%04lx-%04lx-%04lx-%02lx%02lx%02lx%02lx%02lx%02lx\n",
Uuid.guid.d1,Uuid.guid.d2,Uuid.guid.d3,
*(unsigned short*)Uuid.guid.d4,Uuid.guid.d4[2],Uuid.guid.d4[3],
Uuid.guid.d4[4],Uuid.guid.d4[5],Uuid.guid.d4[6],Uuid.guid.d4[7]);
}
return 0;
}
#endif
没有合适的资源?快使用搜索试试~ 我知道了~
safmq
共89个文件
h:37个
cpp:26个
dsp:5个
5星 · 超过95%的资源 需积分: 0 73 下载量 2 浏览量
2008-06-24
14:08:45
上传
评论 1
收藏 163KB ZIP 举报
温馨提示
SAFMQ(全称为Store and Forward Message Queue)是一个简单的消息中间件,采用C++编写,采用Apache授权机制。截至2006年11月SAFMQ的的版本为0.5.2,发布于2006年9月。目前版本具有如下的功能:<br>1. 提供多队列、多优先级的消息转发服务。<br>2. 支持文本、二进制的消息类型。<br>3. 支持转发功能,即多个消息中间件之间的消息转发。<br>4. 支持事务操作<br>5. 支持Java、PHP客户端<br>6. 支持SSL加密<br>7. 支持用户权限<br>8. 支持对消息的标记<br>9. 支持TTL(Time To Live)时间戳
资源推荐
资源详情
资源评论
收起资源包目录
safmq.0.5.2.zip (89个子文件)
META-INF
MANIFEST.MF 62B
safmq.0.5.2
QAccessControl.cpp 22KB
main.cpp 8KB
main.h 2KB
SystemConnection.cpp 2KB
DirectoryList.h 2KB
ServerThread.cpp 2KB
QStorage.cpp 23KB
ForwardThread.cpp 5KB
ServerThread.h 1KB
Signal.h 1KB
DirectoryList.cpp 3KB
SystemDelivery.cpp 4KB
SystemConnection.h 2KB
SSLServerThread.cpp 3KB
param_config.h 2KB
utilities
_getopt_long.c 3KB
_getopt.h 483B
test.sh 2KB
makefile 714B
safmqc.dsp 7KB
safmqc.cpp 43KB
INSTALL 2KB
QManager.cpp 6KB
ServiceThread.cpp 48KB
server_operators.h 12KB
safmq_defs.h 2KB
Mutex.h 1KB
QAccessControl.h 9KB
safmq.dsw 1KB
QManager.h 2KB
configure 3KB
makefile 6KB
QStorage.h 8KB
TODO 964B
TransactionManager.cpp 5KB
SSLServerThread.h 1KB
Signal.cpp 3KB
safmq.h 12KB
ntservice.cpp 10KB
Mutex.cpp 2KB
ServiceThread.h 4KB
quectrl.h 8KB
TransactionManager.h 2KB
README 853B
comdefs.h 10KB
SystemDelivery.h 1KB
ForwardThread.h 1KB
safmq.dsp 10KB
sample
safmq.conf 1KB
lib
safmqlib.dsp 8KB
MessageQueue.cpp 8KB
client_operators.h 9KB
MQConnection.h 8KB
MQFactory.h 2KB
MQFactory.cpp 7KB
safmqclient.dsp 4KB
safmqclient.cpp 26KB
footer.html 161B
makefile 2KB
header.html 20B
MessageQueue.h 2KB
MQConnection.cpp 45KB
php_safmq.help 28KB
safmqclient.dsw 825B
param_config.cpp 3KB
shared
thdlib.h 2KB
utilities.cpp 12KB
uuidgen
mac_addr_dlpi.c 5KB
uuidgenwin.c 934B
uuidgen.h 2KB
uuidgen.c 7KB
dbg_alloc.h 642B
url
urldecode.h 5KB
bufstream.h 6KB
randpq.h 7KB
thdlib.cpp 4KB
utilities.h 6KB
winnt
ntservice.h 6KB
regutil.h 4KB
ntservice.cpp 11KB
winnt.dsw 533B
winnt.dsp 4KB
winnt.plg 1KB
tcpsocket
tcpsocket.cpp 19KB
tcpsocket.h 10KB
socstream.h 10KB
sslsocket.cpp 21KB
sslsocket.h 5KB
共 89 条
- 1
资源评论
- wfbhjc20102014-04-14下载这个资料是拿来做实验参考用,理解这代码还是花了一点时间,资料具有参考价值。
badboss119
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功