diff options
Diffstat (limited to 'src/gnunet/social/socialapp.cpp')
-rw-r--r-- | src/gnunet/social/socialapp.cpp | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/src/gnunet/social/socialapp.cpp b/src/gnunet/social/socialapp.cpp new file mode 100644 index 0000000..e739574 --- /dev/null +++ b/src/gnunet/social/socialapp.cpp | |||
@@ -0,0 +1,151 @@ | |||
1 | #include "socialapp.h" | ||
2 | #include "app.h" | ||
3 | |||
4 | #include "host.h" | ||
5 | #include "guest.h" | ||
6 | |||
7 | |||
8 | SocialApp::SocialApp (struct GNUNET_CONFIGURATION_Handle *config, | ||
9 | QObject *parent) | ||
10 | : QObject (parent) | ||
11 | { | ||
12 | m_config = config; | ||
13 | m_app = | ||
14 | GNUNET_SOCIAL_app_connect (m_config, APP_NAME, | ||
15 | egoCallback, | ||
16 | hostPlaceCallback, | ||
17 | guestPlaceCallback, | ||
18 | this); | ||
19 | } | ||
20 | |||
21 | |||
22 | SocialApp::~SocialApp () | ||
23 | { | ||
24 | GNUNET_SOCIAL_app_disconnect (m_app); | ||
25 | } | ||
26 | |||
27 | |||
28 | void | ||
29 | SocialApp::egoCallback (void *cls, | ||
30 | struct GNUNET_SOCIAL_Ego *ego, | ||
31 | const struct GNUNET_CRYPTO_EcdsaPublicKey *egoPubKey, | ||
32 | const char *name) | ||
33 | { | ||
34 | SocialApp *s = (SocialApp *) cls; | ||
35 | s->addEgo (new Ego (*egoPubKey, QString::fromLatin1 (name), ego)); | ||
36 | } | ||
37 | |||
38 | |||
39 | void | ||
40 | SocialApp::addEgo (Ego *ego) | ||
41 | { | ||
42 | m_egos += ego; | ||
43 | emit egoAdded (ego); | ||
44 | } | ||
45 | |||
46 | void | ||
47 | SocialApp::addPlace (Place *place) | ||
48 | { | ||
49 | m_places += place; | ||
50 | place->setIndex (m_places.count () - 1); | ||
51 | emit placeAdded (place); | ||
52 | } | ||
53 | |||
54 | void | ||
55 | SocialApp::hostPlaceCallback (void *cls, | ||
56 | struct GNUNET_SOCIAL_HostConnection *hconn, | ||
57 | struct GNUNET_SOCIAL_Ego *ego, | ||
58 | const struct GNUNET_CRYPTO_EddsaPublicKey *placePubKey, | ||
59 | enum GNUNET_SOCIAL_AppPlaceState placeState) | ||
60 | { | ||
61 | SocialApp *s = (SocialApp *) cls; | ||
62 | s->addPlace (new Host (s->m_app, ego, hconn, s)); | ||
63 | } | ||
64 | |||
65 | |||
66 | void | ||
67 | SocialApp::guestPlaceCallback (void *cls, | ||
68 | struct GNUNET_SOCIAL_GuestConnection *gconn, | ||
69 | struct GNUNET_SOCIAL_Ego *ego, | ||
70 | const struct GNUNET_CRYPTO_EddsaPublicKey *placePubKey, | ||
71 | enum GNUNET_SOCIAL_AppPlaceState placeState) | ||
72 | { | ||
73 | SocialApp *s = (SocialApp *) cls; | ||
74 | s->addPlace (new Guest (s->m_app, ego, gconn, s)); | ||
75 | } | ||
76 | |||
77 | |||
78 | Ego * | ||
79 | SocialApp::findEgo (const QString pubKeyStr) | ||
80 | { | ||
81 | QListIterator<Ego *> it (m_egos); | ||
82 | while (it.hasNext ()) | ||
83 | { | ||
84 | Ego *ego = it.next (); | ||
85 | if (ego->pubKeyString () == pubKeyStr) | ||
86 | return ego; | ||
87 | } | ||
88 | return 0; | ||
89 | } | ||
90 | |||
91 | |||
92 | void | ||
93 | SocialApp::createPlace (GNUNET_SOCIAL_Ego *ego) | ||
94 | { | ||
95 | addPlace (new Host (m_app, ego, GNUNET_PSYC_CHANNEL_PRIVATE, this)); | ||
96 | } | ||
97 | |||
98 | |||
99 | void | ||
100 | SocialApp::createPlaceSlot (QString egoPubKeyStr) | ||
101 | { | ||
102 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating a New Place.\n"); | ||
103 | |||
104 | Ego *ego = findEgo (egoPubKeyStr); | ||
105 | if (!ego) // FIXME: fall back to last added ego for now | ||
106 | { | ||
107 | Q_ASSERT (!m_egos.isEmpty ()); | ||
108 | ego = m_egos.last (); | ||
109 | } | ||
110 | |||
111 | createPlace (ego->ego ()); | ||
112 | } | ||
113 | |||
114 | |||
115 | void | ||
116 | SocialApp::enterPlace (GNUNET_SOCIAL_Ego *ego, | ||
117 | GNUNET_CRYPTO_EddsaPublicKey *pubKey, | ||
118 | GNUNET_PeerIdentity *peerId) | ||
119 | { | ||
120 | addPlace (new Guest (m_app, ego, pubKey, peerId, 0, NULL, NULL, this)); | ||
121 | } | ||
122 | |||
123 | |||
124 | void | ||
125 | SocialApp::enterPlaceSlot (QString egoPubKeyStr, QString pubKeyStr, QString peerIdStr) | ||
126 | { | ||
127 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Entering place %s as guest.\n", | ||
128 | pubKeyStr.toStdString ().c_str ()); | ||
129 | |||
130 | GNUNET_CRYPTO_EddsaPublicKey pubKey; | ||
131 | if (GNUNET_OK != | ||
132 | GNUNET_CRYPTO_eddsa_public_key_from_string (pubKeyStr.toStdString ().c_str (), | ||
133 | pubKeyStr.length (), | ||
134 | &pubKey)) | ||
135 | return; // Failed conversion | ||
136 | |||
137 | GNUNET_PeerIdentity peerId; | ||
138 | if (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string (peerIdStr.toStdString ().c_str (), | ||
139 | peerIdStr.length (), | ||
140 | &peerId.public_key)) | ||
141 | return; // Failed conversion | ||
142 | |||
143 | Ego *ego = findEgo (egoPubKeyStr); | ||
144 | if (!ego) // FIXME: fall back to last added ego for now | ||
145 | { | ||
146 | Q_ASSERT (!m_egos.isEmpty ()); | ||
147 | ego = m_egos.last (); | ||
148 | } | ||
149 | |||
150 | enterPlace (ego->ego (), &pubKey, &peerId); | ||
151 | } | ||