forked from parc-ccnx-archive/ccnxHelloWorld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccnx-ping-server.c
139 lines (104 loc) · 4.35 KB
/
ccnx-ping-server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* Copyright 2014-2015 Palo Alto Research Center, Inc. (PARC), a Xerox company. All Rights Reserved.
* The content of this file, whole or in part, is subject to licensing terms.
* If distributing this software, include this License Header Notice in each
* file and provide the accompanying LICENSE file.
*/
/**
* @author Glenn Scott, Computing Science Laboratory, PARC
* @copyright 2014-2015 Palo Alto Research Center, Inc. (PARC), A Xerox Company. All Rights Reserved.
*/
#include <config.h>
#include <stdio.h>
#include <LongBow/runtime.h>
#include <time.h>
#include <ccnx/api/ccnx_Portal/ccnx_Portal.h>
#include <ccnx/api/ccnx_Portal/ccnx_PortalRTA.h>
#include <parc/security/parc_Security.h>
#include <parc/security/parc_PublicKeySignerPkcs12Store.h>
#include <parc/security/parc_IdentityFile.h>
#include <ccnx/common/ccnx_Name.h>
PARCIdentity *
createAndGetIdentity(void)
{
const char *keystoreName = "producer_keystore";
const char *keystorePassword = "keystore_password";
unsigned int keyLength = 1024;
unsigned int validityDays = 30;
char *subjectName = "producer";
bool success = parcPublicKeySignerPkcs12Store_CreateFile(keystoreName, keystorePassword, subjectName, keyLength, validityDays);
assertTrue(success,
"parcPublicKeySignerPkcs12Store_CreateFile('%s', '%s', '%s', %d, %d) failed.",
keystoreName, keystorePassword, subjectName, keyLength, validityDays);
PARCIdentityFile *identityFile = parcIdentityFile_Create(keystoreName, keystorePassword);
PARCIdentity *identity = parcIdentity_Create(identityFile, PARCIdentityFileAsPARCIdentity);
parcIdentityFile_Release(&identityFile);
return identity;
}
CCNxPortalFactory *
setupPortalFactory(void)
{
PARCIdentity *identity = createAndGetIdentity();
CCNxPortalFactory *factory = ccnxPortalFactory_Create(identity);
parcIdentity_Release(&identity);
return factory;
}
PARCBuffer *
makePayload(void)
{
time_t theTime = time(0);
PARCBufferComposer *composer = parcBufferComposer_Create();
parcBufferComposer_Format(composer, "Ping World. The time is %s", ctime(&theTime));
PARCBuffer *payload = parcBufferComposer_ProduceBuffer(composer);
parcBufferComposer_Release(&composer);
return payload;
}
int
producer(void)
{
parcSecurity_Init();
CCNxPortalFactory *factory = setupPortalFactory();
CCNxPortal *portal = ccnxPortalFactory_CreatePortal(factory, ccnxPortalRTA_Message);
assertNotNull(portal, "Expected a non-null CCNxPortal pointer.");
CCNxName *listenName = ccnxName_CreateFromURI("lci:/Ping");
CCNxName *goodbye = ccnxName_CreateFromURI("lci:/Ping/Goodbye%21");
CCNxName *contentName = ccnxName_CreateFromURI("lci:/Ping/World");
if (ccnxPortal_Listen(portal, listenName,60 * 60 * 24 * 365,CCNxStackTimeout_Never)) {
while (true) {
CCNxMetaMessage *request = ccnxPortal_Receive(portal,CCNxStackTimeout_Never);
if (request == NULL) {
break;
}
CCNxInterest *interest = ccnxMetaMessage_GetInterest(request);
if (interest != NULL) {
CCNxName *interestName = ccnxInterest_GetName(interest);
if (ccnxName_Equals(interestName, contentName)) {
PARCBuffer *payload = makePayload();
CCNxContentObject *contentObject = ccnxContentObject_CreateWithDataPayload(contentName, payload);
CCNxMetaMessage *message = ccnxMetaMessage_CreateFromContentObject(contentObject);
if (ccnxPortal_Send(portal, message, CCNxStackTimeout_Never) == false) {
fprintf(stderr, "ccnxPortal_Send failed: %d\n", ccnxPortal_GetError(portal));
}
ccnxMetaMessage_Release(&message);
parcBuffer_Release(&payload);
} else if (ccnxName_Equals(interestName, goodbye)) {
break;
}
}
ccnxMetaMessage_Release(&request);
}
}
ccnxName_Release(&listenName);
ccnxName_Release(&goodbye);
ccnxName_Release(&contentName);
ccnxPortal_Release(&portal);
ccnxPortalFactory_Release(&factory);
parcSecurity_Fini();
return 0;
}
int
main(int argc, const char *argv[argc])
{
return producer();
}