浏览代码

init
broad cast

Shayan 4 年之前
父节点
当前提交
c7e841b75d

+ 140 - 0
.gitignore

@@ -0,0 +1,140 @@
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+*$py.class
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+wheels/
+share/python-wheels/
+*.egg-info/
+.installed.cfg
+*.egg
+MANIFEST
+
+# PyInstaller
+#  Usually these files are written by a python script from a template
+#  before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.nox/
+.coverage
+.coverage.*
+.cache
+nosetests.xml
+coverage.xml
+*.cover
+*.py,cover
+.hypothesis/
+.pytest_cache/
+cover/
+
+# Translations
+*.mo
+*.pot
+
+# Django stuff:
+*.log
+local_settings.py
+db.sqlite3
+db.sqlite3-journal
+
+# Flask stuff:
+instance/
+.webassets-cache
+
+# Scrapy stuff:
+.scrapy
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+.pybuilder/
+target/
+
+# Jupyter Notebook
+.ipynb_checkpoints
+
+# IPython
+profile_default/
+ipython_config.py
+
+# pyenv
+#   For a library or package, you might want to ignore these files since the code is
+#   intended to run in multiple environments; otherwise, check them in:
+# .python-version
+
+# pipenv
+#   According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
+#   However, in case of collaboration, if having platform-specific dependencies or dependencies
+#   having no cross-platform support, pipenv may install dependencies that don't work, or not
+#   install all needed dependencies.
+#Pipfile.lock
+
+# PEP 582; used by e.g. github.com/David-OConnor/pyflow
+__pypackages__/
+
+# Celery stuff
+celerybeat-schedule
+celerybeat.pid
+
+# SageMath parsed files
+*.sage.py
+
+# Environments
+.env
+.venv
+env/
+venv/
+ENV/
+env.bak/
+venv.bak/
+
+# Spyder project settings
+.spyderproject
+.spyproject
+
+# Rope project settings
+.ropeproject
+
+# mkdocs documentation
+/site
+
+# mypy
+.mypy_cache/
+.dmypy.json
+dmypy.json
+
+# Pyre type checker
+.pyre/
+
+# pytype static type analyzer
+.pytype/
+
+# Cython debug symbols
+cython_debug/
+
+MP-SPDZ/

+ 34 - 0
main.py

@@ -0,0 +1,34 @@
+from optparse import OptionParser
+from src.oracle import oracle
+from src.user import user
+def main():
+    parser = OptionParser()
+
+    parser.add_option("-o", "--oracle", action="store_true", dest="oracle", default=False, help="if true -> orcle will be run")
+    parser.add_option("-u", "--user", action="store_true", dest="user", default=False, help="if true -> user will be run")
+    parser.add_option("-s", "--server", dest="server", default="127.0.0.1", help="destination Server for feed data to Oracles")
+    parser.add_option("-d", "--data", dest="data", default="", help="Data to send to destination server for response")
+    parser.add_option("-b", "--broadcast", dest="broadcast", default="255.255.255.255", help="Broad cast IP")
+    parser.add_option("-p", "--broadcast-port", dest="broadcastPort", default=6666, help="Broad cast Port")
+    # parser.add_option("-h", "--help",action="store_true", dest="help", default=False)
+
+
+
+    (options, args) = parser.parse_args()
+
+
+
+    if len(args) >= 1 or options.oracle + options.user != 1:
+        print("\n\n\tWhat do you want to run? Oracle or User? \n\n\tplease use -o OR -u. \n\n\tFor more Info you can use -h or --help or i can do it for you\n \n\t~~~ GOOD LUCK :) ~~~\n")
+        parser.print_help()
+        return
+
+
+    if(options.oracle):
+        oracle.run(broadcast=options.broadcast, broadcastPort=options.broadcastPort)
+    
+    if(options.user):
+        user.run(data=options.data, server=options.server, broadcast=options.broadcast, broadcastPort=options.broadcastPort)
+
+if __name__ == '__main__':
+    main()

+ 1 - 0
src/oracle/__init__.py

@@ -0,0 +1 @@
+

+ 22 - 0
src/oracle/broadcast.py

@@ -0,0 +1,22 @@
+
+import socket
+import threading
+
+        
+class Broadcast():
+
+    def __init__(self,  broadcastPort,socketPort): 
+        """ ,broadcast """ # TODO Broadcast
+        self.socketPort = socketPort
+        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+        self.sock.bind(('', broadcastPort)) # TODO Broadcast
+        t1 = threading.Thread(target=self.listen)
+        t1.start()
+        print("Broadcast Listener is created")
+
+    def listen(self):
+        while True:
+            msg, addr = self.sock.recvfrom(1024)
+            print("\n Message",msg," from ",addr)
+            self.sock.sendto(bytes(str(self.socketPort),'utf-8'),addr)

+ 8 - 0
src/oracle/oracle.py

@@ -0,0 +1,8 @@
+
+from .broadcast import Broadcast
+import random 
+
+socketPort = int(random.random()*50000+1024)
+def run(broadcast, broadcastPort):
+    b = Broadcast(broadcastPort=broadcastPort,socketPort=socketPort)
+    # b.start()

+ 22 - 0
src/oracle/socket.py

@@ -0,0 +1,22 @@
+
+import socket
+import threading
+
+        
+class Socket():
+
+    def __init__(self, socketPort): 
+        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
+            s.bind((HOST, socketPort))
+            s.listen()
+            self.sock = s
+            t1 = threading.Thread(target=self.connection)
+            t1.start()
+            print("socket is created and Listener is created")
+
+    def connection(self):
+        while True:
+            conn, addr = self.sock.accept()
+            print(conn, addr)
+            # u = _thread.start_new_thread( user, (conn, addr))
+            # connectedUser.append((addr,u))

+ 0 - 0
src/oracle/worker/bearer/__init__.py


+ 0 - 0
src/oracle/worker/connector/__init__.py


+ 0 - 0
src/oracle/worker/cryptor/__init__.py


+ 26 - 0
src/oracle/worker/worker.py

@@ -0,0 +1,26 @@
+import threading
+
+
+class worker(threading.Thread):
+    def __init__(self,connection,address):
+        super(worker,self).__init__(None, None, 
+			              None, None)
+        self.conn = connection
+        self.addr = address
+        self.history = []
+        _thread.start_new_thread( self.receive, ())
+        self.send("Connected")
+
+    def send(self,Message):
+        Message=bytes(Message,'utf-8')
+        self.conn.sendall(Message)
+        self.history.append(("send",Message))
+
+    def receive(self):
+        while True: 
+            len = self.conn.recv(LenFunc)
+            if not len:
+                break
+            data = self.conn.recv(int(len))
+            padra.parse(data.decode("utf-8"))
+            self.history.append(("receive",data))

+ 0 - 0
src/user/__init__.py


+ 24 - 0
src/user/broadcast.py

@@ -0,0 +1,24 @@
+
+import socket
+import threading
+
+        
+class Broadcast():
+
+    def __init__(self,broadcast,  broadcastPort): 
+        msg = bytes("Hello, Who can do somthing for me? Tnx",'utf-8')
+        self.onlineOracles=[]
+        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
+        self.sock.sendto(msg,(broadcast, broadcastPort))
+        t1 = threading.Thread(target=self.listen)
+        t1.start()
+        print("Broadcast  sended")
+
+
+
+    def listen(self):
+        while True:
+            msg, addr = self.sock.recvfrom(32)
+            self.onlineOracles.append((addr[0],msg.decode("utf-8")))
+            # print("message",msg," from ",addr)

+ 6 - 0
src/user/socket.py

@@ -0,0 +1,6 @@
+
+from .broadcast import Broadcast
+
+def run(broadcast, broadcastPort):
+    b = Broadcast(None, None,  None,None, broadcastPort=broadcastPort)
+    b.start()

+ 19 - 0
src/user/user.py

@@ -0,0 +1,19 @@
+
+from .broadcast import Broadcast
+import time
+
+
+minOracles = 3
+
+
+def run(data, server, broadcast, broadcastPort):
+
+    b = Broadcast(broadcast=broadcast,broadcastPort=broadcastPort)
+
+    while(len(b.onlineOracles)<minOracles):
+        print("\tWe need min  ",minOracles," Oracles. Waiting for mor Oracle. we have now ",len(b.onlineOracles)," online Oracles")
+        time.sleep(.5)
+
+
+    print("\nnow we have min ",minOracles,"Oracles. List of All Online Oracles ->\n \t\n",b.onlineOracles,"\n\n")
+