Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
opendht
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
opendht
Commits
7db0c154
Commit
7db0c154
authored
8 years ago
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
tools: catch exceptions
parent
2fc7cc57
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
tools/dhtchat.cpp
+67
-63
67 additions, 63 deletions
tools/dhtchat.cpp
tools/dhtscanner.cpp
+26
-22
26 additions, 22 deletions
tools/dhtscanner.cpp
with
93 additions
and
85 deletions
tools/dhtchat.cpp
+
67
−
63
View file @
7db0c154
...
...
@@ -62,80 +62,84 @@ main(int argc, char **argv)
#endif
DhtRunner
dht
;
dht
.
run
(
params
.
port
,
dht
::
crypto
::
generateIdentity
(
"DHT Chat Node"
),
true
,
params
.
network
);
try
{
dht
.
run
(
params
.
port
,
dht
::
crypto
::
generateIdentity
(
"DHT Chat Node"
),
true
,
params
.
network
);
if
(
not
params
.
bootstrap
.
first
.
empty
())
dht
.
bootstrap
(
params
.
bootstrap
.
first
.
c_str
(),
params
.
bootstrap
.
second
.
c_str
());
if
(
not
params
.
bootstrap
.
first
.
empty
())
dht
.
bootstrap
(
params
.
bootstrap
.
first
.
c_str
(),
params
.
bootstrap
.
second
.
c_str
());
print_node_info
(
dht
,
params
);
std
::
cout
<<
" type 'c {hash}' to join a channel"
<<
std
::
endl
<<
std
::
endl
;
print_node_info
(
dht
,
params
);
std
::
cout
<<
" type 'c {hash}' to join a channel"
<<
std
::
endl
<<
std
::
endl
;
bool
connected
{
false
};
InfoHash
room
;
const
InfoHash
myid
=
dht
.
getId
();
bool
connected
{
false
};
InfoHash
room
;
const
InfoHash
myid
=
dht
.
getId
();
#ifndef WIN32_NATIVE
// using the GNU History API
using_history
();
#ifndef WIN32_NATIVE
// using the GNU History API
using_history
();
#endif
while
(
true
)
{
// using the GNU Readline API
std
::
string
line
=
readLine
(
connected
?
PROMPT
:
"> "
);
if
(
!
line
.
empty
()
&&
line
[
0
]
==
'\0'
)
break
;
if
(
line
.
empty
())
continue
;
static
constexpr
dht
::
InfoHash
INVALID_ID
{};
std
::
istringstream
iss
(
line
);
std
::
string
op
,
idstr
;
iss
>>
op
;
if
(
not
connected
)
{
if
(
op
==
"x"
||
op
==
"q"
||
op
==
"exit"
||
op
==
"quit"
)
while
(
true
)
{
// using the GNU Readline API
std
::
string
line
=
readLine
(
connected
?
PROMPT
:
"> "
);
if
(
!
line
.
empty
()
&&
line
[
0
]
==
'\0'
)
break
;
else
if
(
op
==
"c"
)
{
iss
>>
idstr
;
room
=
InfoHash
(
idstr
);
if
(
room
==
INVALID_ID
)
{
room
=
InfoHash
::
get
(
idstr
);
std
::
cout
<<
"Joining h("
<<
idstr
<<
") = "
<<
room
<<
std
::
endl
;
}
dht
.
listen
<
dht
::
ImMessage
>
(
room
,
[
&
](
dht
::
ImMessage
&&
msg
)
{
if
(
msg
.
from
!=
myid
)
std
::
cout
<<
msg
.
from
.
toString
()
<<
" at "
<<
printTime
(
msg
.
date
)
<<
" (took "
<<
print_dt
(
std
::
chrono
::
system_clock
::
now
()
-
std
::
chrono
::
system_clock
::
from_time_t
(
msg
.
date
))
<<
"s) "
<<
(
msg
.
to
==
myid
?
"ENCRYPTED "
:
""
)
<<
": "
<<
msg
.
id
<<
" - "
<<
msg
.
msg
<<
std
::
endl
;
return
true
;
});
connected
=
true
;
}
else
{
std
::
cout
<<
"Unknown command. Type 'c {hash}' to join a channel"
<<
std
::
endl
<<
std
::
endl
;
}
}
else
{
auto
now
=
std
::
chrono
::
system_clock
::
to_time_t
(
std
::
chrono
::
system_clock
::
now
());
if
(
op
==
"d"
)
{
connected
=
false
;
if
(
line
.
empty
())
continue
;
}
else
if
(
op
==
"e"
)
{
iss
>>
idstr
;
std
::
getline
(
iss
,
line
);
dht
.
putEncrypted
(
room
,
InfoHash
(
idstr
),
dht
::
ImMessage
(
rand_id
(
rd
),
std
::
move
(
line
),
now
),
[](
bool
ok
)
{
//dht.cancelPut(room, id);
if
(
not
ok
)
std
::
cout
<<
"Message publishing failed !"
<<
std
::
endl
;
});
static
constexpr
dht
::
InfoHash
INVALID_ID
{};
std
::
istringstream
iss
(
line
);
std
::
string
op
,
idstr
;
iss
>>
op
;
if
(
not
connected
)
{
if
(
op
==
"x"
||
op
==
"q"
||
op
==
"exit"
||
op
==
"quit"
)
break
;
else
if
(
op
==
"c"
)
{
iss
>>
idstr
;
room
=
InfoHash
(
idstr
);
if
(
room
==
INVALID_ID
)
{
room
=
InfoHash
::
get
(
idstr
);
std
::
cout
<<
"Joining h("
<<
idstr
<<
") = "
<<
room
<<
std
::
endl
;
}
dht
.
listen
<
dht
::
ImMessage
>
(
room
,
[
&
](
dht
::
ImMessage
&&
msg
)
{
if
(
msg
.
from
!=
myid
)
std
::
cout
<<
msg
.
from
.
toString
()
<<
" at "
<<
printTime
(
msg
.
date
)
<<
" (took "
<<
print_dt
(
std
::
chrono
::
system_clock
::
now
()
-
std
::
chrono
::
system_clock
::
from_time_t
(
msg
.
date
))
<<
"s) "
<<
(
msg
.
to
==
myid
?
"ENCRYPTED "
:
""
)
<<
": "
<<
msg
.
id
<<
" - "
<<
msg
.
msg
<<
std
::
endl
;
return
true
;
});
connected
=
true
;
}
else
{
std
::
cout
<<
"Unknown command. Type 'c {hash}' to join a channel"
<<
std
::
endl
<<
std
::
endl
;
}
}
else
{
dht
.
putSigned
(
room
,
dht
::
ImMessage
(
rand_id
(
rd
),
std
::
move
(
line
),
now
),
[](
bool
ok
)
{
//dht.cancelPut(room, id);
if
(
not
ok
)
std
::
cout
<<
"Message publishing failed !"
<<
std
::
endl
;
});
auto
now
=
std
::
chrono
::
system_clock
::
to_time_t
(
std
::
chrono
::
system_clock
::
now
());
if
(
op
==
"d"
)
{
connected
=
false
;
continue
;
}
else
if
(
op
==
"e"
)
{
iss
>>
idstr
;
std
::
getline
(
iss
,
line
);
dht
.
putEncrypted
(
room
,
InfoHash
(
idstr
),
dht
::
ImMessage
(
rand_id
(
rd
),
std
::
move
(
line
),
now
),
[](
bool
ok
)
{
//dht.cancelPut(room, id);
if
(
not
ok
)
std
::
cout
<<
"Message publishing failed !"
<<
std
::
endl
;
});
}
else
{
dht
.
putSigned
(
room
,
dht
::
ImMessage
(
rand_id
(
rd
),
std
::
move
(
line
),
now
),
[](
bool
ok
)
{
//dht.cancelPut(room, id);
if
(
not
ok
)
std
::
cout
<<
"Message publishing failed !"
<<
std
::
endl
;
});
}
}
}
}
catch
(
const
std
::
exception
&
e
)
{
std
::
cerr
<<
std
::
endl
<<
e
.
what
()
<<
std
::
endl
;
}
std
::
cout
<<
std
::
endl
<<
"Stopping node..."
<<
std
::
endl
;
...
...
This diff is collapsed.
Click to expand it.
tools/dhtscanner.cpp
+
26
−
22
View file @
7db0c154
...
...
@@ -85,35 +85,39 @@ main(int argc, char **argv)
}
DhtRunner
dht
;
dht
.
run
(
params
.
port
,
{},
true
,
params
.
network
);
try
{
dht
.
run
(
params
.
port
,
{},
true
,
params
.
network
);
if
(
not
params
.
bootstrap
.
first
.
empty
())
dht
.
bootstrap
(
params
.
bootstrap
.
first
.
c_str
(),
params
.
bootstrap
.
second
.
c_str
());
if
(
not
params
.
bootstrap
.
first
.
empty
())
dht
.
bootstrap
(
params
.
bootstrap
.
first
.
c_str
(),
params
.
bootstrap
.
second
.
c_str
());
std
::
cout
<<
"OpenDht node "
<<
dht
.
getNodeId
()
<<
" running on port "
<<
params
.
port
<<
std
::
endl
;
std
::
cout
<<
"Scanning network..."
<<
std
::
endl
;
auto
all_nodes
=
std
::
make_shared
<
NodeSet
>
();
std
::
cout
<<
"OpenDht node "
<<
dht
.
getNodeId
()
<<
" running on port "
<<
params
.
port
<<
std
::
endl
;
std
::
cout
<<
"Scanning network..."
<<
std
::
endl
;
auto
all_nodes
=
std
::
make_shared
<
NodeSet
>
();
// Set hash to 1 because 0 is the null hash
dht
::
InfoHash
cur_h
{};
cur_h
.
setBit
(
8
*
HASH_LEN
-
1
,
1
);
// Set hash to 1 because 0 is the null hash
dht
::
InfoHash
cur_h
{};
cur_h
.
setBit
(
8
*
HASH_LEN
-
1
,
1
);
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
seconds
(
2
));
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
seconds
(
2
));
std
::
atomic_uint
done
{
0
};
step
(
dht
,
done
,
all_nodes
,
cur_h
,
0
);
std
::
atomic_uint
done
{
0
};
step
(
dht
,
done
,
all_nodes
,
cur_h
,
0
);
{
std
::
mutex
m
;
std
::
unique_lock
<
std
::
mutex
>
lk
(
m
);
cv
.
wait
(
lk
,
[
&
](){
return
done
.
load
()
==
0
;
});
}
{
std
::
mutex
m
;
std
::
unique_lock
<
std
::
mutex
>
lk
(
m
);
cv
.
wait
(
lk
,
[
&
](){
return
done
.
load
()
==
0
;
});
}
std
::
cout
<<
std
::
endl
<<
"Scan ended: "
<<
all_nodes
->
size
()
<<
" nodes found."
<<
std
::
endl
;
for
(
const
auto
&
n
:
*
all_nodes
)
std
::
cout
<<
"Node "
<<
*
n
<<
std
::
endl
;
std
::
cout
<<
std
::
endl
<<
"Scan ended: "
<<
all_nodes
->
size
()
<<
" nodes found."
<<
std
::
endl
;
for
(
const
auto
&
n
:
*
all_nodes
)
std
::
cout
<<
"Node "
<<
*
n
<<
std
::
endl
;
}
catch
(
const
std
::
exception
&
e
)
{
std
::
cerr
<<
std
::
endl
<<
e
.
what
()
<<
std
::
endl
;
}
dht
.
join
();
#ifdef WIN32_NATIVE
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment