| 1 | from django.db import models |
|---|
| 2 | """Models for the DomainMan application |
|---|
| 3 | -Node makes it easy to extract the ip and port fields |
|---|
| 4 | """ |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | """Creates a node with ip and port fields |
|---|
| 8 | """ |
|---|
| 9 | class Node(models.Model): |
|---|
| 10 | ip = models.IPAddressField() |
|---|
| 11 | port=models.IntegerField() |
|---|
| 12 | floor=models.IntegerField() |
|---|
| 13 | num=models.IntegerField() |
|---|
| 14 | |
|---|
| 15 | """ |
|---|
| 16 | Constructor defines its internal ip address and port number for the node |
|---|
| 17 | @param floor: The floor for the node |
|---|
| 18 | @param num: The number of the node |
|---|
| 19 | """ |
|---|
| 20 | def __init__(self, thisFloor, thisNum): |
|---|
| 21 | self.ip = self.getIP(thisFloor, thisNum) |
|---|
| 22 | self.port = self.getPort(thisFloor,thisNum) |
|---|
| 23 | self.floor = thisFloor |
|---|
| 24 | self.num = thisNum |
|---|
| 25 | |
|---|
| 26 | def __unicode__(self): |
|---|
| 27 | return unicode("node_id: ".__add__(self.floor).__add__("-").__add__(self.num)) |
|---|
| 28 | |
|---|
| 29 | """Calculates the internal ip address of the node based on the ICTAS floor and number |
|---|
| 30 | |
|---|
| 31 | @param floor: The floor of ICTAS where the node is located |
|---|
| 32 | @param num: The number of the node on the particular floor |
|---|
| 33 | @return: The port number used in the CORNET server to access the node; exception if params are not in the range |
|---|
| 34 | """ |
|---|
| 35 | def getIP(self, floor, num): |
|---|
| 36 | if floor > 0 and floor < 5: |
|---|
| 37 | if num > 0 and num < 13: |
|---|
| 38 | ip_num=floor*12-2+num |
|---|
| 39 | return "192.168.1.".__add__(str(ip_num)) |
|---|
| 40 | else: |
|---|
| 41 | raise Exception("Num input must be greater than 0 and less than 13") |
|---|
| 42 | else: |
|---|
| 43 | raise Exception("Floor input must be greater than 0 and less than 5") |
|---|
| 44 | |
|---|
| 45 | """Calculates the port number based on the floor and node number of the floor |
|---|
| 46 | |
|---|
| 47 | @param floor: The floor of ICTAS where the node is located |
|---|
| 48 | @param num: The number of the node on the particular floor |
|---|
| 49 | @return: The port number used in the CORNET server to access the node; exception if params are not in the range |
|---|
| 50 | """ |
|---|
| 51 | def getPort(self, floor, num): |
|---|
| 52 | if floor > 0 and floor < 5: |
|---|
| 53 | if num > 0 and num < 13: |
|---|
| 54 | port_num=(floor-1)*12+num |
|---|
| 55 | return 7000+port_num |
|---|
| 56 | else: |
|---|
| 57 | raise Exception("Num input must be greater than 0 and less than 13") |
|---|
| 58 | else: |
|---|
| 59 | raise Exception("Floor input must be greater than 0 and less than 5") |
|---|
| 60 | |
|---|
| 61 | class SSHClientList(): |
|---|
| 62 | clientList = {}; |
|---|
| 63 | |
|---|
| 64 | def addClient(self, id, client): |
|---|
| 65 | clientList = {id: client}; |
|---|
| 66 | |
|---|
| 67 | def getClient(self, id): |
|---|
| 68 | if clientList.__contains__(id): |
|---|
| 69 | return clientList.get(id) |
|---|
| 70 | else: |
|---|
| 71 | raise Exception("no".__add__(id).__add__("found")) |
|---|