I have a python server that I write and read from, using the stream delegate. It works fine but when I start a match and the server sends data back it crashes.
Here's where it crashes:
func checkForMessages() {
while true {
if inputBuffer.length < 4 {
print("buffer length\(inputBuffer.length)")
print( MemoryLayout.size(ofValue: Int()))
print("application quit here")
return
}
var msgLength = (inputBuffer.bytes).load(as: UInt32.self)
msgLength = UInt32(bigEndian: msgLength)
print(inputBuffer.length)
print(msgLength)
if inputBuffer.length < msgLength {
print("its returning here!")
return
}
// ******Crashes on the line Below******
let message: Data? = inputBuffer.subdata(with: NSRange(location: 4, length: Int(msgLength)))
processMessage(message!)
let amtRemaining: Int = inputBuffer.length - Int(msgLength) - 4
if amtRemaining == 0 {
inputBuffer = NSMutableData()
}
else {
print("Creating input buffer of length \(amtRemaining)")
inputBuffer = NSMutableData(bytes: inputBuffer.bytes + 4 + Int(msgLength), length: amtRemaining)
}
}
}
It crashes when setting "let message".
I have tried a couple of things like trying to mess with the range itself but I'm just not quite sure why its doing what its doing, I can post the server code used to send the message back if needed.
So the problem was fixed by replacing the following part of the code:
if inputBuffer.length < msgLength {
return
}
with
if inputBuffer.length < msgLength + 4 {
return
}
Related
I have a Django project that I deployed to Heroku. I noticed that the calculations are not working when I used smaller numbers for some reason. Everything works fine on my local Windows machine.
For example this calculation newBalance = Decimal(str(userObj.user_coins)) - Decimal(str(betValue)) when the calculation would be 2000 - 12 I get an answer of 2.0E+3 instead of 1988. If the calculation would be 2000 - 120 I get an answer of 1900 or 1.9E+3 instead of 1880. On my local machine this was working correctly.
I don't understand what might be going wrong here.
//Template script
$('.bet-submit').click(function() {
const betValue = document.getElementById('bet-value').value
betSocket.send(JSON.stringify({
'id': id,
'betValue': betValue
}))
})
betSocket.onmessage = function(e) {
const data = JSON.parse(e.data)
update_coins()
for(const key in data.bets){
document.querySelector('#bets').innerHTML += '<span>' +data.bets[key].bet_value +'$</span>'
}
}
function update_coins() {
$.ajax({
method: "POST",
headers: { "X-CSRFToken": token },
url: "/api/coins/",
data: {},
success: function(data) {
document.querySelector('#user_coins').innerHTML = data.coins
}
})
};
//consumers.py
async def receive(self, text_data):
id= data['id']
betValue = data['betValue']
await save_bet(id, betValue)
bets = await get_bets(round)
await self.channel_layer.group_send(
self.room_group_name,
{
'type': 'send_bet',
'bets': bets
}
)
#database_sync_to_async
def save_bet(id, betValue):
userObj = CustomUser.objects.filter(steam_id=steamid)[0]
newBalance = userObj.user_coins - Decimal(betValue)
print(newBalance) // 2.0E+3
CustomUser.objects.filter(steam_id=steamid).update(user_coins=newBalance)
...
#database_sync_to_async
def get_bets(round):
...
bets = {}
for idx, bet in enumerate(betsObj):
bets[str(idx)] = {}
bets[str(idx)].update({
...,
'bet_value': str(bet.bet_value),
})
return bets
I think you'd better use float rather than Decimal.
newBalance = float(str(userObj.user_coins)) - float(str(betValue))
When consuming a Hug REST endpoint from .net JSON has embedded characters. A complete failing example posted below. Any help greatly appreciated.
Python
#hug.post('/test')
def test(response, body=None):
input = body.get('input')
print('INSIDE TEST ' + input)
if input:
dict = {"lastname":"Jordan"}
dict["firstname"] = input
return json.dumps(dict, sort_keys=True, default=str)
.NET (can only use .net 3.5)
private static object GetParsedData(string data)
{
var posturl = "http://localhost:8000/test";
try
{
using (var client = new WebClient())
{
// upload values is the POST verb
var values = new NameValueCollection()
{
{ "input", data },
};
var response = client.UploadValues(posturl, values);
var responseString = Encoding.UTF8.GetString(response);
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
JObject rss = JObject.Parse(responseString);
Console.WriteLine((string)rss["lastname"]);
}
}
catch (WebException ex)
{
if (ex.Response is HttpWebResponse)
{
var code = ((HttpWebResponse)ex.Response).StatusCode;
var desc = ((HttpWebResponse)ex.Response).StatusDescription;
}
//_logger.Error(ex.Message);
}
return false;
}
responseString looks like this:
"\"{\\\"firstname\\\": \\\"Mike\\\", \\\"lastname\\\": \\\"Jordan\\\"}\""
JObject.Parse throws error:
Newtonsoft.Json.JsonReaderException:
'Error reading JObject from JsonReader. Current JsonReader item is not an object: String. Path '', line 1, position 53.
Workaround - If I do something horrible like this to responseString JObject parses correctly:
str = str.Replace("\\", "");
str = str.Substring(1, len - 2);
Whats going on?
The default hug output format is json; it is not necessary to call json.dumps on return values, hug will do this automatically.
So i'm working on converting a java program I have over to python. In my java code I have a http get call that looks like this.
sslContext.init(null, new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} }, new SecureRandom());
try {
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(new org.apache.http.conn.ssl.SSLSocketFactory(sslContext)).build();
String authString = username + ":" + password;
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
HttpGet httpGet = new HttpGet(envURL);
httpGet.setHeader("Content-Type", "application/json");
httpGet.setHeader("Authorization", "Basic " + authStringEnc);
CloseableHttpResponse httpGetResponse = httpclient.execute(httpGet);
HttpEntity entityResponse = httpGetResponse.getEntity();
String result = EntityUtils.toString(entityResponse);
EntityUtils.consume(entityResponse);
JSONParser parser = new JSONParser();
thresholdContent = (JSONArray) parser.parse(result);
} catch (Exception e) {
e.printStackTrace();
}
i'm trying to find the cleanist way to do this in python 3.x. Or I guess the standered for doing something like this in python.
I've tried soemthing like:
conn = requests.get(env, headers={"content-type":"application/json"}, auth=(userName,password))
but have not had much luck.
With requests in python you need to pass the url
conn = requests.get(url = 'https://myurl', headers = {'Content-Type':'application/json'})
Python has two very useful library method (binascii.a2b_hex(keyStr) and binascii.hexlify(keyBytes)) which I have been struggling with in Swift. Is there anything readily available in Swift. If not, how would one implement it? Given all the bounds and other checks (like even-length key) are done.
Data from Swift 3 has no "built-in" method to print its contents as
a hex string, or to create a Data value from a hex string.
"Data to hex string" methods can be found e.g. at How to convert Data to hex string in swift or How can I print the content of a variable of type Data using Swift? or converting String to Data in swift 3.0. Here is an implementation from the first link:
extension Data {
func hexEncodedString() -> String {
return map { String(format: "%02hhx", $0) }.joined()
}
}
Here is a possible implementation of the reverse "hex string to Data"
conversion (taken from Hex String to Bytes (NSData) on Code Review, translated to Swift 3 and improved)
as a failable inititializer:
extension Data {
init?(fromHexEncodedString string: String) {
// Convert 0 ... 9, a ... f, A ...F to their decimal value,
// return nil for all other input characters
func decodeNibble(u: UInt8) -> UInt8? {
switch(u) {
case 0x30 ... 0x39:
return u - 0x30
case 0x41 ... 0x46:
return u - 0x41 + 10
case 0x61 ... 0x66:
return u - 0x61 + 10
default:
return nil
}
}
self.init(capacity: string.utf8.count/2)
var iter = string.utf8.makeIterator()
while let c1 = iter.next() {
guard
let val1 = decodeNibble(u: c1),
let c2 = iter.next(),
let val2 = decodeNibble(u: c2)
else { return nil }
self.append(val1 << 4 + val2)
}
}
}
Example:
// Hex string to Data:
if let data = Data(fromHexEncodedString: "0002468A13579BFF") {
let idata = Data(data.map { 255 - $0 })
// Data to hex string:
print(idata.hexEncodedString()) // fffdb975eca86400
} else {
print("invalid hex string")
}
Not really familiar with Python and the checks it performs when convert the numbers, but you can expand the function below:
func convert(_ str: String, fromRadix r1: Int, toRadix r2: Int) -> String? {
if let num = Int(str, radix: r1) {
return String(num, radix: r2)
} else {
return nil
}
}
convert("11111111", fromRadix: 2, toRadix: 16)
convert("ff", fromRadix: 16, toRadix: 2)
Swift 2
extension NSData {
class func dataFromHexString(hex: String) -> NSData? {
let regex = try! NSRegularExpression(pattern: "^[0-9a-zA-Z]*$", options: .CaseInsensitive)
let validate = regex.firstMatchInString(hex, options: NSMatchingOptions.init(rawValue: 0), range: NSRange(location: 0, length: hex.characters.count))
if validate == nil || hex.characters.count % 2 != 0 {
return nil
}
let data = NSMutableData()
for i in 0..<hex.characters.count/2 {
let hexStr = hex.substring(i * 2, length: 2)
var ch: UInt32 = 0
NSScanner(string: hexStr).scanHexInt(&ch)
data.appendBytes(&ch, length: 1)
}
return data
}
}
let a = 0xabcd1234
print(String(format: "%x", a)) // Hex to String
NSData.dataFromHexString("abcd1234") // String to hex
1>How can I run the equivalent using pymongo?
a>
cfg = rs.conf()
b>
db.printSlaveReplicationInfo()
2>Using PyMongo, how can I get the details of other replica sets CLI output in the created clusters .
(Note:I have already successfully created cluster.Just I am writing a python script in primary to check the outputs of rs.conf() and db.printSlaveReplicationInfo() in all the replica sets inside cluster and parse the output.)
Any help on this regard is greatly appreciable.
The replica set configuration is stored in the "local" database in a collection called "system.replset" so the equivalent of rs.conf() would be db.system.replset.findOne() when db is local or its equivalent find_one() in Python.
db.printSlaveReplicationInfo() is a big more involved, but you can get all of that information in the local database as well.
It may be easier to get it via admin database command replSetGetStatus which returns a document containing oplog information for each member of the replica set, along with other details. Python MongoDB driver pymongo provides a method to run commands, so you can run it against the admin DB and parse out the output for information about where each member of the replica set is relative to the primary.
I'm basically going to give you a hint rather than directly answer it, because the full answer is to simply code it. But you probably are unaware that you can do this simple thing in the shell:
> db.printSlaveReplicationInfo
function () {
var startOptimeDate = null;
function getReplLag(st) {
assert( startOptimeDate , "how could this be null (getReplLag startOptimeDate)" );
print("\tsyncedTo: " + st.toString() );
var ago = (startOptimeDate-st)/1000;
var hrs = Math.round(ago/36)/100;
print("\t" + Math.round(ago) + " secs (" + hrs + " hrs) behind the primary ");
};
function getMaster(members) {
var found;
members.forEach(function(row) {
if (row.self) {
found = row;
return false;
}
});
if (found) {
return found;
}
};
function g(x) {
assert( x , "how could this be null (printSlaveReplicationInfo gx)" )
print("source: " + x.host);
if ( x.syncedTo ){
var st = new Date( DB.tsToSeconds( x.syncedTo ) * 1000 );
getReplLag(st);
}
else {
print( "\tdoing initial sync" );
}
};
function r(x) {
assert( x , "how could this be null (printSlaveReplicationInfo rx)" );
if ( x.state == 1 || x.state == 7 ) { // ignore primaries (1) and arbiters (7)
return;
}
print("source: " + x.name);
if ( x.optime ) {
getReplLag(x.optimeDate);
}
else {
print( "\tno replication info, yet. State: " + x.stateStr );
}
};
var L = this.getSiblingDB("local");
if (L.system.replset.count() != 0) {
var status = this.adminCommand({'replSetGetStatus' : 1});
startOptimeDate = getMaster(status.members).optimeDate;
status.members.forEach(r);
}
else if( L.sources.count() != 0 ) {
startOptimeDate = new Date();
L.sources.find().forEach(g);
}
else {
print("local.sources is empty; is this db a --slave?");
return;
}
}
I love REPL's, and much like python's own famous REPL you can just get a dump of what the implemented function does.
Simples.