forked from jpecarin/BitDrizzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit_drizzle_cmd_line.py
142 lines (127 loc) · 6.4 KB
/
bit_drizzle_cmd_line.py
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
140
141
142
'''
BitDrizzle - Small, academic DHT based P2P system
Supports network operations of find_peer, lookup and insert
Supports data operation of writing to a key value store
Written by John Pecarina, Air Force Institute of Technology,
john.pecarina@afit.edu
Feb 1 2016
Modified by _______________, Date:
'''
import time
from app_support.bd_api import BitDrizzle
def print_command_line_help():
print("H - Help Menu")
print("LL - Local Listener: Checks if the Drizzler app_support listener is listening for commands")
print("MM - Multi Mode: Makes the program set up all network nodes")
print("FN - Find Network: Enter FN, future options could be host and/or port range, sets an EntryNode")
print("LH - Locate Hash: Enter LH with a hash value, as a search function in the network")
print("JN - Join Network: Enter JN, no options, will enter the host through Entry Node, sets routing information")
print("LN - Leave Network: Enter LN, no options, will leave the network, removes routing info")
print("SZ - Get Network Size: Enter SZ, no options, will send a message to estimate size of the network")
print("RI - Print Routing Info: Enter RI, no options, will print the routing info for this node")
print("W - Write Data: Enter W, with options Ex: WL <filename=test_file.txt> <piece_size=256>")
print(" - option L for app_support, N for network, stores data to app_support or network store")
print(" - filename default is test_file.txt, piece_size default is 256 chars (bytes)")
print("R - Read Data: Enter R, with options Ex: RN <key> (read data from network at key)")
print(" - option L for app_support, N for network, reads data from app_support or network store")
print(" - option K takes a key, default is test_key entry in config.py")
print("D - Delete Data: Enter D, with options Ex: DN <key> (delete data from network at key)")
print(" - option L for app_support, N for network, deletes data from app_support or network store")
print(" - option K takes a key, default is test_key entry in config.py")
print("LD - Local Data Dump: Output all the data in the local store ")
print("PD - Local Data Dump: Output all the data in the peer head data store ")
print("X - Exit BitDrizzle: Just exits the app")
def file_reader(file_name):
file_contents = ""
#opening the file
with open('test_file.txt', 'r') as myfile:
#read all the bytes and replace line returns with a blank
file_contents=myfile.read().replace('\n', '')
return file_contents
import config
'''
Main program:
'''
def main():
import os
os.system("mode con: cols=120 lines=75")
print("Welcome to BitDrizzle CLI!")
bd = BitDrizzle()
print("Binding to ports in Drizzler setup and start all servers")
bd.drizzlerSetup()
print("Setting up app_support head")
bd.localSetup()
print("#######################################################################")
print("# BIT DRIZZLE 1.0 #")
print("# CMD LINE INTERFACE #")
print("#######################################################################")
while True:
try:
cmd_string = input("Enter a command or type H for help: " or "!")
except EOFError:
cmd_string = 'X'
if not (len(cmd_string) == 0):
cmd = cmd_string.upper().split()
if cmd[0] == 'H':
print_command_line_help()
elif cmd[0] == 'LL':
if bd.is_cmd_ready():
print("Drizzler is listening for commands")
else:
print("No services")
elif cmd[0] == 'MM':
print("Going multi mode...")
print(bd.go_multi_mode())
elif cmd[0] == 'FN':
print("Found network at: " + bd.find_net())
elif cmd[0] == 'LH':
print(bd.locate_hash(cmd[0] if len(cmd) is 2 else config.test_key))
elif cmd[0] == 'JN':
print(bd.join_net())
elif cmd[0] == 'RI':
print(bd.get_net_neighbors(cmd[1] if len(cmd) is 2 else "FULL"))
elif cmd[0] == 'LN':
print("Leave Network")
elif cmd[0] == 'SZ':
print("Get Network Size")
elif cmd[0] == 'WL':
file_contents = file_reader(cmd[1] if len(cmd) is 2 and os.access(cmd[1]) else config.data_file)
try:
piece_size = (int(cmd[2]) if len(cmd) is 3 else config.piece_size)
print(bd.write_local(file_contents, piece_size))
except ValueError:
print("Piece size must be a positive integer")
elif cmd[0] == 'RL':
hash_code = cmd[1] if len(cmd) is 2 else config.test_key
print(bd.read_local(hash_code))
#print("Read Data at " + hash_code)
elif cmd[0] == 'DL':
hash_code = cmd[1] if len(cmd) is 2 else config.test_key
print(bd.delete_local(hash_code))
#print("Delete Data at " + hash_code)
elif cmd[0] == 'WN':
file_contents = file_reader(cmd[1] if len(cmd) is 2 and os.access(cmd[1]) else config.data_file)
try:
piece_size = (int(cmd[2]) if len(cmd) is 3 else config.piece_size)
print(bd.write_local(file_contents, piece_size))
except ValueError:
print("Piece size must be a positive integer")
elif cmd[0] == 'RN':
print("Read Network Data...")
elif cmd[0] == 'DN':
hash_code = cmd[1] if len(cmd) is 2 else config.test_key
print(bd.delete_from_net(hash_code))
#print("Delete Data at " + hash_code)
elif cmd[0] == 'LD':
print(bd.local_dump())
elif cmd[0] == 'PD':
print(bd.peer_dump())
elif cmd[0] == 'X': #just a hard crash really
print("Exiting Application")
time.sleep(1)
os._exit(0)
else:
print("Command Not Found")
# main entry point to program
if __name__ == "__main__":
main()