1 
2 /* Web Polygraph       http://www.web-polygraph.org/
3  * Copyright 2003-2011 The Measurement Factory
4  * Licensed under the Apache License, Version 2.0 */
5 
6 #include "pgl/pgl.h"
7 
8 #include "pgl/PglBoolSym.h"
9 #include "pgl/PglNumSym.h"
10 #include "pgl/PglTimeSym.h"
11 #include "pgl/PglSizeSym.h"
12 #include "pgl/PglBwidthSym.h"
13 
14 #include "xstd/gadgets.h"
15 
16 
17 String BwidthSym::TheType = "bwidth";
18 
19 
20 
bytes2size(double bytes)21 static BigSize bytes2size(double bytes) { return BigSize::MBd(bytes/1024/1024); }
22 
23 
BwidthSym(BigSize aSize,Time anInterval)24 BwidthSym::BwidthSym(BigSize aSize, Time anInterval): ExpressionSym(TheType),
25 	theVal(::Bandwidth(aSize, anInterval)) {
26 }
27 
BwidthSym(double aVal)28 BwidthSym::BwidthSym(double aVal): ExpressionSym(TheType) {
29 	val(aVal);
30 }
31 
isA(const String & type) const32 bool BwidthSym::isA(const String &type) const {
33 	return ExpressionSym::isA(type) || type == TheType;
34 }
35 
dupe(const String & type) const36 SynSym *BwidthSym::dupe(const String &type) const {
37 	if (isA(type))
38 		return new BwidthSym(theVal);
39 	return ExpressionSym::dupe(type);
40 }
41 
val() const42 Bwidth BwidthSym::val() const {
43 	return theVal;
44 }
45 
val(Bwidth aVal)46 void BwidthSym::val(Bwidth aVal) {
47 	theVal = aVal;
48 }
49 
unOper(const Oper & op) const50 ExpressionSym *BwidthSym::unOper(const Oper &op) const {
51 	if (op.plus())
52 		return new BwidthSym(+theVal);
53 	else
54 	if (op.minus())
55 		return new BwidthSym(-theVal);
56 	else
57 		return ExpressionSym::unOper(op);
58 }
59 
bnOper(const Oper & op,const SynSym & s) const60 ExpressionSym *BwidthSym::bnOper(const Oper &op, const SynSym &s) const {
61 	if (BwidthSym *rs = (BwidthSym*)s.clone(BwidthSym::TheType))
62 		return operBB(op, rs, s);
63 
64 	if (TimeSym *ts = (TimeSym*)s.clone(TimeSym::TheType))
65 		return operBT(op, ts, s);
66 
67 	if (NumSym *ns = (NumSym*)s.clone(NumSym::TheType))
68 		return operBN(op, ns, s);
69 
70 	return ExpressionSym::bnOper(op, s);
71 }
72 
73 // bwidth op bwidth
operBB(const Oper & op,BwidthSym * rs,const SynSym & s) const74 ExpressionSym *BwidthSym::operBB(const Oper &op, BwidthSym *rs, const SynSym &s) const {
75 	double otherVal = rs->val();
76 	delete rs;
77 
78 	if (op.div()) {
79 		checkDenom(otherVal);
80 		return new NumSym(val() / otherVal);
81 	}
82 
83 	if (op.lessTrue())
84 		return new BoolSym(val() < otherVal);
85 	if (op.lessOrEq())
86 		return new BoolSym(val() <= otherVal);
87 	if (op.greaterTrue())
88 		return new BoolSym(val() > otherVal);
89 	if (op.greaterOrEq())
90 		return new BoolSym(val() >= otherVal);
91 	if (op.plus())
92 		return new BwidthSym(val() + otherVal);
93 	if (op.minus())
94 		return new BwidthSym(val() - otherVal);
95 
96 	return ExpressionSym::bnOper(op, s);
97 }
98 
operBT(const Oper & op,TimeSym * ts,const SynSym & s) const99 ExpressionSym *BwidthSym::operBT(const Oper &op, TimeSym *ts, const SynSym &s) const {
100 	Time otherInterval = ts->val();
101 	delete ts;
102 
103 	if (op.mult())
104 		return new SizeSym(bytes2size(BytesPerSec(theVal)*otherInterval.secd()));
105 
106 	return ExpressionSym::bnOper(op, s);
107 }
108 
operBN(const Oper & op,NumSym * ns,const SynSym & s) const109 ExpressionSym *BwidthSym::operBN(const Oper &op, NumSym *ns, const SynSym &s) const {
110 	double otherFactor = ns->val();
111 	delete ns;
112 
113 	if (op.div()) {
114 		checkDenom(otherFactor);
115 		return new BwidthSym(theVal/otherFactor);
116 	}
117 
118 	if (op.mult())
119 		return new BwidthSym(theVal*otherFactor);
120 
121 	return ExpressionSym::bnOper(op, s);
122 }
123 
print(ostream & os,const String &) const124 ostream &BwidthSym::print(ostream &os, const String &) const {
125 	return os << bytes2size(theVal*1) << "/sec";
126 }
127 
128 
Bandwidth(BigSize sz,Time tm)129 Bwidth Bandwidth(BigSize sz, Time tm) {
130 	return sz.byted()/tm.secd();
131 }
132