forked from howlanderson/xipmsg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xipmsg.py
executable file
·128 lines (113 loc) · 4.43 KB
/
xipmsg.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import threading
import sys
import string
import protocol
import time
import socket
import os
#init env
sys.setcheckinterval(1)
PORT = protocol.PORT
address = ('', PORT)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#server_socket.settimeout(1)
server_socket.setblocking(1)
server_socket.bind(address)
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
client_socket.settimeout(1)
#print '#init finished'
protocol.init(client_socket)
#print '#protocol init finished'
print ">"
class clientthread(threading.Thread):
def __init__(self, client_socket, server_socket):
threading.Thread.__init__(self)
self.client_socket = client_socket
self.server_socket = server_socket
def run(self):
while True:
try:
msg = raw_input() #get user input
msgsquence = msg.strip().split(':') #strip the input and explode the string by ":"
#print msgsquence
msgsquence2 = msgsquence[0].strip().split('/');
host = string.join(msgsquence2[1], '')
msg = msgsquence[1:]
msg = string.join(msg, ':')
protocol.sendmsg(self.client_socket, host, msg)
protocol.printhost()
print ">"
except EOFError:
protocol.logout()
print "byte byte"
os.abort()
s.close()
class serverthread(threading.Thread):
def __init__(self, client_socket, server_socket):
threading.Thread.__init__(self)
self.client_socket = client_socket
self.server_socket = server_socket
def run(self):
while True:
try:
data, (addr, port) = self.server_socket.recvfrom(1024)
strlist = data.split(':')
commandword = strlist[4]
#print repr(strlist)
if commandword == protocol.commandword['IPMSG_SENDMSG'] :
remotehost = addr
msg = strlist[5]
protocol.optresp(commandword, remotehost)
protocol.printhost()
#print '#ipmsg_sendmsg>'
print strlist[2],"@",remotehost,">",
print msg
print ">"
#sys.stdout.write(">")
elif commandword == protocol.commandword['IPMSG_SENDMSG_WITHCHECK'] :
msgnum = strlist[1]
remotehost = addr
protocol.sendrecvmsg(client_socket, remotehost, msgnum) #send msg check
msg = strlist[5]
protocol.optresp(commandword, remotehost)
protocol.printhost()
print strlist[2],"@",remotehost,">",
print msg
print ">"
elif commandword == protocol.commandword['IPMSG_ANSENTRY'] :
remotehost = addr
protocol.optresp(protocol.commandword['IPMSG_ANSENTRY'], remotehost)
elif commandword == protocol.commandword['IPMSG_BR_EXIT'] :
remotehost = addr
protocol.optresp(protocol.commandword['IPMSG_BR_EXIT'], remotehost)
elif commandword == protocol.commandword['IPMSG_NOOPERATION'] :
#no operation
pass
elif commandword == protocol.commandword['IPMSG_BR_ENTRY'] :
remotehost = addr
protocol.optresp(protocol.commandword['IPMSG_BR_ENTRY'], remotehost)
protocol.sendansentry(client_socket, remotehost)
elif commandword == protocol.commandword['IPMSG_ANSENTRY'] :
remotehost = addr
protocol.optresp(protocol.commandword['IPMSG_ANSENTRY'], remotehost)
else :
print "Notice > Uncatched message :",strlist, ":" , commandword
print ">"
protocol.printhost()
except socket.timeout:
pass
s.close()
tc = clientthread(client_socket, server_socket)
ts = serverthread(client_socket, server_socket)
ts.start()
tc.start()
while True:
try:
time.sleep(1)
except EOFError:
protocol.logout()
print "byte! byte!"
os.abort()