Advertisement
|
[x]
Attachment Details
|
||
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: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: |
"""Server for the Sample Generic Device DDI
"""
from twisted.internet import reactor
from twisted.internet.protocol import Factory, Protocol
from twisted.protocols import policies
from string import lower
import time, random, sys
class variableSender(Protocol, policies.TimeoutMixin):
"""The Protocol variableSender sends numToSend random variables chosen in varList with random values every timeout seconds.
The packet sent does not have a fixed length but is composed of a header, numToSend variable/value blocks and end marker.
Its general format is "$LLLL#AAAA:BBBB.CCCCCCCCC,;" where "AAAA:BBBB.CCCCCCCCC," is sent "LLLL" times.
The format of the header is "$LLLL#" where:
- "$": Is the beginning marker at offset 0
- "LLLL": Is the number of variable/value blocks in the group at offset 1 as an unsigned integer in a 4-character long string padded with 0
- "#": Is the header end marker at offset 5
The format of the fixed-length 20-byte packet sent is "AAAA:BBBB.CCCCCCCCC," where:
- "AAAA": Is the variable number at offset 0 sent as an unsigned integer in a 4-character long string padded with 0
- ":": Is the separator between the variable number and its value at offset 4
- "BBBB.CCCCCCCCC": Is the value at offset 5 sent as a floating point number with a fixed-sized 4-character signed integer part padded with 0 and a fixed-sized 9-character decimal part padded with 0
- ",": Is the end market at offset 19
The packet end marker is ";".
The values are all between -999.999999999 and 9999.999999999.
"""
timeout = 1
varList = range(1, 251)
numToSend = 50
def __init__(self):
print "1"
self.timeout = timeout
def connectionMade(self):
print "2"
print "Got new client!"
self.timeSent = time.clock()
self.setTimeout(self.timeout)
def connectionLost(self, reason):
print "3"
print "Lost a client:", reason.getErrorMessage()
self.setTimeout(None)
def sendHeader(self):
print "4"
self.transport.write("$%04d#" % self.numToSend)
def sendBlockDelimiter(self):
# print "5"
self.transport.write(",")
def sendEndMarker(self):
print "6"
self.transport.write(";")
def sendVariable(self, variable, value):
# print "7"
self.transport.write("%04d:%014.9f" % (variable, value))
def sendVariables(self):
"""Send a random selection of numToSend variables from varList with random values.
"""
print "8"
varListToSend = random.sample(self.varList, self.numToSend)
self.sendHeader()
for i in varListToSend:
self.sendVariable(i, random.random()*10999-999)
self.sendBlockDelimiter()
self.sendEndMarker()
def timeoutConnection(self):
"""Send variables once the timeout is reached.
"""
print "9a"
# print "timeout is", self.timeout
self.setTimeout(self.timeout)
print "9b"
currentTime = time.clock()
print "9c"
self.sendVariables()
print "9d"
print "Sending Variables", repr(currentTime - self.timeSent)
print "9e"
self.timeSent = currentTime
class samplegenericdevice(variableSender):
"""Sample Generic Device, sends a random selection of 200 variables among 250 every 5 seconds.
"""
def __init__(self):
self.timeout = 1
self.varList = range(1, 251)
self.numToSend = 200
class samplegenericdevice2(variableSender):
"""Sample Generic Device 2, sends 100 variables every 10 seconds.
"""
def __init__(self):
self.timeout = 1
self.varList = range(1, 101)
self.numToSend = 100
class samplegenericdevice3(variableSender):
"""Sample Generic Device 3, sends 10 variables with predefined values every 3 seconds.
"""
def __init__(self):
print "10"
self.timeout = 1
self.varValuesList1 = [90, 85, 84, 89, 12, 67, 93, 23, 56, 92]
self.varValuesList2 = [10, 25, 34, 49, 52, 17, 73, 83, 96, 12]
def wait(self, seconds):
"""wait(5) => 5
print "11"
Wait for a certain number of seconds before returning.
Returns the same number passed in."""
# print time.asctime(), "Waiting %s seconds" % seconds
time.sleep(seconds)
# print time.asctime(), "Finished waiting %s seconds" % seconds
return seconds
def sendVariables(self):
"""Send all variables from varValuesList.
"""
print "12"
a=0
for a in range(10):
self.wait(1)
self.sendHeader()
for i in range(0, len(self.varValuesList1)):
# print "in i loop", i
self.sendVariable(i+1, self.varValuesList1[i])
self.sendBlockDelimiter()
self.sendEndMarker()
b=0
for b in range(10):
self.wait(1)
self.sendHeader()
for z in range(0, len(self.varValuesList2)):
# print "in z loop", z
self.sendVariable(z+1, self.varValuesList2[z])
self.sendBlockDelimiter()
self.sendEndMarker()
def main():
print "13"
if len(sys.argv) < 3:
print sys.argv[0]+": You must provide a port and DDI"
sys.exit(2)
f = Factory()
f.protocol = globals()[lower(sys.argv[2])]
reactor.listenTCP(int(sys.argv[1]), f)
reactor.run()
if __name__ == '__main__':
print "14"
main()
|
|
[x]
The Solution Rating System
|
||
|
With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.
Your Input Matters If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support. Thank you! |
||
|
Loading Advertisement... |